Docsity

Big Data Computing - Padua University, Sbobinature di Tecniche Di Analisi Dei Dati

Toggle sidebar
bg1
Federico Luisetto
Big Data Computing notes
Table of Contents
MapReduce ...................................................................................................................................................... 3
MapReduce .................................................................................................................................................. 3
Computation ............................................................................................................................................ 4
Algorithm .................................................................................................................................................. 5
Performance analysis ............................................................................................................................... 6
Word count example ................................................................................................................................ 6
Design goals .............................................................................................................................................. 7
Pros and cons ........................................................................................................................................... 8
Partitioning ................................................................................................................................................... 8
Deterministic partitioning – Class count ................................................................................................... 8
Random partitioning – Class count ......................................................................................................... 10
Deterministic vs. Random partitioning ................................................................................................... 11
Largeness of 𝑚 ....................................................................................................................................... 12
Apache Spark .............................................................................................................................................. 12
Resilient Distributed Dataset (RDD) ........................................................................................................ 13
Coreset Technique ......................................................................................................................................... 15
Coresets and Composable Coresets ........................................................................................................... 15
Case study: center-based clustering ........................................................................................................... 15
Metric Space ........................................................................................................................................... 16
Distance functions .................................................................................................................................. 16
Clustering ............................................................................................................................................... 17
Center-based clustering.......................................................................................................................... 18
k-center / Farthest-First Traversal .......................................................................................................... 20
k-center as a coreset primitive ............................................................................................................... 23
k-means/k-median ................................................................................................................................. 24
Clustering evaluation .............................................................................................................................. 28
Streaming ....................................................................................................................................................... 30
Streaming Model ........................................................................................................................................ 30
Finding the majority element ..................................................................................................................... 31
Sampling ..................................................................................................................................................... 32
Sampling-based applications: Frequent Items ........................................................................................ 33
Sticky Sampling ....................................................................................................................................... 33
Sketching .................................................................................................................................................... 36
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32

Anteprima parziale del testo

Scarica Big Data Computing - Padua University e più Sbobinature in PDF di Tecniche Di Analisi Dei Dati solo su Docsity!

Federico Luisetto

Big Data Computing notes

  • MapReduce Table of Contents
    • MapReduce
      • Computation
      • Algorithm..................................................................................................................................................
      • Performance analysis
      • Word count example
      • Design goals..............................................................................................................................................
      • Pros and cons
    • Partitioning
      • Deterministic partitioning – Class count
      • Random partitioning – Class count
      • Deterministic vs. Random partitioning
      • Largeness of 𝑚
    • Apache Spark
      • Resilient Distributed Dataset (RDD)........................................................................................................
  • Coreset Technique
    • Coresets and Composable Coresets
    • Case study: center-based clustering
      • Metric Space
      • Distance functions
      • Clustering
      • Center-based clustering..........................................................................................................................
      • k-center / Farthest-First Traversal
      • k-center as a coreset primitive
      • k - means/ k - median
      • Clustering evaluation
  • Streaming
    • Streaming Model
    • Finding the majority element
    • Sampling
      • Sampling-based applications: Frequent Items
      • Sticky Sampling
    • Sketching
      • Count-min sketch
      • Count sketch
      • Analysis of performance metrics
    • Filtering
      • Bloom filter.............................................................................................................................................
  • Similarity Search - r - Near neighbor search
    • Similarity search in low dimensions
      • r - NNS and Range Reporting in ℝ𝐷
    • Similarity search in high dimensions
      • (c,r) - Approximate Near Neighbor Search
      • ANNS with Locality Sensitive Hashing.....................................................................................................
      • LSH for Hamming and Euclidean distances

A typical cluster architecture:

  • Racks of 16-64 compute nodes (commodity hardware), connected (within each rack and among

racks) by fast switches (e.g., 10 Gbps Ethernet).

  • Distributed File System (DFS)

o Files divided into chunks (e.g., 64MB per chunk).

o Each chunk replicated (e.g., 2x o 3x) with replicas in different nodes to ensure fault-

tolerance.

Examples: Google File System (GFS); Hadoop Distributed File System (HDFS).

Several software frameworks have been proposed to support MapReduce programming. For example,

Apache Hadoop and Apache Spark.

MapReduce → conceptual model for high-level algorithm design and analysis.

Spark → programming framework for implementing MapReduce algorithms.

Computation

A MapReduce computation can be viewed as a sequence of rounds. A round transforms a set of key-value

pairs into another set of key-value pairs (data centric view!), through the following two phases:

  • Map phase : for each input key-value pair separately, a user-specified map function is applied to the

pair and produces ≥ 0 other key-value pairs, sometimes called intermediate pairs.

  • SHUFFLE : intermediate pairs are grouped by key, by creating, for each key 𝑘, a list 𝐿

𝑘

of values of

intermediate pairs with key 𝑘.

  • Reduce phase : for each key 𝑘 separately, a user-specified reduce function is applied to

𝑘

) which produces ≥ 0 key-value pairs, which is the output of the round.

→ Terminology: the application of the reduce function to a pair (𝑘, 𝐿

𝑘

) is referred to as reducer.

A MapReduce computation may require multiple rounds. If so:

  • The input of a round may comprise a subset of the initial input and/or subset of the outputs of

previous rounds.

  • Same shared variables available to all executors are employed to maintain information across

different rounds.

Often, the output of round 𝑖 is the input of round 𝑖 + 1 but, in general, a round can use, as input, data

produced from previous rounds or even the original inputs.

Input data: Input data / Output of round 𝑗 ≤ 𝑖 ⟹ Input of round 𝑖 + 1.

The use of key-value pairs in MapReduce seems an unnecessary complication, and one might be tempted

to regard individual data items simply as objects belonging to some domain, without imposing a distinction

between keys and values. Justification : MapReduce is data-centric and focuses on data transformations

which are independent of where data actually reside. The keys are needed as addresses to reach the

objects, and as labels to define the groups in the reduce phases.

Practical considerations:

  • One should choose the key-value representations in the most convenient way. The domains for key

and values may change from one round to another.

  • Programming frameworks such as Spark, while containing explicit provisions for handling key-value

pairs, allow also to manage data without the use of keys. In this case, internal addresses/labels, not

explicitly visible to the programmer, are used.

Algorithm

A MapReduce (MR) algorithm should be specified so that:

  • The input and output of the algorithm is clearly defined.
  • The sequence of rounds executed for any given input instance is unambiguously implied by the

specification.

  • For each round the following aspects are clear:

o input, intermediate and output sets of key-value pairs.

o functions applied in the map and reduce phases.

  • Meaningful values (or asymptotic) bounds for the key performance indicators (defined later) can be

derived.

To specify a MR algorithm with a fixed number of rounds 𝑅, we will use the following style:

Input : description of the input as set of key-value pairs

Output : description of the output as set of key-value pairs

Round 1:

  • Map phase: description of the function applied to each key-value pair.
  • Reduce phase: description of the function applied to each group of key-value pairs with the same

key.

Round 2, 3, ..., 𝑅: similarly

Observation : for simplicity, we sometime provide a high-level description of a round, which, however, must

enable a straightforward yet tedious derivation of the Map and Reduce phases.

Execution of a round on a distributed platform:

ii. Reduce phase: for each word w separately let 𝐿

𝑤

1

2

𝑘

(𝑤) (some may

not exist). (𝑤, 𝐿

𝑤

𝑖

𝑐

𝑖

(𝑤)∈𝐿

𝑤

⟹ reduce function

Performance analysis:

𝐿

𝑖

𝑖

𝑚𝑎𝑥

𝑖

  • Map phase: 𝑂(𝑁

max

  • Reduce phase: 𝑂

𝐿

= 𝑂(max{𝑁

𝑚𝑎𝑥

𝑚𝑎𝑥

𝐴

𝑖

𝑘

𝑖=𝑖

, total number of word occurrences across all documents

  • Space occupied by input pairs = 𝑂
  • Space occupied by intermediate pairs = 𝑂
  • Space occupied by output pairs = 𝑂

𝐴

Design goals

Observation: For every problem solvable by a sequential algorithm in space 𝑆 there exists a 1-round

MapReduce algorithm with 𝑀

𝐿

𝐴

= 𝛩(𝑆): run the sequential algorithm on the whole input with one

reducer.

Remark: the trivial solution implied by the observation is impractical for very large inputs for the following

reasons:

  • A platform with very large main memory is needed.
  • No parallelism is exploited.

Design goals for MapReduce algorithms:

  1. Few rounds (e.g., 𝑅 = 𝑂( 1 )).
  2. Sublinear local space (e.g., 𝑀

𝐿

𝜖

, with 𝜖 < 1 ).

  1. Linear aggregate space (i.e., 𝑀

𝐴

= 𝑂(|𝑖𝑛𝑝𝑢𝑡|)), or only slightly super linear.

  1. Low complexity of each map or reduce function.

These goals are conflicting with one another and, in practice, suitable tradeoffs must be sought.

1 ,4: aim at time efficiency.

  • 1 ⟹ low communication/synchronization costs.
  • 4 ⟹ low computation costs.

2, 3: aim at space efficiency.

  • 2 ⟹ low main memory requirements hence feasibility on commodity hardware.
  • 3 ⟹ low data replication.

Pros and cons

✓ Data-centric view. Algorithm design focuses on data transformations targeting the above design

goals.

✓ Usability. The programmer is lifted from performing crucial but difficult activities such as allocation

of tasks to workers, data management, and handling of failures, which are instead dealt with

transparently by the supporting framework.

✓ Portability/Adaptability. Applications can run on different platforms and the supporting framework

will do best effort to exploit parallelism and minimize data movement costs.

✓ Cost. MapReduce applications can be run on moderately expensive platforms, and many popular

cloud providers support their execution.

× Running time is only coarsely captured by 𝑅. More sophisticated (yet, less usable) time-

performance measures should be used.

× Curse of the last reducer. In some cases, one or a few reducers may be much slower than the

others, thus delaying the end of the round. Algorithm designers should aim at balancing the load (if

at all possible) among reducers.

× MapReduce is not suitable for applications that require very high performance and a tight coupling

with the underlying architecture (e.g., from computational sciences).

Partitioning

A typical approach to attain the stated goal on local space is to subdivide the relevant data into small

partitions either deterministically or randomly and make reduce functions work separately in individual

partitions → observation: partitioning may increase the number of rounds and, more rarely, the aggregate

space requirements. Hence, suitable tradeoffs must be sought.

Deterministic partitioning – Class count

Problem : given a set 𝑆 of 𝑁 objects with class labels, count how many objects belong to each class.

More precisely:

Input : Set 𝑆 of 𝑁 objects represented by pairs (𝑖, (𝛾

𝑖

𝑖

)), for 0 ≤ 𝑖 < 𝑁, where 𝑜

𝑖

is the i - th object, and 𝛾

𝑖

its class.

Output : The set of pairs (𝛾, 𝑐

) where 𝛾 is a class labeling some object of 𝑆 and 𝑐

is the number of

objects of 𝑆 labeled with 𝛾.

Remark : we will always assume that 𝑁 (the number of input pairs) is a value known to the algorithm.

Class count in one round

Round 1:

i. Map phase: ∀ 0 ≤ 𝑖 < 𝑁 separately (𝑖,

𝑖

𝑖

𝑖

ii. Reduce phase: for each class 𝛾 separately, let 𝐿

𝛾

= list of 𝑖’s from intermediate pairs (𝛾

𝑖

𝛾

𝛾

|) ⟹ output pair.

Obs: initial keys (𝑖’s) are not used. Assumption: each input pair takes 𝑂( 1 ) space.

Analysis:

𝐿

  • Map phase: 𝑂( 1 ).
  • Reduce phase: 𝑂(𝑁) since in the worst case all or a large fraction of the input pairs have the same

class label.

𝐿

• R1:

o Map phase: 𝑂

o Reduce phase: 𝑂 (

𝑁

𝑙

) because of the even partition of the input pairs.

• R2:

o Map phase: empty.

o Reduce phase: 𝑂(𝑙).

𝐿

= 𝑂 (max {

𝑁

𝑙

, 𝑙}) ⟹ setting 𝑙 = √

𝐿

𝐴

same argument as before

Observations:

  • The choice of 𝑙 = √

𝑁 partitions (which we will often make) provides an example of a simple

tradeoff between local space and number of rounds.

  • If more stringent bounds on the local space are imposed, different partitionings may be needed,

yielding different round-space tradeoffs. One of the exercises will explore this.

  • In practice, the number of partitions is fixed as a multiple of the number of available workers, as

long as local space is not an issue.

Random partitioning – Class count

Consider the class count problem, but assume that the input pairs are provided without the integer keys in

[

Namely:

Input : Set 𝑆 of 𝑁 objects represented by pairs

𝑖

𝑖

, for 0 ≤ 𝑖 < 𝑁, where 𝑜

𝑖

is the i - th object, and 𝛾

𝑖

its

class.

Output : The set of pairs (𝛾, 𝑐

) where 𝛾 is a class labeling some object of 𝑆 and 𝑐(𝛾) is the number of

objects of 𝑆 labeled with 𝛾.

We can employ the 2-round approach seen before, but the partitioning in Round 1 will be achieved now by

assigning a random key in

[

to each of the intermediate pair.

Round 1:

i. Map phase: ∀ input pair (𝛾

𝑖

𝑖

) separately

𝑖

𝑖

, 𝑥 ≡ 𝑟𝑎𝑛𝑑𝑜𝑚 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 𝑖𝑛 [ 0 , 𝑙).

ii. Reduce phase: for each 0 ≤ 𝑥 < 𝑙 separately, let 𝐿

𝑥

= list of class 𝛾 form intermediate pairs

with key 𝑥.

𝑥

𝑥

𝑥

Round 2:

i. Map phase: empty.

ii. Reduce phase: for each class label 𝛾 separately, let 𝐿

𝛾

= list of 𝑐

from output pairs of R

with key 𝛾.

Obs: some 𝑐(𝑥, 𝛾) may not exist, and 𝑥 ∈ [ 0 , 𝑙) ⇒ |𝐿

𝛾

𝛾

𝑐(𝑥,𝛾)∈𝐿 𝛾

Analysis:

Let:

𝑥

= number of intermediate pairs from R1 with random key 𝑥.

  • 𝑚 = max

𝑥

𝐿

• R1:

o Map phase: 𝑂( 1 ).

o Reduce phase: 𝑂

• R2:

o Map phase: empty.

o Reduce phase: 𝑂

𝐿

= 𝑂(max{𝑚, 𝑙}).

𝐴

since input/output/intermediate pairs in each round require 𝑂(𝑁) space.

Obs: the space efficiency of this approach depends on 𝑚.

Deterministic vs. Random partitioning

  • Deterministic: 𝑀

𝐿

= 𝑂 (max {

𝑁

𝑙

  • Random: 𝑀

𝐿

= 𝑂(max{𝑚, 𝑙}).

Spark can run:

  • On a single machine in the so called local mode.
  • On a cluster managed by a cluster manager such as Spark’s Standalone, YARN, Mesos.

We need a framework like Spark to process bigdata efficiently and at moderate costs.

Spark:

  • Allows you to pool resources of several (inexpensive) machines into one platform.
  • Manages and coordinates the execution of bigdata jobs on such a platform.

→ Obs: Without careful management and coordination, the potential of the platform is likely to be wasted.

Components:

  • Driver (a.k.a. master) is the heart of the application and:

o Creates the Spark Context, an object which can be regarded as a channel to access all Spark

functionalities.

Obs.: Spark 2.0.0 introduced a Spark Session object which encapsulates the Spark Context

and provides more functionalities.

o Distributes tasks to the executors.

o Monitors the status of the execution.

The driver runs Java/Python/Scala/R code through the Spark’s API.

  • Executors (a.k.a. workers): execute the tasks assigned (through Scala code) by the driver and report

their status to the driver.

  • Cluster manager : when the application is run on a distributed platform, the cluster manager

controls the physical machines (i.e., compute nodes) and allocates resources to applications.

EXECUTION : when the application is run on a single machine (i.e., local mode) both driver and executors

run on that machine as threads. If instead the application is run on a distributed platform (i.e., cluster

mode) driver and executors can run on different machine.

MAP/REDUCE TASKS : if the application implements a MapReduce computation, a task assigned to an

executor typically comprises running a map function on several key-value pairs or a reduce function on

several key-listOfValues pairs.

→ Obs: The code for map/reduce functions is executed on one pair at a time. Spark tries to assign,

map/reduce tasks to minimize data movement and to maximize caching.

Resilient Distributed Dataset (RDD)

RDD is a fundamental abstraction in Spark. An RDD is a collection of elements of the same type, partitioned

and distributed across several machines (if available). An RDD provides an interface based on coarse-

grained transformations and ensure fault-tolerance.

RDDs are created from data in stable storage (e.g., HDFS), or from other RDDs, through transformations.

RDDs are immutable (i.e., read-only) and they are materialized only when needed (lazy evaluation).

Spark maintains the lineage of each RDD, namely the sequence of transformations that generate it, which

enable to materialize it or reconstruct it after a failure, starting from data in stable storage.

Partitioning

Partitioning is the key ingredient for efficiency: each RDD is broken into chunks called partitions which are

distributed among the available machines. A program can specify the number of partitions for each RDD (if

not, Spark will choose one).

Partitions are created by default (using a HashPartitioner, based on objects’ hash codes) or through a

custom partitioner.

Typical number of partitions: is 2x/3x the number of cores, which helps balancing the work.

Partitioning is exploited to enhance performance:

  • Spark creates map tasks so to make each executor apply the map function on data from a locally

stored partition (if possible).

  • To implement algorithms that require partitioning, the programmer can explicitly access RDD

partitions (e.g., mapPartition method).

  • RDD partitions are exploited implicitly by some ready-made Spark aggregation primitives (e.g.,

reduceByKey method).

Operations

The following types of operations can be performed on an RDD A.

  • TRANSFORMATIONS. A transformation generates a new RDD B starting from the data in A. We

distinguish between:

o Narrow transformation. Each partition of A contributes to (at most) one partition of B ,

which is stored in the same machine. No shuffling of data across machines is needed (→

maximum parallelism). E.g., map method.

o Wide transformation. Each partition of A may contribute to many partitions of B. Hence,

shuffling of data across machines may be required. E.g., groupByKey method.

  • ACTIONS. An action is a computation on the elements of A which returns a value to the application.

E.g., count method.

LAZY EVALUATION : RDD A is materialized only when an action is performed. This must be carefully

taken into account when measuring remaining times.

  • Persistence. Methods like persist or cache will save the RDD data in memory after the subsequent

action.

Cache: data are stored in RAM (if they fit) and data that do not fit are recomputed if needed.

Persist: data are stored as instructed. E.g., RAM+DISK.

Implementing a MapReduce algorithm/round in Spark.

RDDs represent the most basic data model in Spark: it is low-level and schema-less.

On top of RDDs, the Spark SQL module provides APIs to operate on the following structured data (similar to

tables in Relational DB):

  • DataFrame: distributed collection of data organized into named columns.
  • Dataset: extension of DataFrame (available only in Java and Scala) with type-safe, object-oriented

programming interface.

Applications in many different fields. Some examples:

  • Marketing: to segment (potential) customers.
  • Biology: to identify protein families.
  • Image processing: to do object recognition.
  • Information retrieval: to categorize documents or web pages.
  • Network analysis: to identify communities.
  • Facility location: to decide where to open network hubs/facilities.

Metric Space

Typically, the input of a clustering problem consists of a set of points from a metric space.

Definition : a metric space is an ordered pair

where 𝑀 is a set and 𝑑

is a metric on 𝑀, i.e., a

function 𝑑 ∶ 𝑀 × 𝑀 → ℝ such that for every 𝑥, 𝑦, 𝑧 ∈ 𝑀 the following holds:

  • 𝑑(𝑥, 𝑦) = 0 if and only if 𝑥 = 𝑦.
  • 𝑑(𝑥, 𝑦) = 𝑑(𝑦, 𝑥) → (symmetry).

→ (triangle inequality).

Remark: The choice of the function may have a significant impact on the effectiveness of the cluster

analysis.

Triangular inequality implies that:

~𝑑(𝑥,𝑦)

~𝑑(𝑥,𝑦)

For what concerns 𝑥, 𝑦 and 𝑧 are almost indistinguishable, and only one of the two can be retained.

Distance functions

Minkowski distances. Let 𝑋, 𝑌 ∈ ℝ

𝑛

, with 𝑋 = (𝑥

1

2

𝑛

) and 𝑌 = (𝑦

1

2

𝑛

). For 𝑟 > 0 the 𝐿

𝑟

𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 between 𝑋 and 𝑌 (a.k.a. 𝐿 𝑟

𝐿

𝑟

𝑖

𝑖

𝑟

𝑛

𝑖= 1

1

𝑟

  • 𝑟 = 2 (Euclidean distance): standard distance in ℝ

𝑛

(also denoted by || ∙ ||).

  • 𝑟 = 1 (Manhattan distance): used in grid-like environments.
  • 𝑟 = ∞ (Chebyshev distance): maximum absolute differences of coordinates (i.e., the limit for 𝑟 that

tends to ∞).

Cosine (or angular) distance. It is used when points are vectors in ℝ

𝑛

. Given 𝑋 = (𝑥

1

2

𝑛

) and 𝑌 =

1

2

𝑛

in ℝ

𝑛

, their cosine distance is the angle between them, that is,

𝑐𝑜𝑠𝑖𝑛𝑒

= arccos (

) = arccos (

𝑖

𝑖

𝑛

𝑖= 1

𝑖

2

𝑛

𝑖= 1

𝑖

2

𝑛

𝑖= 1

[
]

Example: For 𝑋 = (1, 2, −1) and 𝑌 = (2, 1, 1), 𝑑

𝑐𝑜𝑠𝑖𝑛𝑒

(𝑋, 𝑌) = arccos (

3

6

) = arccos (

1

2

𝜋

3

For non-negative coordinates, 𝑑

𝑐𝑜𝑠𝑖𝑛𝑒

(𝑋, 𝑌) ∈ [ 0 ,

𝜋

2

], while for arbitrary coordinates the range is [ 0 , 𝜋]. To

satisfy the second property of metric spaces, scalar multiples of a vector are regarded as the same vector.

→ Remark. Used in information retrieval to assess similarity between documents. A document 𝑋 over a

vocabulary of 𝑛 words can be represented as an n - vector, where 𝑥 𝑖

is the number of occurrences of the i - th

word of the vocabulary in 𝑋.

Hamming distance. Used when points are binary vectors over some n - dimensional space: 𝑋, 𝑌 ∈ { 0 , 1 }

𝑛

The Hamming distance between two vectors is the number of coordinates in which they differ.

𝐻𝑎𝑚𝑚𝑖𝑛𝑔

𝑖

𝑖

Example: for 𝑋 = (0, 1, 1, 0, 1) and 𝑌 = (1, 1, 1, 0, 0), 𝑑

𝐻𝑎𝑚𝑚𝑖𝑛𝑔

Observation: 𝑑 𝐻𝑎𝑚𝑚𝑖𝑛𝑔

𝐿

1

Jaccard distance. Used when points are sets (e.g., documents seen as bags of words). Let 𝑆 and 𝑇 be two

sets over the same ground set of elements. The Jaccard distance between 𝑆 and 𝑇 is:

𝐽𝑎𝑐𝑐𝑎𝑟𝑑

Note that the distance ranges in [ 0 , 1 ], and it is 0 iff 𝑆 = 𝑇 and 1 i.f.f. 𝑆 and 𝑇 are disjoint. The value

| 𝑆∩𝑇

|

| 𝑆∪𝑇

|

is

referred to as the Jaccard similarity of the two sets.

Minkowski and Cosine distances are used when an object is characterized by the numerical values of its

features (e.g., frequency).

  • Minkowski distance: it aggregates the gap in each dimension between two objects. Examples: GPS

coordinates; measurements of physical/chemical processes.

  • Cosine distance: it measures the ratios among features’ values, rather than their absolute values.

Example: distance between documents of different lengths, where each document is characterized

by the relative frequency of words from a given vocabulary.

Hamming and Jaccard distances are used when an object is characterized by having or not having some

features (no multiplicity).

  • Hamming distance: it measures the total number of differences between two objects. Examples:

fixed-length binary words, genetic distance.

  • Jaccard distance: it measures the ratio between the number of differences between two sets and

the total number of points in the sets. Example: the topics describing a document.

Observation: All of the above distances satisfy the four requirements for a metric space.

Clustering

Given a set of points in a metric space, a clustering problem often specifies an objective function to

optimize. The objective function also allows to compare different solutions.

Clustering problems can be categorized based on whether or not:

  • A target number 𝑘 of clusters is given in input.
  • For each cluster a center must be identified.
  • Disjoint clusters are sought.

Optimization problem Π is defined by:

  • Set 𝐼 of instances (i.e., possible inputs).
  • ∀𝑖 ∈ 𝐼, set 𝑆

𝑖

of feasible solutions.

  • Objective function Φ.

Observation: for k - means and k - median, minimizing the sum of (squared) distances is equivalent to

minimizing the average (squared) distance.

Formal definitions of the problems : for any point 𝑥 ∈ 𝑃 and any subset 𝑆 ⊆ 𝑃 we define:

𝑑(𝑥, 𝑆) = min

𝑦∈𝑆

Definition : Let

be a metric space. The k - center/ k - means/ k - median clustering problems are

optimization problems that, given a finite pointset 𝑃 ⊆ 𝑀 and an integer 𝑘 ≤

, require to return the

subset 𝑆 ⊆ 𝑃 of 𝑘 centers which minimizes the following respective objective functions:

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

= max

𝑥∈𝑃

( k - center clustering).

𝑘𝑚𝑒𝑎𝑛𝑠

2

𝑥∈𝑃

( k - means clustering).

𝑘𝑚𝑒𝑑𝑖𝑎𝑛

𝑥∈𝑃

( k - median clustering).

For the k - center/means/median clustering problems we have that:

INPUT: Pointset 𝑃 and integer 𝑘.

FEASIBLE SOLUTIONS: All subsets 𝑆 ⊆ 𝑃 of size 𝑘.

OPTIMAL SOLUTION: 𝑆

∗

(i.e., the feasible solution which minimizes the appropriate objective function).

We also define:

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

𝑜𝑝𝑡

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

∗

𝑘𝑚𝑒𝑎𝑛𝑠

𝑜𝑝𝑡

𝑘𝑚𝑒𝑎𝑛𝑠

∗

𝑘𝑚𝑒𝑑𝑖𝑎𝑛

𝑜𝑝𝑡

𝑘𝑚𝑒𝑑𝑖𝑎𝑛

∗

Remark: for the above 3 clustering problems, given a set of centers 𝑆, the best clustering of 𝑃 around these

centers is the one that assigns each 𝑥 ∈ 𝑃 to the cluster of the closest center (ties broken arbitrarily), which

can be computed efficiently.

Input 𝑃, 𝑆

Output ∀𝑥 ∈ 𝑃, 𝑥. 𝑐𝑒𝑛𝑡𝑒𝑟 = center of 𝑆 closest to 𝑥

Foreach 𝑐 ∈ 𝑆 do 𝑐. 𝑐𝑒𝑛𝑡𝑒𝑟 = 𝑐

Foreach 𝑥 ∈ 𝑃 − 𝑆 do

𝑥

← center of 𝑆 closest to 𝑥

𝑥

→ Observations: The k - center/means/median problems are NP-hard so it is impractical to search for the

optimal solution 𝑆

∗

. There are several efficient approximation algorithms that in practice return good-

quality solutions. However, dealing efficiently with large inputs is still a challenge! How do we decide which

problem suites better our needs?

  • k - center is useful when we need to guarantee that every point is close to a center (e.g., if centers

represent locations of crucial facilities or if they are needed as “representatives” for the data). As

we will see later, it is sensitive to noise.

  • k - means/median provide guarantees on the average (square) distances. k - means is more sensitive

to noise but there exist faster algorithms to solve it.

k - center and k - median belong to the family of facility-location problems.

k - center / Farthest-First Traversal

The algorithm is simple and, somewhat fast, implementation when the input fits in main memory. Powerful

primitive for extracting samples for further analyses.

Input : Set 𝑃 of 𝑁 points from a metric space

, integer 𝑘 > 1.

Output : A set 𝑆 of 𝑘 centers which is a good solution to the k - center problem on 𝑃 (i.e., Φ

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

“close” to Φ

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

𝑜𝑝𝑡

1

1

∈ 𝑃 arbitrary point

for 𝑖 ← 2 to 𝑘 do

Find the point 𝑐

𝑖

∈ 𝑃 − 𝑆 that maximizes 𝑑

𝑖

𝑖

return 𝑆

→ Observation: The best clustering around the centers of 𝑆 can be computed by invoking 𝐴𝑠𝑠𝑖𝑔𝑛(𝑃, 𝑆). In

fact, the assignment of each point to the closest center can be easily maintained in every iteration of the

for-loop.

Theorem : Let 𝑆 be the set of centers returned by running Farthest-First Traversal on 𝑃. Then:

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

𝑜𝑝𝑡

That is, Farthest-First Traversal is a 2 - approximation algorithm.

Proof : let 𝑆 = {𝑐 1

2

𝑘

𝑖

≡ i - th center computed by FFT.

Define 𝑞 ≔ point of 𝑃 farthest from the closest center in 𝑆.

Let 𝑆

∗

1

∗

2

∗

𝑘

∗

optimal set of centers.

Hence:

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

𝑜𝑝𝑡

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

∗

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

∗

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

𝑜𝑝𝑡

For 𝑡 = 1 , 2 , … , 𝑘, 𝐶 𝑡

∗

𝑡

∗

1

∗

2

∗

𝑘

∗

Consider the set {𝑐 1

2

𝑘

𝑆

𝑐 𝑘+ 1

} which contains 𝑘 + 1 points.

By pigeonhole principle there must be points of {𝑐 1

2

𝑘

𝑘+ 1

}, say 𝑎, 𝑏, that belong to the same 𝐶

𝑡

∗

By triangular inequality 𝑑(𝑎, 𝑏) ≤ 𝑑(𝑎, 𝑐

𝑡

∗

𝑡

∗

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

𝑜𝑝𝑡

(𝑃, 𝑘). Without loss of generality let

𝑖

𝑗

with 1 ≤ 𝑖 < 𝑗 ≤ 𝑘 + 1.

Now ∀𝑥 ∈ 𝑃, 𝑑(𝑥, 𝑆) = 𝑑(𝑥, {𝑐

1

2

𝑘

1

2

𝑗− 1

}) since {𝑐

1

2

𝑗− 1

} is subset of 𝑆

𝑗

1

2

𝑗− 1

}) by the way FFT selects the center

𝑗

𝑖

) since 𝑐

𝑖

1

2

𝑗− 1

𝑘𝑐𝑒𝑛𝑡𝑒𝑟

𝑜𝑝𝑡

→ Observations on k - center clustering:

  • The k - center objective focuses on worst-case distance of points from their closest centers.
  • Farthest-First Traversal’s approximation guarantees are almost the best one can obtain in practice.

It was proved that computing a c - approximate solution to k - center is NP-hard for any fixed 𝑐 < 2.

𝑡

∗

a

𝑡

∗

b