DOC PREVIEW
UT Dallas CS 6363 - lec7

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

Save
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

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 several illustrative examples Making Change Suppose 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 some one an amount say 2 89 289 cents we do this with little thinking in a way that in some cases uses the smallest number of coins Assume that we have su cient quantities of each denomination And the way we do it is 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 dollar coin one quarter one dime and four pennies So at each step we use the largest denomination as many times as possible without going over the total The decision made at each step is never revoked These are some of the characteristics of greedy methods It does not provide the correct solution to every problem For example convince yourself that when the available coins are 50 25 20 10 5 1 this method does not always work But it is useful in some instances In some cases there may be more than one way to solve using greedy ideas Some may work while others may not Activity Selection Problem Chapter 17 1 Suppose we have a set S 1 2 n of n activities that wish to use a common resource Activity i needs the resource in the time interval si fi with fi si Two activities i and j are said to be compatible if their intervals do not overlap that is if either si fj or sj fi The activity selection problem is to select the largest set of mutually compatible activities Here is the pesuodcode from the book s and f are arrays Assume that the activities are numbered so that f1 f2 fn GREEDY ACTIVITY SELECTOR s f n length s A 1 j 1 for i 2 to n do if si fj 1 then A A i j i return A Clearly the set A returned by the above algorithm is feasible meaning that the intervals corresponding to activities in A do not overlap What remains to 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 some optimal set the existence of such a set is not in question since there are only finitely many feasible sets Let k min j j S This is under the numbering as per an ordering that satisfies f1 f2 fn Then sj fk f1 for all j S k Moreover there is no overlap in the intervals corresponding to activities in S k Thus S 0 S k 1 is also feasible and S 0 S and hence S 0 is also optimal This contradicts the supposition that 1 doe snot belong to any optimal set 2 Since 1 is a subset of some optimal set eliminating any activity whose interval overlaps with any of the activities 1 does not exclude such optimal sets Activity i 6 1 does not overlap with any activity in 1 implies that si s1 f1 or fi Since we know that fi f1 it follows that f1 si Hence i 6 1does not conflict with 1 implies that si f1 and hence this step of the algorithm is correct 3 Let T represent the set of activities which does not include any of activity 1 or any activity whose interval overlaps with that of 1 No activity in T can conflict with 1 Thus an optimal set S that includes 1 has the property by induction hypothesis that S 1 is optimal for T And the above algorithm applied to T clearly produces S 1 Hence the algorithm works 23 Alternate Step 1 is the same Now suppose that that the algorithm correctly produces A of some step meaning that this A is a subset of some optimal set Then excluding any activity that conflicts with any activity in A will not exclude such optimal sets It is easy to verify that an activity that has not already been excluded i A conflicts with some activity in A i si fj where j is the last activity added to A Thus the algorithm s next addtion is also correct by reasoning similar to that in step 1 2 Hu man Codes Chapter 17 3 HUFFMAN C 1 2 3 4 5 6 7 8 9 n C Q C for i 1 to n 1 do allocate a new node z lef t z x EXTRACT MIN Q right z y EXTRACT MIN Q f z f x f y INSERT Q z return EXTRACT MIN Q Minimum Maximum Spanning Tree Problem Dijkstra s Algorithm in Shortest Paths The discussion of the last two problems also comes under the category Graph Algorithms Several Scheduling Problems Example 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 known in advance customer i will require ti time units 1 i n We want to minimize the average time a customer spends in the system Since the number of customers is fixed this is equivalent to minimizing the Pn total time spent by all customers So we want to minimize C where i 1 i Ci is the time at which customer i completes service Suppose n 3 and t1 3 t2 10 t3 5 the table below summarizes the values for all the ways in which we could render the service Pn Order i 1 Ci 1 2 3 34 1 3 2 29 Optimal 2 3 1 43 2 1 3 41 3 1 2 31 3 2 1 38 Let P p1 p2 pn be a permutation of 1 2 n and let si tpi If customers are served in the order corresponding to P n X Ci s1 s1 s2 s1 sn i 1 n X n k 1 sk k 1 3 This is minimized by the permutation that has s1 s2 sn This corresponds to the greedy strategy of processing the customer with the least time required first Example 2 We have n jobs to execute each one of which takes a unit time to process At any time instant we can do only one job Doing job i earns a profit pi 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 we can process no more than two jobs by their respective deadlines The set of feasible sequences are Sequence 1 2 3 4 1 3 2 1 2 3 3 1 …


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

lec7

lec7

10 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

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
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 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?