Professional Documents
Culture Documents
Solving recurrences
Time Complexity Analysis of Merge Sort
0 if n 1
T( n ) 2 T (n / 2 ) n otherwise
sorting both halves merging
Q. How to prove
that the run-time of merge sort is O(n log n)?
A.
2
Time Complexity Analysis of Merge Sort
0 if n 1
T( n ) 2 T (n / 2 ) n otherwise
sorting both halves merging
Q. How to prove
that the run-time of merge sort is O(n log n)?
A. We have seen several methods:
Recursion tree
Substitution (by induction)
3
Proof by Recursion Tree
0 if n 1
T( n ) 2 T (n / 2 ) n otherwise
sorting both halves merging
#nodes(merge time):
T(n) n
...
T(2 T(2 T(2 T(2) T(2 T(2 T(2 T(2 n/2 (2)
) ) ) ) ) ) )
T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1)
assume n is a power of 2
0 if n 1
T( n ) 2 T (n / 2 ) n otherwise
sorting both halves merging
5
Proof by Induction/Substitution (when n is power of 2)
assume n is a power of 2
0 if n 1
T( n ) 2 T (n / 2 ) n otherwise
sorting both halves merging
6
Proof by Induction/Substitution (when n is power of 2)
assume n is a power of 2
0 if n 1
T( n ) 2 T (n / 2 ) n otherwise
sorting both halves merging
T (2 n ) 2T (n ) 2 n
2 n log 2 n 2 n Induction hypothesis
2 n log 2 (2 n )
7
Proof by Induction/Substitution (when n is power of 2)
assume n is a power of 2
0 if n 1
T( n ) 2 T (n / 2 ) n otherwise
sorting both halves merging
T (2 n ) 2 T (n ) 2 n
log 2 ( 2 n ) 1 log n
2 n log 2 n 2n
2 n log 2 (2 n ) 1 2 n
2 n log 2 (2 n )
8
Proof by Induction/Substitution (with rounding)
Claim. If T(n) satisfies the following recurrence, then T(n) n log n.
log2n
0 if n 1
T( n ) T n / 2 T n / 2 n otherwise
merging
solve left half solve right half
9
Proof by Induction/Substitution (with rounding)
Claim. If T(n) satisfies the following recurrence, then T(n) n log n.
log2n
0 if n 1
T( n ) T n / 2 T n / 2 n otherwise
merging
solve left half solve right half
Pf. (continued)
Step:
T (n) T ( n 1 ) T (n 2 ) n
n 1 log n 1 n 2 log n 2 n Induction hypothesis
n log n
10
Proof by Induction/Substitution (with rounding)
Claim. If T(n) satisfies the following recurrence, then T(n) n log n.
log2n
0 if n 1
T( n ) T n / 2 T n / 2 n otherwise
merging
solve left half solve right half
Pf. (continued)
Step:
T (n) T ( n 1 ) T (n 2 ) n
n 1 log n 1 n 2 log n 2 n
n 1 log n 1 n 2 log n 1 n
n log n 1 n
n log n
11
Proof by Induction/Substitution (with rounding)
Claim. If T(n) satisfies the following recurrence, then T(n) n log n.
log2n
0 if n 1
T( n ) T n / 2 T n / 2 n otherwise
merging
solve left half solve right half
Pf. (continued)
Step:
n1 n / 2
T (n) T ( n 1 ) T (n 2 ) n 2 log n
/2
n 1 log n 1 n 2 log n 2 n
log n
2 /2
log n 1 log n 1
n 1 log n 1 n 2 log n 1 n
log n 1 log n 1
n log n 1 n
n ( log n 1 ) n Because right side is an integer,
n log n rounding to nearest integer is OK.
12
Time Complexity Analysis of Merge Sort
0 if n 1
T( n ) 2 T (n / 2 ) n otherwise
sorting both halves merging
Q. How to prove
that the run-time of merge sort is O(n log n)?
A. We have seen several methods:
Recursion tree
Substitution (by induction)
13
Time Complexity Analysis of Merge Sort
0 if n 1
T( n ) 2 T (n / 2 ) n otherwise
sorting both halves merging
Q. How to prove
that the run-time of merge sort is O(n log n)?
A. We have seen several methods:
Recursion tree
Substitution (by induction)
14
General Recursion Tree
(1) if n 1
for a 1, b 1, T( n ) aT (n / b) f ( n ) otherwise
1 4 2 43 123
recursive calls combining
15
General Recursion Tree
(1) if n 1
for a 1, b 1, T( n ) aT (n / b) f ( n ) otherwise
1 4 2 43 123
recursive calls combining
#nodes(time):
T(n) f(n)
a
16
General Recursion Tree
(1) if n 1
for a 1, b 1, T( n ) aT (n / b) f ( n ) otherwise
1 4 2 43 123
recursive calls combining
#nodescombining:
T(n) f(n)
a
-1+logbn
T(n/b2 T(n/b2 T(n/b2 T(n/b2 a2
) ) ) ) f(n/b2)
...
T(n / bk) ak f(n /
bk )
...
T(1) T(1) T(1) T(1) T(1) T(1 T(1) T(1) a
log b n
f(1)
)
1 log b n
n
n operations in the leaves
log b n log b a
a n
log b a k
a f k
k 0 b
Master Method
(1) if n 1
for a 1, b 1, T( n ) aT (n / b) f ( n ) otherwise
1 4 2 43 123
recursive calls combining
1 log b n
n
So, T ( n ) n log b a
k
a f k where
k 0 b
18
Master Method
19
Summary Master Method
20
Examples
T ( n ) 2T ( n / 2 ) ( n )
a 2 , b 2; n n
log b a log 2 2
n
Q. What is dominant? (leaves, equal, root node)
21
Examples
T (n) n log b a
log n
22
Examples
Binary-search(A, p, r, s):
q(p+r)/2
if A[q]=s then return q
else if A[q]>s then
Binary-search(A, p, q-1, s)
else Binary-search(A, q+1, r, s)
23
Examples
Binary-search(A, p, r, s):
q(p+r)/2
if A[q]=s then return q
else if A[q]>s then
Binary-search(A, p, q-1, s)
else Binary-search(A, q+1, r, s)
24
Examples
A. Analysis:
a 9 , b 3; ...
log 3 9
n
f (n) (n)
25
Examples
A. Analysis:
a 9 , b 3; n
log 3 9 2
n
f (n) (n)
T (n) n log b a
n 2
26
Examples
A. Analysis:
a 3, b 4 ; n
log 4 3 0 . 793
n
f ( n ) ( n log n )
27
Examples
T ( n ) f ( n ) n log n
28
Examples
A. Analysis:
a 2 , b 2; n
log 2 2
n
f ( n ) ( n log n )
29
Examples
30
Examples
A. Analysis:
a 4 , b 2; ...
log 2 4
n
f (n) (n )
3
31
Examples
A. Analysis:
a 4 , b 2; n
log 2 4 2
n
f (n) (n )
3
T (n) f (n) n 3
32