










































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!











































A typical cluster architecture:
racks) by fast switches (e.g., 10 Gbps Ethernet).
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.
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:
pair and produces ≥ 0 other key-value pairs, sometimes called intermediate pairs.
𝑘
of values of
intermediate pairs with key 𝑘.
𝑘
) 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:
previous rounds.
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:
and values may change from one round to another.
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.
A MapReduce (MR) algorithm should be specified so that:
specification.
o input, intermediate and output sets of key-value pairs.
o functions applied in the map and reduce phases.
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:
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:
𝐿
𝑖
𝑖
𝑚𝑎𝑥
𝑖
max
𝐿
= 𝑂(max{𝑁
𝑚𝑎𝑥
𝑚𝑎𝑥
𝐴
𝑖
𝑘
𝑖=𝑖
, total number of word occurrences across all documents
𝐴
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:
Design goals for MapReduce algorithms:
𝐿
𝜖
, with 𝜖 < 1 ).
𝐴
= 𝑂(|𝑖𝑛𝑝𝑢𝑡|)), or only slightly super linear.
These goals are conflicting with one another and, in practice, suitable tradeoffs must be sought.
1 ,4: aim at time efficiency.
2, 3: aim at space efficiency.
✓ 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).
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.
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:
𝐿
class label.
𝐿
o Map phase: 𝑂
o Reduce phase: 𝑂 (
𝑁
𝑙
) because of the even partition of the input pairs.
o Map phase: empty.
o Reduce phase: 𝑂(𝑙).
𝐿
= 𝑂 (max {
𝑁
𝑙
, 𝑙}) ⟹ setting 𝑙 = √
𝐿
𝐴
same argument as before
Observations:
𝑁 partitions (which we will often make) provides an example of a simple
tradeoff between local space and number of rounds.
yielding different round-space tradeoffs. One of the exercises will explore this.
long as local space is not an issue.
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
𝑖
𝑖
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 𝑥.
𝑥
𝐿
o Map phase: 𝑂( 1 ).
o Reduce phase: 𝑂
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 𝑚.
𝐿
= 𝑂 (max {
𝑁
𝑙
𝐿
= 𝑂(max{𝑚, 𝑙}).
Spark can run:
We need a framework like Spark to process bigdata efficiently and at moderate costs.
Spark:
→ Obs: Without careful management and coordination, the potential of the platform is likely to be wasted.
Components:
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.
their status to the driver.
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.
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:
stored partition (if possible).
partitions (e.g., mapPartition method).
reduceByKey method).
Operations
The following types of operations can be performed on an RDD A.
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.
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.
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):
programming interface.
Applications in many different fields. Some examples:
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:
→ (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.
Minkowski distances. Let 𝑋, 𝑌 ∈ ℝ
𝑛
, with 𝑋 = (𝑥
1
2
𝑛
) and 𝑌 = (𝑦
1
2
𝑛
). For 𝑟 > 0 the 𝐿
𝑟
𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 between 𝑋 and 𝑌 (a.k.a. 𝐿 𝑟
𝐿
𝑟
𝑖
𝑖
𝑟
𝑛
𝑖= 1
1
𝑟
𝑛
(also denoted by || ∙ ||).
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, 𝑑
𝑐𝑜𝑠𝑖𝑛𝑒
𝜋
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).
coordinates; measurements of physical/chemical processes.
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).
fixed-length binary words, genetic distance.
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.
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:
Optimization problem Π is defined by:
𝑖
of feasible solutions.
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 𝑘.
∗
(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?
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.
to noise but there exist faster algorithms to solve it.
k - center and k - median belong to the family of facility-location problems.
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:
It was proved that computing a c - approximate solution to k - center is NP-hard for any fixed 𝑐 < 2.
𝑡
∗
a
𝑡
∗
b