53
( J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 1 / 18

CSI - 3105 Design & Analysis of Algorithms (Heaps)cglab.ca/~jdecaruf/CSI3105-F2018/SlidesHeap.pdf · Heaps 4. Heapify(A;i) (sometimes called sift-down): this operation assumes that

  • Upload
    vocong

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

(J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 1 / 18

Heaps

Heaps

An array A[1..n] is called a heap if for all i ≥ 1,

A[i ] ≥ A[2i ] if 2i ≤ n

and

A[i ] ≥ A[2i + 1] if 2i + 1 ≤ n

16

14 10

8 7 9 3

2 4 1

1

2 3

4 5 6 7

8 9 10

16 14 10 8 7 9 3 2 4 1

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 2 / 18

Heaps

Root of the tree: A[1]

Consider a node with index i :

Parent of i has index bi/2c: parent(i) = bi/2cLeft child of i has index 2i : left(i) = 2i

Right child of i has index 2i + 1: right(i) = 2i + 1

A[1..n] is a heap if for all 1 < i ≤ n, A[parent(i)] ≥ A[i ].

16

14 10

8 7 9 3

2 4 1

1

2 3

4 5 6 7

8 9 10

16 14 10 8 7 9 3 2 4 1

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 3 / 18

Heaps

What is the height of a heap?

h

level 0 (height h): 1 nodelevel 1 (height h− 1): 2 nodeslevel 2 (height h− 2): 4 nodes

level h− 1 (height 1): 2h−1 nodes

level h (height 0): between 1 and 2h nodes

1 + 2 + 22 + ...+ 2h−1 + 1 ≤n ≤ 1 + 2 + 22 + ...+ 2h−1 + 2h(2h − 1

)+ 1 ≤n ≤ 2h+1 − 1 < 2h+1

2h ≤n < 2h+1

h ≤ log2(n) < h + 1

h =blog2(n)c

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 4 / 18

Heaps

What is the height of a heap?

h

level 0 (height h): 1 nodelevel 1 (height h− 1): 2 nodeslevel 2 (height h− 2): 4 nodes

level h− 1 (height 1): 2h−1 nodes

level h (height 0): between 1 and 2h nodes

1 + 2 + 22 + ...+ 2h−1 + 1 ≤n ≤ 1 + 2 + 22 + ...+ 2h−1 + 2h

(2h − 1

)+ 1 ≤n ≤ 2h+1 − 1 < 2h+1

2h ≤n < 2h+1

h ≤ log2(n) < h + 1

h =blog2(n)c

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 4 / 18

Heaps

What is the height of a heap?

h

level 0 (height h): 1 nodelevel 1 (height h− 1): 2 nodeslevel 2 (height h− 2): 4 nodes

level h− 1 (height 1): 2h−1 nodes

level h (height 0): between 1 and 2h nodes

1 + 2 + 22 + ...+ 2h−1 + 1 ≤n ≤ 1 + 2 + 22 + ...+ 2h−1 + 2h(2h − 1

)+ 1 ≤n ≤ 2h+1 − 1

< 2h+1

2h ≤n < 2h+1

h ≤ log2(n) < h + 1

h =blog2(n)c

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 4 / 18

Heaps

What is the height of a heap?

h

level 0 (height h): 1 nodelevel 1 (height h− 1): 2 nodeslevel 2 (height h− 2): 4 nodes

level h− 1 (height 1): 2h−1 nodes

level h (height 0): between 1 and 2h nodes

1 + 2 + 22 + ...+ 2h−1 + 1 ≤n ≤ 1 + 2 + 22 + ...+ 2h−1 + 2h(2h − 1

)+ 1 ≤n ≤ 2h+1 − 1 < 2h+1

2h ≤n < 2h+1

h ≤ log2(n) < h + 1

h =blog2(n)c

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 4 / 18

Heaps

What is the height of a heap?

h

level 0 (height h): 1 nodelevel 1 (height h− 1): 2 nodeslevel 2 (height h− 2): 4 nodes

level h− 1 (height 1): 2h−1 nodes

level h (height 0): between 1 and 2h nodes

1 + 2 + 22 + ...+ 2h−1 + 1 ≤n ≤ 1 + 2 + 22 + ...+ 2h−1 + 2h(2h − 1

)+ 1 ≤n ≤ 2h+1 − 1 < 2h+1

2h ≤n < 2h+1

h ≤ log2(n) < h + 1

h =blog2(n)c

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 4 / 18

Heaps

What is the height of a heap?

h

level 0 (height h): 1 nodelevel 1 (height h− 1): 2 nodeslevel 2 (height h− 2): 4 nodes

level h− 1 (height 1): 2h−1 nodes

level h (height 0): between 1 and 2h nodes

1 + 2 + 22 + ...+ 2h−1 + 1 ≤n ≤ 1 + 2 + 22 + ...+ 2h−1 + 2h(2h − 1

)+ 1 ≤n ≤ 2h+1 − 1 < 2h+1

2h ≤n < 2h+1

h ≤ log2(n) < h + 1

h =blog2(n)c

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 4 / 18

Heaps

What is the height of a heap?

h

level 0 (height h): 1 nodelevel 1 (height h− 1): 2 nodeslevel 2 (height h− 2): 4 nodes

level h− 1 (height 1): 2h−1 nodes

level h (height 0): between 1 and 2h nodes

1 + 2 + 22 + ...+ 2h−1 + 1 ≤n ≤ 1 + 2 + 22 + ...+ 2h−1 + 2h(2h − 1

)+ 1 ≤n ≤ 2h+1 − 1 < 2h+1

2h ≤n < 2h+1

h ≤ log2(n) < h + 1

h =blog2(n)cJ.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 4 / 18

Heaps

From now on: we consider an array A[1..N] with an integer n where1 ≤ n ≤ N.

A[1..n] is a heap which contains n integers and A[(n + 1)..N] containsgarbage.

heap garbage

1 n (n + 1) N

1. Finding a maximum elementMaximum(A): returns A[1]

This takes O(1) time.

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 5 / 18

Heaps

2. Increase the value of A[i ] to x .

16

14 10

8 7 9 3

2 4 1

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 6 / 18

Heaps

2. Increase the value of A[i ] to x .

16

14 10

8 7 9 3

2 4 1

1

2 3

4 5 6 7

8 9 10

Increase key(A, 9, 15)

Increase this key to 15

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 6 / 18

Heaps

2. Increase the value of A[i ] to x .

16

14 10

8 7 9 3

2 15 1

1

2 3

4 5 6 7

8 9 10

Increase key(A, 9, 15)

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 6 / 18

Heaps

2. Increase the value of A[i ] to x .

16

14 10

8

7 9 3

2

15

1

1

2 3

4 5 6 7

8 9 10

Increase key(A, 9, 15)

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 6 / 18

Heaps

2. Increase the value of A[i ] to x .

16

14

10

8

7 9 3

2

15

1

1

2 3

4 5 6 7

8 9 10

Increase key(A, 9, 15)

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 6 / 18

Heaps

2. Increase the value of A[i ] to x .

16

14

10

8

7 9 3

2

15

1

1

2 3

4 5 6 7

8 9 10

Increase key(A, 9, 15)

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 6 / 18

Heaps

Algorithm 1 Increase key(A, i , x)

Require: x ≥ A[i ]1: A[i ] = x2: // The following while-loop is sometimes called percolate.3: while i > 1 and A[parent(i)] < A[i ] do4: swap A[i ] and A[parent(i)]5: i = parent(i)6: end while

This takes O(h) = O(log(n)) time.

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 7 / 18

Heaps

3. Insert a new value in A.

16

14

10

8

7 9 3

2

15

1

1

2 3

4 5 6 7

8 9 10

Insert(A, 13)

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 8 / 18

Heaps

3. Insert a new value in A.

16

14

10

8

7 9 3

2

15

1

1

2 3

4 5 6 7

8 9 10

Insert(A, 13)

−∞11

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 8 / 18

Heaps

3. Insert a new value in A.

16

14

10

8

7 9 3

2

15

1

1

2 3

4 5 6 7

8 9 10

Insert(A, 13)

1311

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 8 / 18

Heaps

3. Insert a new value in A.

16

14

10

8 7

9 3

2

15

1

1

2 3

4 5 6 7

8 9 10

Insert(A, 13)

13

11

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 8 / 18

Heaps

3. Insert a new value in A.

16

14

10

8 7

9 3

2

15

1

1

2 3

4 5 6 7

8 9 10

Insert(A, 13)

13

11

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 8 / 18

Heaps

Algorithm 2 Insert(A, x)

Require:

1 ≤ n < N

and A[1..n] is a heap.

1: n = n + 12: A[n] = −∞3: Increase key(A, n, x)

This takes O(h) = O(log(n)) time.

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 9 / 18

Heaps

4. Heapify(A, i) (sometimes called sift-down): this operation assumesthat

1 ≤ i ≤ n,

the subtree rooted at left(i) is a heap

and the subtree rooted at right(i) is a heap.

At termination, the subtree rooted at i is a heap.

i

Heap Heap

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 10 / 18

Heaps

16

4 10

14 7 9 3

2 8 1

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 11 / 18

Heaps

16

4 10

14 7 9 3

2 8 1

1

2 3

4 5 6 7

8 9 10

Heapify(A, 2)

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 11 / 18

Heaps

16

4

1014

7 9 3

2 8 1

1

2 3

4 5 6 7

8 9 10

Heapify(A, 2)

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 11 / 18

Heaps

16

4

1014

7 9 3

2

8

1

1

2 3

4 5 6 7

8 9 10

Heapify(A, 2)

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 11 / 18

Heaps

Algorithm 3 Heapify(A, i)Require:

1 ≤ i ≤ n,

the subtree rooted at left(i) is a heap

and the subtree rooted at right(i) is a heap.

1: ` = left(i)2: r = right(i)3: if ` ≤ n and A[`] > A[i ] then4: max = `5: else6: max = i7: end if8: if r ≤ n and A[r ] > A[max] then9: max = r10: end if11: if max 6= i then12: swap A[i ] and A[max]13: Heapify(A,max)14: end if

This takes O(h) = O(log(n)) time.J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 12 / 18

Heaps

5. Build heap(A)

4

1 3

2 16 9 10

14 8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1 3

2 16 9 10

14 8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1 3

2 16 9 10

14 8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1 3

2 16 9 10

14 8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1 3

2 16 9 10

14 8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1 3

2

16 9 1014

8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1 3

2

16 9 1014

8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1

3

2

16 9

10

14

8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1

3

2

16 9

10

14

8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1 3

2

16

9

10

14

8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1

3

2

16

9

10

14

8

7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1

3

2

16

9

10

14

8

7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1

3

2

16

9

10

14

8

7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4

1

3

2

16

9

1014

8

7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4 1

3

2

16

9

1014

8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

5. Build heap(A)

4 1

3

2

16

9

1014

8 7

1

2 3

4 5 6 7

8 9 10

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 13 / 18

Heaps

Algorithm 4 Build heap(A)

1: for i = bn/2c to 1 do2: Heapify(A, i)3: end for

How many steps to build a heap?

There is 20 node at height h.There are 21 nodes at height h − 1.There are 22 nodes at height h − 2.

...There are 2h−1 nodes at height 1.

h · 20 + (h − 1) · 21 + (h − 2) · 22 + ...+ 1 · 2h−1

=h−1∑i=0

(h − i) · 2i

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 14 / 18

Heaps

Algorithm 4 Build heap(A)

1: for i = bn/2c to 1 do2: Heapify(A, i)3: end for

How many steps to build a heap?

There is 20 node at height h.There are 21 nodes at height h − 1.There are 22 nodes at height h − 2.

...There are 2h−1 nodes at height 1.

h · 20 + (h − 1) · 21 + (h − 2) · 22 + ...+ 1 · 2h−1

=h−1∑i=0

(h − i) · 2i

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 14 / 18

Heaps

Algorithm 4 Build heap(A)

1: for i = bn/2c to 1 do2: Heapify(A, i)3: end for

How many steps to build a heap?

There is 20 node at height h.There are 21 nodes at height h − 1.There are 22 nodes at height h − 2.

...There are 2h−1 nodes at height 1.

h · 20 + (h − 1) · 21 + (h − 2) · 22 + ...+ 1 · 2h−1

=h−1∑i=0

(h − i) · 2i

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 14 / 18

Heaps

h−1∑i=0

(h − i) · 2i

=h−1∑i=0

h · 2i −h−1∑i=0

i · 2i

= hh−1∑i=0

2i −h−1∑i=0

i · 2i

= h(2h − 1

)−((h − 2) · 2h + 2

)= 2 · 2h − h − 2

= O(n)

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 15 / 18

Heaps

6. Extract max A

Algorithm 5 Extract max(A)

1: max = A[1]2: A[1] = A[n]3: n = n − 14: Heapify(A, 1)5: return max

This takes O(h) = O(log(n)) time.

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 16 / 18

Heaps

We have discussed max-heaps.

There is their symmetric counterpart: min-heaps,

where A[parent(i)] ≤ A[i ] (1 < i ≤ n).

J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 17 / 18

Heaps

)J.-L. De Carufel (U. of O.) Design & Analysis of Algorithms Fall 2017 18 / 18