Docsity

Midterm Exam 1 Review Problems - Algorithms and Abstract Data Types | CMPS 101, Study notes of Computer Science

Toggle sidebar
bg1
CMPS 101
Midterm 1 Review Problems
1. Let
)(nf
and
)(ng
be asymptotically non-negative functions which are defined on the positive
integers.
a. State the definition of
))(()( ngOnf
.
b. State the definition of
))(()( ngnf
2. State whether the following assertions are true or false. If any statements are false, give a related
statement which is true.
a.
))(()( ngOnf
implies
))(()( ngonf
.
b.
))(()( ngOnf
if and only if
))(()( nfng
.
c.
))(()( ngnf
if and only if
Lngnf
n
))(/)((lim
, where
L0
.
3. Prove that
. In other words, if
))(()(
1
nfnh
and
))(()(
2
ngnh
, then
))()(()()(
21
ngnfnhnh
.
4. Use limits to prove the following (these are some of the exercises at the end of the asymptotic growth
rates handout):
a. If
)(nP
is a polynomial of degree
0k
, then
)()(
k
nnP
.
b. For any positive real numbers
and
:
)(
non
iff
,
)(
nn
iff
, and
)(
nn
iff
.
c. For any positive real numbers a and b:
)(
nn
boa
iff
ba
,
)(
nn
ba
iff
ba
, and
)(
nn
ba
iff
ba
.
d.
))(())(()( nfnfonf
.
5. Use Stirling’s formula:
)/1(12! n
e
n
nn
n
, to prove that
)log()!log( nnn
.
6. Use Stirling’s formula to prove that
n
n
n
n
4
2
.
7. Consider the following sketch of an algorithm called ProcessArray which performs some unspecified
operation on a subarray
][ rpA
.
ProcessArray(A, p, r) (Preconditions:
1p
and
][ Alengthr
)
1. do something which takes constant time.
2. if
rp
3.
2
rp
q
4. ProcessArray(A, p, q)
5. ProcessArray(A, q+1, r)
Write a recurrence which gives the running time
)(nT
of this algorithm, when called on the full array
]1[ nA
. Give a tight asymptotic solution to this recurrence.
8. Consider the following algorithm which does nothing but waste time:
pf2

Partial preview of the text

Download Midterm Exam 1 Review Problems - Algorithms and Abstract Data Types | CMPS 101 and more Study notes Computer Science in PDF only on Docsity!

CMPS 101

Midterm 1 Review Problems

1. Let f^ ( n ) and g^ ( n ) be asymptotically non-negative functions which are defined on the positive

integers.

a. State the definition of f^ (^ n ) O^ ( g ( n )).

b. State the definition of f (^ n )^ ( g ( n ))

2. State whether the following assertions are true or false. If any statements are false, give a related

statement which is true.

a. f^ (^ n ) O^ ( g ( n )) implies f^ (^ n ) o^ ( g ( n )).

b. f^ (^ n ) O^ ( g ( n )) if and only if g^ (^ n )^ ( f ( n )).

c. f^ (^ n )^ ( g ( n )) if and only if

f n g n L

n

 

lim ( ( )/ ( ))

, where 0 ^ L .

3. Prove that (^ f^ ( n ))( g ( n ))( f ( n ) g ( n )). In other words, if h 1^ ( n )^ ( f ( n )) and

2

h n  gn , then ( ) ( ) ( ( ) ( ))

1 2

h n  h n  f n  g n.

4. Use limits to prove the following (these are some of the exercises at the end of the asymptotic growth

rates handout):

a. If P^ ( n ) is a polynomial of degree k^ ^0 , then (^ ) ( )

k

P n  n.

b. For any positive real numbers^ ^ and^ ^ : ( )

 

n  o n iff^ ^ ^ , ( )

 

n  n iff^ ^ ^ ^ , and

 

n  n iff ^ ^ .

c. For any positive real numbers a and b : (^ )

n n

a  o b iff a  b , ( )

n n

a  b iff a  b , and ( )

n n a   b

iff a  b.

d. f^ (^ n )^ o ( f ( n ))( f ( n )).

5. Use Stirling’s formula:! 2  1 ( 1 / n )

e

n n n

n

  ^  

  

   , to prove that log(^ n!^ )^ ( n log n ).

6. Use Stirling’s formula to prove that

n n

n

n (^2 )

7. Consider the following sketch of an algorithm called ProcessArray which performs some unspecified

operation on a subarray A [^^ p  r^ ].

ProcessArray(A, p, r) (Preconditions: p^ ^1 and r^  length^ [^ A ] )

1. do something which takes constant time.

2. if

p  r

p r q

4. ProcessArray(A, p, q)

5. ProcessArray(A, q+1, r)

Write a recurrence which gives the running time T^ ( n ) of this algorithm, when called on the full array

A [ 1  n ]. Give a tight asymptotic solution to this recurrence.

8. Consider the following algorithm which does nothing but waste time:

WasteTime(n) (pre: n  1 )

1. if n  1

2. for i^ ^1 to

3 n

3. waste a constant amount of time

4. for i^ ^1 to 7

  1. WasteTime ^  n /^2 

6. waste a constant amount of time

Write a recurrence which gives the running time T^ ( n ) of this algorithm. Give a tight asymptotic

solution to this recurrence.

9. Use the Master Theorem to find tight asymptotic solutions for the following recurrences.

a. T^ ( n )^2 T ( n /^4 ) n

b.

2 T ( n ) 7 T ( n / 3 ) n

  1. Define T^ ( n ) by: T^ (^1 )^0 and T^ (^ n ) T^ ^  n /^2 ^ ^1 for n  2. Use the iteration method to show

T ( n )  lg( n ) for all n , whence T ( n ) (log( n )).

11. Define T^ ( n ) by the recurrence

    

 

 

T n n

T n 3 / 3

10

5 ( )

2

2

1

n

n

n

Show that there exists a c^ ^0 such that T^ ( n^ ) cn^ lg n for all n^ ^3. Prove this using induction on n

( not the Master Theorem.) (Note: this problem is a watered down substitution method, i.e. I'm giving

you n 0^ (namely 3), and you must determine c .)

12. Prove that all trees on n vertices have n  1 edges. Do this by (a) induction on the number of vertices,

and (b) by induction on the number of edges.