Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt

Similar presentations


Presentation on theme: "Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt"— Presentation transcript:

1 Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
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 = {a1, …, an} of activities and activity start and finish times, e.g.: i si fi (Note: activities sorted in order of finish time) Activities are compatible if their time intervals do not overlap, i.e., ai is compatible with aj if si ≥ fj or sj ≥ fi. Prob: Find a maximum-size subset of mutually compatible activities. Examples:… Define Sij = {ak  S : fi ≤ sk < fk ≤ sj} c[i,j] = number of activities in a max-size subset of mutually compatible activities in Sij Add fictitious activities a0 with finish time 0 and an+1 with start time ∞. Then, reqd. soln. is c[0, n+1].

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

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 vi dollars and weighs wi pounds, where both vi and wi 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: 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. 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 Why does Huffman’s greedy strategy work?
We want to prove that Huffman’s greedy strategy is correct and indeed gives an optimal prefix code. Consider the following: Suppose we have a character set x1, x2, …, xn with frequencies f1, f2, …, fn, resp. Without loss of generality suppose f1 and f2 are the two smallest frequencies. Consider a new character set X, x3, …, xn with frequencies, f1+f2, f3, …, fn, resp. (i.e., x1 and x2 are “combined” into a new character X). If T is the optimal prefix tree for X, x3, …, xn, then show that the optimal prefix tree for x1, x2, …, xn is obtained by replacing the leaf for X with an internal node whose two children are x1 and x2. Use this to prove the correctness of Huffman.

14 Problems Ex. 16.1-3 Read Sec. 16.3 on Huffman codes Ex. 16.3-2


Download ppt "Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt"

Similar presentations


Ads by Google