compsci 201 201 percol olation u union find s d sorting
play

Compsci 201 201 Percol olation, U , Union Find, S d, Sorting - PowerPoint PPT Presentation

Compsci 201 201 Percol olation, U , Union Find, S d, Sorting and d Prior ority Q y Queues es Par art 1 1 of of 5 Susan Rodger April 1, 2020 4/1/2020 Compsci 201, Spring 2020 1 These ese ar are t e tough ti times Talk lk


  1. Compsci 201 201 Percol olation, U , Union Find, S d, Sorting and d Prior ority Q y Queues es Par art 1 1 of of 5 Susan Rodger April 1, 2020 4/1/2020 Compsci 201, Spring 2020 1

  2. These ese ar are t e tough ti times • Talk lk t to your f friend ends, v virtua uall lly reach o ch out t to other hers 4/1/2020 Compsci 201, Spring 2020 2

  3. S is for … • Stack • Last in, First Out, source of overflow! • Software • Joys and sorrows, eating the world • Sorting • From slow to quick to tim to … 4/1/2020 Compsci 201, Spring 2020 3

  4. Announcements • APT APT-5 d due ue T Tue uesday, M Mar arch 31, s 31, still l tur urn in in today • Need extension, fill out form and take it! • Assig ignm nment nt P5 P Perco cola latio ion n – form d due A ue April il 2 2 • Fill out form to tell us your partner or solo • APT APT-6 o out ut and and d due T ue Tue uesday, A April il 7 7 • Exam am 2 2 is is April il 10 10 • APT APT Quiz iz 2 is is Apr April 11 11-15 15 4/1/2020 Compsci 201, Spring 2020 4

  5. PFFDiA • Perco cola latio ion a n and S Simula ulatio ion • Monte-Carlo for percolation threshold • DFS and BFS: limits of recursion • Union-Find as algorithmic alternative • Sorting ing • Look at several slow sorts, one faster • More T e Tree A e APTs 4/1/2020 Compsci 201, Spring 2020 5

  6. Percolation • Simula ulate w e whet ethe her a an NxN xN grid p perco cola lates • Connecting top to bottom • All sites blocke ked , choose at random to ope pen • Site is full ull if in top row or connected to top 4/1/2020 Compsci 201, Spring 2020 6

  7. When Does System Percolate? • Giv iven an an N -by by- N system w wher here eac each s sit ite is is ope pen wi with pr proba babi bility p , does s s syst stem p percolate? 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) • Open q n questio ion n in statis istic ical p l physic ics • We use s simula ulatio ion: n: a a comput utational a nal approac ach 4/1/2020 Compsci 201, Spring 2020 7

  8. Monte Carlo Percolation • For lar large N N, , ther here is is a a per ercolation t thr hreshold ld p p* • Probability p < p* -- no percolation • Probability p > p* -- system percolates • Sim imulatio ion: t tak ake all all NxN xN grid c cells lls, s , shuf uffle le them • Open one at a time until system percolates • How many must be opened until this happens? • Probability is count/(NxN) – estimate of p* 4/1/2020 Compsci 201, Spring 2020 8

  9. Simulating with DFS + BFS • Star art w with b basic D ic DFS, m , make i e it f fas aster • Don't test by starting at every cell in top row • Test after opening site PercolationDFSFast • Use Queue not recursion PercolationBFS • Base y your ur c code and and id idea eas o on n BlobFill ill code de • Threshold is near 0.592, so O(N 2 ) at least since have to open that many sites 4/1/2020 Compsci 201, Spring 2020 9

  10. Object-Oriented view of Percolation • IPercolate te is is an int an interface • PercolateDFS, Per..DFSFast, Per..BFS, Per..UF • Eac ach of the hese c can an b be e us used in in a a sim imulation • PercolationStats or in InteractiveVisualizer • Methods: open , isOpen , isFull , percolates • Perco cola latio ionU nUF need needs a a Unio nion-Find o d obj bject • IUnionFind: has union(x,y), connected(x,y) … • Different implementations, but ut no not a a prio iority 4/1/2020 Compsci 201, Spring 2020 10

  11. Visualize • When hen a aut utomated t tes ests ar aren en't eno enough? • Use the visualizer to see what's happening • Watch a video to see what's happening • Visua uali lize e Perco cola latio ionD nDFSFast, BFS, and and UF • More tests than can be done in automated way • Be sure you can test with concepts 4/1/2020 Compsci 201, Spring 2020 11

  12. Question • In n a a 10x 10x10 g grid id, w wha hat is is the he m minim inimum num number o of cel ells I I need need t to mar ark (cl click o on) n) f for wh which i it m may s say tha hat it it per ercolates? 10 10 • Can an y you u do it it in in fewer t than han t tha hat? NO NO • Is it it p possible t to open half en half t the he c cell ells and and ha have t the he system n not p percolate? YES YES 4/1/2020 Compsci 201, Spring 2020 12

  13. Two-minute WOTO http://bit. t.ly/ y/201 01spring2 g20-0401 401-1 4/1/2020 Compsci 201, Spring 2020 14

  14. Margaret Martonosi • Comput uter er Archit itecture a and d Mobile ile C Compu putin ting • Desig igned d and d d deplo ployed mobile bile t tracking w with z zebr bras • Low-power GPS devices • Profes essor Prince nceton, n, C Curren ently H Head ad of Nat ational al S Scienc ence e Founda datio tion CISE ( (Compu mputer I Informa matio tion S Scie ience and d En Engineer eering ng. “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 15

  15. Compsci 201 201 Percol olation, U , Union Find, S d, Sorting and d Prior ority Q y Queues es Pa Part 2 2 of f 5 Susan Rodger April 1, 2020 4/1/2020 Compsci 201, Spring 2020 16

  16. Union Find Alternative • Unio nion F Find ind aka a Disjoint int S Set : A : Algorit ithm hm • 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 • Repres esent ent e each P ch Perco cola lation s n site/g e/grid id c cell a l as a a num number, init initiall lly eac each cel cell l is is a a set b by it itself • If a site is open? Union with adjacent open sites 4/1/2020 Compsci 201, Spring 2020 17

  17. Percolation: Union-Find • Adjacent o open en sit ites/cells ar are e in in the s he sam ame s set • Initially each site/cell its own set: 0, 1, … N 2 -1 0 1 2 3 4 open 5 6 7 8 9 blocked 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 4/1/2020 Compsci 201, Spring 2020 18

  18. Union and Connected • System per ercolates c conc ncept and and c code … e … • Conceptually: open path top to bottom • Code: adjacent open cells in same set • Code: connected(VTOP, VBOTTOM) • Eac ach tim ime a a cel cell is is open en … … NO RECURSION • Check adjacent cells: if open? Union sets • VTOP is open and VBOTTOM is open 4/1/2020 Compsci 201, Spring 2020 19

  19. Union and Connected VTOP • Open? myGrid[r][c] == true 0 1 2 3 4 • Calling union: open 18? 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 VBOTTOM 4/1/2020 Compsci 201, Spring 2020 20

  20. When 18 is open … VTOP Adjacent: 13, 13, 17, 17, 19, 19, 23 23 • • 13 and 23 are open 0 1 2 3 4 • Union(13,18) • Union(18,23) 5 6 7 8 9 System em p percolat ates es! • • Connected(VTOP ,VBOTTOM) 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 VBOTTOM 4/1/2020 Compsci 201, Spring 2020 21

  21. Union-Find Algorithms • Init nitialize w wit ith h N dis isjoin int s sets: O O(N) for all all • In Percolation we have O(N 2 ) disjoint sets • Imple lementations: unio union and and find ind bot oth ef efficient icient • 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

  22. Two-minute WOTO http:// //bi bit.ly/2 /201spr pring20-04 0401 01-2 4/1/2020 Compsci 201, Spring 2020 23

  23. Craig Gentry, Duke '95 • Harvar ard L Law, S , Stanf anford Compsci Ph PhD • ACM 2010 2010 Hopper er A Awar ard • Mac acArthur Fel ellow 2014 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 informed decisions, based on more studied analysis, without compromising privacy." Research Scientist at IBM Now at Algorand Foundation 4/1/2020 Compsci 201, Spring 2020 24

  24. Compsci 201 201 Percol olation, U , Union Find, S d, Sorting and d Prior ority Q y Queues es Pa Part 3 3 of f 5 Susan Rodger April 1, 2020 4/1/2020 Compsci 201, Spring 2020 25

  25. PriorityQueues top to bottom • All o operations a are O O(log N N) whe here N N size of PQ • This for add and remove; can peek in O(1) • Details after midterm • Always r remove t e the e smal alles est elem ement nt, minPQ PQ • Can change by providing a Comparator • Shorte test-path, e e.g., G Goog oogle M Maps. ps. B Best st-first s search i in n games s • Best element removed from queue, not first 4/1/2020 Compsci 201, Spring 2020 26

  26. PriorityQueues top to bottom • How can an w we e sort elem elements us usin ing Prio iority Q Que ueue? • Add all elements to pq, then remove them • Every operation is O(log N), so this sort? • O(N log N) – basis for hea heap sor ort 4/1/2020 Compsci 201, Spring 2020 27

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend