Compsci 201 These are tough times Percolation, Union Find, Sorting and Priority Queues • Talk to your friends, virtually reach out to others Part 1 of 5 Susan Rodger April 1, 2020 4/1/2020 Compsci 201, Spring 2020 1 4/1/2020 Compsci 201, Spring 2020 2 Announcements S is for … • APT-5 due Tuesday, March 31, still turn in today • Need extension, fill out form and take it! • Stack • Last in, First Out, source of overflow! • Assignment P5 Percolation – form due April 2 • Software • Fill out form to tell us your partner or solo • Joys and sorrows, eating the world • Sorting • APT-6 out and due Tuesday, April 7 • From slow to quick to tim to … • Exam 2 is April 10 • APT Quiz 2 is April 11-15 4/1/2020 Compsci 201, Spring 2020 3 4/1/2020 Compsci 201, Spring 2020 4
Percolation PFFDiA • Simulate whether an NxN grid percolates • Connecting top to bottom • Percolation and Simulation • Monte-Carlo for percolation threshold • All sites blocked , choose at random to open • DFS and BFS: limits of recursion • Site is full if in top row or connected to top • Union-Find as algorithmic alternative • Sorting • Look at several slow sorts, one faster • More Tree APTs 4/1/2020 Compsci 201, Spring 2020 5 4/1/2020 Compsci 201, Spring 2020 6 When Does System Percolate? Monte Carlo Percolation • Given an N -by- N system where each site is open • For large N, there is a percolation threshold p* with probability p , does system percolate? • Probability p < p* -- no percolation • Probability p > p* -- system percolates • Simulation: take all NxN grid cells, shuffle them • Open one at a time until system percolates p = 0.3 p = 0.4 p = 0.5 p = 0.6 p = 0.7 (does not percolate) (does not percolate) (does not percolate) (percolates) (percolates) • How many must be opened until this happens? • Open question in statistical physics • Probability is count/(NxN) – estimate of p* • We use simulation: a computational approach 4/1/2020 Compsci 201, Spring 2020 7 4/1/2020 Compsci 201, Spring 2020 8
Simulating with DFS + BFS Object-Oriented view of Percolation • IPercolate is an interface • Start with basic DFS, make it faster • PercolateDFS, Per..DFSFast, Per..BFS, Per..UF • Don't test by starting at every cell in top row • Each of these can be used in a simulation • Test after opening site PercolationDFSFast • PercolationStats or in InteractiveVisualizer • Use Queue not recursion PercolationBFS • Methods: open , isOpen , isFull , percolates • Base your code and ideas on BlobFill code • PercolationUF needs a Union-Find object • Threshold is near 0.592, so O(N 2 ) at least since • IUnionFind: has union(x,y), connected(x,y) … have to open that many sites • Different implementations, but not a priority 4/1/2020 Compsci 201, Spring 2020 9 4/1/2020 Compsci 201, Spring 2020 10 Visualize Question • When automated tests aren't enough? • In a 10x10 grid, what is the minimum number of cells I need to mark (click on) for which it may say • Use the visualizer to see what's happening that it percolates? • Watch a video to see what's happening 10 • Can you do it in fewer than that? • Visualize PercolationDFSFast, BFS, and UF NO • More tests than can be done in automated way • Is it possible to open half the cells and have the • Be sure you can test with concepts system not percolate? YES 4/1/2020 Compsci 201, Spring 2020 11 4/1/2020 Compsci 201, Spring 2020 12
Two-minute WOTO Margaret Martonosi http://bit.ly/201spring20-0401-1 • Computer Architecture and Mobile Computing • Designed and deployed mobile tracking with zebras • Low-power GPS devices • P Professor Princeton, Currently Head of National Science Foundation CISE (Computer Information Science and Engineering. “ZebraNet was an unusual and risky project for a computer architect to embark on, but it was unique and rewarding and we learned a lot personally and technically.” 4/1/2020 Compsci 201, Spring 2020 14 4/1/2020 Compsci 201, Spring 2020 15 Compsci 201 Union Find Alternative Percolation, Union Find, Sorting and Priority Queues • Union Find aka Disjoint Set : Algorithm Part 2 of 5 • Sets have empty intersection, they are disjoint • Creating the union of two sets should be fast • Finding what set an element is in should be fast • Represent each Percolation site/grid cell as a number, initially each cell is a set by itself Susan Rodger • If a site is open? Union with adjacent open sites April 1, 2020 4/1/2020 Compsci 201, Spring 2020 16 4/1/2020 Compsci 201, Spring 2020 17
Percolation: Union-Find Union and Connected • Adjacent open sites/cells are in the same set • System percolates concept and code … • Initially each site/cell its own set: 0, 1, … N 2 -1 • Conceptually: open path top to bottom • Code: adjacent open cells in same set 0 1 2 3 4 open • Code: connected(VTOP, VBOTTOM) 5 6 7 8 9 blocked • Each time a cell is open … NO RECURSION 10 11 12 13 14 • Check adjacent cells: if open? Union sets 15 16 17 18 19 • VTOP is open and VBOTTOM is open 20 21 22 23 24 4/1/2020 Compsci 201, Spring 2020 18 4/1/2020 Compsci 201, Spring 2020 19 Union and Connected When 18 is open … VTOP VTOP • Adjacent: 13, 17, 19, 23 • Open? myGrid[r][c] == true • 13 and 23 are open 0 1 2 3 4 0 1 2 3 4 • Calling union: open 18? • Union(13,18) • Union(18,23) 5 6 7 8 9 5 6 7 8 9 System percolates! • • Connected(VTOP ,VBOTTOM) 10 11 12 13 14 10 11 12 13 14 15 16 17 18 19 15 16 17 18 19 20 21 22 23 24 20 21 22 23 24 VBOTTOM VBOTTOM 4/1/2020 Compsci 201, Spring 2020 20 4/1/2020 Compsci 201, Spring 2020 21
Union-Find Algorithms Two-minute WOTO • Initialize with N disjoint sets: O(N) for all http://bit.ly/201spring20-0401-2 • In Percolation we have O(N 2 ) disjoint sets • Implementations: union and find both efficient • Easy: QuickFind or QuickUnion: O(N) • Medium: WeightedQuickUnion: O(log N ) • Harder: + Path-compression: O(1) • Technically not O(1), but in our universe it is 4/1/2020 Compsci 201, Spring 2020 22 4/1/2020 Compsci 201, Spring 2020 23 Compsci 201 Craig Gentry, Duke '95 Percolation, Union Find, Sorting and Priority Queues • Harvard Law, Stanford Compsci PhD Part 3 of 5 • ACM 2010 Hopper Award • MacArthur Fellow 2014 "Fully homomorphic encryption is a bit like enabling a layperson to perform flawless neurosurgery while blindfolded, and without later remembering the episode. We believe this breakthrough will enable businesses to make more Susan Rodger informed decisions, based on more studied analysis, April 1, 2020 without compromising privacy." Research Scientist at IBM Now at Algorand Foundation 4/1/2020 Compsci 201, Spring 2020 24 4/1/2020 Compsci 201, Spring 2020 25
PriorityQueues top to bottom PriorityQueues top to bottom • How can we sort elements using Priority Queue? • All operations are O(log N) where N size of PQ • Add all elements to pq, then remove them • This for add and remove; can peek in O(1) • Every operation is O(log N), so this sort? • Details after midterm • O(N log N) – basis for heap sort • Always remove the smallest element, minPQ • Can change by providing a Comparator • Shortest-path, e.g., Google Maps. Best-first search in games • Best element removed from queue, not first 4/1/2020 Compsci 201, Spring 2020 26 4/1/2020 Compsci 201, Spring 2020 27 Problem: Finding Top M or N First way: Finding top M of N • Given N items. Find the Top M largest items. • Sort all and get first (or last) M Return them with largest first, then second • O(N log N) to sort, then O(M), typically N >> M largest… • Code below doesn't alter list parameter • Why is comp.reversed() used? • Example: [5, 9, 2, 32, 8, 41, 27, 11, 7, 24] • Find top 4: • [ 41, 32, 27, 24] • If strings, find “largest” means “those last in alphabetical order” 4/1/2020 Compsci 201, Spring 2020 28 4/1/2020 Compsci 201, Spring 2020 30
Details for M of N Faster way: Finding top M of N • Keep only M elements in the priority queue • Can do this in O(N log M) using priority queue • Every time one removed? It's the smallest • Not intuitive? largest M using min PQ? • When done? Top M remain, removed smallest! • First element removed? Smallest, so … • Why is LinkedList used? O(1) add to front 4/1/2020 Compsci 201, Spring 2020 31 4/1/2020 Compsci 201, Spring 2020 33 Compsci 201 Sorting: 201 in a Percolation, Union Find, Sorting and Priority Queues • Algorithms: traditionally foundation of compsci Part 4 of 5 • Study, analyze, develop, use • APIs: tested, proven, configurable • Algorithms encapsulated and usable • You should know how to write your own sort Susan Rodger • You should know how to call library sort April 1, 2020 4/1/2020 Compsci 201, Spring 2020 34 4/1/2020 Compsci 201, Spring 2020 35
Recommend
More recommend