Beruflich Dokumente
Kultur Dokumente
Analysis of Algorithms
3
Recurrences
Master’s Method
Heapsort and Priority Queue
Many of the slides are taken from Monica Nicolescu’s slides, Univ. Nevada, Reno, monica@cs.unr.edu
Fibonacci numbers
Leonardo Pisano
n
T (n) aT f (n)
b
1. Guess a solution
• Experiences, creativity
• iteration method, recursion-tree method
i 0
2
i
2 W (1) n
2
2
i 0
2
nn 2
i 0
O(n) n
1 1
O ( n) 2n 2
2
W(n) = O(n2)
5/18/2004 Lecture 3 COSC3101A 23
Recursion-tree Example 2
E.g.: T(n) = 3T(n/4) + cn2
3 2 3 1
T (n) cn n log4 3
cn 2 n log4 3 cn 2 n log4 3 O(n 2 )
i 0 16 i 0 16
3
1
T(n) = O(n2) 16
5/18/2004 Lecture 3 COSC3101A 24
Master method
• “Cookbook” for solving recurrences of the form:
where, a ≥ 1, b > 1, and f(n) > 0
n
T (n) aT f (n)
b
Case 1: if f(n) = O(nlogba -) for some > 0, then: T(n) = (nt)
af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then:
T(n) = (f(n))
regularity condition
n • Case 1:
T (n) aT
b – If f(n) is dominated by nlogba:
n
a 2T 2 • T(n) = (nlogbn)
b
n
a 3T 3
b • Case 3:
– If f(n) dominates nlogba:
n
T (n) a iT i i • T(n) = (f(n))
b
• Case 2:
• Assume n = bk k = logbn – If f(n) = (nlogba):
• At the end of iteration i = k: • T(n) = (nlogba logn)
bi
T ( n) a
T i a logb nT (1) a logb n n logb a
log b n
b
5/18/2004 Lecture 3 COSC3101A 26
Examples (1)
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
T(n) = (nlgn)
5/18/2004 Lecture 3 COSC3101A 27
Examples (2)
T(n) = 2T(n/2) + n2
a = 2, b = 2, log22 = 1
Compare n with f(n) = n2
f(n) = (n1+) Case 3 verify regularity cond.
a f(n/b) ≤ c f(n)
2 n2/4 ≤ c n2 c = ½ is a solution (c<1)
T(n) = (n2)
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
T(n) = (n)
a = 3, b = 4, log43 = 0.793
T(n) = (nlgn)
5/18/2004 Lecture 3 COSC3101A 30
Examples (5)
a = 2, b = 2, log22 = 1
• Operations
– Insert, remove maximum
• Data structures
– Priority queues
– Ordered array/list, unordered array/list
5/18/2004 Lecture 3 COSC3101A 32
Example
ordered array N 1
ordered list N 1
unordered array 1 N
unordered list 1 N
4
• Def: Complete binary tree = a
1 3
binary tree in which all leaves
2 16 9 10
have the same depth and all
internal nodes have degree 2. Complete binary tree
1 3
Height of (2)= 1 2 16 9 10 Depth of (10)= 2
14 8
A[2] A[4]
A[2] violates the heap property A[4] violates the heap property
A[4] A[9]
Alg: BUILD-MAX-HEAP(A) 1
4
1. n = length[A]
2 3
3. do MAX-HEAPIFY(A, i, n) 8
2 9 10
16 9 10
14 8 7
A: 4 1 3 2 16 9 10 14 8 7
4 4 4
2 3 2 3 2 3
1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7
8
2 9 10
16 9 10 8 2 9 10
16 9 10 8 14 9 10
16 9 10
14 8 7 14 8 7 2 8 7
i=2 i=1
1 1 1
4 4 16
2 3 2 3 2 3
1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7
8
14 9 10
16 9 3 8
14 9 10
7 9 3 8
8 9 10
7 9 3
2 8 7 2 8 1 2 4 1
5/18/2004 Lecture 3 COSC3101A 48
Correctness of BUILD-MAX-HEAP
• Loop invariant:
– At the start of each iteration of the for loop, each node
i + 1, i + 2,…, n is the root of a max-heap
• Initialization:
– i = n/2: Nodes n/2+1, n/2+2, …, n are leaves
they are the root of trivial max-heaps
4
2 3
1 3
4 5 6 7
8
2 9 10
16 9 10
14 8 7
4
nodes i + 1, i + 2, …, n are roots of max-
heaps 2 3
1 3
– Decrementing i in the for loop 4 5 6 7
2 16 9 10
reestablishes the loop invariant 8 9 10
14 8 7
• Termination:
– i = 0 each node 1, 2, …, n is the root
of a max-heap (by the loop invariant)
Alg: BUILD-MAX-HEAP(A)
1. n = length[A]
2. for i ← n/2 downto 1
O(n)
3. do MAX-HEAPIFY(A, i, n) O(lgn)
h1 = 2 i=1 21
h2 = 1 i=2 22
h3 = 0 i = 3 (lgn) 23
h
hi = h – i height of the heap rooted at level i T (n) ni hi
ni = 2i number of nodes at level i i 0
• Idea:
– Build a max-heap from the array
– Swap the root (the maximum element) with the last
element in the array
– “Discard” this last node by decreasing the heap size
– Call MAX-HEAPIFY on the new root
– Repeat this process until only one node remains
1. BUILD-MAX-HEAP(A) O(n)
MAX-HEAPIFY(A, 1, 1)
Heap-Maximum(A) returns 7
5/18/2004 Lecture 3 COSC3101A 57
HEAP-EXTRACT-MAX
Goal:
– Extract the largest element of the heap (i.e., return the max
value and also remove that element from the heap
Idea:
– Exchange the root element with the last
– Decrease the size of the heap by 1 element
– Call MAX-HEAPIFY on the new root, on a heap of size n-1
Alg: HEAP-EXTRACT-MAX(A, n)
1. if n < 1
2. then error “heap underflow”
3. max ← A[1]
4. A[1] ← A[n]
6. return max
Running time: O(lgn)
5/18/2004 Lecture 3 COSC3101A 59
Example: HEAP-EXTRACT-MAX
16 1
14 10 max = 16 14 10
8 7 9 3 8 7 9 3
2 4 1 2 4
Heap size decreased with 1
14
14 10
8 7 9 3
i
Key [i] ← 15 2 4 1
14 10 14 10
8 7 9 3 8 7 9 3
i i
2 4 1 2 15 1
Key [i ] ← 15
16 16
i
14 10 15 10
i
15 7 9 3 14 7 9 3
2 8 1 2 8 1
max-heap property
5/18/2004 Lecture 3 COSC3101A 64
MAX-HEAP-INSERT
16
Alg: MAX-HEAP-INSERT(A, key, n)
14 10
1. heap-size[A] ← n + 1 8 7 9 3
2 4 1 -
2. A[n + 1] ← -
3. HEAP-INCREASE-KEY(A, n + 1, key)
14 10 14 10
8 7 9 3 8 7 9 3
2 4 1 - 2 4 1 15
16 16
14 10 15 10
8 15 9 3 8 14 9 3
2 4 1 7 2 4 1 7
5/18/2004 Lecture 3 COSC3101A 66
Summary
• We can perform the following operations on
heaps:
– MAX-HEAPIFY O(lgn)
– BUILD-MAX-HEAP O(n)
– HEAP-SORT O(nlgn)
– MAX-HEAP-INSERT O(lgn)
– HEAP-EXTRACT-MAX O(lgn)
– HEAP-INCREASE-KEY O(lgn)
– HEAP-MAXIMUM O(1)
5/18/2004 Lecture 3 COSC3101A 67
Readings
• Chapter 8, 6