Sie sind auf Seite 1von 5

UML CS Analysis of Algorithms 91.

404 Spring, 2001


Homework Set #1 & SOLUTIONS
Prepared by TA Wenhua (Michelle) Shi
Assigned: Friday, 2/2 Due: Friday, 2/9 (sar !" lecure)
This assignment covers textbook material in Chapters1-2.
Note: Partial credit for wrong answers is onl given if work is shown.
1. (10 points) Let R be a problem. Suppose that the worst-case asymptotic time complexity of
R is in O(n
2
) and also in (n lg n). Now, let A be an alorithm that sol!es R. "hich of the
followin statements about A are consistent with this information about the asymptotic time
complexity of R# (Note: The answer might include more than one of the choices.)
A has worst-case time complexity O(n
2
).
A has worst-case time complexity O(n
3/2
).
A has worst-case time complexity O(n).
A has worst-case time complexity ( n
2
).
A has worst-case time complexity ( n
3
).
Answer: Statements 1, $, % and & are consistent.
Note: 'or probem (, O(n
2
) can be tight or loose upper bound and (n lg n) can be
tight or loose lower bound. )n alorithm that sol!es ( can be so efficient that it achie!es
its complexity as the same as the problem ( but can not be smaller), or it can be so
inefficient that it is much more complex (say, exponential) than the problem.
2. (10 points) 'or (a) and (b), answer yes or no and briefly explain.
(a) *f * pro!e that an alorithm ta+es O(n
2
) worst-case time, is it possible that it ta+es O(n) time
on some inputs#
Answer: ,es. Note that O(n
2
) worst-case time is an upper bound and we ha!e not i!en a
lower bound limit. -ecause (n) is smaller than (n
2
), it is possible that an alorithm
satisfies both O(n
2
) worst-case time and O(n) time on some inputs.
(b) *f * pro!e that an alorithm ta+es (n) best-case time, is it possible that it ta+es (n lg n)
time on all inputs#
Answer: ,es. Note that (n) best-case time can be a loose lower bound. *f an alorithm
ta+es (n lg n) time on all inputs, it is absolutely riht that it is larer than (n).
3. ($0 points) (an+ the followin & functions by order of asymptotic rowth. .hat is, find an
arranement g1, g2, g3, g4, g5 of the functions satisfyin
g1 = ( g2 ), g2 = ( g3 ), ..., g4 = ( g5 ). /ustify your answer mathematically.


6n
4
5n
3
2
n
3n 6n

(lg
2
n) (1/4)
n
!!!log n 65,"""
Answer: (1/4)
n
# !!!log n 65,""" # 6n

(lg
2
n) # 6n
4
5n
3
# 2
n
3n
UML CS Analysis of Algorithms 91.404 Spring, 2001
/ustification0 let1s inore all coefficients and minor parts, since
(1/4)
n
= ( (1/4)
n
)
!!!log n 65,""" = ( lg n )
6n

(lg
2
n) = ( n

(lg
2
n) )
6n
4
5n
3
= ( n
4
)
2
n
3n = ( 2
n
) .
Let g1 = (1/4)
n
, g2 = lg n, n" = 2 an$ % =1,
.hen, 0 23 (14%)
n
23 l n, n53$.
Let g2 = lg n, g3 = n

(lg
2
n), n" = 2 an$ % =1,
.hen, 0 23 l n 23 n

(l
$
n), n53$ since 1 23 n

l n n53$.
Let g3 = n

(lg
2
n), g4 = n
4
, n" = 2 an$ % =1,
.hen, 0 23 n

(l
$
n) 23 n
4
, n53$ since (l
$
n) 23 n
6
, which is true n53$.
Let g4 = n
4
, g5 = 2
n
, n" = 2 an$ % =1,
.hen, 0 23 n
%
23 $
n
, n53$
%
since l (n
%
) 23 l ($
n
) % l n 23 n l $
% l n 23 n , which is true n53$
%
.
4. (10 points) 'ind the smallest (positi!e) inteer !alue for which 5"n
3
# 3
n

Answer: n310 is the smallest inteer !alue for which 5"n
3
# 3
n
.
Note that we ha!e 0 n &0n
6
6
n
7 68,%&0 17,896
10 &0,000 &7,0%7
5. ($& points) .extboo+, (1
st
edition) exercise 1.6-& on p.1&. :onsider the problem of searchin
for a !alue & in a se;uence that is already sorted. Since the se;uence is sorted, we can
compare with & the !alue at the midpoint of the se;uence and eliminate half of the
se;uence from further consideration. -inary search is an alorithm that repeats this
procedure, hal!in the si<e of the remainin portion of the se;uence each time. "rite
pseudocode for binary search. =xplain why your pseudocode is correct. Show that its
worst-case runnin time is in (l n).
UML CS Analysis of Algorithms 91.404 Spring, 2001
Answer:
-inarySearch(), !)
1. left 1
$. riht lenth>)?
6. while left 23 riht
%. do middle (left @ riht)4$
&. if ! 3 )>middle? then (eturn middle
8. else if ! 2 )>middle?
A. then riht middle B1 44reduces to left half of the array
9. else
7. left middle @1 44reduces to riht half of the array
10. (eturn N*L
:orrectness0 Since the pseudocode contains a while loop, we need to show that the loop
terminates and that when it terminates the correct answer is produced. .he !alues in the
array appear in sorted orderC this allows us to discard D the remainin possibilities each
time throuh the loop. *f & is somewhere in the array, then we will encounter it as the middle
element in some iteration. .his ta+es at most lg(n) iterations, because after throwin away
D the elements lg(n) times we ha!e only 1 element left to examine, and le't=right=(i$$le.
So, if & is in the array we return out of the loop and stop. *f & is not in the array, then after
lg(n) iterations we won1t find it in that one remainin element. *n this case, le't or right will
be adEusted so that le't 5 right. .his adEustment causes the while condition to fail so that
the pseudocode terminates and returns N*L (i.e. failure).
(unnin .ime0 "e arue that the worst-case runnin time of binary search is ( lg n ). .he
correctness arument abo!e shows that the loop executes at most lg(n) times. .his i!es
us an upper bound of O(lgn). .he case in which & is not in the array is a worst-case input
for binary search because in this case the loop is not exited early and lg(n) iterations are
re;uired. .his worst-case input pro!ides a worst-case lower bound of (lgn). .oether the
matchin lgn lower and upper bounds allow us to conclude that the worst-case runnin time
of binary search is ( lg n ).
UML CS Analysis of Algorithms 91.404 Spring, 2001
6. ($& points) .his problem refers to the )*ster*( ) pseudocode below0
Mysery(A, n)
"!r i # ! n
d! "!r $ # ! n
d! i" A%i&%$& e'uals #
hen prin ()( *ih!u ne*line
else prin ( ( *ih!u ne*line
prin ne*line
(a) Fi!en the followin inputs, what is the output of a call )*ster*(A, n) # *nputs0 n3& and $-
dimensional n+n array A is initiali<ed as follows (row elements are listed in increasin
column order)0
row 10 0, 0, 1, 0, 0 row $0 0, 1, 0, 1, 0 row 60 0, 1, 1, 1, 0 row %0 1, 0, 0, 0, 1 row &0 1, 0, 0, 0, 1
Answer:
G
G G
G G G
G G
G G
(b) Fi!e a function '(n) that is an upper bound on the worst-case runnin time of )*ster*.
/ustify your answer.
Answer: .he upper bound worst-cast runnin time is O(n
2
) since each element of array is
chec+ed at (ost once by two for loops that each iterate a maximum of n times.
(c) Fi!e an example of a worst-case input for array A for )*ster* when n3&.
Answer: )ll elements of array e;ual to 0. *ts runnin time is (n
2
). )ctually, every example
forces the runnin time to be proportional to n
2
, since there is no way to exit the for loops
early.
UML CS Analysis of Algorithms 91.404 Spring, 2001
(d) :an you conclude from your answers to (b) and (c) that )*ster*1s worst-case runnin
time is 3 ('(n)), "hy or why not#
Answer: ,es. 'rom (b), we et an upper bound on the worst-case runnin time O('(n)), and
from (d), we et an lower bound on the worst-case runnin time ('(n)). So, we can conclude
that the worst case runnin time is ('(n)).

Das könnte Ihnen auch gefallen