This action might not be possible to undo. Are you sure you want to continue?
https://www.scribd.com/doc/234882328/Ds102Ex1Sol
07/23/2014
text
original
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 c·n
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 c·nlogn 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 c1·f(n) for n > n
01
.
2)  c2 > 0 and n
02
> 0 so that e(n) s c2·g(n) for n > n
02
.
From 1 and 2 we get
d(n)+e(n) s c1·f(n) +c2·g(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 c1·f(n) for n > n
01
.
2)  c2 > 0 and n
02
> 0 so that e(n) s c2·g(n) for n > n
02
.
From 1 and 2 we get d(n) ·e(n) s c1·f(n) ·c2·g(n) = (c1·c2) ·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 c·max(f(n),
g(n)) for n > n
0
.
h(n) s c·max(f(n), g(n)) s c·f(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 c·f(n) + g(n) s c·2·max{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, j1)
Solution:
T(n) = 2T(n1) + Θ(1).
By iteration method T(n) = Θ(2
n
)
This action might not be possible to undo. Are you sure you want to continue?