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 Document
Unformatted text preview: CSC C73 Midterm Exam Oct 29, 2004 NAME: 0L lbw/7 When you are asked to “justify your answer”, it means that you must explain why your answe
true. Calculators are not permitted (nor would they be useful). This is a closed book exam. Ask an invigilator if there is anything that you do not understand completely. 1. (25 pts) You have a single machine which runs several jobs, one at a time. Each day, there is a new
schedule of jobs. For monitoring purposes, the machine must issue intermittent reports of its
own state. These reports take a negligible amount of time to issue, but they are expensive
and so you want to generate as few of them as possible. The only rule is that for every job
that is run, a report must have been generated within at most K minutes of the start time
of that job. It can be before the start time, or after the start time, as long as it is within at most K minutes. If several jobs have start times that are sufﬁciently close together, then a
single report could sufﬁce for all of them. The start times of the jobs and the value of K vary from day to day. Each start time is an
integer number of minutes, and each job takes exactly one minute. It is possible to generate
a report at the exact start time of a job. You must design an algorithm that, given as input the value of K and the start times 31,32,...,sn (ordered so that .91 < 32 < < s"), will output an optimal set of times for
reports to be generated. For example, if K = 4 and the start times are 2, 15, 17, 20, 21,28,39, an optimal solution
would be that the reports are generated at times 2,16,25,37. (There are several possible
optimal solutions, each of which has exactly four reports.) (a) (10 pts) Provide a greedy algorithm to solve this problem. 4" s\ y or [2%. 1, ‘60 ’Y\ MO I if [éfhrepoﬂl’d’k < S; gm [dsfvrayﬁorjre 3; + 1<
(CPOFT h‘l— Fer: cor'l' emf. m (b) (5 pts) What is the running time of your algorithm? Justify your answer. 9m. awe/s 11%; J “M
WNW W {1/ NA out/1 a99<7MW+ ‘ (Cc/£165, 0%"CL [(afor’hn ) fn3/‘JO, WINE/L Me, can S4M’t —+l‘m&. (c) (10 pts) Prove that your algorithm is correct. W0 Pfoue’ deal/9W9 ’Mvun'dn'h I
14" +\/\{/ met a! t‘kf/flﬂl'l‘Oﬂ D! {W [00p In our’ agomtkm, «lb bun/0 3Wera+ai m.'y\;y4uw\ mums“ ai refmﬂfg 1195.104 ‘60 couM joés 1 ‘Eo 2:) m4 Margot/WC QMOU (by
Fo$$h§£ 0F+IVYMI go‘uh'bﬂﬁl ours («as Me (ﬁfe/Sf F595;;[e Map 0/ ‘Hn& (as+ report. 2': ﬁnoo/z (‘2' n'nJUkC‘f, Pen on
Ease case: z‘:( : £9r .ﬁ‘rgL jag) wajenerwf 0.. reps ('l' at 1L Vlrme
51+ k Ulu'ol‘ is affﬁewty 4L0 (offer/L [Mung/’5 VLt'nme +0 (afor'f/ amL onf/ refs/1" 1‘s «mach/J ayw‘ﬂ‘ Inca/£679,613 57%,9: W67 know We z'nwm’w'f ,‘5 +yu6, {or 2;!)
NOWWVF/ for W awe/{WM W *9 eonWJ/W: Lat/5+ Mof‘f cach 1%» z. (WT,¢9,P0(T+IC ng)‘. (11/0
94”? ,4 In (“H's m5, MJ({VI;’~21/?) to 509/ {Lamb <H've/ iolu'fl’on from [48+ mwno‘r 54A”: fies ‘MZ/ I‘KVWI'IIMVIL {or *Hth nwncﬂvl'oo.
[43+ MPOW‘" 05% no'(' aver )‘05 z' ((M'f—r‘efor‘t+k<g;) ; 61mg, our 50(W‘h’0n ¥ram tat/SF VomJ, (m) {1” {do “fakh'mg ‘
if Mfr/(t W023 Of+!'ma/ sahﬁv’ong aml 341‘” Inf—ripe,»ka <53) {Lt/h Harry’s no op‘f‘fmal so/m'fn’an WNW 'tk'r “W691 0?? mporefg (1055;1/e_ 30 Ne/ “w; “< Ma‘ﬂ’w (Warf’ M4 “’5 cabled. OéVu’auab
S'Ark ’3 (H‘e [afesf Foss/1"” +'.Me’ {or swokacfepwr. Ct 2. (30 pts) You run a company whose main service involves a lot of physical labour. You have
job opportunities lined up for the next several days; speciﬁcally, you know that on day 2' you
have the Opportunity to make a proﬁt of p, where 2‘ runs from 1 to 12. However, you may
choose to decline some of those jobs in order to give your work crew holidays. Each job
requires your entire crew, so on any given day, either the entire crew gets a holiday, or no one
gets one. You have two motivations for giving a holiday: (1) There is a limit to how long they can work efﬁciently without a break. You can make
a proﬁt of at most Q between two consecutive holidays, even if the pi’s add up to more
than Q during that interval. (ii) Your crew is rejuvenated the day after a holiday. If day i —— 1 is a holiday then if they
work on day 2‘, you make a proﬁt of 210,. If you wish, you may choose to schedule two or more consecutive holidays. Days zero and
n + 1 are mandatory holidays. For example, suppose that Q 2 10 and that
P1 — 3,192 — 1,193 —~ 4:104 6,125 2,106 1,107 67P8 1,109 3 If you have holidays on days 4, 6, 8 then your total proﬁt is 30 because: You make 6 on day 1 (since it is the day after a holiday), 1 on day 2, 3 on day 3 (P3 = 4,
but you’ve already earned 7 since the last holiday so you can only earn 3 more), 0 on day 4
(since it is a holiday), 4 on day 5 (since it is the day after a holiday), 0 on day 6, 10 on day
7 (since it is the day after a holiday you could have earned 2p7 : 12, but there is a limit of
Q : 10 between holidays), 0 on day 8, 6 on day 9. (This is not an optimal solution; if you have holidays on days 3,5,6,8 then your total proﬁt is
33.) For each 0 S 2' S n let OPT(2') denote the maximum total proﬁt that you can make during
days 1 to i. (a) (5 pts) Suppose that you decide to make day k the last holiday before day 72 + 1. What
is the maximum total proﬁt that you can make? Justify your answer. Note: If you cannot solve this part, then pretend that your answer is “OPT(k — 1) +pk ” in order to answer part (433m {:1} Pt. ’5 >/ a) h
I + ) '5 girl/(K4) + mm / Kl JzkH + (Mfr? that {Walwyﬁ (yer/9'12 0°“ 71'er “(ﬂ/"m "‘4
titCi (/5 w .3 mt . Mug/g.) (b) (5pts) Use your answer in part (a) to give a recursive formula for OPT(2'). Don’t forget
to deﬁne the initial values. Note: If you cannot solve this part, then pretend that your answer is
OPT(72) = max{OPT(z' — 1) + 3pi, OPT(2' — 2) + 1} fort > 0; OPT(0) = Q
in order to answer part (c AHjO‘L nee/4 +0 0‘0 /5 {’o deq‘MI‘Ze Dl/W t9
the, mth at (av/4(4): 2.
K I + %’z‘>‘
k:@ .+ J: kH J
DPT (0]: 0 PT (4); 0
(c) (10 pts) Write a dynamic program to compute OPT(i) for 0 g i S n. LI
0PTU}: mt!” (WﬂkV’ Jr min(6?,/7 OPT 1:43:02
091’ co] =5 0; jar z.:: 1 4:6 Y1 060 lg”;
Ci] ::0/ sum 3:9)
for“ k I: livl elm/vita 0 0/0
‘9'" sum :: sun/1+ yuan];
Jerry] < opt [1(4) + mm, juMv—pCKHJ) {Lea
0f‘!’[u~}::¢f'l'[k'lj+ Ml'n(@,5wm+17[k+’])j
m4; ‘
0M3; (d) (5 pts) Write an additional part to your algorithm that will efﬁciently produce an
optimal set of holidays, Le. a set that yields a proﬁt of OPT(n). (“Jam/fat, ’60 [VI] {’0 0.
I“ “*5 “ﬁ‘v’l'ibvy I'AS/Je/ “L6 79/ sila'kml ale:
Ms a/esiLCU ck; ‘ ’ {’LJ'S coale/
m a/onﬁllﬂ” '3 WAn/ (
jmy‘wwés ‘HW/ 145 ('M 4A5 0f+rrma( So (uteri 1"“? . .
ullule/ '_£P>0 9/0 ﬁejgfl‘
‘ 6 CU? W _ , h
tiff51’ Last 0‘], " I“: a. [dell JKj.‘ / and; (z: 5&51’5‘7") (e) (5 pts) What is the total running time of your algorithm? Justify your answer. TIM/barb '(l/uvo metal leafs 0% H“ Mdsf I‘ 64614. 7740/6, ,‘g 1/ mm OfO/ml'by‘ I‘M/Jo
er'oL (a aom'rml/ {CW Sl‘hcc ill £314) ml‘n w 14% rims? WM’V we
jM'l cons'lm‘f”+1‘”‘€ 6090mf'W4' ’30, a/Jfoﬁ'llm {ab/3 6U?) ’Ct'Me. 3. (25 pts) In this problem, you are given a sequence of integers m1, ..., 3:”. Some of the integers may be
negative. They are not necessarily in nonincreasing or nondecreasing order. Your goal is to
ﬁnd a subsequence of the form 33,, 33,41, ..., 33,1, 23, with the maximum sum. The subsequence
must not have any gaps; i.e. you may choose 237, mg, 339, 1:10 but you cannot choose $7, $9, .7313. For example, if the input is —5,3,7,—12,2,6,—1,4,—4,2 then the solution is 2,6,~1,4
which sums to 11. If the input is 3, —2,4, —6, 7, —3,2 then the solution is 7  a subse
quence of length one which sums to 7. If the input is 2, —~1,4, —4,3,2, —3, 5 then the so
lution is 2,—l,4, —4,3,2, —3, 5, i.e. the entire sequence, which sums to 8. If the input is —2, —1, —4, —1,—1 then the solution is (Z), i.e. the subsequence containing no numbers, which
sums to zero. For the purposes of this problem, we will assume that the numbers are all small enough that
it takes only 0(1) time to add two of them together. (a) (5 pts) Give a very easy exhaustive—search algorithm which solves the problem in 0(n3) time. (“Exhaustive—search” means that you check every possible solution in order to
choose an optimal one.) ‘ ( \ 2\>‘v\ QO< V5“ 0[ \
r‘ » ; \eéK Wis u “(rm—“‘0. was QQCV “31“;X‘“ \E .2) i is: Rigfjgmczﬂ OCK 5
. 'L) W3WV\,W’C§L/ \ (b) BONUS QUESTION: (5 pts) Modify the algorithm from (a) to solve the problem in 0(n2) time. Do not use divideandconquer for this part.
_ ‘ rm,m~xx,wmsesé—i
QC \ i —“7 \r\ €90 ‘
\ (“QC 4)" if“ ‘2 Wales We 0&6ch 3+: “:3 ' g§mr I 3/ xé—S
ﬁdei You will now design a divide—andconquer algorithm for this problem that is faster than
0(n2) time. Split the sequence into 2 parts, A : :31, Mann/2] and B : x[n/21+1,...,mn. Let S A be the optimal subsequence of A and let S B be the optimal subsequence of B. In
addition, you will be interested in two more subsequences: R A is the best subsequence of A amongst all subsequences ending at amt/2]. R3 is the best subsequence of B amongst
all subsequences beginning at ash/21+}. (c) (5 pts) Explain how to ﬁnd RA and R3 in O(n) time.
If you can’t solve this part, you can still attempt the remaining problems. W — "W? M e m s <— ‘x m ‘QCC ‘\ ~\ Q l (d) (5 pts) Explain how, once you know S A,SB,RA,RB, you can ﬁgure out the optimal
subsequence of the entire sequence of numbers. $eqn
Wag/*(gMSB) EMva
NSVQ : quncb 243‘ 3m Cmme XV»; Cm“
Cause "\SV *0 SM oy *0 o. anytime \ldwé. lslﬁweoeﬂ ‘Ms
\o\\\ ASK M0339? 5m (e) (5 pts) Use your answers to parts (0) and (d) to provide a divide—and—conquer algorithm
to solve the problem. If you did not solve part (c) then just assume that you have an 0(n) time subroutine for
ﬁnding RA and RB and use that subroutine in your algorithm. \¥ >\ (3*qu
'j\70\C\:\\‘\\Q(\ ‘ivéVO A g :3
SR (r saw/:05
S?) e— QJDLVE (B) R“ [r QO‘VVVV‘XVQSK GS Mk}
$1ng SOLWQ(S> ‘ <8 ) émit \3 >0 (8*“Cﬁ m gp‘ ) S?) 6—1 ,_ 6x8? Qua/C (\% QQ NWM We {S \DQé" a: §vx
NSVQ i \‘ m 3kss§o~b meC AQAVQ—“S % QbQ «V W > qu geo‘uaguoés (XVQ , *MQT
“39¢an (weakest. (f) (5 pts) Let T(n) denote the worse case running time on an input with n numbers.
Determine the asymptotic order of T(n). Justify your answer. Tm : 7. we) + 903
mboylz : h 2 \93 Cane 013 03; MT) T(“\:@(n\egn>) ...
View
Full Document
 Fall '04
 Molloy
 pts, Start time

Click to edit the document details