HERE

Transcription

HERE
Problem 1 (15 pts):
I,
12, 15,4,18, 13, 6, 2 using quicksort, by
Sort the array containing the sequence 9,
using in-place partitioning and always selecting the leftmost element as pivot. Write the
contents of the array after each step. (Each new selection of a pivot constitutes a new
step). For the purposes of this question, consider a "small" instance to be of s:ze 2 ot
less.
rI ,l It 13 t 2
r4l L L' I 'tgt3(St2
.-----:--'-1
i.-l
q t Iz
'L \ ]-j {, 1 '!z 13 tt ly'
Iz ? (, ? r,-16; tF
tt* Lt t 9 t7 ri li tr
I
Problem 2 (10 pts):
Starting with the anay 121,25, 49,25,93, 62,72,8,37, 16,54], show each step involved
in a Natural Merge Sort. For this sorting algorithm, consider a small instance to be of
size 2 or less.
t,l6,Lttut&f,jV,hl ,tv
../ \.
t,.
.,
?, zt, Ll)z I )3+, v l, 6? )7Zti
L' ::,21
,/ f
,1j
f , 3? ,62)f
/
/,
zl"L\,9',
*-:
\,?:
3
V
lLtt y
'r,
\\\\
(z +z i,t'I
/i i?
){Z
,}Z1t3
Problem 3 (20 pts):
a)
(15 pts.) Consider the directed graph G as follows:
Use Dijkstra's single-source shortest path algorithm to calculate the shortest path from
vertex I to each vertex ofthe graph shown above. Show the predecessor and distance
arrays following each step of Dijkstra's algorithm.
b)
G)
d
(5 pts.) Construct an example to show why Dirjkstra's algorithm does not work on a
directed graph with negative edges.
| 7 3 q .rL
o
toc
Lo iTrl
by
7o
O{-o,
\z
t
d
o@t[o
ltu
P
J
,7o
\
uI
r
ur
S
l
I
i
a
\, ,z''
?0
EI rn
Ll
J o \lj
I'
7o
t
-;,
;-";
-*-*
'zoi
,So)
r.r
t-'
i,o\ 6.fl
v
i....--*
j
3
7
l
6
r
+0
I
l\0/
:t0
I
\
E
4
J
c
3
-5
I,
,l
:ike
nl.sr*rce
to
c. i: f,i"*\"-o'l
'
Problem 4 (15 pts):
Ustng Kruskal's method, construct a minimum-cost spanning tree. Draw the spanning tree
that is constructed and also give the steps (draw figures and state the decision made at
each step) used to arrive at this tree. Choose vertex A as your starting vertex,
O
I
qJt (t-,0) (,)
ald ( n,c) tz,)
ib
aJJ ( n,r)
l:)
b
qJ aJl (Drql
1q)
odi (6,s) ii\
aJJ
(c,r)
( /)
aJJ I n,r) {+)
q,dJoA [ il,{^ ( l-t),,
o dnw
/
Problem 5: (15 pts.)
Consider the following undirected graph
A
B
vi
I
t\
t\
:y'
6
I
H
(a) (5 pts.) Using Breadth First Search, show how the exploration proceeds if we start
at Vertex A. Show the state of the data structure at each step.
(b) (10 pts.) Using Depth First Search" show how the exploration proceeds if we start
at Vertex
.\
A.
Show the state of the data structure at each step.
W,A/a-1
wU
u'0,,6J
q
b) s{stu*:
u'^
YA orJrx (orrnuj ou-+ ea
6rytr. Y{",,vu}
,.
-tr:----L
,_6
f
Vtti;.rf -' F
t3
D
--+-:-:L
-
h**l,o
G
e++*
/1#
rlt1
__e
-n:-14+-- 6+Ar-,-/
I
-*+
'6 Ds
a
r
I
H
fg*)
Problem 6: (20 pts.)
N children have been playrng in a swimming pool, after leaving their shoes at poolside.
When it's time to leave, all the shoes look identical; the only way for a child to identi$
his/her shoes is to try one on (doesn't matter which one, left or right) and see if the shoe
fits EXACTLY (not too tight nor too loose). Assume that the foot-sizes of all the children
are distinct. There is no way to directly sort the children (or the shoes) based on their
size, i.e. you cannot compare any two children's foot sizes (or any two shoes) directly.
After trying on a shoe, a child can tell if the shoe is 1) an exact fit 2) too tight or 3) too
loose. However, if two different shoes are too tight, he/she can't tell which one is tighter.
Similarly, if trvo diflerent shoes are too loose, the child cannot tell which one is looser.
Devise an efficient algorithm that would allow all the children to identifu their shoes in a
minimum number of trials (each trial consists ofa single child @ing on one shoe). Your
algorithm's average running time should be O(n log(n)). Describe your algorithm using
pseudo-code.
child try each shoe will have a running time of
O(n'), both in the average and in the worst case.
Note: The trivial method of letting
Ilint:
Once the
each
"pivot" child's shoe is found, all other people and
shoes can be split into
two groups.
+
cn
lJ**
C['r
sl,,agn
5n
t- F'*J+u, sl^oe .[o" ", r\? fI^ -if
cLczr
Jr ..S,^
,*lL {k-
cyrs srjr
sc, ov,J 1r*tf,* !t"
olar lJ*Pn
?*I1I*- Sr .. Sn r*h i*, 1tq,,ys:
)s s,y .
o/nJ l** lavgtv W"^ c'
At yu
1o
As,,,re
o
]&* cru""$
hfl*
,lr" {o,
Cr
ilh ho qro^yt
t\rcrrn e ,,
.
ltJo
r,,r
u{
{L*r^ s"oalLa
:
l'!!y-*'Vw"'c
i
sl^,,*
A
f
fh
tu^q
hc,r
{**
ct t a^.J fiL*u l* *,.
[nn-c
{fu*
1
Ct
sl
,
5-('l
u " C''' lo
alr ,tr*, Jt, lJ-m
cr .- Cr^
|s
l|csle
ctarli-a", ilr,,fun
ihn"
.,
sfuss q*.ubr {*o,rt
a!o* #*vt re'otrtiv*h to 'll& 1u), u^,isod/
I
1I
d
:
i
c1
erTvn*,^fi -
I
I
\
Problem 7: (20 pts.)
Consider the 0/1 knapsack problem. We have a knapsack of capacity c:7, and n:3 items
with weights w:[7,3,2], and corresponding profits p:[10,8,6]. Recall that we are
attempting to maximize the profit of the items selected to go into the knapsack, subject to
the constraint that the weights of those items cannot exceed the capacity of the knapsack.
(a) (5 pts.) Show that being greedy on profit does not provide the optimum profit.
(b) (15 pts.) Find the optimum profit using dynamic programming.
Recall that (i,y) is the profit value of the optimal solution to the knapsack instance
defined by the state (i,y) (items i throughBre available, and the available capacity is
To calculate (1,c), recall also that 1
y)
: pn, if w,, <= y and
(n,y) :0 otherwise.
(n,y)
(i,y): (i+l,y),
(i,y) :
/.
*,.--\-/
: t. .'t I-10
(6ecuaa ,
r,
n
if y < w;, and
max{(i+l,y), (i+l,y-wi) + pi} otherwise.
* (2,7-wr) +lo =
1ftt o)+to = la
{ tt,+) = wx(6rr{)=
\
tl
r\
/'\,,\
t t'
{
{it,+)r8 =
ti,+ -+}
It^*pw,lr.
I
L + 6 =lY
\
W, N, {uu4l**
ry
01v'{w+:
\L*o*^,\
1or
f (3,0)=
o
T*
wt&A4awt
'
B,J
rnrrrl.,,
+
(3,0-r)
=o
.,i{
Problem 8: (10 pts.)
Given a number x and an integer
algorithm is:
11
we'd like to compute x to the powern' The naive
result :
1;
Naive-Power
(x, n) :
for 1 to n:
result=x*result;
return result;
end;
xn
Clearly this algorithm is O(n). To use a divide-and-conquer approaclq express
as:
{2 . *'
if n is even" or
*(n-l)D. *(n-l)2. )c, if n is odd.
Based on this way of expressing
Recursive-Power
ifn::L
(x,
n)
xn, write pseudo- code for the following function:
:
return x;
/
/ you f:-Ll- in the rest
rt s${r\tvr) tfuf = [to*.r,*"-Po*,'f "rt/);
\{f,,^^ te"'y * f*p t
r,\sc
\N,
t trT = lt*nr*-P*o,, ( *, r,u/z-r) i
I{fi,n t"f -) *a
\
,r"A
i