Binary Heaps Autumn 2018 Shrirang (Shri) Mare - - PowerPoint PPT Presentation

binary heaps
SMART_READER_LITE
LIVE PREVIEW

Binary Heaps Autumn 2018 Shrirang (Shri) Mare - - PowerPoint PPT Presentation

CSE 373: Data Structures and Algorithms Binary Heaps Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges,


slide-1
SLIDE 1

Binary Heaps

CSE 373: Data Structures and Algorithms

Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...

Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu

slide-2
SLIDE 2

Problems

2

  • 1. Merging multiple sorted arrays
  • OutArray[k] = min(Array1[i1], Array2[i2]) // for 2 sorted arrays
  • OutArray[k] = min(Array1[i1], Array2[i2], …, Arrayk[ik]) // for k sorted arrays
  • 2. Given n 2D points, find k points which are closest to point P(x, y)

S = Set of k distances For each point Q in the remaining points: if dist(P, Q) is less than max(S) removeMax from S Insert dist(P, Q) in S

  • 3. Priority queues: Job schedulers
  • Among all the job in a queue, get the job with the highest priority: removeMax(Priority Queue)
slide-3
SLIDE 3

Problems

3

  • 1. Merging multiple sorted arrays
  • OutArray[k] = min(Array1[i1], Array2[i2]) // for 2 sorted arrays
  • OutArray[k] = min(Array1[i1], Array2[i2], …, Arrayk[ik]) // for k sorted arrays
  • 2. Given n 2D points, find k points which are closest to point P(x, y)

S = Set of k distances For each point Q in the remaining points: if dist(P, Q) is less than max(S) removeMax from S Insert dist(P, Q) in S

  • 3. Priority queues: Job schedulers
  • Among all the job in a queue, get the job with the highest priority: removeMax(Priority Queue)
slide-4
SLIDE 4

Desired behavior: Get extreme values (min or max)

4

  • 1. Merging multiple sorted arrays
  • OutArray[k] = min(Array1[i1], Array2[i2]) // for 2 sorted arrays
  • OutArray[k] = min(Array1[i1], Array2[i2], …, Arrayk[ik]) // for k sorted arrays
  • 2. Given n 2D points, find k points which are closest to point P(x, y)

S = Set of k distances For each point Q in the remaining points: if dist(P, Q) is less than max(S) removeMax from S Insert dist(P, Q) in S

  • 3. Priority queues: Job schedulers
  • Among all the job in a queue, get the job with the highest priority: removeMax(Priority Queue)
slide-5
SLIDE 5
  • Collection where elements ordered based on priority.
  • Behavior:
  • removeMin(): return element with smallest priority, removes element

from collection

  • peekMin(): find, but do not remove, the element with smallest priority
  • insert(element): add element to the collection
  • Max Priority Queue ADT:
  • Same as Min Priority Queue ADT, just returns the largest instead of the smallest

Min Priority Queue ADT

5

slide-6
SLIDE 6
  • Invented in 1964 for sorting
  • Priority Queues is one of the main applications for binary heaps
  • Lots of other applications: greedy algorithms, shortest path
  • Basically, min-heap (or max-heap) is ideal when you want to

maintain a collection of elements where you need to add arbitrary values but need an efficient removeMin (or removeMax).

Binary heap data structure

6

slide-7
SLIDE 7

Binary heap is a

  • 1. binary tree
  • 2. that satisfies the heap property, and
  • 3. where every heap is a “complete” tree

Binary heap

7

slide-8
SLIDE 8

Binary heap: Heap property

8

slide-9
SLIDE 9

max-heap: Every node is larger than (or equal to) its children

Binary heap: Heap property

8

slide-10
SLIDE 10

max-heap: Every node is larger than (or equal to) its children min-heap: Every node is smaller than (or equal to) its children

Binary heap: Heap property

8

slide-11
SLIDE 11

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. There are no “gaps” in a complete tree.

Binary heap: Complete binary tree

9

slide-12
SLIDE 12

Complete binary tree

10

1 3 2 7 4 5 6 9

depth 0 depth 1 depth 2 depth 3

Complete binary tree There as not complete binary trees

slide-13
SLIDE 13

Question: Valid min-heap?

11

1 3 2 7 4 5 6 9

Complete binary tree? Heap property satisfied?

slide-14
SLIDE 14

Question: Valid min-heap?

12

1 3 2 7 4 5 6 9

Complete binary tree? Heap property satisfied? Yes! Yes!

slide-15
SLIDE 15

Question: Valid min-heap?

13

1 8 2 7 4 5 6 9

Complete binary tree? Heap property satisfied?

slide-16
SLIDE 16

Question: Valid min-heap?

14

1 8 2 7 4 5 6 9

Complete binary tree? Heap property satisfied? No! No!

slide-17
SLIDE 17

Binary heap insert

15

2 5 3 17 4 13 6 9

insert(12)

slide-18
SLIDE 18

Binary heap insert

16

2 5 3 17 4 13 6 9

insert(12)

12

slide-19
SLIDE 19

Binary heap insert

17

2 5 3 17 4 13 6 9

insert(12)

12

insert(7)

slide-20
SLIDE 20

Binary heap insert

18

2 5 3 17 4 13 6 9

insert(12)

12

insert(7)

7

slide-21
SLIDE 21

Binary heap insert

19

2 5 3 17 4 13 6 9

insert(12)

12

insert(7)

7

Heap broken

slide-22
SLIDE 22

Binary heap insert

20

2 5 3 7 4 13 6 9

insert(12)

12

insert(7)

17

Heap broken

slide-23
SLIDE 23

Binary heap insert

21

2 5 3 7 4 13 6 9

insert(12)

12

insert(7)

17

slide-24
SLIDE 24

Binary heap insert

22

2 5 3 7 4 13 6 9

insert(12)

12

insert(7)

17

insert(1)

slide-25
SLIDE 25

Binary heap insert

23

2 5 3 7 4 13 6 9

insert(12)

12

insert(7)

17

insert(1)

1

slide-26
SLIDE 26

Binary heap insert

24

2 5 3 7 4 13 6 9

insert(12)

12

insert(7)

17

insert(1)

1

Heap broken

slide-27
SLIDE 27

Binary heap insert

25

2 5 3 1 4 13 6 9

insert(12)

12

insert(7)

17

insert(1)

7

Heap broken

slide-28
SLIDE 28

Binary heap insert

26

2 5 1 3 4 13 6 9

insert(12)

12

insert(7)

17

insert(1)

7

Heap broken

slide-29
SLIDE 29

Binary heap insert

27

1 5 2 3 4 13 6 9

insert(12)

12

insert(7)

17

insert(1)

7

Heap broken

slide-30
SLIDE 30

Binary heap insert

28

1 5 2 3 4 13 6 9

insert(12)

12

insert(7)

17

insert(1)

7

slide-31
SLIDE 31

Binary heap insert

29

1 5 2 3 4 13 6 9

insert(12)

12

insert(7)

17

insert(1)

7

percolateUp

slide-32
SLIDE 32

Binary heap removeMin

30

1 5 2 3 4 13 6 9

removeMin()

12 17 7

slide-33
SLIDE 33

Binary heap removeMin

31

5 2 3 4 13 6 9

removeMin()

12 17 7

slide-34
SLIDE 34

Binary heap removeMin

32

7 5 2 3 4 13 6 9

removeMin()

12 17

slide-35
SLIDE 35

Binary heap removeMin

33

7 5 2 3 4 13 6 9

removeMin()

12 17

Heap broken

slide-36
SLIDE 36

Binary heap removeMin

34

7 5 2 3 4 13 6 9

removeMin()

12 17

Heap broken percolateDown

slide-37
SLIDE 37

Binary heap removeMin

35

2 5 7 3 4 13 6 9

removeMin()

12 17

Heap broken percolateDown

slide-38
SLIDE 38

Binary heap removeMin

36

2 5 3 7 4 13 6 9

removeMin()

12 17

Heap broken percolateDown

slide-39
SLIDE 39

Binary heap removeMin

37

2 5 3 7 4 13 6 9

removeMin()

12 17

percolateDown

slide-40
SLIDE 40

Binary heap: removeMin() runTime

38

slide-41
SLIDE 41

findLastNodeTime + removeRootTime + numOfSwaps * swapTime

Binary heap: removeMin() runTime

38

slide-42
SLIDE 42

findLastNodeTime + removeRootTime + numOfSwaps * swapTime n + 1 + log(n) * 1 = O (n)

Binary heap: removeMin() runTime

38

slide-43
SLIDE 43

findLastNodeTime + removeRootTime + numOfSwaps * swapTime n + 1 + log(n) * 1 = O (n) How can we do better?

Binary heap: removeMin() runTime

38

slide-44
SLIDE 44

findLastNodeTime + removeRootTime + numOfSwaps * swapTime n + 1 + log(n) * 1 = O (n) How can we do better? What do we make efficient in the above expression?

Binary heap: removeMin() runTime

38

slide-45
SLIDE 45

Array implementation of a complete binary tree

39

1 3 2

slide-46
SLIDE 46

Array implementation of a complete binary tree

40

1 3 2

slide-47
SLIDE 47

Binary heap: Array implementation

41

2 11 3 7 4 13 6 9 12 17

Indices 1 2 3 4 5 6 7 8 9 10 11 12

slide-48
SLIDE 48

Binary heap: Array implementation

42

2 5 3 7 4 13 6 9 12 17

Indices 1 2 3 4 5 6 7 8 9 10 11 12

slide-49
SLIDE 49

Binary heap: Array implementation

43

2 5 3 7 4 13 6 9 12 17

Indices 1 2 3 4 5 6 7 8 9 10 11 12

slide-50
SLIDE 50

Binary heap: Array implementation

44

2 5 3 7 4 13 6 9 12 17

Indices 1 2 3 4 5 6 7 8 9 10 11 12

slide-51
SLIDE 51

Binary heap: Array implementation

45

2 5 3 7 4 13 6 9 12 17

Indices 1 2 3 4 5 6 7 8 9 10 11 12

slide-52
SLIDE 52

leftChild(i) = 2i rightChild(i) = 2i + 1 parent(i) = i / 2

Binary heap: Array implementation

46

2 5 3 7 4 13 6 9 12 17

Indices 1 2 3 4 5 6 7 8 9 10 11 12