DOC PREVIEW
UT Dallas CS 6363 - lec7

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture #7:0.0.1 Greedy Methods:(Chapter 17)We now take up the concept of the greedy paradigm. We will consider severalillustrative examples.• Making ChangeSuppose we have the following coins: Dollar(100 cents), Quarter(25 cents),Dime(10 cents), Nickel(5 cents), and penny(1 cent). When we to pay someone an amount (say $2.89=289 cents), we do this with little thinking in away that in some cases uses the smallest number of coins. [Assume thatwe have sufficien t quantities of each denomination). And the way we do itis known as the greedy method. We use 2 dollar coins, three quarters,one dime, and four pennies. If there was a half-dollar (=50 cents) coin,there are two possibilities — the greedy one is 2 dollar coins, one half-dollarcoin, one quarter, one dime, and four pennies. So at each step, we usethe largest denomination as many times as possible without going overthe total. The decision made at each step is neve r revoked. These aresome of the characteristics of greedy methods. It does not provide thecorrect solution to every problem! For example, convince yourselfthat when the available coins are: {50,25,20,10,5,1), this method does notalways work.But it is useful in some instances. In some cases, there may be more thanone way to solve using greedy ideas. Some may work while others maynot.• Activity Selection Problem (Chapter 17.1)Suppose we have a set S = {1, 2, ..., n} of n activities that wish to use acommon resource. Activity i needs the resource in the time interval [si,fi) withfi>si. Two activities i and j are said to be compatible if their intervals do notoverlap — that is if either si≥ fjor sj≥ fi. The activity selection problem is toselect the largest set of mutually compatible activities. Here is the pesuodcodefrom the book: s and f are arrays. Assume that the activities are numbered sothat f1≤ f2≤ ... ≤ fn.GREEDY-ACTIVITY-SELECTOR(s, f)n ←− length(s)A ←− {1}j ←− 1for i =2to ndo if si≥ fj1then A ←− A ∪ {i}j ←− ireturn AClearly the set A returned by the above algorithm is "feasible" — meaningthat the intervals corresponding to activities in A do not overlap. What remainsto be shown is that among such sets A has the largest size (such sets are called"optimal"). It is the proof of this that we do now.1. We need to show that 1 should belong to some optimal set.Proof: Suppose 1 does not belong to any optimal set. Let S be someoptimal set (the existence of such a set is not in question since there areonly finitely many feasible sets). Let k =min[j : j ∈ S]. This is underthe numbering as per an ordering that satisfies f1≤ f2≤ ... ≤ fn.Thensj≥ fk≥ f1for all j ∈ S − {k}. Moreover, there is no overlap in theintervals corresponding to activities in S −{k}.ThusS0= S −{k}+{1} isalso feasible and |S0| = |S| and hence S0is also optimal. This contradictsthe supposition that 1 doe snot belong to any optimal set.2. Since 1 is a subset of some optimal set, eliminating any activit y whoseinterval overlaps with any of the activities 1 does not exclude suc h optimalsets. Activity i 6=1does not overlap with any activity in 1 implies thatsi≥ f1ors1≥ fiSince we know that fi≥ f1it follows that f1≥ siHence i 6=1does notconflict with 1 implies that si≥ f1and hence this step of the algorithmis correct.3. Let T represent the set of activities which does not include any of activity1 or any activity whose interval overlaps with that of 1.NoactivityinT can conflict with 1. Thus, an optimal set S that includes 1 has theproperty (by induction hypothesis) that S − {1} is optimal for T .Andthe above algorithm applied to T clearly produces S − {1}. Hence thealgorithm "works".{23}’ Alternate: Step 1 is the same. Now suppose that that the algorithmcorrectly produces A of some step — meaning that this A is a subset ofsome optimal set. Then excluding any activity that conflicts with anyactivity in A will not exclude such optimal sets. It is easy to verify thatan activity (that has not already been excluded) i/∈ A conflicts with someactivity in A iff si° fjwhere j is the last activity added to A.Thusthe algorithm’s next addtion is also correct by reasoning similar to thatin step 1.2• Huffman Codes (Chapter 17.3)HUFFMAN(C)1. n ← |C|2. Q ← C3. for i ← 1 to n − 14. do allocate a new node z5. left[z] ← x ←EXTRACT-MIN(Q)6. right[z] ← y ←EXTRACT-MIN(Q)7. f[z] ← f [x]+f[y]8. INSERT(Q, z)9. return EXTRA CT-MIN(Q)• Minim u m/Maximum Spanning Tree Problem• Dijkstra’s Algorithm in Shortest PathsThe discussion of the last two problems also comes under the category GraphAlgorithms.• Several Scheduling ProblemsExample 1 A single server (such as a processor, a cashier in a bank, etc.)has n customers to serve. The service time required by each customer is knownin advance: customer i will re quire titime units (1 ≤ i ≤ n ). We want tominimize the average time a customer spends in the system.Since the number of customers is fixed, this is equivalent to minimizing thetotal time spent by all customers. So we want to minimizePni=1CiwhereCiis the time at which customer i completes service. Suppose n =3andt1=3;t2= 10; t3=5the table below summarizes the values for all the ways inwhich we could render the service:OrderPni=1Ci1, 2, 3 341, 3, 2 29 Optimal2, 3, 1 432, 1, 3 413, 1, 2 313, 2, 1 38Let P = p1p2...pnbe a permutation of {1, 2, ..., n} and let si= tpi.Ifcustomers are served in the order corresponding to P ,nXi=1Ci= s1+(s1+ s2)+... +(s1+ ...sn)=nXk=1(n − k +1)sk3This is minimized by the permut ation that has s1≤ s2≤ ... ≤ sn.Thiscorresponds to the greedy strategy of processing the customer with the leasttime required first.Example 2 We have n jobs to execute, each one of which takes a unit time toprocess. At any time instant we can do only one job. Doing job i earns a profitpi. The deadline for job i is di.Suppose n =4;p =[50, 10, 15, 30]; d =[2, 1, 2, 1]. It should be clear that wecan process no more than two jobs by their respective deadlines. The set offeasible sequences are:Sequence Profit1 502 103 154 301, 3 652, 1 602, 3 253, 1 654, 1 80 optimal4, 3 45A set of jobs is feasible if there is a sequence that is feasible for this set. Thegreedy method chooses jobs in decreasing order of their profits. If the inclusionof the next job does not affect feasibility, w e do so. Else we do not include thelast job that was considered. We need to show that this algorithm provides anoptimal solution. We also


View Full Document

UT Dallas CS 6363 - lec7

Documents in this Course
Exam #1

Exam #1

5 pages

lec4

lec4

5 pages

lec3

lec3

5 pages

lec1

lec1

3 pages

lec14

lec14

11 pages

lec13

lec13

22 pages

lec12

lec12

8 pages

lec11

lec11

3 pages

lec10new

lec10new

11 pages

lec9

lec9

13 pages

lec8

lec8

9 pages

lec7

lec7

10 pages

lec6

lec6

8 pages

lec7

lec7

10 pages

lec6

lec6

8 pages

lec4

lec4

5 pages

lec3

lec3

5 pages

lec1

lec1

3 pages

lec14

lec14

11 pages

lec13

lec13

22 pages

lec12

lec12

8 pages

lec11

lec11

3 pages

lec10new

lec10new

11 pages

lec9

lec9

13 pages

lec8

lec8

9 pages

lec4

lec4

5 pages

lec3

lec3

5 pages

lec1

lec1

3 pages

lec14

lec14

11 pages

lec13

lec13

22 pages

lec12

lec12

8 pages

lec11

lec11

3 pages

lec10new

lec10new

11 pages

lec9

lec9

13 pages

lec8

lec8

9 pages

lec6

lec6

8 pages

lec14

lec14

11 pages

lec13

lec13

22 pages

lec12

lec12

8 pages

lec11

lec11

3 pages

lec10new

lec10new

11 pages

lec9

lec9

13 pages

lec8

lec8

9 pages

lec7

lec7

10 pages

lec6

lec6

8 pages

lec4

lec4

5 pages

lec3

lec3

5 pages

lec1

lec1

3 pages

greedy

greedy

4 pages

siphon

siphon

18 pages

hwk5

hwk5

3 pages

Load more
Download lec7
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lec7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lec7 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?