This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CSE 340 Algorithms — First Exam Wednesday, February 28, 9:10—10 AM
Packard Lat) 10] (Auditorturn) Please strive for clarity and completeness in your answers.
To enable us to award partial credit? show details of your work
(unless you are speciﬁcally instructed otherwise).
Try not to leave the answer to any problem (or subproblern) blank. The solutions shown are far more detailed than needed for full credit:
the extra detail is intended to help explain the solutions clearly.
In some cases). alternative solutions are equally valid. l. [Orders of Growth; 30 points]
Note: In this question, the foigll), lit). etc are all functions from the non
negative integers into the nonnegative reals. Answer these three questions
[10 points each]: (a) Give a mathematically precise1 complete deﬁnition of the set {2(f(n)).
A Solution:
9(a) E Qlftnll if?
Turn equiyalent deﬁnitions (either is acceptable):
1.
3 constants c > 0. no at. Wu 2 no 9(n) 2 cfln)
ii.
. n .
l?.mn_,OOE("l > 0 i.e., a constant > 0, or + no
flul
(b) ls ri3+nlgn+n E Illn2] ? Whether you answer true or false, justify your
answer with a formal and precise argument that refers to the deﬁnition
you gave in (a) above. [Recall that lgr E log2(a‘)1] Solution: True. Two acceptable justifications {depending on the deﬁnition you gave in {a)):
i. Note that Vn 33 2, n3+nlg n+n > n3 > n2. Thus choose “witnesses” c = l and “llg = 2= and refer to deﬁnition i. above. (Of course1 any other
witnesses O <: c < 1 and n0 > 2 would work too.) ..  3 a . .
11. hate that ‘v’n > U= 7" “L”! H” > “1 = n. Of course limnnxn : +00. n2 — 71—2
Peter tn rletinitinn ii tilintro (e) For each of the ten assertions below: say simply whether it is tree or false
— there is no need to add any justiﬁcation._[Note: each of these is worth
only a single point: so be careful not to spend too much time on any one of there] [The justiﬁcations shown below in were not required
for full credit.] 1 90°00) Q 00%))
Solution: True. [swan = O(f(n)) 0 52mm] ii fit») 63 Oiflnll
Solution: False. [ Reﬂexivity holds for O( . ), 6% . ), and . :1; but
neither for 0( nor for w( . 1 iii. an) 6 were» «a an) e we» Solution: True. [ Almost immediately; by deﬁnition. Similarly
1%) E O(9(n)) s» ate) E 90%)
l iv. o(f(n)l ﬂ w(f(n)) is the empty set. Solution: True. [ By contrast, O(f(n)) ﬂ is not empty (it
contains ﬂan.)5 for one). ] Vflnl E 0(Qiﬂl) 3i 9W E 005101)) e: fin) E Git9W) Solution: True. [ Justiﬁcation: I E O(g[n)) => Ele, an. at. Vn>ng < cy(n) 9(a) E 0(h(n)) :> 3e: angst. Vn>nb g(n) g efhfn) H I H J'
Now choose 0 = cc: and n0 = mam(n0,n0), then note that Vn>ng <1 0901) < cofﬁn) : CUIEUL) and the result follows. Note: transitivity holds also for all the other four
order—of—growth sets] ' vi. 11min“; : 00 a ﬁn} e Q[g(n])
Solution: False. [ But both of these are true: E .Q(g[n)) and f X .— ﬂf'Pi” \\ ii 1%”) E 9(910’0) 35 fem) E QWEWD
25' fifﬂi‘l—fsm) E Mmin(mmLamD) Solution: True. [ This is analogous to HWS, Problem No. 1. J nstiﬁca—
tion: an) e was» each n’ast vwnimn) a cairn) fﬂn) E .QKQQU’ID => 3c”, n3 at V n :> n; fﬂn) .2 ‘engﬁnj Let. nu : maﬁnb, 113), and notice that. ‘v’ n 2 n0, f1('n)+f2(”) :3 591W) + CHEM”) :2: msin(c’,c") (9101) + 92W” 2 mimc’, e”) min(g1(n), g2(n)) i so ng and e : mime, e“) are witnesses for the result. ] iii. n! E 601”) Solution: False. [ Since V71. :3 1, n! < as”, it’s certainly true thai
n! E 001.”) but it‘s not true that n! E QUE”)5 since as! is strictlg
dominated by n”: n! 1x2><3><4><...><n 1
m 2 . g — forngl
a” rt><ﬂXn><n><...><a a and of course Eimnwg'oﬁ = 0. ] ix. lg(nl) E ﬂight“N Solution: True. [ This was assigned in H‘NB, Problem 40?). Start by
noting; that lg( n” ) = a lg n. There are several ways to analyse lg{ n? }
perhaps the briefest starts with Stirling’s approximation: n; z * 9(3) (where e is the base of the natural logarithm % 2.?1828}; take logs: n(lgn—lge) E QUtlgnl ash = lg(1 + e(—)) ‘e 9(1) n
and thus lg(n.!) E 9{nlgn), same as lg(n.“). It is interesting to
contrast this result with viii. just above (1 still ﬁnd this fact counterin
tuitive). ] ' . If sequential search is performed within an array containing 210 items: then the werstcase time complexity of this algorithm is exponential:
0(2”). Solution: False. [ More precisely1 the statement is nonsense: 210 is a
large number, but it is a constant not a function of the problem size n1
so this statement implies nothing whatever about the asymptotic time
complexity of the algorithm. ] !. [Sorting Algorithms; 50 points]
Consider the following algorithm that attempts to solve the Sorting an Arv
ray of Numbers in Nondecreasing Order problem= written in seven lines oi
pseudocode: BUBBLESORT( A[1..n]) l D Input: A[i..n] is an array of in numbers, in no known order 2 HJO‘JOTH‘AQO integer i} j
for 3' <— 2 to is do for j <— rt downto 2'
do if AU — 1] < AU]
then swap ALj — 1] <—> AU] D Output: A[1..rt] is now sorted in nondecreasing order Now, this algorithm does not work as specified: it has a bug, which happens
to be in a single line of pseudocode. Answer these ﬁve questions [10 points
each]: fail Friend prim HM) Ram Elfh;ﬂ]’1 “11:: fr? nou11rlnnnr1o 1113:! +lin lirInr'? in increasing order, and so it sorts in non—tncreestng order, which is the
reverse of what the problem calls for. Rewrite that line of pseudocode to correct the algorithm (ﬁx the bug}. A Solution: do if AU u 1] > AU] An Alternative Solution: do if AU — 1] 3 Ab] Both of these corrections give algorithms that sort correctlyr in nondecreasing
order. Of course, both yield the same asymptotic runtirne complexities.
However, as we will see in (ii) below, one is stable and the other is not. (rt) Show How the Algorithm Works. Apply your corrected algorithm to this
instance of the problem:
A[1..4] = <: 7,9,6,8 >
Show the result of each iteration, as. of both the outermost (t—indexed)
loop and the innermost [gtindexed] loop. Solution: I will apply the ﬁrst solution in (b) above, is. do if AU w
1] > Initial state of A: <1 7,9,6, 8 >
t = 2
j =4 :2 anest(A[j—1]:> «1:5 [AB] “2 AM)? <=> (6 :> 8)? no: no swaps; array is unchanged.
jﬁ (A]2] > A[3] )7 42> {9 > 6)"? — yes! swap A]2] <—> AB], giving
< 7, 6,9,8 ‘2
j : 2 (:t1(A[1]> A[2]]? <2) (7 > 6}? — yes! swap A[1] <—> A]2],
giving < 5, 7,9,8 >
Note: the value 6, which is the minimum of all them, has “bubbled”
down from its original position A[3] to its ﬁnal sorted position A[1].
t z 3
'=4 I rt (A]3] > ALP1])? at} (9 > 8)? — yes! swap A]3] <—> AH],
giving < 6, 7,8, 9 >
i = 3 l: t} (A[3] 3‘» AM] )7 <2} (8 > 9)? — no; no change.
t : 4 1:11)
" 2 4 : r1 3:, 22' [AB] > AM] )7 {2}" [8 > 9)? — no; no change.
Final state of A“: < 6, 7,8,9 >. Prove that the Algorithm ts Correct. State a loop tneertent for the out— Then give a rigorous argument that the algorithm is correct. using thc
loop invariant. Recall that correctness arguments using loop invariants
have three parts: initialization. maintenance, 85 termination. A Solution: Initialization: At the start of the ﬁrst iteration of the loop. loop index
i. : 2. A[l..(i — 2)] = Alluﬂl is an empty arav: so the invariant is
satisﬁed triviallyr (“vacuously‘i Maintenance: Assume that the invariant holds for some value if of the loop
index 31: this implies that all items in — 2)] are in ﬁnal position.
The code of the body of the loop examines all values in AWC — l)..n]
and “bubbles down" the smallest value seen among them. which ends
up in in AW: — (If the smallest is already in that position. it doesn’t
have to be be bubbled down.) Thus the value in Awe — 1)] is now
in it ﬁnal sorted position. At the bottom of the loop. the index i is
incremented. giving 15 = (h + 1) at the start of the next iteration. But
we have just seen that All..(h — 1)] are in ﬁnal sorted position: thus the
invariant holds for i = (h + 1). and we have proved that the invariant
is maintained. Termination: The 1o0p terminates wheni > n: that is. the indent = n+1.
rl‘he truth of invariant thus implies that all items in A[1..((n + 1) —
2)] : A[1..(n — 1)] are in their ﬁnal sorted position. The last item. thus must also be in ﬁnal sorted position. Thus the entire arag,r is
sorted. Thus the algorithm as a whole is correct. (in) Analyze the Rnntime Complexity of the Algorithm. Answer these three
questions: (a) Let us agree to measure the runtime of this algorithm by counting only
the nnmher of key comparisons performed a <_: h. or a :> h. and
similar pairwise arithmetic comparisons; we won’t bother counting com parisons performed to control the loops). What is the asymptotic order
of growth of rimtimp, in flip, “rams map at this aimrnhm? mom on Hereis a derivation: i1)=I§(n—i+1)=TE(n—Li):Elll E Elﬁn?) ' '=R :7? kzl I_1 2 1:2 (b) Is the above order of growth asymptotically strictly better (more slowly
growing), strictly worse (more rapidly growing), or equivalent to the
order of growth of the MERGESORT algorithms runtime, in the worst
case? Why [very brieﬂy, 1—3 sentences)? A Solution: MERGESORT runs in 6(nlg time in the worst case, so
it is asymptotically strictly better (its runtime grows far more slowly)
than BUBBLE—SORT. (c) Now let us agree to measure the runtime of this algorithm by count
ing only the another of key swaps performed ﬂag. swap AB] <—> What is the algorithms asymptotic order of growth of runtime, in the
worst case? In the best case? (You don’t have to give any details or
justiﬁcation.) A Solution: 6mg) worst—case. 9(1) bestcase.
Details (not required for full credit): If the array, on input, is sorted in reverse order (that is, non—increasing),
then every iteration of the innermost (j—indexed) loop must. execute a
swap, so the analysis is the same as above. If the array on input is already in sorted order (that is, nondecreasing),
then no swaps are needed: this is clearly constant time. Now, admit—
tedly, this way of measuring runtime may seem unrealistic, since the
outermost loop runs 9(a) times no matter how few swaps occur so
the algorithm as a whole surely doesn’t run in constant time. However,
it sometimes happens in practice—a9. for huge lists which can’t ﬁt in
main memory—that swaps, if the two items are far apart, require disk
access operations which are so slow that they dominate all other costs:
in cases like this, the number of swaps can be the overwhelmingly most
important measure of cost. A Solution: If your correction (in (b) above) was: do if — 1} > AU]
then the corrected algorithm is stable since. it refuses to swap any pair of
keys which are equal. thus preserving their original order.
An Alternative Solution: If your correction (in above) was: do if AL}; — 1] 2 AU]
then the corrected algorithm is not stable since it always swaps pairs of
keys which are equal: it is easy to see that this can lead. in some instances,
of original order of equal keys being lost.
Note: this illustrates the fact that stability often depends on subtle coding
details. (But some sorting algorithms—such as HEAPSORT— cannot be
modiﬁed to ensure stability without sweeping changes that turn them into
very different algorithms.) :. [Recursive algorithms; 40 points]
Fibonacci numbers are deﬁned by this recurrence on the integers: 17(8) E 0 F (1) E 1 E F(n—l) + F(n—2) Vn>l
The ﬁrst ten Fibonacci numbers are U. l. 1. 2. 3. 5. 8. 13. 21. 34. It is known
that F{n) : 713$“ rounded up to the nearest integer. where 45': E 1‘95 :5 1.61803 (famous as the “golden ratio”). Thus F (it) grows exponentially
with TL: F(n) E 9(a)"). Answer these four questions [10 points each]: (a) Tiii"rite pseudocodc for a. recursive algorithm to compute the Fibonacci num
ber F(Ti) for any integer n, 2 2. A Solution: RECURSIVE—FIBDNACCI( ii) 1 D Input: n. is an integer 2 U 2 if ii = 0 return 0 3 if ii = 1 return 1 4 return Raouasws—FIBONACCI(a —— 1) + RECURSIVEFIBONACCI(n — 2) (b) W’rite pseudocode for an iterative (nonrecursive. looping) algorithm to
compute F for n. 2 2 in the following way: compute and store all of
the Fm) Trallies in an array V[U..n] (so that VD E i 3 ii. z as rrI . 1 .1 A Solution: lTERATIVE—FIBONACCI( n.) 1 D Input: it is an integer L‘_> 2
2 integer array V[U..n} 3 integer i
4 VH1] +— O
5 H1] <— 1
6 for i {— 2 to n, 7 do <— V[i. — 1] + V[i —— 2]
8 return This is the one lill analyze below. There is An Alternative Solution
which doesn’t need an array: CONSTANT—SPACE—lTERATIVEFIBONACCI( n)
1 integer 3', result, prev}, prev}? 2 prev}? <— D 3 preel <— 1 4 for i <— 2 to e 5 do ree'eli <— gereel + prevB
6 preoB Jr prev} 7 preoI <— result 8 return result There is1 in addition} An Unexpected Alternative Solution (which a
student proposed during this Exam; I hadn‘t thought of it): CURIOUSITERA‘TIVE—FIBONACCI( e)
integer array V[U..n]
integer i
for i <— 0 to u
do V111 * lﬁrﬂ
return V[n] (cl Let. us agree that we will measure the runtime of these two algorithms by
counting only the number of additions performed [ignoring everything else
they do). Now answer these two questions: 1. Which of the two algorithms is asymptotically the faster one: and why
do you think so (brieﬂya 1—2 sentences)? ii. lellThat is the asymptotic order of growth of runtime of the faster algo—
rithm? Explain in detail. A Solution: ITERATIVE—FIBONAC—CI is the faster algorithm since it runs
a single loop “a — 1 times: each iteration performs one addition for an
asymptotic runtime of 9(a). By contrast1 RECURSIVEFIBONACCI recom
putes many values of F over and over again: and so its runtime seems
to be superlinear. (d) What is the runtime complexity of the asymptotically slower algorithm?
[For fall credit, you need merely to state the asymptotic order of growth,
you don’t have to give details. For extra credit, which, can. “top up” you?
Exam grade to the maarii'aam= give a detailed justiﬁcation. ] A Solution: RECURSIVE—FIBONACCIFS runtime grows exponentially with
a. Let Ala] be the number of additions performed by this algorithm; it iE
deﬁned by the recurrence: 14(0) = El 44(1) = 1 = A(n—1}+A(a—2)+1 Vii32
The ﬁrst nine values of A(a) are: n A(a) 0 OED‘IHAWMI—l
"JPlthl—‘CDCD 12
7 20
8 33 Exactly how test this grows can be analyzed as follows. Notice that Alal : L [Heaps and H capsurt; 40 points}
You are given this list. of key values: <1 1, 8, 6, 5,3, 7,4 >2 Answer these four
questions [10 points each]: (a) Make the list. into a maxheap by the BUILDMl’xerEAP algorithm. Recall
that BUILD—hIAXHEAP calls MAX—HEAPIFY on each interior node of a
certain array A[1..n] (here, 71. = 7’), starting from the last interior node, in
“bottom—up” order, up to the root node. Illustrate the operation of each
call of MAXHEAPIFY, and show the ﬁnal maxheap. (You don‘t have to
write pseudoeode.) A Solution:
The initial state of the list of keys, interpreted as having heapshape, is: MAX—HEAPIFY is ﬁrst called on the last interior node AB] (key:ﬁ), and
the left—child (keyt'T) is the largest of the three and so is swapped
with AB], giving: Next, MAXHEAPIFY is called on interior node AB] (keyzéﬁl, which domi—
nates both its children so no swaps are needed. Lastly, the root. is processed:
the leftchild A[2] (keyzES) is the largest and is swapped, giving: Now MAXHEAPIFY recursively checks the nowsuspect left—subtree and (b) New make the same list into a max—heap in a different way: start with an
empty,»r maxheap A[1,.0], then call MamHEAP—INSERTM,key) for each
key value in the order given. Recall that NIAX—HEAPINSERT(A[1..n], key)
creates a new node A[n + 1], initializes its value to —oe, then calls the
priority—queue Operation HEAPINCREASE—KEY(A,n + 1, key). Illustrate
the operation of each insertion, and show the ﬁnal max—heap. (You donlt
have to write pseudoeode.) A Solution:
First, insert the ﬁrst key from the list into an empty heap, giving this heap of size 1:
CD (I won‘t show the insertion of the —oo values, for brevity.) The next inser—
tion is of key=8, which we add at a new node created at index 2: and since it is greater than its parents key value, it must “ﬂoat” up, swap—
ping with the root key value: Next, the key value 6 is appended to the heap, in new node 3: Inserting lie}r value 3, in new node (index 5) does not require ﬂeeting: —but when, next, key value 7 is inserted—— —it Fleets up one level: The last key value 4 is put in appended nede indexed 7, and does net need
to ﬂeet up. This gives the ﬁnal state of the heap: [a (e) Note that the two max—heaps are identical. “fill it always (for every possible
list of keys) happen that these two different algorithms for building a max
heap out of a list of keys result in exactly the same heap? If your answer
is yes, give a brie}C justiﬁcation (no need to write a rigorous proof). If your
answer is no. give a brief justiﬁcation or a counterexample. A Solution: No, not always. Now ofcourse they will always be legal heaps—possessing
correct heap shape and heap order—but they may not always be exactly
the same, in the sense that all corresponding pairs of nodes contain the
same key value. Here’s a counterexample: < l, 2, 3 >. The ﬁrst algorithm
yields: while the second algorithm yields: (d) What is the maximum number of swaps {of pairs of keys) that may occur
when executing MAXHEAPINSERT(A[1..h], hey), where A is a max—heap
containing a keys, expressed as function of n? For full credit: give the
asymptotic order of growth of this function. (For extra credit, gave the
exact function.) A Solution: The algorithm MAX—HEAPINSERT executes no swaps directly, but its sub
routine HEAP—INCREASEKEY may execute enough swaps to “float” the
new key from the last leaf all the way up to the root of the tree: this must
occur when the new key is greater than every other key value in the heap.
Thus the maximum number of Swaps equals the height of the tree: which
is [lg n] (where a. tree with a single node has height 0): this is the extra
credit an8wer. The asymptotic order of growth is 0( lg"; ), for full credit. done! End of Exam.
(Go back. cheek vour work add detail....l ...
View
Full Document
 Fall '08
 Baird

Click to edit the document details