Professional Documents
Culture Documents
Fall 2007
Lecture 3: Heaps
Analyzing algorithms using Θ-notation
Proof: by induction on n
Base case: n = n0
T(2) = 2T(1) + 2 = 2c1 + 2 = O(2)
Inductive step: n > n0
T(n) = 2T( n/2 ) + n
= 2O( n/2 ) + n (ind. hyp.)
= O(n) ■
Example
Example
Example (A)
► A is an array of length n
1. n ← length[A]
2. if n=1
3. then return A[1]
4. else begin
5. Copy A[1… n/2 ] to auxiliary array B[1... n/2 ]
6. Copy A[1… n/2 ] to auxiliary array C[1… n/2 ]
7. b ← Example(B); c ← Example(C)
8. for i ← 1 to n
9. do for j ← 1 to i
10. do A[i] ← A[j]
11. return 43
12. end
Example
Example (A) Let T(n) be the worst case running time of
Example on an array of length n.
► A is an array of length n
1. n ← length[A] Lines 1,2,3,4,11, and 12 take Θ(1) time.
2. if n=1 Lines 5 and 6 take Θ(n) time.
3. then return A[1] Line 7 takes Θ(1) + 2 T( n/2 ) time.
4. else begin Lines 8 until 10 take
n i n
5.
Copy A[1… n/2 ] to auxiliary array B[1...
i1 j1
n/2 ] Θ(i) Θ(n2 ) time.
Θ(1)
i1
6. Copy A[1… n/2 ] to auxiliary array C[1… n/2 ]
7. If n=1 lines 1,2,3 are executed,
b ← Example(B); c ← Example(C)
else lines 1,2, and 4 until 12 are executed.
8. for i ← 1 to n
9. do for j ← 1 to i ➨ T(n): Θ(1) if n=1
2T(n/2) + Θ(n2) if n>1
10. do A[i] ← A[j]
11. return 43 ➨ use master theorem …
12. end
Quiz
Recurrence Master theorem?
Operations
Insert(S, x): inserts element x into S, that is, S ← S ⋃ {x}
Maximum(S): returns the element of S with the largest key
Extract-Max(S): removes and returns the element of S with the
largest key
Increase-Key(S, x, k): give key[x] the value k
condition: k is larger than the current value of key[x]
Min-priority queue …
Implementing a priority queue
Today
30 19
35 30 8 21
30 19 35
30 19
30 8 12 11 30 8 12
17 5 2
Heap
nearly complete binary tree, filled on all levels except possibly the lowest.
(lowest level is filled from left to right)
Proof:
Properties of a max-heap
Lemma
The largest element in a max-heap is stored at the root.
Proof: x root
y arbitrary node x
z1, z2, …, zk nodes on path z1
between x and y
z2
30 19
30 8 12 11
35 30 19 30 8 12 11 17 2 5
1 2 3 4
heap-size[A]
level 1
level 2, position 3
1 2 3 4
heap-size[A]
kth node on level j is stored at position A[2j-1+k]
left child of node at position i = Left(i) = 2i
right child of node at position i = Right(i) = 2i + 1
parent of node at position i = Parent(i) = i / 2
Priority queue
Max-priority queue
abstract data structure (ADS) that stores a set S of elements, each
with an associated key (integer value).
Operations
Insert(S, x): inserts element x into S, that is, S ← S ⋃ {x}
Maximum(S): returns the element of S with the largest key
Extract-Max(S): removes and returns the element of S with the
largest key
Increase-Key(S, x, k): give key[x] the value k
condition: k is larger than the current value of key[x]
Implementing a max-priority queue
Set S is stored as a heap in an array A.
Operations: Maximum, Extract-Max, Insert, Increase-Key.
35 30 19 30 8 12 11 17 2 5
1 2 3 4
heap-size[A]
Implementing a max-priority queue
Set S is stored as a heap in an array A.
Operations: Maximum, Extract-Max, Insert, Increase-Key.
35 30 19 30 8 12 11 17 2 5
1 2 3 4
heap-size[A]
Maximum (A)
1. if heap-size[A] < 1
2. then error
3. else return A[1]
35 30 19 30 8 12 11 17 2 5
1 2 3 4
heap-size[A]
Implementing a max-priority queue
Set S is stored as a heap in an array A.
Operations: Maximum, Extract-Max, Insert, Increase-Key.
35
30
30 19
30
17 8 12 11
17 2 5
Implementing a max-priority queue
Set S is stored as a heap in an array A.
Operations: Maximum, Extract-Max, Insert, Increase-Key.
35
5
30 19
Heap-Extract-Max (A) 30 8 12 11
1. if heap-size[A] < 1
2. then error 17 2 5
3. max ← A[1]
4. A[1] ← A [heap-size[A]] restore heap property
5. heap-size[A] ← heap-size[A]–1
6. Max-Heapify(A,1)
7. return max
Max-Heapify
Max-Heapify(A, i)
► ensures that the heap whose root is stored at position i has the
max-heap property
► assumes that the binary trees rooted at Left(i) and Right(i) are max-
heaps
Max-Heapify
Max-Heapify(A, i)
► ensures that the heap whose root is stored at position i has the
max-heap property
► assumes that the binary trees rooted at Left(i) and Right(i) are max-
heaps
Max-Heapify(A,1) 10
40
35
30 19
Increase-Key(A, i, key) 30 8 12
42 11
1. if key < A[i]
2. then error 17 2 5
3. A[i] ← key
4. while i>1 and A[parent(i)] < A[i]
5. do exchange A[parent(i)] and A[i]
6. i ← parent(i)
Invariant
P(k+1) holds before line 3 is executed with i=k,
P(k) holds afterwards
Building a heap
Build-Max-Heap(A)
1. heap-size ← length[A]
2. for i ← length[A] downto 1
3. do Max-Heapify(A,i) O(height of node i)
height of node i
∑i O(1 + height of i) # edges longest simple downward
= ∑0≤h≤ log n (# nodes of height h) ∙ O(1+h) path from i to a leaf.
h=1
h=0
The sorting problem
Invariant
P(k) holds before lines 3-5 are executed with i=k,
P(k-1) holds afterwards
Sorting algorithms