Lecture 7 Greedy Algorithms Interval Scheduling There are

  • Slides: 22
Download presentation
Lecture 7 Greedy Algorithms

Lecture 7 Greedy Algorithms

Interval Scheduling • There are n meeting requests, meeting i takes time (si, ti)

Interval Scheduling • There are n meeting requests, meeting i takes time (si, ti) • Cannot schedule two meeting together if their intervals overlap. • Goal: Schedule as many meetings as possible. • Example: Meetings (1, 3), (2, 4), (4, 5), (4, 6), (6, 8) • Solution: 3 meetings ((1, 3), (4, 5), (6, 8))

Recap: Algorithm • Always try to schedule the meeting that ends earliest Schedule 1.

Recap: Algorithm • Always try to schedule the meeting that ends earliest Schedule 1. Sort the meetings according to end time 2. For i = 1 to n 3. If meeting i does not conflict with previous meetings 4. Schedule meeting i

Proof of correctness in pictures I have scheduled the most number of possible meetings!

Proof of correctness in pictures I have scheduled the most number of possible meetings! ……

I have scheduled the most number of possible meetings! …… …… No you are

I have scheduled the most number of possible meetings! …… …… No you are wrong! There is a way to schedule more meetings.

Can you show me how to do that? Here are the first two meetings

Can you show me how to do that? Here are the first two meetings I scheduled. …… …… Yes, those are fine. I made the same decisions.

and this is my third meeting i 3 …… j 3 …… Wait, you

and this is my third meeting i 3 …… j 3 …… Wait, you can’t do that. You need to schedule meeting j 3

My meeting i 3 ends before your meeting j 3, so if you swap

My meeting i 3 ends before your meeting j 3, so if you swap j 3 to i 3, you are still OK right? i 3 …… ij 33 …… I guess you are right. Fine I will do that.

But isn’t this the same thing? You could have swapped to my 5 th

But isn’t this the same thing? You could have swapped to my 5 th meeting. …… …… OK you got me on the 3 rd meeting. But you made another mistake on the 5 th meeting!

May rounds later……

May rounds later……

…… …… OK, you can keep all of your meetings, but I could still

…… …… OK, you can keep all of your meetings, but I could still schedule a meeting jk+1!

That doesn’t make any sense. If there is such a meeting, I must consider

That doesn’t make any sense. If there is such a meeting, I must consider it after ik, and I would have scheduled that meeting! …… …… OK, you win.

The Encoding Problem (Hoffman Tree) • Problem: Given a long string with n different

The Encoding Problem (Hoffman Tree) • Problem: Given a long string with n different characters in alphabet, find a way to encode these characters into binary codes that minimizes the length. • Example: “aababc”, n = 3, alphabet = {a, b, c} • If a = ‘ 0’, b = ’ 11’, c = ‘ 10’, then the string can be encoded as ‘ 001101110’.

Why don’t just use ASCII? • Different letters appear with very different frequency. •

Why don’t just use ASCII? • Different letters appear with very different frequency. • Using an encoding of varying length can save space. • Now of course memory/disk are cheap, but some communications can be expensive.

What kind of encodings are allowed? • Bad example: If a = ‘ 0’,

What kind of encodings are allowed? • Bad example: If a = ‘ 0’, b = ’ 01’, c = ‘ 1’ • Given encoding 00010011, it can be ‘aababc’, but can also be ‘aaacabc’ (and others) • Want an encoding that allow unique decoding. • Sufficient condition: Prefix free encoding • Definition: No code should be a prefix of another code. • (In previous example, encoding of a is a prefix for encoding of b. )

Prefix-free encoding vs. Trees 0 a = 00 b = 01 c = 110

Prefix-free encoding vs. Trees 0 a = 00 b = 01 c = 110 d = 111 0 a 1 1 1 b 0 c Character = Leaves of the Tree Encoding = Path from root to the character Prefix free = no characters for intermediate nodes 1 d

Cost of the Tree 0 0 a Cost = sum of Frequency * Depth

Cost of the Tree 0 0 a Cost = sum of Frequency * Depth = 5*2 + 10*2 + 3*3 + 6*3 = 57 1 1 1 b 0 5 10 c 3 1 d 6

Hoffman Tree problem • Given a long string with n different characters in alphabet,

Hoffman Tree problem • Given a long string with n different characters in alphabet, find a way to encode these characters into binary codes that minimizes the length. • Given an alphabet of n characters, character i appears wi times, find an encoding tree with minimum cost.

Algorithm Hoffman Tree 1. REPEAT 2. Select two characters with smallest frequencies 3. Merge

Algorithm Hoffman Tree 1. REPEAT 2. Select two characters with smallest frequencies 3. Merge them into a new character, whose frequency is the sum 4. UNTIL there is only one character abcd 24 acd 14 ac 8 a b c d 5 10 3 6

Proof Sketch • Use induction: Assume algorithm finds the optimal solution for alphabet of

Proof Sketch • Use induction: Assume algorithm finds the optimal solution for alphabet of size n, we want to prove that it works for alphabet of size n+1. • Base Case: n = 1 is trivial. (cost = 0 in this case)

Induction Step i j First merged i, j (characters with lowest frequency) i j

Induction Step i j First merged i, j (characters with lowest frequency) i j i, j may be in different branches Goal: Convince OPT that it is OK to merge i, j first.

Induction Step i j j i After ALG and OPT both merged i, j,

Induction Step i j j i After ALG and OPT both merged i, j, it reduces to a problem of size n! Induction hypothesis ALG is optimal, OPT cannot be better.