Slide 1

Slide 1 text

Union-based heaps Alexey Kachayev KyivPy #11 Sunday, October 27, 13

Slide 2

Slide 2 text

•@kachayev •github: kachayev •kachayev@gmail.com •attendify.com Sunday, October 27, 13

Slide 3

Slide 3 text

Problem extract min (quickly) Sunday, October 27, 13

Slide 4

Slide 4 text

•priority queue •bounded leadership •greedy algorithms •.. and more Sunday, October 27, 13

Slide 5

Slide 5 text

Solution heaps Sunday, October 27, 13

Slide 6

Slide 6 text

•binary •binominal •2/3 •fibonacci •leftist •skew •pairing Heaps Sunday, October 27, 13

Slide 7

Slide 7 text

In Python heapq.* Sunday, October 27, 13

Slide 8

Slide 8 text

•binary heap •push •pop •heapify •nlargest •merge heapq. Sunday, October 27, 13

Slide 9

Slide 9 text

Sunday, October 27, 13

Slide 10

Slide 10 text

Sunday, October 27, 13

Slide 11

Slide 11 text

Sunday, October 27, 13

Slide 12

Slide 12 text

Sunday, October 27, 13

Slide 13

Slide 13 text

Use: Dijkstra Sunday, October 27, 13

Slide 14

Slide 14 text

Sunday, October 27, 13

Slide 15

Slide 15 text

Sunday, October 27, 13

Slide 16

Slide 16 text

•push = O(log N) •pop = O(log N) •merge = O(N+M) •build = O(N) Binary Sunday, October 27, 13

Slide 17

Slide 17 text

•union :( •size bounding •array resizing •mutable But.. Sunday, October 27, 13

Slide 18

Slide 18 text

Union-based Sunday, October 27, 13

Slide 19

Slide 19 text

•still one tree •union is a King •low branching •persistent Idea Sunday, October 27, 13

Slide 20

Slide 20 text

Persistent each operation returns new structure Sunday, October 27, 13

Slide 21

Slide 21 text

Skew heaps Sunday, October 27, 13

Slide 22

Slide 22 text

Sunday, October 27, 13

Slide 23

Slide 23 text

Haskell Sunday, October 27, 13

Slide 24

Slide 24 text

Skew Python 2.* only Sunday, October 27, 13

Slide 25

Slide 25 text

Sunday, October 27, 13

Slide 26

Slide 26 text

•push= O(log N)* •pop= O(log N)* •union= O(log N)* Skew Sunday, October 27, 13

Slide 27

Slide 27 text

Pairing heaps Sunday, October 27, 13

Slide 28

Slide 28 text

Sunday, October 27, 13

Slide 29

Slide 29 text

Haskell Sunday, October 27, 13

Slide 30

Slide 30 text

Pairing Python 2.* only Sunday, October 27, 13

Slide 31

Slide 31 text

Sunday, October 27, 13

Slide 32

Slide 32 text

Sunday, October 27, 13

Slide 33

Slide 33 text

•push= O(1) •pop= O(1)* •union= O(1) Pairing Sunday, October 27, 13

Slide 34

Slide 34 text

More more persistent structure Sunday, October 27, 13

Slide 35

Slide 35 text

More fn.py module Sunday, October 27, 13

Slide 36

Slide 36 text

Links http://en.wikipedia.org/wiki/Heap_(data_structure) http://en.wikipedia.org/wiki/Skew_heap http://interactivepython.org/runestone/static/ pythonds/Trees/heap.html https://gist.github.com/kachayev/7161521 https://gist.github.com/kachayev/6081643 https://gist.github.com/kachayev/7167046 Sunday, October 27, 13

Slide 37

Slide 37 text

Q/A Sunday, October 27, 13