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
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Compsci 201 201 Percol

  • lation, U

, Union Find, S d, Sorting and d Prior

  • rity Q

y Queues es Par art 1 1 of

  • f 5

4/1/2020 Compsci 201, Spring 2020 1

Susan Rodger April 1, 2020

slide-2
SLIDE 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

slide-3
SLIDE 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

slide-4
SLIDE 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

  • ut

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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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?

  • 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 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)

slide-8
SLIDE 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

slide-9
SLIDE 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

  • n

n BlobFill ill code de

  • Threshold is near 0.592, so O(N2) at least since

have to open that many sites

4/1/2020 Compsci 201, Spring 2020 9

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 12

Question

  • In

n a a 10x 10x10 g grid id, w wha hat is is the he m minim inimum num number o

  • f

cel ells I I need need t to mar ark (cl click o

  • n)

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

slide-13
SLIDE 13

Two-minute WOTO

http://bit. t.ly/ y/201 01spring2 g20-0401 401-1

4/1/2020 Compsci 201, Spring 2020 14

slide-14
SLIDE 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.

4/1/2020 Compsci 201, Spring 2020 15

“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.”

slide-15
SLIDE 15

Compsci 201 201 Percol

  • lation, U

, Union Find, S d, Sorting and d Prior

  • rity Q

y Queues es Pa Part 2 2 of f 5

4/1/2020 Compsci 201, Spring 2020 16

Susan Rodger April 1, 2020

slide-16
SLIDE 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

slide-17
SLIDE 17

Percolation: Union-Find

  • Adjacent o
  • pen

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, … N2-1

4/1/2020 Compsci 201, Spring 2020 18

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

  • pen

blocked

slide-18
SLIDE 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

slide-19
SLIDE 19

Union and Connected

  • Open? myGrid[r][c] == true
  • Calling union: open 18?

4/1/2020 Compsci 201, Spring 2020 20

VTOP

VBOTTOM 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 24 23

slide-20
SLIDE 20

When 18 is open …

4/1/2020 Compsci 201, Spring 2020 21

VTOP

VBOTTOM 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 24 23

  • Adjacent:

13, 13, 17, 17, 19, 19, 23 23

  • 13 and 23 are open
  • Union(13,18)
  • Union(18,23)
  • System

em p percolat ates es!

  • Connected(VTOP

,VBOTTOM)

slide-21
SLIDE 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(N2) disjoint sets
  • Imple

lementations: unio union and and find ind bot

  • th 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

slide-22
SLIDE 22

Two-minute WOTO

http:// //bi bit.ly/2 /201spr pring20-04 0401 01-2

4/1/2020 Compsci 201, Spring 2020 23

slide-23
SLIDE 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

4/1/2020 Compsci 201, Spring 2020 24

"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

slide-24
SLIDE 24

Compsci 201 201 Percol

  • lation, U

, Union Find, S d, Sorting and d Prior

  • rity Q

y Queues es Pa Part 3 3 of f 5

4/1/2020 Compsci 201, Spring 2020 25

Susan Rodger April 1, 2020

slide-25
SLIDE 25

PriorityQueues top to bottom

  • All o
  • perations 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

  • ogle 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

slide-26
SLIDE 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

  • rt

4/1/2020 Compsci 201, Spring 2020 27

slide-27
SLIDE 27

Problem: Finding Top M or N

  • Giv

iven N N it item

  • ems. F

Find ind t the T he Top M lar largest it items. Return them hem w wit ith lar largest f fir irst, then hen s sec econd large gest… …

  • Exam

ample: [ [5, 9, 5, 9, 2, 32, 8, 2, 32, 8, 41, 27, 11, 7, 24] 41, 27, 11, 7, 24]

  • Find

ind to top 4: 4:

  • [ 41, 32, 27, 24]
  • If

If str trings, find ind “lar largest” means ans “thos

  • se las

ast in in alphab habetical ical ord rder”

4/1/2020 Compsci 201, Spring 2020 28

slide-28
SLIDE 28

First way: Finding top M of N

  • Sort all and

all and g get f fir irst ( (or la last) M

  • O(N log N) to sort, then O(M), typically N >> M
  • Code b

belo elow d does esn't alt alter lis list p par arameter

  • Why is comp.reversed() used?

4/1/2020 Compsci 201, Spring 2020 30

slide-29
SLIDE 29

Faster way: Finding top M of N

  • Can

an d do thi his in in O(N lo log M M) us using p prio iority que ueue

  • Not intuitive? largest M using min PQ?

4/1/2020 Compsci 201, Spring 2020 31

slide-30
SLIDE 30

Details for M of N

  • Kee

eep o

  • nl

nly M M ele elements in in the he prio iority q que ueue

  • Every time one removed? It's the smallest
  • 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 33

slide-31
SLIDE 31

Compsci 201 201 Percol

  • lation, U

, Union Find, S d, Sorting and d Prior

  • rity Q

y Queues es Pa Part 4 4 of f 5

4/1/2020 Compsci 201, Spring 2020 34

Susan Rodger April 1, 2020

slide-32
SLIDE 32

Sorting: 201 in a

  • Algorit

ithm hms: t : trad adit itio ionall nally f foundatio ion o n of compsc sci

  • Study, analyze, develop, use
  • APIs: t

tested, p , proven, en, c config igur urab able le

  • Algorithms encapsulated and usable
  • You s

u sho hould kno now ho how to w writ ite your ur own s n sort

  • You

u sho hould kno now ho how to cal all lib library s sort

4/1/2020 Compsci 201, Spring 2020 35

slide-33
SLIDE 33

Sorting: From Theory to Practice

  • Why

hy do we e study m more t than han o

  • ne alg

ne algorithm?

  • Paradigms of trade-offs and algorithmic design
  • Know your history

4/1/2020 Compsci 201, Spring 2020 36

slide-34
SLIDE 34

Review - Selection Sort

  • Sort a list of numbers.
  • Idea:

– Repeat til sorted

  • Find the smallest element in part of list not sorted
  • Put it where it belongs in sorted order.
  • Swap it with the element where it should be
  • Sort example

Sorted, won’t move final position

???

Compsci 201, Spring 2020 37 4/1/2020

slide-35
SLIDE 35

Selection Sort – red area sorted

9 5 5 4 4 1 1 3 3 6 6

  • find s

smalle allest, s , swap 1 5 5 4 4 9 9 3 3 6 6

  • end

end o

  • f 1st

st pas

ass 1 5 5 4 4 9 9 3 3 6 6

  • find s

smalle allest, s , swap 1 3 3 4 4 9 9 5 5 6 6

  • end

end o

  • f 2nd

nd pass

1 1 3 3 4 4 9 9 5 5 6 6 - find s smalles llest, s , swap ap

38 Compsci 201, Spring 2020 4/1/2020

slide-36
SLIDE 36

Insertion Sort

  • Sort a list of numbers.
  • Idea:

– Sort by repeated inserting another element

  • Leftmost element is sorted part of list
  • Insert another element in that sublist keeping it

sorted

  • Insert another element in that sublist keeping it

sorted

  • Etc.
  • Sort example

Sorted relative to each other

???

Compsci 201, Spring 2020 41 4/1/2020

slide-37
SLIDE 37

Insertion Sort – red area sorted

9 5 1 4 3 6 - insert 5 5 9 1 4 3 6 - 1st pass, now insert 1 1 5 9 4 3 6 - 2nd pass, now insert 4 1 4 5 9 3 6 - 3rd pass, now insert 3 1 3 4 5 9 6 - 4th pass, now insert 6

42 Compsci 201, Spring 2020 4/1/2020

slide-38
SLIDE 38

Review Bubble Sort

  • Sort a list of numbers.
  • Idea:

– Repeat til sorted

  • Compare all adjacent pairs, one at a time. If out of
  • rder then swap them
  • Sort example

Sorted, won’t move final position

???

Compsci 201, Spring 2020 45 4/1/2020

slide-39
SLIDE 39

Bubble Sort – red area sorted

9 5 4 1 3 6 - compare, swap 5 9 4 1 3 6 - compare, swap 5 4 9 1 3 6 - compare, swap 5 4 1 9 3 6 - compare, swap 5 4 1 3 9 6 - compare, swap 5 4 1 3 6 9 - end of 1st pass 5 4 1 3 6 9

Compsci 201, Spring 2020 46 4/1/2020

slide-40
SLIDE 40

Simple, O(n2) sorts

  • Selectio

tion s sort t --- n2 compa mparis isons, n n swaps ps

  • Find min, swap to front, increment front, repeat
  • In

Inse sertion sor sort --- n2 com

  • mpariso

sons, n no

  • sw

swap, sh shift

  • stable

ble, fast on sorted data, slide into place

  • Bubble

bble s sort --- n2 ever erythi hing ng, s slow*

  • Catchy name, but slow and ugly*

*this isn't everyone's opinion, but it should be

  • Shell s

ll sort: q : quasi-in insertio tion, f fast i in practi tice

  • Not quadratic with some tweaks

4/1/2020 Compsci 201, Spring 2020 50

slide-41
SLIDE 41

Sorting: Visualizations

  • Good
  • od site

te fo for sorting ng visuali ualizatio ions ns:

  • http://www.sorting-algorithms.com/
  • Ther

There ar are lo lots o

  • f sorting d

dem emonstratio ions o

  • ut

ut there: e:

  • Google dance sorting algorithms

4/1/2020 Compsci 201, Spring 2020 51

slide-42
SLIDE 42

WOTO on sorting

http:// //bi bit.ly/2 /201spr pring20-04 0401 01-3

4/1/2020 Compsci 201, Spring 2020 52

slide-43
SLIDE 43

Alan Turing

  • 2:46

2:46 m mar arathon

  • 15:2

15:20 t thr hree m mile ile

  • Enig

nigma m mac achin ine and and W WWI WII

  • Entscheid

heidung ungsproblem lem

  • He

He wa was gay a at a a tim ime w when hen society w was no not v ver ery accep cepting ing

4/1/2020 Compsci 201, Spring 2020 53

Those who can imagine anything, can create the impossible.

slide-44
SLIDE 44

Compsci 201 201 Percol

  • lation, U

, Union Find, S d, Sorting and d Prior

  • rity Q

y Queues es Pa Part 5 5 of f 5

4/1/2020 Compsci 201, Spring 2020 54

Susan Rodger April 1, 2020

slide-45
SLIDE 45

Tree APTs: Recursion Idiom for Trees

  • Us

Use root = caller(root, val);

  • Modifies Tree at root and returns the result
  • Recursive calls works similarly
  • root.left = caller(root.left, val);
  • Process the subtree, use the result
  • Where d

e do we see t e this is? I Insertio ion i n into s sear arch t ch tree

  • We also see it in tree tighten APT

4/1/2020 Compsci 201, Spring 2020 55

slide-46
SLIDE 46

Tightening a Tree (hint1)

Remove all the nodes that have only one child. Return modified tree.

  • https://w

://www2.cs cs.duke.edu .edu/c /csed/n ed/newap apt/t /tree reetighten en.h .html

  • Call

ll: : tree = tighten(tree)

  • A node has two children (13 and 8)
  • They are still in modified tree
  • Reset left, reset right, return itself
  • tree.left = tighten(tree.left)

4/1/2020 Compsci 201, Spring 2020 56

slide-47
SLIDE 47

Tightening a Tree (hint2)

Remove all the nodes that have only one child. Return modified tree.

  • A node has no children (5, 7, and 25)
  • They are still in modified tree
  • A node has one child (22, 18 and 15)
  • Don’t want the node, instead return the child.

4/1/2020 Compsci 201, Spring 2020 57

slide-48
SLIDE 48

APT TrimTree

Remove all nodes from tree NOT in range [x,y]

  • https://w

://www2.cs cs.duke.edu .edu/c /csed/n ed/newap apt/t /trimtree ee.h .html

  • It's a

a search t ch tree ee, s , so we can u n use r rang nge s e sear arch ch

  • Left subtree <= root. Right subtree > root
  • With no duplicates, < and not <=
  • trim

im(root, 5, 5, 12) 12) for [ [5, 5,12 12]

4/1/2020 Compsci 201, Spring 2020 58

slide-49
SLIDE 49

APT TrimTree (hints)

  • If r

root in r in rang ange?

  • Process subtrees, return root
  • t.left = trim(t.left, …)
  • t.right = trim(t.right, …)
  • If root no

not in in ran ange? R Return res esult o

  • f one

ne cal all!

  • return trim(t.left, ..) or …

4/1/2020 Compsci 201, Spring 2020 60