Algorithms and Data Structures: Simonas Šaltenis
Algorithms and Data Structures: Simonas Šaltenis
+O >
solving_trivial_problem if 1
( )
num_pieces ( / subproblem_size_factor) dividing combining if 1
n
T n
T n n
=
+ + >
+ >
( )
( ) ( )
2
2
3
lg
( ) 2 / 2 substitute
2 2 / 4 / 2 expand
2 ( / 4) 2 substitute
2 (2 ( / 8) / 4) 2 expand
2 ( / 8) 3 observe the pattern
( ) 2 ( / 2 )
2 ( / ) lg lg
i i
n
T n T n n
T n n n
T n n
T n n n
T n n
T n T n in
T n n n n n n n
= +
= + +
= +
= + +
= +
= +
= + = +
September 25, 2002 6
Repeated Substitution Method
The procedure is straightforward:
Substitute
Expand
Substitute
Expand
Observe a pattern and write how your expression
looks after the i-th substitution
Find out what the value of i (e.g., lgn) should be to
get the base case of the recurrence (say T(1))
Insert the value of T(1) and the expression of i into
your expression
September 25, 2002 7
Repeated Substitution (Ex. 2)
Lets find a more exact running time of merge
sort (lets assume that n=2
b
, for some b).
2 if 1
( )
2 ( / 2) 2 3 if 1
n
T n
T n n n
=
+ + >
( )
( ) ( )
( )
( ) ( )
( ) ( )
2
2
3 3 2 1 0
( ) 2 / 2 2 3 substitute
2 2 / 4 3 2 3 expand
2 / 4 4 2 3 3 substitute
2 2 / 8 / 2 3 4 2 3 3 expand
2 / 2 2 3 2 +2 +2 3 observe the pattern
( ) 2 ( / 2 ) 2 3 2
i i j
j
T n T n n
T n n n
T n n
T n n n
T n n
T n T n in
=
= + +
= + + + +
= + + +
= + + + + +
= + +
= + +
1
0
lg lg
2 ( / ) 2 lg 3(2 1) 5 2 lg 3
i
n n
T n n n n n n n
= + + = +
+ >
( )
( ) ( )
( )
( ) ( )
( ) ( )
2
2
3 2 1 0
1
0
1
1
( ) 4 1 1 substitute
4 4 2 1 1 expand
4 2 4 1 substitute
4 4 3 1 4 1 expand
4 3 4 +4 +4 observe the pattern
( ) 4 ( ) 4
4 1 4 1
4 (1)
4 1 3
i
i j
j
n n
n
T n T n
T n
T n
T n
T n
T n T n i
T
= +
= + +
= + +
= + + +
= +
= +
= + =
=
= +
= + +
= + + +
+ +
= + O
log
b
a
n
September 25, 2002 18
MM Intuition
Three common cases:
Running time dominated by cost at leaves
Running time evenly distributed throughout
the tree
Running time dominated by cost at the root
Consequently, to solve the recurrence, we
need only to characterize the dominant
term
In each case compare with
( ) f n
log
( )
b
a
O n
September 25, 2002 19
MM Case 1
for some constant
f(n) grows polynomially (by factor ) slower
than
The work at the leaf level dominates
Summation of recursion-tree levels
Cost of all the leaves
Thus, the overall cost
log
( ) ( )
b
a
f n O n
c
=
log
b
a
n
0 c >
n
c
log
( )
b
a
O n
log
( )
b
a
n O
log
( )
b
a
n O
September 25, 2002 20
MM Case 2
and are asymptotically the same
The work is distributed equally
throughout the tree
(level cost) (number of levels)
log
( ) ( )
b
a
f n n =O
( ) f n
log
( ) ( lg )
b
a
T n n n =O
log
b
a
n
September 25, 2002 21
MM Case 3
for some constant
Inverse of Case 1
f(n) grows polynomially faster than
Also need a regularity condition
The work at the root dominates
log
( ) ( )
b
a
f n n
c +
=O
log
b
a
n
0 0
1 and 0 such that ( / ) ( ) c n af n b cf n n n - < > s >
( ) ( ( )) T n f n = O
0 c >
September 25, 2002 22
Master Theorem Summarized
Given a recurrence of the form
The master method cannot solve every recurrence of this
form; there is a gap between cases 1 and 2, as well as
cases 2 and 3
( ) ( / ) ( ) T n aT n b f n = +
( )
( )
( )
( )
( )
( )
log
log
log
log
log
0
1. ( )
( )
2. ( )
( ) lg
3. ( ) and ( / ) ( ), for some 1,
( ) ( )
b
b
b
b
b
a
a
a
a
a
f n O n
T n n
f n n
T n n n
f n n af n b cf n c n n
T n f n
c
+c
=
= O
= O
= O
= O s < >
= O
September 25, 2002 23
Strategy
Extract a, b, and f(n) from a given recurrence
Determine
Compare f(n) and asymptotically
Determine appropriate MT case, and apply
Example merge sort
log
b
a
n
( )
( )
2
log log 2
log
( ) 2 ( / 2) ( )
2, 2; ( )
Also ( ) ( )
Case 2 ( ) lg lg :
b
b
a
a
T n T n n
a b n n n n
f n n
T n n n n n
= +O
= = = = = O
= O
= O = O
log
b
a
n
September 25, 2002 24
Examples of MM
( )
2
3
log 1
log 9
2
( ) ( / 2) 1
1, 2; 1
also ( ) 1, ( ) (1)
( ) (lg )
(
Case 2:
Cas
) 9 ( / 3)
9, 3;
( ) , ( ) ( ) with 1
( ) e 1:
T n T n
a b n
f n f n
T n n
T n T n n
a b
f n n f n O n
T n n
c
= +
= = =
= = O
= O
= +
= =
= = c =
= O
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)
September 25, 2002 25
Examples (2)
4
4
2
log 3 0.793
log 3
log 2 1
( ) 3 ( / 4) lg
3, 4;
( ) lg , ( ) ( ) with 0.2
Regularity condition
( / ) 3( / 4) l
Case
g( / 4) (3/ 4) lg ( ) for 3/ 4
( ) ( lg )
( ) 2 ( / 2) lg
2, 2;
3:
T n T n n n
a b n n
f n n n f n n
af n b n n n n cf n c
T n n n
T n T n n n
a b n n
f
+c
= +
= = =
= = O c ~
= s = =
= O
= +
= = =
1
1
( ) lg , ( ) ( ) with ?
also l
neither Case 3 nor Case 2!
g / lg
n n n f n n
n n n n
+c
= = O c
=
>
Solution:
2
log 3 1.585
( ) T n n n = =
September 25, 2002 29
Next Week
Sorting
QuickSort
HeapSort