Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.

Similar presentations


Presentation on theme: "Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro."— Presentation transcript:

1 Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro. To Algorithms” book website (copyright McGraw Hill) adapted and supplemented

2 CLRS “Intro. To Algorithms” Ch. 16: Greedy Algorithms

3 Activity selection problem Given set S = {a 1, …, a n } of activities and activity start and finish times, e.g.: i 1 2 3 4 5 6 7 8 9 10 11 s i 1 3 0 5 3 5 6 8 8 2 12 f i 4 5 6 7 8 9 10 11 12 13 14 (Note: activities sorted in order of finish time) Activities are compatible if their time intervals do not overlap, i.e., a i is compatible with a j is s i ≥ f j or s j ≥ f i. Prob: Find a maximum-size subset of mutually compatible activities. Examples:… Define S ij = {a k  S : f i ≤ s k < f k ≤ s j } c[i,j] = number of activities in a max-size subset of mutually compatible activities in S ij Add fictitious activities a 0 with finish time 0 and a n+1 with start time ∞. Then, reqd. soln. is c[0, n+1].

4 c[i,j] = 0 if S ij =  max i<k<j {c[i,k] + c[k,j] + 1} if S ij ≠  Recursive equation leads to dynamic programming solution! We can do better with: Th. 16.1: Let a m be the activity in S ij with the earliest finish time, i.e., f m = min{f k : a k  S ij }. Then: 1. Activity a m is used in some max-size subset of mutually compatible activities of S ij. 2. The subproblem S im is empty, so that choosing a m leaves the subproblem S mj as the only one that may be non-empty. Recursive greedy solution: a k  S ij

5

6 Iterative greedy solution:

7 0-1 vs. fractional knapsack problem A thief robbing a store finds n items: the i th item is worth v i dollars and weighs w i pounds, where both v i and w i are integers. The thief wants to take as valuable a load as possible but he can carry only W pounds in his knapsack, where W is some integer. Prob: Which items should the thief take? Two versions of the problem: (a) 0-1 : For each item, the thief must either take it or leave it, in other words, he cannot take a fraction of an item. (b) Fractional : The thief can take fractions of items. Ques: Can one or both versions be solved by the greedy method?!

8

9 A variable-length code should be a prefix code or decoding could be problematic. A prefix code is one in which no codeword is a prefix of another codeword.

10

11

12

13 Problems Ex. 16.1-3 Read Sec. 16.3 on Huffman codes Ex. 16.3-2 Ex. 16.3-5 Prob. 16-1 Prob. 16-2


Download ppt "Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro."

Similar presentations


Ads by Google