heaps 3 Nov. 6, 2017 1 STEM Support https://infomcgillste - - PowerPoint PPT Presentation

β–Ά
heaps 3
SMART_READER_LITE
LIVE PREVIEW

heaps 3 Nov. 6, 2017 1 STEM Support https://infomcgillste - - PowerPoint PPT Presentation

COMP 250 Lecture 25 heaps 3 Nov. 6, 2017 1 STEM Support https://infomcgillste m.wixsite.com/stems upportmcgill MSSG = McGill Space systems group http://www.mcgillspace.com/#!/ 2 RECALL: min Heap (definition) a b e u f l k m


slide-1
SLIDE 1

1

COMP 250

Lecture 25

heaps 3

  • Nov. 6, 2017
slide-2
SLIDE 2

2

STEM Support https://infomcgillste m.wixsite.com/stems upportmcgill

MSSG = McGill Space systems group http://www.mcgillspace.com/#!/

slide-3
SLIDE 3

RECALL: min Heap (definition)

3

Complete binary tree with (unique) comparable elements, such that each node’s element is less than its children’s element(s). l u f e a b k m

slide-4
SLIDE 4

Heap index relations

4

l u f e a b k m

1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Not used

parent = child / 2 left = 2*parent right = 2*parent + 1

slide-5
SLIDE 5

5

buildHeap() add() removeMin() upHeap(element) downHeap(1, size)

slide-6
SLIDE 6

buildHeap(){ // assume that an array already contains size elements for (k = 2; k <= size; k++) upHeap( k ) } }

How to build a heap ? (slight variation)

6

slide-7
SLIDE 7

buildHeap(){ // assume that an array already contains size elements for (k = 2; k <= size; k++) upHeap( k ) } } upHeap(k){ i = k while (i > 1) and ( heap[i] < heap[i / 2] ){ swapElement(i, i/2) i = i/2 } }

How to build a heap ? (slight variation)

7

slide-8
SLIDE 8

𝑗 π‘œ

0 1000 2000 3000 4000 5000 12 8 4

π‘šπ‘π‘•2 π‘œ

1 2 π‘œ π‘šπ‘π‘•2π‘œ ≀ 𝑒 π‘œ

≀ π‘œ π‘šπ‘π‘•2π‘œ

Recall last lecture: Worse case of buildHeap

slide-9
SLIDE 9

Thus, worst case: buildHeap is Θ π‘œ π‘šπ‘π‘•2 π‘œ Next, I will show you a Θ(π‘œ) algorithm for building a heap.

9

slide-10
SLIDE 10

How to build a heap ? (fast)

10

e a c f m

Half the nodes of a heap are leaves. (Each leaf is a heap with one node.) The last non-leaf node has index size/2.

slide-11
SLIDE 11

How to build a heap ? (fast)

11

buildHeapFast(){

// assume that heap[ ] array contains size elements

for (k = size/2; k >= 1; k--) downHeap( k, size ) }

slide-12
SLIDE 12

1 2 3 4 5 6

  • w

x t p r f

r f p

x

w

t

1

2 3 4 5 6

12

k = 3

slide-13
SLIDE 13

1 2 3 4 5 6

  • w

x t p r f

r f p

x

w

t

1

2 3 4 5 6

downHeap( 3, 6 )

13

k = 3

slide-14
SLIDE 14

1 2 3 4 5 6

  • w x f p r t

r t p

x

w f

1

2 3 4 5 6

downHeap( 3, 6 )

14

k = 3

slide-15
SLIDE 15

1 2 3 4 5 6

  • w x

f p r t

r t p

x

w f

1

2 3 4 5 6

downHeap( 2, 6 )

15

k = 2

slide-16
SLIDE 16

1 2 3 4 5 6

  • w p f x

r t

r t x

p

w f

1

2 3 4 5 6

16

k = 2

slide-17
SLIDE 17

1 2 3 4 5 6

  • w

p f x r t

r t x

p

w f

1

2 3 4 5 6

downHeap( 1, 6 )

17

k = 1

slide-18
SLIDE 18

1 2 3 4 5 6

  • f p w

x r t

r t x

p

f w

1

2 3 4 5 6

18

k = 1

slide-19
SLIDE 19

1 2 3 4 5 6

  • f p t x r w

r w x

p

f t

1

2 3 4 5 6

19

k = 1

slide-20
SLIDE 20

buildHeapFast(list){ copy list into a heap array for (k = size/2; k >= 1; k--) downHeap( k, size ) } Claim: this algorithm is Θ(n). What is the intuition for why this algorithm is so fast?

20

slide-21
SLIDE 21

We tends to draw binary trees like this: But the number of nodes doubles at each level. So we should draw trees like this: height h height h

21

slide-22
SLIDE 22

buildheap algorithms

22

last lecture

Most nodes swap ~h times in worst case.

today

Few nodes swap ~h times in worst case. height h

slide-23
SLIDE 23

How to show buildHeapFast is Θ(π‘œ) ?

𝑒 π‘œ = 𝑗=1

π‘œ

β„Žπ‘“π‘—π‘•β„Žπ‘’ 𝑝𝑔 π‘œπ‘π‘’π‘“ 𝑗

The worst case number of swaps needed to downHeap node 𝑗 is the height of that node.

23

Β½ of the nodes do no swaps. ΒΌ of the nodes do at most one swap.

1/8 of the nodes do at most two swaps….

slide-24
SLIDE 24

24

e g a c f m d j j d

1

2 3 4 5 6 7 8 9 10 11 12 13 14 15

d d d

level

1 2 3

j d

height

3 2 1

Let’s do the calculation for a tree that whose last level is full.

slide-25
SLIDE 25

Worse case of buildHeapFast ?

How many elements at π‘šπ‘“π‘€π‘“π‘š π‘š ? (π‘š ∈ 0, . . ., β„Ž) What is the height of each π‘šπ‘“π‘€π‘“π‘š π‘š node?

25

slide-26
SLIDE 26

Worse case of buildHeapFast ?

𝑒 π‘œ = 𝑗=1

π‘œ

β„Žπ‘“π‘—π‘•β„Žπ‘’ 𝑝𝑔 π‘œπ‘π‘’π‘“ 𝑗

π‘šπ‘“π‘€π‘“π‘š π‘š has 2π‘š elements, π‘š ∈ 0, . . ., β„Ž π‘šπ‘“π‘€π‘“π‘š π‘š nodes have height β„Ž βˆ’ π‘š.

=

?

26

slide-27
SLIDE 27

Worse case of buildHeapFast ?

𝑒 π‘œ = 𝑗=1

π‘œ

β„Žπ‘“π‘—π‘•β„Žπ‘’ 𝑝𝑔 π‘œπ‘π‘’π‘“ 𝑗

π‘šπ‘“π‘€π‘“π‘š π‘š has 2π‘š elements, π‘š ∈ 0, . . ., β„Ž π‘šπ‘“π‘€π‘“π‘š π‘š nodes have height β„Ž βˆ’ π‘š.

= π‘š=0

β„Ž

β„Ž βˆ’ π‘š 2π‘š

27

slide-28
SLIDE 28

Easy Difficult (number

  • f nodes)

28

(sum of node depths)

slide-29
SLIDE 29

29

(See next slide)

Since , we get :

slide-30
SLIDE 30

30

Second term index goes to h-1 only

slide-31
SLIDE 31

31

Since , we get :

slide-32
SLIDE 32

Summary: buildheap algorithms

32

last lecture 𝑃 π‘œ π‘šπ‘π‘•2 π‘œ today

𝑃(π‘œ)

height h

slide-33
SLIDE 33

Given a list with size elements: Build a heap. Repeatedly call removeMin() and put the removed elements into a list.

Heapsort

slide-34
SLIDE 34

Given an array heap[ ] with size elements: heapsort(){ buildheap( ) for i = 1 to size{ swapElements( heap[1], heap[size + 1 - i]) downHeap( 1, size – i ) } return reverse(heap) }

β€œin place” Heapsort

slide-35
SLIDE 35

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |
slide-36
SLIDE 36

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

w d b e l u k f | a

slide-37
SLIDE 37

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

w d b e l u k f | a b d w e l u k f | a

slide-38
SLIDE 38

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

w d b e l u k f | a b d w e l u k f | a b d k e l u w f | a

slide-39
SLIDE 39

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

b d k e l u w f | a

slide-40
SLIDE 40

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

b d k e l u w f | a f d k e l u w | b a

slide-41
SLIDE 41

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

b d k e l u w f | a f d k e l u w | b a d f k e l u w | b a

slide-42
SLIDE 42

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

b d k e l u w f | a f d k e l u w | b a d f k e l u w | b a d e k f l u w | b a

slide-43
SLIDE 43

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

b d k e l u w f | a d e k f l u w | b a

slide-44
SLIDE 44

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

b d k e l u w f | a d e k f l u w | b a e f k w l u | d b a

slide-45
SLIDE 45

1 2 3 4 5 6 7 8 9

  • a d b e l u k f w |

b d k e l u w f | a d e k f l u w | b a e f k w l u | d b a f l k w u | e d b a k l u w | f e d b a l w u | k f e d b a u w | l k f e d b a w | u l k f e d b a w u l k f e d b a

slide-46
SLIDE 46

heapsort(list){ buildheap(list) for i = 1 to size{ swapElements( heap[1], heap[size + 1 - i]) downHeap( 1, size - i) } return reverse(heap) }

Heapsort