cmpe371 fall 2015 problems 17112015

advertisement
Alexander G. Chefranov, 26.11.2014, CMPE Dept., EMU, Famagusta, TRNC
Some material from
was used.
CMPE371 Fall 2014
Problems to be discussed
3.1, 3.2, 3.3, 3.4
3.2.
lg k n  cn
a. m  lg n
m k  c 2m
2m / k
m  c 2 ;1  c
m
m m
(
 1)
m
c(1 
 k k
)
2m / k
k
2
c
m
m
m / k
m m
(
 1)
m
 m
 m
k
k
c(1 

)
(
 1)
(
 1)
c
c

c

k
2
 
k k

k k
1
m
m k
2
k
2
m(
2(1 

c
)
k  1) k

k
n
n/2
2
2
n/2
d. 2   (2 ) ; m  2 ; m   (m) : m  cm; m  c
lg c
lg n
e. n  c
n
e
n
n
f. lg( n!)   (lg n ) ; n!  2n ( ) e n ,

1
1
 n 
12n  1
12n
n
lg( n!)  lg( 2n ( ) n e n )  0.5  lg   lg n  n lg n  n lg e   n lg e   (n lg n)
e
a.
b.
a.1.
22
n1
 (22 ) ; 22
n
n1
 222  (22 )2  (22 )
n
n
n
a.2. 22  ((n  1)!) ;Prove by mathematical induction: n  1;22  4  (1  1)! 2 ;
n
22
n1
1
 222  (22 )2  ((n  1)!)2  (n  1)!(n  1)! (n  1)!(n  1)n  (n  1)!(n  2)  (n  2)!, n  2
n
n
(n  1)n  n  2 ; n 2  2 ; n  2
a.3
4.1, f and g; 4.3, d,e,h,i,j recursion and master method, solve both of them by substitution method also
f.
f.1.Use recursion tree
N0.5
0.5
(N/4)
0.5
0.5
0.5
(N/4)
T(1)
(N/4)
(N/4)
(N/4)
0.5
T(1)
0.5
(N/4)
T(1)
T(1)
On the i-th level, there are 2i nodes. The bottom level has number log 4 n . Each bottom level node has
log n
log 2
0.5
complexity T(1)=1, hence the bottom level complexity is 2 4  n 4  n . Sum of complexities of
the tree nodes is
T (n) 
log4 n 1

i 0
log4 n 1
n
2i ( i )0.5 n0.5  n0.5 ( 1  1)  n0.5 (log 4 n  1)
4
i 0
(1)
f.2. Use substitution method to prove (1)
1, n  1
T (n)   0.5
 n (log 4 n  1), n  1
0.5
Let T (n / 4)  (n / 4) (log 4 (n / 4)  1) . Then
T (n)  2(n / 4)0.5 (log 4 (n / 4)  1)  n0.5  n0.5 (log 4 n  1  1)  n0.5  n0.5 (log 4 n  1) , qed
f.3. Use master method: a=2, b=4, f(n)=n0.5=  (n
T (n)   (n0.5 lg n)
0.5
Prove that (1) is O(n lg n) :
n0.5 (log 4 n  1)  cn0.5 lg n
Hence
logb a
)   (n0.5 ) . So case 2 takes place, and
log 4 n
1
lg n
1
1
1



 
c
lg n
lg n lg 4 lg n lg n 2 lg n
Hence, 1  c, if n  n0  4
(2)
0.5
Now, prove that (1) is (n lg n)
n0.5 (log 4 n  1)  n0.5 log 4 n  0.5n0.5 lg n  cn0.5 lg n
Hence, 0.5  c, n  n0  1
(2), (3) prove that (1) is  (n
(3)
0.5
lg n) .
2
g) T (n)  T (n  2)  n
g.1. Use recursion tree:
T (n)  T (n  2)  n 2  T (n  4)  (n  2)2  n 2  T (n  6)  (n  4)2  (n  2)2  n 2 
k
T (1)  32  ..  (n  4) 2  (n  2) 2  n 2  1  32  ..  (n  4)2  (n  2) 2  n 2   (1  2i ) 2 , n  2k  1
i 0
g.2. Substitution method.
T (n)   (n3 )
Show first that
k
T (n)  O(n3 ) : T (n)   (1  2i ) 2  cn3
i 0
k
k
i 0
i 0
T (n)   (1  2i)2   n2 
n 2 n3
1
n   cn3 , c 
2
2
2
k
T (n)  (n3 ) : T (n)   (1  2i ) 2  cn3 ; n=1: T (1)  1  c
i 0
k 1
1
2
1
T (n  2)   (1  2i ) 2  cn3  1  2(k  1) 2  cn3  (n  1) 2  cn3  n 2  2n  1  c(n3  n 2  n  ) 
c
c
c
i 0
1
c(n3  6n 2  12n  8)  c(n  2)3 , c 
24
D.
T (n)  3T (n / 3  2)  n / 2
d.1. Recursion tree
N/2
n/6-1
n/18-4/3
T(1)
0
1
2
3
4
n/18-4/3
n/6-1
n/18-4/3
T(1)
n
n/3-2
n/9-8/3
n/27-26/9
n/81-80/27
n/6-1
n/18-4/3
T(1)
n/3-2
n/9-8/3
n/27-26/9
n/81-80/27
n/243-242/81
n/2
n/6-1
n/18-4/3
n/54-13/9
n/162-40/27
T(1)
n/3i-(3i-1)/
3i-1
i
n/3i+1-(3i+1-1)/ 3i
n/(2*3i)-(3i1)/ (2*3i-1)
m 1
3i  1
1
m 1
m 1 3
T (n)  n / 2  3(n / 6  1)  9(n / 36  4 / 3)  ..  3 (n /( 2 3 ) 
)  ..  3 (n / 3  m  2 )  3m
i 1
23
3
m
n  3 n 3 1
m  log 3
; ( m  m 1  1, n  3(3m  1)  3m ,4  3m  n  3; m  log 3 (n  3)  log 3 4)
4
3
3
i
i
3i  1
3m 1  1
m 1
m 1
)

..

3
(
n
/(
2

3
)

)  3m 
2  3i 1
2  3m  2
m 1
n
3i  1
n  3 m 1 n 3i  1 n  3 (3n  1)
n  3 1 m 1 i
i
3
(

)


(

)


m

 3 


2  3i 2  3i 1
4
23
4
6
4
6 i 0
i 0
i 0 2
T (n)  n / 2  3(n / 6  1)  9(n / 36  4 / 3)  ..  3i (n /( 2 3i ) 
(3n  1)
n  3 1 1  3m (3n  1)
n  3 1 n 1
m


(log 3 (n  3)  log 3 4) 

  (n lg n)
6
4
6 1 3
6
4
12 4
d.2. Substitution method
T (n)  3T (n / 3  2)  n / 2  cn lg n  dn, c  1
T (3n)  3T (n  2)  3n / 2  3c(n  2) lg( n  2)  d 3n  3n / 2  c(3n) lg n  d 3n  c(3n) / 2  c(3n)(lg n  0.5) 
c(3n)(lg n  lg 3)  d 3n  c(3n) lg( 3n)  d 3n
T (n)  3T (n / 3  2)  n / 2  cn lg n  dn
T (3n)  3T (n  2)  3n / 2  3c(n  2) lg( n  2)  d (n  2)  3n / 2  3c(n  2) lg( n / 2)  3d (n  2)  3n / 2 
3c(n  2) lg n  3c(n  2)  3d (n  2)  3n / 2  3cn lg n  6c lg n  3cn  6c  3dn  6d  3n / 2 
3cn lg 3n  3cn lg 3  6c lg n  3cn  6c  3dn  6d  3n / 2  (c3n lg 3n  3dn)  (3cn lg 3
 6c lg n  3cn  6c  6d  3n / 2)  c3n lg 3n  3dn
if
 3cn lg 3  6c lg n  3cn  6c  6d  3n / 2  0
2c  n / 2  cn lg 3  2c lg n  cn  2d
1
2c lg n
1
1
n(  c lg 3  c 
)  n(  c lg 3  2c)  2(d  c), n  n0  4, d  c, c 
2
n
2
2(2  lg 3)
log a
e. T (n)  2T (n / 2)  n / lg n, a  2, b  2, f (n)  n / lg n, log b a  1, n b  n
f (n)  O(nlogb a ) but not f (n)  O(nlogb a  ),   0 as it is required by Master method (MM), case 1.
Hence, MM is not applicable.
e.1 Recursion tree. T (n) 
logb a 1

2i
i 0
lg n 1
n
2i lg(
n
)
2i
 2lg n  n 
i 0
lg n
1
1
 n  n(  1)   (n lg lg n)
lg n  i
i 1 i
e.2. Substitution method. Prove that T (n)   (n lg lg n) . Let
T (n)  cn lg lg n
T (2n)  2T (n)  2n / lg 2n  2cn lg lg n  2n /(lg n  1)  2cn(lg lg n  1 /(lg n  1)), c  1
lg n 1
1
1
1
2cn(lg lg n  1 /(lg n  1))  2cn( 
)  2cn   c(2n) lg(lg n  1)  c(2n) lg lg( 2n)
lg n  1
i 1 i
i 1 i
lg n
T (n)  cn lg lg n
T (2n)  2T (n)  2n / lg 2n  2cn lg lg n  2n /(lg n  1)  2cn(lg lg n  1 /(lg n  1)), c  1
lg n 1
1
1
1
2cn(lg lg n  1 /(lg n  1))  2cn( 
)  2cn   c(2n) lg(lg n  1)  c(2n) lg lg( 2n)
lg n  1
i 1 i
i 1 i
lg n
h. T (n)  T (n  1)  lg n
h.1. MM not applicable
lg n
2
h.2. Recursion tree method. T (n)  lg n  lg( n  1)  ..  1   i  (lg n)
i 1
2
h.3. Substitution method. T (n)   (lg n)
T (n)  c lg 2 n
T (n  1)  T (n)  lg( n  1)  c lg 2 n  lg( n  1)  c(lg 2 n  lg( n  1)), c  1
lg n
c(lg n  lg( n  1))  c( i  lg( n  1))  c
2
i 1
lg(n 1)
 i  c lg
i 1
T (n)  c lg 2 n
T (n  1)  T (n)  lg( n  1)  c lg 2 n  lg( n  1)
2
(n  1)
c lg 2 n  lg( n  1)  c(lg 2 n  lg( n  1)), c  1
lg n
c(lg n  lg( n  1))  c( i  lg( n  1))  c lg 2 (n  1)
2
i 1
i. T (n)  T (n  2)  1 / lg n
n/2
1
1
1 n2
1
1

 ..   

  (lg lg n)
lg n lg( n  2)
1 i 0 lg( n  2i) i 1 lg 2i
i.1. Recursion tree. T (n) 
i.2. Substitution method. T (n)   (lg lg n)
T (n)  c lg lg n
1
1
1
 c lg lg( n  2) 
 c(lg lg( n  2) 
), c  1
lg n
lg n
lg n
T (n)  T (n  2) 
c(lg lg( n  2) 
n / 2 1
n/2
1
1
1
1
)  c( 

) c
 c lg lg n
lg n
lg n
i 1 lg 2i
i 1 lg 2i
T (n)  c lg lg n
1
1
1
 c lg lg( n  2) 
 c(lg lg( n  2) 
), c  1
lg n
lg n
lg n
T (n)  T (n  2) 
c(lg lg( n  2) 
n / 2 1
n/2
1
1
1
1
)  c( 

) c
 c lg lg n
lg n
lg n
i 1 lg 2i
i 1 lg 2i
j. T (n)  nT ( n )  n
j.1. Recursion method. Let n  2
m1
2m
m1
T ( 22 )  22 T ( 22 )  22
m
m
There will be a tree of the height m with (m+1) levels. In the i-th level, i=1,m, number of
nodes is ni 
i
2
2 m j
(it is equal to 1, if i=0), a node on the i-th level has the weight of
j 1
mi
wi  22 ,i=0,m-1; let T(2)=2.
m 1
i
m
m
i
T (22 )   ni wi   22
m
i 0
m
2
i 0
2 m i
1 2 i
 2 m i
1 2
m j
22
i  0 j 1
m
 22
m i
( 2 i 1)  2 mi
i 0
m
 2 m  j  2 m i
m
 2 j 1
i 0
m
 22
i 0
j.2. Substitution method. T (22 )   (22 m)
m
m i
m
 2 m i  2 m i
 2 j  2 m i
m
  2 j  m i
i 0
m
 22  22
i 0
m
m
 2
2 m i
i 1
 2 j  2 m i
j 0
i 0
m
m
1  2
i 0
2m
(m  1)

T (22 )  c22 m
m
m
m 1
m
T ( 22 )  22 T ( 22 )  22
m 1
c 22 m  22
m 1
m
m 1
 c 22 22 m  22
m
m
m 1
 c( 22 ) 2 m  22
m 1
 c 2 2 2 m  2 2
m
m 1

m 1
 c( 22 ) 2 m  22
m 1
 c 2 2 2 m  2 2
m 1

m
m 1
 c 22 (m  1), c  1
T (22 )  c22 m
m
m
m 1
m
T ( 22 )  22 T ( 22 )  22
m 1
c 22 m  22
m 1
m
m 1
m 1
 c 22 22 m  22
m
 c 22 (m  1), c  1
m
m
m
Download