CS577 – (Solution)

$ 29.99
Category:

Description

Assignment 3 – Greedy Algorithms
Answer the questions in the boxes provided on the question sheets. If you run out of room for an answer, add a page to the end of the document.
Name: Wisc id:
Greedy Algorithms
1. In one or two sentences, describe what a greedy algorithm is. Your definition should be informal, something you could share with a non computer scientist.
Solution: Grey algorithmis a short sighted algorithm tying to maximizelocal gainprofit at eachstep It searchforthecurrentoptimalsolution ineachsgand ignore the futuretrend
2. There are many different problems all described as “scheduling” problems. In the following questions, pay attention to the details of the problem setup, as they will change each time!
(a) Let each job have a start time, an end time, and a value. We want to schedule as much value of non-conflicting jobs as possible. Use a counterexample to show that Earliest Finish First (the greedy algorithm we used for jobs with all equal value) does NOT work in this case.

(b) Kleinberg, Jon. Algorithm Design (p. 191, q. 7) Now let each job consist of two durations. A job i must be preprocessed for p time on a supercomputer, and then finished for f time on a standard PC. There are enough PCs available to run all jobs at the same time, but there is only one
supercomputer (which can only run a single job at a time). The completion time of a schedule isi 7 i
defined as the earliest time when all jobs are done running on both the supercomputer and the PCs.
Give a polynomial time algorithm that finds a schedule with the earliest completion time possible.
Solution:
pi.fi.pcsc r ljij.fr
we usethe longest finished timefirstalgorithm considerjinset hid S an entryset
whilechootf sedoji withsmallest fi within T Ibreak ties arbitrarilyI addji to S remove ji front
end returns thisrequiressortingjobbasedon fi causeOflog
(c) Prove the correctness and efficiency of your algorithm from part (c).
Solution: we define scheduleA M inversion fibfejhtfic.fi lemma 肌 schedules withnoinversionsandno idk inehavethesameuterus ifrearranwegetheonlyfanoonfthenthejobwown’itthchargesame filatetheynt mustbesequential order
Thm wThee were isexchangeanoptimalargumentschtechneme hasique Connosiidernverswioenandbe anoptio.ij.withiafterj.fimidlealschedulestime ifIf i hasinvasionweknowthereisatleastonepairofjobs i
Ǜkchange ij wehave i j j.fj afteri i wehavenewschemes
definesinicepwisiethenttahkeettimeimiÈaheade akifinilltfjsupercomputerwshedehaveinsisjobssifjiifinished.iemilarEforFkjjtFfi点weapisiknowi.lnihiusitheÈfPKtfi.liit.imfieifini⼗shedĚ毙inpu熊tfi諔以fi we heli ⼆点Put 靠
3. Kleinberg, Jon. Algorithm Design (p. 190, q. 5) werepeatthesesteps untilnoinversions 四
(a) Consider a long, straight road with houses scattered along it. We want to place cell phone towers along the road so that every house is within four miles of at least one tower. Give an efficient algorithm that achieves this goal using the minimum possible number of towers.
Solution: consider the road a straightlines withsomepointsknown
as houses IN
setencweweouostastanntererrtowerttffromonthelef4housemilestrightwalkweribaghtrepeattowndyawayardsoforifrghtrangeomuthisntilofhoupreviousencounteringse towefirrswhenthousweewe
another
we repeat d.eu nil allthehorses are covered
(b) Prove the correctness of your algorithm.
Solution: we we5 in iz in denotedattheethseteoptimaloftowsolutionersfunlefttoright let n.kk.vn denotefi Ltheinjirightjibandyn Arange towerin alsotherangtechniquee ofwe alwaysstaysahead of Li in similarfor it in A we
lemmanzIi indfuctionurnrniitpf.blitholdrtitswholdsinceehavesweychoseIn4thetowerasright aspossiblewhile
suppose
ThanHiwOurByigalgorithmthecontnextradictiproducehouseon assuoptionalwemecksovhaveemarrangementr usin_nthenuhglihorsesn enmaw四edoIn.io.itnnecedancoverimtyj.ijik.hn.ru itcontradicts
rage and i ⼼ jm Wehaves is a optimal以G
4. Kleinberg, Jon. Algorithm Design (p. 197, q. 18) Your friends are planning to drive north from Madison 四 to the town of Superior, Wisconsin over winter break. They have drawn a directed graph with nodes representing potential stops and edges representing the roads between them.
They have also found a weather forecasting site that can accurately predict how long it will take to traverse one of the edges on their graph, given the starting time t. This is important because some of the roads on their graph are affected strongly by the seasons and by extreme weather. It’s guaranteed that it never takes negative time to traverse an edge, and that you can never arrive earlier by starting later.
whilese.StluVnodevs.tvS Visoneedge mㄣ d⼼⼆点ilduitld
I fro
addv to sanddefine did’M
if visitbreak or
End shortest westarfromt hpandfindtheedgeInSup thelaststeplucy
Thentofindwelocatenpatodehuandfindtheedge yu atstepwhereuaddedto Slyas wedothis recursively untilwe reach Madison Thenwe have anedgetogether is pathfrom Snp to wereversethepath getMresulte
(b) Demonstrate how your algorithm works using a small example with 6 nodes. Your demonstration should include any data structures you maintain during the execution of your algorithm and any queries you make to the weather forecasting site. For example, if your algorithm maintains a “current path” that grows from (M)adison to (S)uperior, you might show something like the following table: Path
Coding Question
5. Implement the optimal algorithm for interval scheduling (for a definition of the problem, see the Greedy slides on Canvas) in either C, C++, C#, Java, or Python. Be efficient and implement it in O(nlogn) time, where n is the number of jobs.
The input will start with an positive integer, giving the number of instances that follow. For each instance, there will be a positive integer, giving the number of jobs. For each job, there will be a pair of positive integers i and j, where i < j, and i is the start time, and j is the end time. A sample input is the following:
2
1
1 4
3
1 2
3 4
2 6
The sample input has two instances. The first instance has one job to schedule with a start time of 1 and an end time of 4. The second instance has 3 jobs.
For each instance, your program should output the number of intervals scheduled on a separate line. Each output line should be terminated by a newline. The correct output to the sample input would be:
1
2

Reviews

There are no reviews yet.

Be the first to review “CS577 – (Solution)”

Your email address will not be published. Required fields are marked *