Anda di halaman 1dari 7

DS102 Assignment #1 Solutions

Problem 1:

a. (n+1)
5
=(n
5
).
b. 3nlogn+2n= (nlogn).
c.
1 2
(2 3 ) ( 9 )
n n n
n O n
+
+ =

d.
e. n!=((logn)
2
)

Solution:
a. (n+1)
5
=O(n
5
).
We need to show that (n+1)
5
s cn
5
for n > n
0
.
divide both sides by n
5
=>
c
n
s |
.
|

\
|
+
5
1
1
c= 32, n
0
=1
b. 3nlogn+2n=(nlogn).
We need to show that cnlogn s 3nlogn+2n for n > n
0

divide both sides by nlogn =>
c s 3+2/logn
c = 3, n
0
=1





Problem 2:
Prove or give a counter example for the following asymptotic notation properties:
a. If d(n)=O(f(n)), then ad(n)=O(f(n)), for any constant a>0.
b. If d(n)=O(f(n)) and e(n)=O(g(n)), then d(n)+e(n) = O(f(n)+g(n)).
c. If d(n)=O(f(n)) and e(n)=O(g(n)), then the product d(n)e(n) = O(f(n)g(n)).
d. O(max{f(n), g(n)})=O(f(n)+g(n)).
e. If f(n)=O(n) then 2
f(n)
=O(2
n
)
Solution:
a. If d(n)=O(f(n)), then - c > 0 and n
0
> 0 so that d(n) s cf(n) for n > n
0
.
a > 0 => ad(n) ) s ac f(n) for n > n
0
=>- (ac) > 0 and n
0
> 0 =>
ad(n) = O(f(n)).

b. From the data given it is follows than
1) - c1 > 0 and n
01
> 0 so that d(n) s c1f(n) for n > n
01
.
2) - c2 > 0 and n
02
> 0 so that e(n) s c2g(n) for n > n
02
.
From 1 and 2 we get
d(n)+e(n) s c1f(n) +c2g(n) s max(c1,c2)(f(n) +g(n)) for n > max{ n
01
,
n
02
}. => the sum d(n)+e(n) = O(f(n)+g(n)).

c. From the data given it is follows than
1) - c1 > 0 and n
01
> 0 so that d(n) s c1f(n) for n > n
01
.
2) - c2 > 0 and n
02
> 0 so that e(n) s c2g(n) for n > n
02
.
From 1 and 2 we get d(n) e(n) s c1f(n) c2g(n) = (c1c2) f(n) g(n) for
n > max{ n
01
, n
0
2}. => the product d(n)e(n) = O(f(n)g(n)).

d. We need to show that
1) O(max{f(n), g(n)}) _ O(f(n)+g(n)) and
2) O(f(n)+g(n)) _ O(max{f(n), g(n)}).
If h(n) = O(max{f(n), g(n)}) = > - c > 0 and n
0
> 0 so that h(n) s cmax(f(n),
g(n)) for n > n
0
.
h(n) s cmax(f(n), g(n)) s cf(n) + g(n) for n > n
0
=> h(n) = O(f(n)+g(n)).
We showed that O(max{f(n), g(n)}) _ O(f(n)+g(n)) .

If h(n) = O(f(n) + g(n)) = > - c > 0 and n
0
> 0 so that h(n) s c(f(n)+ g(n)) for
n > n
0
.
h(n) s cf(n) + g(n) s c2max{f(n), g(n)} for n > n
0
=> h(n) =
O(max{f(n),g(n)}).
We showed that O(f(n)+g(n)) _ O(max{f(n), g(n)}).

e. Counter example:
f(n) = 5n = O(n)
2
f(n)
= 2
5n
O(2
n
)


Problem 3:
Find the asymptotic running time of the following code sections. Explain your
answers.

a. k = 0;
for(int i = 1; i s n ; i++)
for(int j = 1; j n s ; j++)
k++


b. k = 0;
for(int i = 1; i s n ; i++)
for(int j = 1; j i s ; j++)
k++



The second way to solve this is:


c. k = 0;
for(int i = 1; i s n
2
; i++)
for(int j = 1; j si; j++)
k++


d. for(int i = n; i > 1 ; i--)
x = 1;
while(x<i)
x = x * 2;
while(x>2)
x = x ;


Problem 4

Solve the following recurrence. Assume that T(1) = O(1)
a.
3
) 2 / ( 2 ) ( n n T n T + =

b. n n T n T + = ) 1 ( ) (

c. 1 ) ( ) ( + = n T n T

d.
n
n
n T n T
log
) 2 / ( 2 ) ( + =
The Master theorem is no good in this case. Using the iteration method, we get

Since the harmonic series is bounded by ln(k):
. )) (ln(
1
1
k O
i
k
i
=

=

Substituting for k=log(n) we get
))) log(log( ( ))) ln(log( ( ) ( n n O n n n O n T = + = .

Note: we reduced 10 points for the answer of )) log( ( ) ( n n O n T = , since a better upper bound
can be found.

e. ) 1 ( ) 1 ( 2 ) ( O n T n T + =



Problem 5

We must first find an approximation of n. We begin with i=1 and execute:
while (A[i] is not 0) set ii*2
Now n<i<2n+1. We use binary search on the first i elements of A, correcting for the fact that
hitting 0 means we have to go back. This takes ( ) ( ) lg lg i n O = O time.


Problem 6
:
k g(n,k) f(n)
- k . g - n :

C n T n n T
T
+ + =
=
)) 1 ( 2 ( ) (
1 ) 0 (


C . ( C=2 : )

) ! ( ) 1 ( 4 !* 4 !
2
1
4 !* 4 !
)! (
1
! 4 !
!
)! (
1
! 4 !
)! (
!
4
1) n 1) - ...n(n /2! n! 2(n!
T(0) n! 2n! ) ! 2 )...( 1 ( 2
... 2) - 1)(n - 2n(n 1) - 2n(n 2n ...
1) 2(n 2) - 1)T(n - n(n 1) - 2n(n 2n
2 ) 2 ) 2 ( ) 1 ( ) 1 ( 2 ( 2
2 ) 1 ( 2 2 )) 1 ( 2 ( ) (
n
1 i
1 - n
0 i
n
1 i
n
1 i
n O O n n
n n
i
n n
n
i n
n n
i n
n
n n
n T n n n n
n nT n n T n n T
i
= + =
= + s + =
+

= +

s
= + + + +
+ + +
+ + + + = =
+ + + + =
+ + + + =
+ + = + + =


= =
= =

: O((n+1)!) .

Problem 7

Compute the runtime of the following function.
Assume that the top call to the function is MysteryPrint(A, 1, n), where n is the size of array A.

Function MysteryPrint(A, i, j )
Input: array A, indexes i, j

if i = j
print A[i]
else
print A[j]
MysteryPrint(A, i+1, j)
MysteryPrint(A, i, j-1)



Solution:
T(n) = 2T(n-1) + (1).
By iteration method T(n) = (2
n
)

Anda mungkin juga menyukai