![Page 1: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/1.jpg)
MapReduce Theory, Implementation and Algorithms
http://net.pku.edu.cn/~course/cs402
Hongfei YanSchool of EECS, Peking University
7/1/2008
Refer to Aaron Kimball’s slides
![Page 2: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/2.jpg)
Outline
• Functional Programming Recap
• MapReduce Theory & Implementation
• MapReduce Algorithms
![Page 3: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/3.jpg)
What is Functional Programming?
• Functional programming is style of programming in which the basic method of computation is the application of functions to arguments;
• A functional language is one that supports and encourages the functional style.
Opinions differ, and it is difficult to give a precise definition, but generally speaking:
![Page 4: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/4.jpg)
Example
Summing the integers 1 to 10 in Java:
total = 0;
for (i = 1; i 10; ++i)
total = total+i;
The computation method is variable assignment.
4
![Page 5: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/5.jpg)
Example
Summing the integers 1 to 10 in Haskell:
sum [1..10]
The computation method is function application.
5
![Page 6: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/6.jpg)
Why is it Useful?
Again, there are many possible answers to this question, but generally speaking:
• The abstract nature of functional programming leads to considerably simpler programs;
• It also supports a number of powerful new ways to structure and reason about programs.
![Page 7: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/7.jpg)
What is Hugs?
• An interpreter for Haskell, and the most widely used implementation of the language;
• An interactive system, which is well-suited for teaching and prototyping purposes;
• Hugs is freely available from:
www.haskell.org/hugs
![Page 8: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/8.jpg)
The Standard Prelude
When Hugs is started it first loads the library file Prelude.hs, and then repeatedly prompts the user for an expression to be evaluated.
For example:
> 2+3*414
> (2+3)*420
![Page 9: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/9.jpg)
> length [1,2,3,4]4
> product [1,2,3,4]24
> take 3 [1,2,3,4,5][1,2,3]
The standard prelude also provides many useful functions that operate on lists. For example:
![Page 10: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/10.jpg)
Function Application
In mathematics, function application is denoted using parentheses, and multiplication is often denoted using juxtaposition or space.
f(a,b) + c d
Apply the function f to a and b, and add the result to the product of c
and d.
![Page 11: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/11.jpg)
In Haskell, function application is denoted using space, and multiplication is denoted using *.
f a b + c*d
As previously, but in Haskell syntax.
![Page 12: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/12.jpg)
Functional Programming Review
• Functional operations do not modify data structures: They always create new ones
• Original data still exists in unmodified form
• Data flows are implicit in program design
• Order of operations does not matter
![Page 13: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/13.jpg)
Functional Programming Review
fun foo(l: int list) =
sum(l) + mul(l) + length(l)
• Order of sum() and mul(), etc does not matter
• They do not modify l
![Page 14: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/14.jpg)
Functional Updates Do Not Modify Structures
fun append(x, lst) =
let lst' = reverse lst in reverse ( x :: lst' )
The append() function above reverses a list, adds a new element to the front, and returns all of that, reversed, which appends an item.
But it never modifies lst!
![Page 15: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/15.jpg)
Functions Can Be Used As Arguments
fun DoDouble(f, x) = f (f x)
It does not matter what f does to its argument; DoDouble() will do it twice.
A function is called higher-order if it takes a function as an argument or returns a function as a result
![Page 16: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/16.jpg)
Map
map f lst: (’a->’b) -> (’a list) -> (’b list)
Creates a new list by applying f to each element of the input list; returns output in order.
f f f f f f
![Page 17: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/17.jpg)
Fold
fold f x0 lst: ('a*'b->'b)->'b->('a list)->'b
Moves across a list, applying f to each element plus an accumulator. f returns the next accumulator value, which is combined with the next element of the list
f f f f f returned
initial
![Page 18: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/18.jpg)
fold left vs. fold right
• Order of list elements can be significant• Fold left moves left-to-right across the list• Fold right moves from right-to-left
SML Implementation:
fun foldl f a [] = a | foldl f a (x::xs) = foldl f (f(x, a)) xs
fun foldr f a [] = a | foldr f a (x::xs) = f(x, (foldr f a xs))
![Page 19: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/19.jpg)
Example
fun foo(l: int list) =
sum(l) + mul(l) + length(l)
How can we implement this?
![Page 20: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/20.jpg)
Example (Solved)
fun foo(l: int list) =
sum(l) + mul(l) + length(l)
fun sum(lst) = foldl (fn (x,a)=>x+a) 0 lst
fun mul(lst) = foldl (fn (x,a)=>x*a) 1 lst
fun length(lst) = foldl (fn (x,a)=>1+a) 0 lst
![Page 21: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/21.jpg)
map Implementation
• This implementation moves left-to-right across the list, mapping elements one at a time
• … But does it need to?
fun map f [] = [] | map f (x::xs) = (f x) :: (map f xs)
![Page 22: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/22.jpg)
Implicit Parallelism In map
• In a purely functional setting, elements of a list being computed by map cannot see the effects of the computations on other elements
• If order of application of f to elements in list is commutative, we can reorder or parallelize execution
• This is the “secret” that MapReduce exploits
![Page 23: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/23.jpg)
References
• http://net.pku.edu.cn/~course/cs501/2008/resource/haskell/functional.ppt
• http://net.pku.edu.cn/~course/cs501/2008/resource/haskell/
![Page 24: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/24.jpg)
Outline
• Functional Programming Recap
• MapReduce Theory & Implementation
• MapReduce Algorithms
![Page 25: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/25.jpg)
Motivation: Large Scale Data Processing
• Want to process lots of data ( > 1 TB)
• Want to parallelize across hundreds/thousands of CPUs
• … Want to make this easy
![Page 26: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/26.jpg)
MapReduce
• Automatic parallelization & distribution
• Fault-tolerant
• Provides status and monitoring tools
• Clean abstraction for programmers
![Page 27: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/27.jpg)
Programming Model
• Borrows from functional programming
• Users implement interface of two functions:
– map (in_key, in_value) ->
(out_key, intermediate_value) list
– reduce (out_key, intermediate_value list) ->
out_value list
![Page 28: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/28.jpg)
map
• Records from the data source (lines out of files, rows of a database, etc) are fed into the map function as key*value pairs: e.g., (filename, line).
• map() produces one or more intermediate values along with an output key from the input.
![Page 29: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/29.jpg)
reduce
• After the map phase is over, all the intermediate values for a given output key are combined together into a list
• reduce() combines those intermediate values into one or more final values for that same output key
• (in practice, usually only one final value per key)
![Page 30: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/30.jpg)
Data store 1 Data store nmap
(key 1, values...)
(key 2, values...)
(key 3, values...)
map
(key 1, values...)
(key 2, values...)
(key 3, values...)
Input key*value pairs
Input key*value pairs
== Barrier == : Aggregates intermediate values by output key
reduce reduce reduce
key 1, intermediate
values
key 2, intermediate
values
key 3, intermediate
values
final key 1 values
final key 2 values
final key 3 values
...
![Page 31: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/31.jpg)
reducereduce (out_key, intermediate_value list) ->
out_value list
returned
initial
![Page 32: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/32.jpg)
Parallelism
• map() functions run in parallel, creating different intermediate values from different input data sets
• reduce() functions also run in parallel, each working on a different output key
• All values are processed independently• Bottleneck: reduce phase can’t start until
map phase is completely finished.
![Page 33: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/33.jpg)
Example: Count word occurrencesmap(String input_key, String input_value):
// input_key: document name
// input_value: document contents
for each word w in input_value:
EmitIntermediate(w, "1");
reduce(String output_key, Iterator intermediate_values):
// output_key: a word
// output_values: a list of counts
int result = 0;
for each v in intermediate_values:
result += ParseInt(v);
Emit(AsString(result));
![Page 34: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/34.jpg)
Example vs. Actual Source Code
• Example is written in pseudo-code
• Actual implementation is in C++, using a MapReduce library
• Bindings for Python and Java exist via interfaces
• True code is somewhat more involved (defines how the input key/values are divided up and accessed, etc.)
![Page 35: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/35.jpg)
Locality
• Master program divides up tasks based on location of data: tries to have map() tasks on same machine as physical file data, or at least same rack
• map() task inputs are divided into 64 MB blocks: same size as Google File System chunks
![Page 36: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/36.jpg)
Fault Tolerance
• Master detects worker failures– Re-executes completed & in-progress map()
tasks– Re-executes in-progress reduce() tasks
• Master notices particular input key/values cause crashes in map(), and skips those values on re-execution.– Effect: Can work around bugs in third-party
libraries!
![Page 37: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/37.jpg)
Optimizations
• No reduce can start until map is complete:– A single slow disk controller can rate-limit the
whole process
• Master redundantly executes “slow-moving” map tasks; uses results of first copy to finish
Why is it safe to redundantly execute map tasks? Wouldn’t this mess up the total computation?
![Page 38: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/38.jpg)
Optimizations
• “Combiner” functions can run on same machine as a mapper
• Causes a mini-reduce phase to occur before the real reduce phase, to save bandwidth
Under what conditions is it sound to use a combiner?
![Page 39: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/39.jpg)
The Example Againmap(String input_key, String input_value):
// input_key: document name
// input_value: document contents
for each word w in input_value:
EmitIntermediate(w, "1");
reduce(String output_key, Iterator intermediate_values):
// output_key: a word
// output_values: a list of counts
int result = 0;
for each v in intermediate_values:
result += ParseInt(v);
Emit(AsString(result));
![Page 40: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/40.jpg)
MapReduce Conclusions
• MapReduce has proven to be a useful abstraction
• Greatly simplifies large-scale computations at Google
• Functional programming paradigm can be applied to large-scale applications
• Fun to use: focus on problem, let library deal w/ messy details
![Page 41: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/41.jpg)
References
• [Dean and Ghemawat,2004] J. Dean and S. Ghemawat, "MapReduce: Simplified Data Processing on Large Clusters," presented at OSDI'04: Sixth Symposium on Operating System Design and Implementation, San Francisco, CA, 2004. http://net.pku.edu.cn/~course/cs501/2008/resource/mapreduce_in_a_week/mapreduce-osdi04.pdf
![Page 42: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/42.jpg)
Outline
• Functional Programming Recap
• MapReduce Theory & Implementation
• MapReduce Algorithms
![Page 43: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/43.jpg)
Algorithms for MapReduce
• Sorting• Searching• Indexing• Classification• TF-IDF• Breadth-First Search / SSSP• PageRank• Clustering
![Page 44: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/44.jpg)
MapReduce Jobs
• Tend to be very short, code-wise– IdentityReducer is very common
• “Utility” jobs can be composed
• Represent a data flow, more so than a procedure
![Page 45: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/45.jpg)
Sort: Inputs
• A set of files, one value per line.
• Mapper key is file name, line number
• Mapper value is the contents of the line
![Page 46: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/46.jpg)
Sort Algorithm
• Takes advantage of reducer properties: (key, value) pairs are processed in order by key; reducers are themselves ordered
• Mapper: Identity function for value(k, v) (v, _)
• Reducer: Identity function (k’, _) -> (k’, “”)
![Page 47: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/47.jpg)
Sort: The Trick
• (key, value) pairs from mappers are sent to a particular reducer based on hash(key)
• Must pick the hash function for your data such that k1 < k2 => hash(k1) < hash(k2)
M1 M2 M3
R1 R2
Partition and
Shuffle
![Page 48: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/48.jpg)
Final Thoughts on Sort
• Used as a test of Hadoop’s raw speed
• Essentially “IO drag race”
• Highlights utility of GFS
![Page 49: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/49.jpg)
Search: Inputs
• A set of files containing lines of text
• A search pattern to find
• Mapper key is file name, line number
• Mapper value is the contents of the line
• Search pattern sent as special parameter
![Page 50: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/50.jpg)
Search Algorithm
• Mapper:– Given (filename, some text) and “pattern”, if
“text” matches “pattern” output (filename, _)
• Reducer:– Identity function
![Page 51: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/51.jpg)
Search: An Optimization
• Once a file is found to be interesting, we only need to mark it that way once
• Use Combiner function to fold redundant (filename, _) pairs into a single one– Reduces network I/O
![Page 52: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/52.jpg)
Indexing: Inputs
• A set of files containing lines of text
• Mapper key is file name, line number
• Mapper value is the contents of the line
![Page 53: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/53.jpg)
Inverted Index Algorithm
• Mapper: For each word in (file, words), map to (word, file)
• Reducer: Identity function
![Page 54: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/54.jpg)
Inverted Index: Data flow
This page contains so much text
My page contains text too
Foo
Bar
contains: BarMy: Barpage : Bartext: Bartoo: Bar
contains: Foomuch: Foopage : Fooso : Footext: FooThis : Foo
contains: Foo, Barmuch: FooMy: Barpage : Foo, Barso : Footext: Foo, BarThis : Footoo: Bar
Reduced output
Foo map output
Bar map output
![Page 55: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/55.jpg)
An Aside: Word Count
• Word count was described in codelab I
• Mapper for Word Count is (word, 1) for each word in input line– Strikingly similar to inverted index– Common theme: reuse/modify existing
mappers
![Page 56: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/56.jpg)
Bayesian Classification
• Files containing classification instances are sent to mappers
• Map (filename, instance) (instance, class)
• Identity Reducer
![Page 57: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/57.jpg)
Bayesian Classification
• Existing toolsets exist to perform Bayes classification on instance– E.g., WEKA, already in Java!
• Another example of discarding input key
![Page 58: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/58.jpg)
TF-IDF
• Term Frequency – Inverse Document Frequency– Relevant to text processing– Common web analysis algorithm
![Page 59: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/59.jpg)
The Algorithm, Formally
•| D | : total number of documents in the corpus • : number of documents where the term ti appears (that is ).
![Page 60: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/60.jpg)
Information We Need
• Number of times term X appears in a given document – word frequency
• Number of terms in each document – word count for a document
• Number of documents X appears in - Doc Frequency In Corpus
• Total number of documents
![Page 61: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/61.jpg)
Job 1: Word Frequency in Doc
• Mapper– Input: (docname, contents)– Output: ((word, docname), 1)
• Reducer– Sums counts for word in document– Outputs ((word, docname), n)
• Combiner is same as Reducer
![Page 62: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/62.jpg)
Job 2: Word Counts For Docs
• Mapper– Input: ((word, docname), n)– Output: (docname, (word, n))
• Reducer– Sums frequency of individual n’s in same doc– Feeds original data through– Outputs ((word, docname), (n, N))
![Page 63: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/63.jpg)
Job 3: Doc Frequency In Corpus
• Mapper– Input: ((word, docname), (n, N))– Output: (word, (docname, n, N, 1))
• Reducer– Sums counts for word in corpus– Outputs ((word, docname), (n, N, m))
![Page 64: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/64.jpg)
Job 4: Calculate TF-IDF
• Mapper– Input: ((word, docname), (n, N, m))– Assume D is known (or, easy MR to find it)– Output ((word, docname), TF*IDF)
• Reducer– Just the identity function
![Page 65: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/65.jpg)
Final Thoughts on TF-IDF
• Several small jobs add up to full algorithm
• Lots of code reuse possible– Stock classes exist for aggregation, identity
• Jobs 3 and 4 can really be done at once in same reducer, saving a write/read cycle
![Page 66: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/66.jpg)
BFS: Motivating Concepts
• Performing computation on a graph data structure requires processing at each node
• Each node contains node-specific data as well as links (edges) to other nodes
• Computation must traverse the graph and perform the computation step
• How do we traverse a graph in MapReduce?
• How do we represent the graph for this?
![Page 67: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/67.jpg)
Breadth-First Search
• Breadth-First Search is an iterated algorithm over graphs
• Frontier advances from origin by one level with each pass
12
2 2
3
3
3
3
4
4
![Page 68: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/68.jpg)
Breadth-First Search & MapReduce(1/2)
• Problem: – This doesn't “fit” into MapReduce
• Solution:– Iterated passes through MapReduce – map
some nodes, result includes additional nodes which are fed into successive MapReduce passes
![Page 69: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/69.jpg)
Breadth-First Search & MapReduce(2/2)
• Problem: – Sending the entire graph to a map task (or
hundreds/thousands of map tasks) involves an enormous amount of memory
• Solution: – Carefully consider how we represent graphs
![Page 70: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/70.jpg)
Graph Representations
• The most straightforward representation of graphs uses references from each node to its neighbors
![Page 71: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/71.jpg)
Direct References
• Structure is inherent to object
• Iteration requires linked list “threaded through” graph
• Requires common view of shared memory (synchronization!)
• Not easily serializable
class GraphNode{ Object data; Vector<GraphNode> out_edges; GraphNode iter_next;}
![Page 72: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/72.jpg)
Adjacency Matrices
• Another classic graph representation. M[i][j]= '1' implies a link from node i to j.
• Naturally encapsulates iteration over nodes
01014
00103
11012
10101
4321
![Page 73: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/73.jpg)
Adjacency Matrices: Sparse Representation
• Adjacency matrix for most large graphs (e.g., the web) will be overwhelmingly full of zeros.
• Each row of the graph is absurdly long
• Sparse matrices only include non-zero elements
![Page 74: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/74.jpg)
Sparse Matrix Representation
1: (3, 1), (18, 1), (200, 1)2: (6, 1), (12, 1), (80, 1), (400, 1)3: (1, 1), (14, 1)…
1: 3, 18, 2002: 6, 12, 80, 4003: 1, 14…
![Page 75: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/75.jpg)
Finding the Shortest Path
• A common graph search application is finding the shortest path from a start node to one or more target nodes
• Commonly done on a single machine with Dijkstra's Algorithm
• Can we use BFS to find the shortest path via MapReduce?
This is called the single-source shortest path problem. (a.k.a. SSSP)
![Page 76: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/76.jpg)
Finding the Shortest Path: Intuition
• We can define the solution to this problem inductively: – DistanceTo(startNode) = 0– For all nodes n directly reachable from
startNode, DistanceTo(n) = 1– For all nodes n reachable from some other set
of nodes S, DistanceTo(n) = 1 + min(DistanceTo(m), m S)
![Page 77: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/77.jpg)
From Intuition to Algorithm
• A map task receives a node n as a key, and (D, points-to) as its value– D is the distance to the node from the start– points-to is a list of nodes reachable from n– p points-to, emit (p, D+1)
• Reduce task gathers possible distances to a given p and selects the minimum one
![Page 78: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/78.jpg)
What This Gives Us
• This MapReduce task can advance the known frontier by one hop
• To perform the whole BFS, a non-MapReduce component then feeds the output of this step back into the MapReduce task for another iteration– Problem: Where'd the points-to list go?– Solution: Mapper emits (n, points-to) as well
![Page 79: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/79.jpg)
Blow-up and Termination
• This algorithm starts from one node• Subsequent iterations include many more
nodes of the graph as frontier advances• Does this ever terminate?
– Yes! Eventually, routes between nodes will stop being discovered and no better distances will be found. When distance is the same, we stop
– Mapper should emit (n, D) to ensure that “current distance” is carried into the reducer
![Page 80: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/80.jpg)
Adding weights
• Weighted-edge shortest path is more useful than cost==1 approach
• Simple change: points-to list in map task includes a weight 'w' for each pointed-to node– emit (p, D+w
p) instead of (p, D+1) for each
node p– Works for positive-weighted graph
![Page 81: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/81.jpg)
Comparison to Dijkstra
• Dijkstra's algorithm is more efficient because at any step it only pursues edges from the minimum-cost path inside the frontier
• MapReduce version explores all paths in parallel; not as efficient overall, but the architecture is more scalable
• Equivalent to Dijkstra for weight=1 case
![Page 82: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/82.jpg)
PageRank: Random Walks Over The Web
• If a user starts at a random web page and surfs by clicking links and randomly entering new URLs, what is the probability that s/he will arrive at a given page?
• The PageRank of a page captures this notion– More “popular” or “worthwhile” pages get a
higher rank
![Page 83: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/83.jpg)
PageRank: Visually
www.cnn.com
en.wikipedia.org
www.nytimes.com
![Page 84: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/84.jpg)
PageRank: Formula
Given page A, and pages T1 through T
n
linking to A, PageRank is defined as:
PR(A) = (1-d) + d (PR(T1)/C(T
1) + ... +
PR(Tn)/C(T
n))
C(P) is the cardinality (out-degree) of page Pd is the damping (“random URL”) factor
![Page 85: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/85.jpg)
PageRank: Intuition
• Calculation is iterative: PRi+1
is based on PRi
• Each page distributes its PRi to all pages it
links to. Linkees add up their awarded rank fragments to find their PR
i+1
• d is a tunable parameter (usually = 0.85) encapsulating the “random jump factor”
PR(A) = (1-d) + d (PR(T1)/C(T
1) + ... + PR(T
n)/C(T
n))
![Page 86: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/86.jpg)
PageRank: First Implementation
• Create two tables 'current' and 'next' holding the PageRank for each page. Seed 'current' with initial PR values
• Iterate over all pages in the graph, distributing PR from 'current' into 'next' of linkees
• current := next; next := fresh_table();• Go back to iteration step or end if converged
![Page 87: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/87.jpg)
Distribution of the Algorithm
• Key insights allowing parallelization:– The 'next' table depends on 'current', but not on
any other rows of 'next'– Individual rows of the adjacency matrix can be
processed in parallel– Sparse matrix rows are relatively small
![Page 88: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/88.jpg)
Distribution of the Algorithm
• Consequences of insights:– We can map each row of 'current' to a list of
PageRank “fragments” to assign to linkees– These fragments can be reduced into a single
PageRank value for a page by summing– Graph representation can be even more
compact; since each element is simply 0 or 1, only transmit column numbers where it's 1
![Page 89: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/89.jpg)
Map step: break page rank into even fragments to distribute to link targets
Reduce step: add together fragments into next PageRank
Iterate for next step...
![Page 90: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/90.jpg)
Phase 1: Parse HTML
• Map task takes (URL, page content) pairs and maps them to (URL, (PR
init, list-of-urls))
– PRinit
is the “seed” PageRank for URL– list-of-urls contains all pages pointed to by URL
• Reduce task is just the identity function
![Page 91: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/91.jpg)
Phase 2: PageRank Distribution
• Map task takes (URL, (cur_rank, url_list))– For each u in url_list, emit (u, cur_rank/|url_list|)– Emit (URL, url_list) to carry the points-to list
along through iterations
PR(A) = (1-d) + d (PR(T1)/C(T
1) + ... + PR(T
n)/C(T
n))
![Page 92: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/92.jpg)
Phase 2: PageRank Distribution
• Reduce task gets (URL, url_list) and many (URL, val) values– Sum vals and fix up with d– Emit (URL, (new_rank, url_list))
PR(A) = (1-d) + d (PR(T1)/C(T
1) + ... + PR(T
n)/C(T
n))
![Page 93: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/93.jpg)
Finishing up...
• A non-parallelizable component determines whether convergence has been achieved (Fixed number of iterations? Comparison of key values?)
• If so, write out the PageRank lists - done!• Otherwise, feed output of Phase 2 into
another iteration
![Page 94: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/94.jpg)
PageRank Conclusions• MapReduce isn't the greatest at iterated
computation, but still helps run the “heavy lifting”
• Key element in parallelization is independent PageRank computations in a given step
• Parallelization requires thinking about minimum data partitions to transmit (e.g., compact representations of graph rows)– Even the implementation shown today doesn't
actually scale to the whole Internet; but it works for intermediate-sized graphs
![Page 95: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/95.jpg)
Clustering
• What is clustering?
![Page 96: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/96.jpg)
Google News
• They didn’t pick all 3,400,217 related articles by hand…
• Or Amazon.com
• Or Netflix…
![Page 97: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/97.jpg)
Other less glamorous things...
• Hospital Records• Scientific Imaging
– Related genes, related stars, related sequences• Market Research
– Segmenting markets, product positioning• Social Network Analysis• Data mining• Image segmentation…
![Page 98: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/98.jpg)
The Distance Measure
• How the similarity of two elements in a set is determined, e.g.– Euclidean Distance– Manhattan Distance– Inner Product Space– Maximum Norm – Or any metric you define over the space…
![Page 99: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/99.jpg)
• Hierarchical Clustering vs.• Partitional Clustering
Types of Algorithms
![Page 100: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/100.jpg)
Hierarchical Clustering
• Builds or breaks up a hierarchy of clusters.
![Page 101: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/101.jpg)
Partitional Clustering
• Partitions set into all clusters simultaneously.
![Page 102: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/102.jpg)
Partitional Clustering
• Partitions set into all clusters simultaneously.
![Page 103: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/103.jpg)
K-Means Clustering • Simple Partitional Clustering
• Choose the number of clusters, k• Choose k points to be cluster centers• Then…
![Page 104: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/104.jpg)
K-Means Clustering
iterate { Compute distance from all points to all k- centers Assign each point to the nearest k-center Compute the average of all points assigned to all specific k-centers
Replace the k-centers with the new averages}
![Page 105: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/105.jpg)
But!
• The complexity is pretty high: – k * n * O ( distance metric ) * num (iterations)
• Moreover, it can be necessary to send tons of data to each Mapper Node. Depending on your bandwidth and memory available, this could be impossible.
![Page 106: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/106.jpg)
Furthermore
• There are three big ways a data set can be large:– There are a large number of elements in the
set.– Each element can have many features.– There can be many clusters to discover
• Conclusion – Clustering can be huge, even when you distribute it.
![Page 107: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/107.jpg)
Canopy Clustering
• Preliminary step to help parallelize computation.
• Clusters data into overlapping Canopies using super cheap distance metric.
• Efficient
• Accurate
![Page 108: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/108.jpg)
Canopy Clustering
While there are unmarked points { pick a point which is not strongly marked call it a canopy centermark all points within some threshold of
it as in it’s canopystrongly mark all points within some
stronger threshold }
![Page 109: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/109.jpg)
After the canopy clustering…
• Resume hierarchical or partitional clustering as usual.
• Treat objects in separate clusters as being at infinite distances.
![Page 110: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/110.jpg)
MapReduce Implementation:
• Problem – Efficiently partition a large data set (say… movies with user ratings!) into a fixed number of clusters using Canopy Clustering, K-Means Clustering, and a Euclidean distance measure.
![Page 111: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/111.jpg)
The Distance Metric
• The Canopy Metric ($)
• The K-Means Metric ($$$)
![Page 112: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/112.jpg)
Steps!
• Get Data into a form you can use (MR)• Picking Canopy Centers (MR)• Assign Data Points to Canopies (MR)• Pick K-Means Cluster Centers• K-Means algorithm (MR)
– Iterate!
![Page 113: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/113.jpg)
Data Massage
• This isn’t interesting, but it has to be done.
![Page 114: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/114.jpg)
Selecting Canopy Centers
![Page 115: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/115.jpg)
![Page 116: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/116.jpg)
![Page 117: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/117.jpg)
![Page 118: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/118.jpg)
![Page 119: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/119.jpg)
![Page 120: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/120.jpg)
![Page 121: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/121.jpg)
![Page 122: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/122.jpg)
![Page 123: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/123.jpg)
![Page 124: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/124.jpg)
![Page 125: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/125.jpg)
![Page 126: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/126.jpg)
![Page 127: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/127.jpg)
![Page 128: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/128.jpg)
![Page 129: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/129.jpg)
![Page 130: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/130.jpg)
Assigning Points to Canopies
![Page 131: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/131.jpg)
![Page 132: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/132.jpg)
![Page 133: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/133.jpg)
![Page 134: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/134.jpg)
![Page 135: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/135.jpg)
![Page 136: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/136.jpg)
K-Means Map
![Page 137: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/137.jpg)
![Page 138: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/138.jpg)
![Page 139: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/139.jpg)
![Page 140: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/140.jpg)
![Page 141: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/141.jpg)
![Page 142: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/142.jpg)
![Page 143: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/143.jpg)
![Page 144: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/144.jpg)
Elbow Criterion• Choose a number of clusters s.t. adding a
cluster doesn’t add interesting information.
• Rule of thumb to determine what number of Clusters should be chosen.
• Initial assignment of cluster seeds has bearing on final model performance.
• Often required to run clustering several times to get maximal performance
![Page 145: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/145.jpg)
Clustering Conclusions
• Clustering is slick• And it can be done super efficiently• And in lots of different ways
![Page 146: MapReduce Theory, Implementation and Algorithms course/cs402 Hongfei Yan School of EECS, Peking University 7/1/2008 Refer to Aaron](https://reader030.vdocuments.mx/reader030/viewer/2022033017/56649e9a5503460f94b9ced7/html5/thumbnails/146.jpg)
Overall Conclusions
• Lots of high level algorithms
• Lots of deep connections to low-level systems
• Clean abstraction layer for programmers between the two