Algorithm Complexity I
Algorithm Complexity I
Exercise
Exercise
Exercise
Note that the first step is important to get the result, this is straight forward
from the following fact:
jnk n 1
2k
2
jn
n
lg
lg
2
4
By multiplying the
positive
increasing functions:
jnk jnk
n1
n
lg
lg
2
2
2
4
The term needed is obtained.
Exercise
T (n) = 4T (n/2) + n
Assuming T (n) cn2
n 2
+n
T (n) 4c
2
= cn2 + n cn2
There is no n > 0 such that the last inequality remains true. Then, it is possible
to use a tightly lower bound like T (n) cn2 bn
n 2
n
b +n
T (n) 4c
2
2
b
2
= cn (n n) cn2
2
b
The last holds for n n 0. Then b 2 and n > 0.
2
Exercise
PP
PP
?
)
q
P
b n2 c
b n2 c
b n2 c
lg n
@
?
R
@
@
?
R
@
- 3b n c
2
@
?@
R
..
.
..
.
..
.
..
.
..
.
..
.
..
.
..
.
jnk
j n k
+ 32 2 + . . . + 3lg n lg n + (nlg 3 )
2 2
lg n 2
2
3
3
3
n+ n+
n + ... +
n + (nlg 3 )
2
2
2
lg n i
X
3
=n
2
i=0
T (n) = n + 3
=n
jnk
1 (3/2)lg n
= 2nlg 3 2n = O(nlg 3 )
1 3/2
2
- 32 b n2 c
2
..
.
- (nlg 3 )
Exercise
n/a
?
T (a) + c(n 2a)
?
T (a) + c(n 3a)
..
.
T (1)
n/a
n/a
X
X
n
= T (a) + c
na
i
a
i=0
i=0
n n
n
n
a ( a + 1)
= T (a) + c n a
a
a
2
n
cn2
cn
= T (a) +
a
2a
2
2
Hence, T (n) = O(n )
Exercise
T (n) = 4T (n/2) + n2 lg n
Checking the cases for the master theorem:
1st case: n2 lg n
/ O(nlg2 4 ) for > 0
2nd case: n2 lg n
/ O(nlg2 4 ) for > 0
?
lg
42 2n2 2
n
22
?
43 2n3 2 lg
..
.
n
23
T (1)
Adding:
lg n
X
n2 n
T (n) =
4i i lg i
2
2
i=0
lg n
X
n2
(lg n lg 2i )
2i
2
i=0
lg n
X
lg n(lg n + 1)
n2 2
n2
2
2
2
=n
(lg n i) = n lg n
=
lg n
lg n
2
2
2
i=0
=
22i
Exercise
T (n) = 2T (n/4) + 1
1st case: 1 O(nlog4 2 ) , for > 0
Then, T (n) = (n0.5 )
T (n) = 2T(n/4) + n
1st case: n
/ O(nlog4 2 ) , for > 0
nd
2 case: n O(nlog4 2 )
n
cn
4
n
cn
2
Hence, T (n) = (n) for
2
1
2
c < 1 and n 0.
T (n) = 2T (n/4) + n2
1st case: n2
/ O(nlog4 2 )
2nd case: n2
/ O(nlog4 2 )
3rd case: n2 O(nlog4 2+ ), for = 1.5
2f (n/4) cn2
n 2
cn2
2
4
n2
cn2
8
Hence, T (n) = (n2 ) for 18 c < 1 and n 0