Docsity

Homework 1 Solutions - Spring 2008 - Data Structures and Algorithms | CMPSC 465, Assignments of Computer Science

Toggle sidebar
bg1
CMPSC 465, Spring 2008, Homework 1, solutions
Problem 1.
Foreach program find a plausible nice function that it describes of growth of the running time. The table pro-
vides the running time in miliseconds for several input sizes:
input size
1000 2000 4000 8000
program
Program A 1248
Program B 15 20 25 30
Program C 10 40 160 650
Program D 15 80 400 1300
Program E 55 290 1550 8200
Example: TA(n)has order of growth n,the ratio TA(n)
nis between 1
1000 and 1
1000.
In some of the other cases, this ratio will not be exactly constant, more like“between 53 and 55”.
Solution.
AThe ratio TA(2n)/TA(n)is2for n=1000, 2000, 4000, which is consistent with TA(n)=cn,for c=0. 001;
this is Θ(n).
BThe ratio TB(2n)/TA(n)for n=1000, 2000, 4000 is 4/3, 5/4, 6/5 which is not consistent with a power func-
tion, but the difference is constant, as it is for logarithms. When log2nincreases by 1 and TB(n)increases
by 5, so we have TB(n)≈5log2A−35, this is Θ(log n).
CThe ratio TC(2n)/TC(n)for n=1000, 2000, 4000, is 4, 4, 4, 4.06. Ignoring the small discrepancyfor 4.06
rather then 4 we have TC(n)≈cn2for c=10−5,this is Θ(n2).
DOne can see that for n=1000, 2000, 4000 we have TD(n)=TB(n)TC(n)/10, which would suggest
Θ(n2log n). However, for 8000 the value is only 2/3 ofthat. One possibility is that 1300 was obtained by
multiplying 650 with 20 rather than 30 (human error), another,that the test was actually run on a faster
machine, and the third possibility is that the program uses some optimizations for larger inputs. As a
result, it is not possible to coverall four values with a single rule.
EThe ratio TA(2n)/TA(n)is5.3 for n=1000, 2000, 4000, which is consistent with TA(n)=cn2.4;this is
Θ(n2.4).
pf2

Partial preview of the text

Download Homework 1 Solutions - Spring 2008 - Data Structures and Algorithms | CMPSC 465 and more Assignments Computer Science in PDF only on Docsity!

CMPSC 465, Spring 2008, Homework 1, solutions

Problem 1.

For each program find a plausible nice function that it describes of growth of the running time. The table pro- vides the running time in miliseconds for several input sizes:

input size 1000 2000 4000 8000

program

Program A 1 2 4 8 Program B 15 20 25 30 Program C 10 40 160 650 Program D 15 80 400 1300 Program E 55 290 1550 8200

Example: T (^) A ( n ) has order of growth n , the ratio

T (^) A ( n ) n

is between

and

In some of the other cases, this ratio will not be exactly constant, more like “between 53 and 55”.

Solution.

A The ratio T (^) A (2 n )/ T (^) A ( n ) is 2 for n = 1000, 2000, 4000, which is consistent with T (^) A ( n ) = cn , for c = 0. 001; this is Θ( n ).

B The ratio TB (2 n )/ T (^) A ( n ) for n = 1000, 2000, 4000 is 4/3, 5/4, 6/5 which is not consistent with a power func- tion, but the difference is constant, as it is for logarithms. When log 2 n increases by 1 and TB ( n ) increases by 5, so we have TB ( n ) ≈ 5 log 2 A − 35, this is Θ(log n ).

C The ratio TC (2 n )/ TC ( n ) for n = 1000, 2000, 4000, is 4, 4, 4, 4.06. Ignoring the small discrepancy for 4. rather then 4 we have TC ( n ) ≈ cn^2 for c = 10 −^5 , this is Θ( n^2 ).

D One can see that for n = 1000, 2000, 4000 we have TD ( n ) = TB ( n ) TC ( n )/10, which would suggest Θ( n^2 log n ). However, for 8000 the value is only 2/3 of that. One possibility is that 1300 was obtained by multiplying 650 with 20 rather than 30 (human error), another, that the test was actually run on a faster machine, and the third possibility is that the program uses some optimizations for larger inputs. As a result, it is not possible to cover all four values with a single rule.

E The ratio T (^) A (2 n )/ T (^) A ( n ) is 5.3 for n = 1000, 2000, 4000, which is consistent with T (^) A ( n ) = cn 2.4; this is Θ( n 2.4).

Problem 3.

a.

n →∞

lim

( n^2 + 1)^10 n^20

n →∞

lim

n^2 + 1 n^2

10 =   n →∞

lim

2 n 2 n

10 = 1, hence Θ( n^20 )

The first equality is by continuity of x^20 , the second, by d’Hospital.

b.

n →∞ lim

√ 10 n^2 + 7 n + 3

n

√ n →∞

lim

10 n^2 + 7 n + 3 n^2

= √ 10 hence Θ( n )

The first equality is by continuity of (^) √ x , the second, by d’Hospital used twice.

c.

n →∞

lim

2 n log( n + 2)^2 + ( n + 2)^2 log

n 2 n^2 log n

n →∞

lim

4 n log n + 4 n (log

n + 1 n

) + ( n^2 + 4 n + 4)(log n − log 2)

n^2 log n

hence Θ( n^2 log n ). The equality comes from finiding the largest (fastest growing) term in the numerator, which is the same as the denominator.

d.

n →∞

lim

2 n +^1 + 3 n −^1 3 n^

n →∞

lim  

n = 3 hence Θ(3 n )

e. 1 = n →∞

lim

log 2 − 1 log 2 n

n →∞

lim

 log 2  log 2 n

n →∞

lim

log 2 log 2 n

= 1 hence Θ(log n )

Problem 5. 5 log( n + 100)^10 = Θ(log n ), ln^2 n , (^3) √ n , 0. 001 n^4 + 3 n^3 _1, 3 n , 22 n^ = 4 n , ( n − 2)!

Problem 7.

a. True, if t ( n ) ∈ O ( g ( n )) then for some c > 0, for almost all n we have t ( n ) ≤ cg ( n ), hence for c −^1 > 0 we have g ( n ) ≥ c −^1 t ( n ), and this means that t ( n ) ∈Ω( g ( n )).

b. True. α g ( n ) and g ( n ) fit the definitions of O and Ω with multiplicative constant α −^1.

c. True, this is our definition of Θ.

d. False: let t ( n ) be n for even numbers and 1 for odd, while g ( n ) is 1 for even numbers and n for odd. Then because of even numbers t ( n ) is not in O ( g ( n ), while because of odd numbers, t ( n ) is not in Ω( g ( n ).