Lecture 24 - McGill University

31
1 COMP 250 Lecture 24 heaps 3 Nov. 4, 2016

Transcript of Lecture 24 - McGill University

Page 1: Lecture 24 - McGill University

1

COMP 250

Lecture 24

heaps 3

Nov. 4, 2016

Page 2: Lecture 24 - McGill University

RECALL: min Heap (definition)

2

Complete binary tree with (unique) comparable elements, such that each node’s element is less than its children’s element(s).

l uf

e

a

b

k

m

Page 3: Lecture 24 - McGill University

Heap index relations

3

l uf

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 / 2left = 2*parentright = 2*parent + 1

Page 4: Lecture 24 - McGill University

Given a list with size elements:

buildHeap(list){ create new heap array // length > list.sizefor (k = 0; k < list.size; k++)

add( list[k] ) // add to heap[ ]}

RECALL: How to build a heap

4

Page 5: Lecture 24 - McGill University

5

e ga

c

f

m

d

j

jd

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

ddd

2𝑙𝑒𝑣𝑒𝑙 ≀ 𝑖 < 2𝑙𝑒𝑣𝑒𝑙 +1

𝑙𝑒𝑣𝑒𝑙 ≀ π‘™π‘œπ‘”2 𝑖 < 𝑙𝑒𝑣𝑒𝑙 + 1

level0

1

2

3

Thus, 𝑙𝑒𝑣𝑒𝑙 = π‘“π‘™π‘œπ‘œπ‘Ÿ( π‘™π‘œπ‘”2 𝑖 )

jd

Page 6: Lecture 24 - McGill University

Worse case of buildHeap

= 𝑖=1𝑛 𝑙𝑒𝑣𝑒𝑙 π‘œπ‘“ π‘›π‘œπ‘‘π‘’ 𝑖

= 𝑖=1𝑛 π‘“π‘™π‘œπ‘œπ‘Ÿ( π‘™π‘œπ‘”2 𝑖 )

𝑑 𝑛 = 𝑖=1𝑛 π‘›π‘’π‘šπ‘π‘’π‘Ÿ π‘œπ‘“ π‘ π‘€π‘Žπ‘π‘  π‘“π‘œπ‘Ÿ π‘›π‘œπ‘‘π‘’ 𝑖

6

Page 7: Lecture 24 - McGill University

π‘™π‘œπ‘”2 𝑖

𝑖 𝑛

𝑑 𝑛 = 𝑖=1𝑛 π‘“π‘™π‘œπ‘œπ‘Ÿ( π‘™π‘œπ‘”2 𝑖 )

Area under the dashed curve is the totalnumber of swaps (worst case) of buildHeap.

0

1

2

8

4

0

π‘™π‘œπ‘”2 𝑛

7

Page 8: Lecture 24 - McGill University

𝑖 𝑛0 1000 2000 3000 4000 5000

12

8

4

0

π‘™π‘œπ‘”2 𝑛

1

2𝑛 π‘™π‘œπ‘”2𝑛 ≀ 𝑑 𝑛 ≀ 𝑛 π‘™π‘œπ‘”2𝑛

8

Page 9: Lecture 24 - McGill University

Thus, worst case: buildHeap is 𝑂 𝑛 π‘™π‘œπ‘”2 𝑛

This is a tight 𝑂( ) bound.

Today, I will show you a 𝑂(𝑛) algorithm.

9

Page 10: Lecture 24 - McGill University

Given a list with size elements:

buildHeapFast(list){ copy list into a heap arrayfor (k = size/2; k >= 1; k--)

downHeap( k, size ) }

10

Page 11: Lecture 24 - McGill University

1 2 3 4 5 6

----------------------

w x t p r f

r fp

x

w

t

1

2 3

4 5 6

11

Page 12: Lecture 24 - McGill University

1 2 3 4 5 6

----------------------

w x t p r f

r fp

x

w

t

1

2 3

4 5 6

downHeap( 3, 6 )

12

Page 13: Lecture 24 - McGill University

1 2 3 4 5 6

----------------------

w x f p r t

r tp

x

w

f

1

2 3

4 5 6

downHeap( 3, 6 )

13

Page 14: Lecture 24 - McGill University

1 2 3 4 5 6

----------------------

w x f p r t

r tp

x

w

f

1

2 3

4 5 6

downHeap( 2, 6 )

14

Page 15: Lecture 24 - McGill University

1 2 3 4 5 6

----------------------

w p f x r t

r tx

p

w

f

1

2 3

4 5 6

15

Page 16: Lecture 24 - McGill University

1 2 3 4 5 6

----------------------

w p f x r t

r tx

p

w

f

1

2 3

4 5 6

downHeap( 1, 6 )

16

Page 17: Lecture 24 - McGill University

1 2 3 4 5 6

----------------------

f p w x r t

r tx

p

f

w

1

2 3

4 5 6

17

Page 18: Lecture 24 - McGill University

1 2 3 4 5 6

----------------------

f p t x r w

r wx

p

f

t

1

2 3

4 5 6

18

Page 19: Lecture 24 - McGill University

buildHeapFast(list){ copy list into a heap arrayfor (k = size/2; k >= 1; k--)

downHeap( k, size ) }

Claim: this algorithm is O(n).

Intuition for why this algorithm is so fast?

19

Page 20: Lecture 24 - McGill University

buildheap algorithms

20

last lecture

upHeap based downHeap based

today

Page 21: Lecture 24 - McGill University

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

Page 22: Lecture 24 - McGill University

buildheap algorithms

22

last lecture

Most nodes swap htimes in worst case.

today

Few nodes swap h times in worst case.

height h

Page 23: Lecture 24 - McGill University

How to show buildHeapFast is 𝑂(𝑛) ?

𝑑 𝑛 = 𝑖=1𝑛 β„Žπ‘’π‘–π‘”β„Žπ‘‘ π‘œπ‘“ π‘›π‘œπ‘‘π‘’ 𝑖

Worst case number of swaps needed to add node 𝑖 is the the height of that node.

(Recall the height of a node is the length of the longest path from that node to a leaf.)

23

Page 24: Lecture 24 - McGill University

24

e ga

c

f

m

d

j

jd

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

ddd

level

0

1

2

3jd

height

3

2

1

0

Page 25: Lecture 24 - McGill University

Worse case of buildHeapFast ?

How many elements at 𝑙𝑒𝑣𝑒𝑙 𝑙 ? (𝑙 ∈ 0, . . ., β„Ž)

What is the height of each 𝑙𝑒𝑣𝑒𝑙 𝑙 node?

25

Page 26: Lecture 24 - McGill University

Worse case of buildHeapFast ?

𝑑 𝑛 = 𝑖=1𝑛 β„Žπ‘’π‘–π‘”β„Žπ‘‘ π‘œπ‘“ π‘›π‘œπ‘‘π‘’ 𝑖

𝑙𝑒𝑣𝑒𝑙 𝑙 has 2𝑙 elements, 𝑙 ∈ 0, . . ., β„Ž

𝑙𝑒𝑣𝑒𝑙 𝑙 nodes have height β„Ž βˆ’ 𝑙.

= ?26

Page 27: Lecture 24 - McGill University

Worse case of buildHeapFast ?

𝑑 𝑛 = 𝑖=1𝑛 β„Žπ‘’π‘–π‘”β„Žπ‘‘ π‘œπ‘“ π‘›π‘œπ‘‘π‘’ 𝑖

𝑙𝑒𝑣𝑒𝑙 𝑙 has 2𝑙 elements, 𝑙 ∈ 0, . . ., β„Ž

𝑙𝑒𝑣𝑒𝑙 𝑙 nodes have height β„Ž βˆ’ 𝑙.

= 𝑙=0β„Ž β„Ž βˆ’ 𝑙 2𝑙

27

Page 28: Lecture 24 - McGill University

Easy Difficult

(number of nodes)

28

(sum of nodedepths)

Page 29: Lecture 24 - McGill University

29

I have removed the next two slides which derived a closed for expression for the second summation (the difficult one). Please see lecture notes for a slightly different derivation, which is simpler.

Page 30: Lecture 24 - McGill University

30

(See lecture notes)

Page 31: Lecture 24 - McGill University

Summary: buildheap algorithms

31

last lecture

𝑂 𝑛 π‘™π‘œπ‘”2 𝑛

today

𝑂(𝑛)

height h