330 likes | 498 Views
Compsci 201 Percolation+Union-Find. Jeff Forbes March 2, 2018. M is for …. Machine Learning Combining Math, Stats, & CompSci to recognize patterns at scale Markov Forgetting the past to predict the future Method A function by any other name Monte Carlo Randomnesss -> solutions?.
E N D
Compsci 201Percolation+Union-Find Jeff Forbes March 2, 2018 CompSci 201, Spring 2018, Percolation+UF
M is for … • Machine Learning • Combining Math, Stats, & CompSci to recognize patterns at scale • Markov • Forgetting the past to predict the future • Method • A function by any other name • Monte Carlo • Randomnesss -> solutions? CompSci 201, Spring 2018, Percolation+UF
Plan for the Day • Review key parts of the Percolation Assignment • Monte Carlo simulations • PercolationDFS and PercolationUF • Recursive depth-first search AND union-find • Sorting: how is recursion used and useful? • Solving real problems • When should recursion be avoided CompSci 201, Spring 2018, Percolation+UF
Towards Percolation • A model for physical systems • Pour liquid on top of porous material. • Will it reach the bottom? • Applications: modeling flow of electricity, spread of forest fires, gas flow, … • System percolatesiff top and bottom are connected by open sites. CompSci 201, Spring 2018, Percolation+UF
Random Percolation • Given an N-by-N system where each site is openwith probability p, what is the probability that system percolates? • Open question in statistical physics • Take a computational approach • Monte Carlo Simulation p = 0.3(does not percolate) p = 0.4(does not percolate) p = 0.5(does not percolate) p = 0.6(percolates) p = 0.7(percolates) CompSci 201, Spring 2018, Percolation+UF
Phase Transition • For large N, there will be a sharp threshold p* • p>p*: almost certainly percolates. • p<p*: almost certainly does not percolate. CompSci 201, Spring 2018, Percolation+UF
Finding the Threshold • Initialize N-by-N grid of sites as blocked • Randomlyopen sites until system percolates • Percentage of open sites gives an estimate of p* CompSci 201, Spring 2018, Percolation+UF
Solution 1: Depth first search How to check whether an N-by-N system percolates? • Try searching from all of the openspots on the top row • Search from all legaladjacent spots you have not visited • Recurseuntil you can’t search any further or have reached the bottom row • What are the base cases? • Out of bounds? • Blocked? • Already full/visited? • How can we improve? Don’t search from the top. • open site CompSci 201, Spring 2018, Percolation+UF • blocked site
PercolationDFSFast (0,4) • Open (2,4) • Open (3,3) • Open (0,4) • Open (1,4) (0,4) (1,4) (1,4) (2,4) (2,4) (3,3) • What do open” & full mean? • How is state maintained and changed when a new grid cell is open? • Try out PercolationVisualizer and InteractivePercolationVisualizer CompSci 201, Spring 2018, Percolation+UF
Solution 2: Union-Find • 0 • 1 • 2 • 3 • 4 • How to check whether an N-by-N system percolates? • Create an object for each site and name them 0 to N 2 – 1. • 5 • 6 • 7 • 8 • 9 • 10 • 11 • 12 • 13 • 14 • 15 • 16 • 17 • 18 • 19 N = 5 • 20 • 21 • 22 • 23 • 24 • open • blocked
System Percolates? • virtual top site • 0 • 1 • 2 • 3 • 4 • N = 5 • top row • 5 • 6 • 7 • 8 • 9 • 10 • 11 • 12 • 13 • 14 • 15 • 16 • 17 • 18 • 19 • 20 • 21 • 22 • 23 • 24 • bottom row • virtual bottom site • open site • full site • Percolates iff virtual top site is connected to virtual bottom site. • blocked site CompSci 201, Spring 2018, Percolation+UF
Efficient Algorithms – Union Find • Steps to developing a usable algorithm • Model the problem • Find an algorithm to solve it • Analyze • Optimal? If not, improve • Fast enough? Fits in memory? • Iterate until satisfied Union Find Example: Connect the Dots! N objects, 2 operations: Connect 2 objects Path connecting the two objects? • 0 • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9 CompSci 201, Spring 2018, Percolation+UF
Union Find • Connect 4 & 3 • Connect 8 & 3 • Connect 5 & 6 • Connect 4 & 9 • Connect 2 & 1 • 0 & 7 connected? • No • 8 & 9 connected? • Yes • Connect 5 & 0 • Connect 7 & 2 • Connect 6 & 1 • Connect 1 & 0 • 0 & 7 connected? • Yes Union Find Example: Connect the Dots! N objects, 2 operations: Connect 2 objects Path connecting the two objects? • 0 • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9 CompSci 201, Spring 2018, Percolation+UF
Maze Connectivity • p Is there a path that connects p & q? • q CompSci 201, Spring 2018, Percolation+UF
Connectivity is an Equivalence Relation • "is connected to" is an equivalence relation: • Reflexive: p p • Symmetric: if p q, then q p • Transitive: if p q and q r, then p r • Connected component • Maximal set of objects that are mutually connected • 0 • 1 • 2 • 3 3 connected components {0} {1 4 5} {2 3 6 7} • 4 • 5 • 6 • 7 CompSci 201, Spring 2018, Percolation+UF
Union-Find API • Goal: Design efficient data structure for union-find • Many objects N • Many operations M • Union and find operations may be intermixed https://coursework.cs.duke.edu/201spring18/classwork-unionfind publicinterfaceIUnionFind{ voidinitialize(int N); // initialize with N objects // (0 to N – 1) voidunion(int p, intq); // add connection p to q intfind(int p); // id component for p (0 to N–1) booleanconnected(int p, intq); // p & q in the same // component? } CompSci 201, Spring 2018, Percolation+UF
0 • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9 Quick Find • 0 • 1 • 1 • 8 • 8 • 0 • 0 • 1 • 8 • 8 Data structure: Integer array id[N] Interpretation: id[p] is the id of the component iff id[p] contains p • id[] 0, 5, 6 connected 1, 2, 7 connected3, 4, 8, 9 connected • 0 • 1 • 2 • 3 • 4 Find: What is the id of p?Connected: Do p and q have the same id? Union: Components with p & q, change all entries with id = id[p] to id[q] • 5 • 6 • 7 • 8 • 9 CompSci 201, Spring 2018, Percolation+UF
Quick-find Union is Too Slow • Union: Components with p & q, change all entries with id = id[p] to id[q] N2 array accesses for N unions CompSci 201, Spring 2018, Percolation+UF
Quick Union • Data structure: • Int array id[N] • id[i] is parent of i • Root of i is id[id[id[... id[i]...]]] • 0 • 1 • 9 • 6 • 7 • 8 • 2 • 4 • 5 parent of 3 is 4 root of 3 is 9 • 3 keep going until it doesn’t change (union algorithm ensures no cycles) CompSci 201, Spring 2018, Percolation+UF
Quick Union: Find & Connected • Integer array id[N]: • id[i] is parent of i & Root of i is id[id[id[... id[i]...]]] • Find: What is the root of p? • Connected: Do p and q have the same root? • 0 • 1 • 9 • 6 • 7 • 8 root of 3 is 9 root of 5 is 6different roots means 3 & 5 are not connected • 2 • 4 • 5 • q • 3 • p CompSci 201, Spring 2018, Percolation+UF
Quick Union: Union • id[N]: id[i] is parent of I; root of i is id[id[… id[i]...]] • Union:To merge components containing p and q,set the id of p's root to the id of q's root • 1 • 0 • 7 • 8 • 6 • 0 • 1 • 9 • 6 • 7 • 8 • 9 • q • 5 Union 3 & 5 • 2 • 4 • 5 • q • 2 • 4 • p • 3 • p • 3 only one value changes CompSci 201, Spring 2018, Percolation+UF
Quick Union is Also Too Slow • Quick-find improvement • Union too expensive (N array accesses) • Trees are flat, but too expensive to keep them flat • Quick-union improvement • Trees can get tall • Find/connected too expensive in worst case WOTO: http://bit.ly/201-s18-0302-1 CompSci 201, Spring 2018, Percolation+UF
Improve? Weighted Quick-Union • Modify quick-union to avoid tall trees • Track: • size of each tree (number of objects) • Balance: • link root of smaller tree to root of larger tree WeightedQuickUnionUF.java CompSci 201, Spring 2018, Percolation+UF
0 • 3 • 9 • 6 Path compression! • root • 7 • 1 • 11 • 12 • 2 • 8 • Just after computing the root of p, • set the id[] of each examined node to point to that root • Recursive • 4 • 5 • 10 Examined Nodes 9, 6, 3, 1 • p • x 1 is already “compressed”
0 • 3 • 9 • 6 Path compression! • root • 7 • 1 • 11 • 12 • 2 • 8 • Just after computing the root of p, • set the id[] of each examined node to point to its grandparent • 4 • 5 • 10 • p • x
0 • 3 • 9 • 6 Path compression! • root • p • 7 • 1 • 11 • 12 • 2 • 8 • 4 • 5 • 10 • x Examined Nodes 9, 6, 3 26
0 • 3 • 9 • 6 Path compression! • 7 • 1 • 11 • 12 • 2 • 8 • root • p • 4 • 5 • 10 • x Examined Nodes 9,6, 3 CompSci 201, Spring 2018, Percolation+UF 27
0 • 3 • 9 • 6 Path compression! • 7 • 1 • 11 • 12 • 2 • 8 • root • 4 • 5 • 10 • p • x Examined Nodes 9, 6, 3 CompSci 201, Spring 2018, Percolation+UF 28
0 • 3 • 9 • 6 Path compression! • 7 • 1 • 11 • 12 • 2 • 8 • root • p • 4 • 5 • 10 • x CompSci 201, Spring 2018, Percolation+UF 29
0 • 3 • 9 Path compression! • root • 7 • 1 • 11 • 12 • 2 • Path-Halving is easier • As you compute the root of p, • set the id[] of each examined node to point to that node’s grandparent • One extra line • 4 • 5 Examined Nodes 9, 6, 3, 1 • 6 • p • 8 • 10
Scoreboard • Weighted quick union and/or path compression leads to efficient algorithm order of growth for initialize + M union-find operations on a set of N objects • ∈N + M lg* function for reasonable N CompSci 201, Spring 2018, Percolation+UF
PercolationUF VTOP • Open (2,4) • Open (3,3) • Open (0,4) • Connect(4,VTOP) • Open (1,4) • Connect(9,4) • Connect(9,14) • Open(4,3) • Connect(23,VBOTTOM) • Connect(23,18) (0,4) (1,4) (2,4) (3,3) (4,3) VBOTTOM CompSci 201, Spring 2018, Percolation+UF
Recap • Percolation • Union-Find (Efficient Algorithms) • Quick Find • Quick Union • Improvements • Balancing Trees • Path Compression • Reflect • What’s clear? What’s still muddy? • http://bit.ly/201-s18-reflect CompSci 201, Spring 2018, Percolation+UF