Top Banner
CSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo
298

Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

Apr 27, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

CSE 431/531: Algorithm Analysis and Design (Spring 2020)

Greedy Algorithms

Lecturer: Shi Li

Department of Computer Science and EngineeringUniversity at Buffalo

Page 2: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

2/68

Def. In an optimization problem, our goal of is to find a validsolution with the minimum cost (or maximum value).

Trivial Algorithm for an Optimization Problem

Enumerate all valid solutions, compare them and output the bestone.

However, trivial algorithm often runs in exponential time, as thenumber of potential solutions is often exponentially large.

f(n) is a polynomial if f(n) = O(nk) for some constant k > 0.

convention: polynomial time = efficient

Goals of algorithm design

1 Design efficient algorithms to solve problems

2 Design more efficient algorithms to solve problems

Page 3: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

2/68

Def. In an optimization problem, our goal of is to find a validsolution with the minimum cost (or maximum value).

Trivial Algorithm for an Optimization Problem

Enumerate all valid solutions, compare them and output the bestone.

However, trivial algorithm often runs in exponential time, as thenumber of potential solutions is often exponentially large.

f(n) is a polynomial if f(n) = O(nk) for some constant k > 0.

convention: polynomial time = efficient

Goals of algorithm design

1 Design efficient algorithms to solve problems

2 Design more efficient algorithms to solve problems

Page 4: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

2/68

Def. In an optimization problem, our goal of is to find a validsolution with the minimum cost (or maximum value).

Trivial Algorithm for an Optimization Problem

Enumerate all valid solutions, compare them and output the bestone.

However, trivial algorithm often runs in exponential time, as thenumber of potential solutions is often exponentially large.

f(n) is a polynomial if f(n) = O(nk) for some constant k > 0.

convention: polynomial time = efficient

Goals of algorithm design

1 Design efficient algorithms to solve problems

2 Design more efficient algorithms to solve problems

Page 5: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

2/68

Def. In an optimization problem, our goal of is to find a validsolution with the minimum cost (or maximum value).

Trivial Algorithm for an Optimization Problem

Enumerate all valid solutions, compare them and output the bestone.

However, trivial algorithm often runs in exponential time, as thenumber of potential solutions is often exponentially large.

f(n) is a polynomial if f(n) = O(nk) for some constant k > 0.

convention: polynomial time = efficient

Goals of algorithm design

1 Design efficient algorithms to solve problems

2 Design more efficient algorithms to solve problems

Page 6: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

2/68

Def. In an optimization problem, our goal of is to find a validsolution with the minimum cost (or maximum value).

Trivial Algorithm for an Optimization Problem

Enumerate all valid solutions, compare them and output the bestone.

However, trivial algorithm often runs in exponential time, as thenumber of potential solutions is often exponentially large.

f(n) is a polynomial if f(n) = O(nk) for some constant k > 0.

convention: polynomial time = efficient

Goals of algorithm design

1 Design efficient algorithms to solve problems

2 Design more efficient algorithms to solve problems

Page 7: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

2/68

Def. In an optimization problem, our goal of is to find a validsolution with the minimum cost (or maximum value).

Trivial Algorithm for an Optimization Problem

Enumerate all valid solutions, compare them and output the bestone.

However, trivial algorithm often runs in exponential time, as thenumber of potential solutions is often exponentially large.

f(n) is a polynomial if f(n) = O(nk) for some constant k > 0.

convention: polynomial time = efficient

Goals of algorithm design

1 Design efficient algorithms to solve problems

2 Design more efficient algorithms to solve problems

Page 8: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

2/68

Def. In an optimization problem, our goal of is to find a validsolution with the minimum cost (or maximum value).

Trivial Algorithm for an Optimization Problem

Enumerate all valid solutions, compare them and output the bestone.

However, trivial algorithm often runs in exponential time, as thenumber of potential solutions is often exponentially large.

f(n) is a polynomial if f(n) = O(nk) for some constant k > 0.

convention: polynomial time = efficient

Goals of algorithm design

1 Design efficient algorithms to solve problems

2 Design more efficient algorithms to solve problems

Page 9: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

2/68

Def. In an optimization problem, our goal of is to find a validsolution with the minimum cost (or maximum value).

Trivial Algorithm for an Optimization Problem

Enumerate all valid solutions, compare them and output the bestone.

However, trivial algorithm often runs in exponential time, as thenumber of potential solutions is often exponentially large.

f(n) is a polynomial if f(n) = O(nk) for some constant k > 0.

convention: polynomial time = efficient

Goals of algorithm design

1 Design efficient algorithms to solve problems

2 Design more efficient algorithms to solve problems

Page 10: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

2/68

Def. In an optimization problem, our goal of is to find a validsolution with the minimum cost (or maximum value).

Trivial Algorithm for an Optimization Problem

Enumerate all valid solutions, compare them and output the bestone.

However, trivial algorithm often runs in exponential time, as thenumber of potential solutions is often exponentially large.

f(n) is a polynomial if f(n) = O(nk) for some constant k > 0.

convention: polynomial time = efficient

Goals of algorithm design

1 Design efficient algorithms to solve problems

2 Design more efficient algorithms to solve problems

Page 11: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

3/68

Common Paradigms for Algorithm Design

Greedy Algorithms

Divide and Conquer

Dynamic Programming

Greedy algorithms are often for optimization problems.

They often run in polynomial time due to their simplicity.

Page 12: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

3/68

Common Paradigms for Algorithm Design

Greedy Algorithms

Divide and Conquer

Dynamic Programming

Greedy algorithms are often for optimization problems.

They often run in polynomial time due to their simplicity.

Page 13: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

3/68

Common Paradigms for Algorithm Design

Greedy Algorithms

Divide and Conquer

Dynamic Programming

Greedy algorithms are often for optimization problems.

They often run in polynomial time due to their simplicity.

Page 14: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

4/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

(key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

(usuallyeasy)

Def. A strategy is safe: there is always an optimum solution thatagrees with the decision made according to the strategy.

Page 15: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

4/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

(key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

(usuallyeasy)

Def. A strategy is safe: there is always an optimum solution thatagrees with the decision made according to the strategy.

Page 16: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

4/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Def. A strategy is safe: there is always an optimum solution thatagrees with the decision made according to the strategy.

Page 17: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

4/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Def. A strategy is safe: there is always an optimum solution thatagrees with the decision made according to the strategy.

Page 18: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

5/68

Outline

1 Toy Example: Box Packing

2 Interval Scheduling

3 Offline Caching

4 Data Compression and Huffman Code

5 Summary

Page 19: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

6/68

Box Packing

Input: n boxes of capacities c1, c2, · · · , cnm items of sizes s1, s2, · · · , smCan put at most 1 item in a box

Item j can be put into box i if sj ≤ ci

Output: A way to put as many items as possible in the boxes.

Example:

Box capacities: 60, 40, 25, 15, 12

Item sizes: 45, 42, 20, 19, 16

Can put 3 items in boxes: 45→ 60, 20→ 40, 19→ 25

Page 20: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

6/68

Box Packing

Input: n boxes of capacities c1, c2, · · · , cnm items of sizes s1, s2, · · · , smCan put at most 1 item in a box

Item j can be put into box i if sj ≤ ci

Output: A way to put as many items as possible in the boxes.

Example:

Box capacities: 60, 40, 25, 15, 12

Item sizes: 45, 42, 20, 19, 16

Can put 3 items in boxes: 45→ 60, 20→ 40, 19→ 25

Page 21: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

7/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Designing a Reasonable Strategy for Box Packing

Q: Take box 1. Which item should we put in box 1?

A: The item of the largest size that can be put into the box.

Page 22: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

7/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Designing a Reasonable Strategy for Box Packing

Q: Take box 1. Which item should we put in box 1?

A: The item of the largest size that can be put into the box.

Page 23: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

7/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Designing a Reasonable Strategy for Box Packing

Q: Take box 1. Which item should we put in box 1?

A: The item of the largest size that can be put into the box.

Page 24: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

8/68

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Lemma The strategy that put into box 1 the largest item it canhold is “safe”: There is an optimum solution in which box 1 containsthe largest item it can hold.

Intuition: putting the item gives us the easiest residual problem.

formal proof via exchanging argument:

Page 25: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

8/68

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Lemma The strategy that put into box 1 the largest item it canhold is “safe”: There is an optimum solution in which box 1 containsthe largest item it can hold.

Intuition: putting the item gives us the easiest residual problem.

formal proof via exchanging argument:

Page 26: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

8/68

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Lemma The strategy that put into box 1 the largest item it canhold is “safe”: There is an optimum solution in which box 1 containsthe largest item it can hold.

Intuition: putting the item gives us the easiest residual problem.

formal proof via exchanging argument:

Page 27: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

8/68

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Lemma The strategy that put into box 1 the largest item it canhold is “safe”: There is an optimum solution in which box 1 containsthe largest item it can hold.

Intuition: putting the item gives us the easiest residual problem.

formal proof via exchanging argument:

Page 28: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

9/68

Lemma There is an optimum solution in which box 1 contains thelargest item it can hold.

Proof.

Let j = largest item that box 1 can hold.

Take any optimum solution S. If j is put into Box 1 in S, done.

Otherwise, assume this is what happens in S:

box 1

item j

· · · · · ·S:

sj′ ≤ sj, and swapping gives another solution S ′

S ′ is also an optimum solution. In S ′, j is put into Box 1.

Page 29: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

9/68

Lemma There is an optimum solution in which box 1 contains thelargest item it can hold.

Proof.

Let j = largest item that box 1 can hold.

Take any optimum solution S. If j is put into Box 1 in S, done.

Otherwise, assume this is what happens in S:

box 1

item j

· · · · · ·S:

sj′ ≤ sj, and swapping gives another solution S ′

S ′ is also an optimum solution. In S ′, j is put into Box 1.

Page 30: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

9/68

Lemma There is an optimum solution in which box 1 contains thelargest item it can hold.

Proof.

Let j = largest item that box 1 can hold.

Take any optimum solution S. If j is put into Box 1 in S, done.

Otherwise, assume this is what happens in S:

box 1

item j

· · · · · ·S:

sj′ ≤ sj, and swapping gives another solution S ′

S ′ is also an optimum solution. In S ′, j is put into Box 1.

Page 31: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

9/68

Lemma There is an optimum solution in which box 1 contains thelargest item it can hold.

Proof.

Let j = largest item that box 1 can hold.

Take any optimum solution S. If j is put into Box 1 in S, done.

Otherwise, assume this is what happens in S:

box 1

item j

· · · · · ·S:

sj′ ≤ sj, and swapping gives another solution S ′

S ′ is also an optimum solution. In S ′, j is put into Box 1.

Page 32: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

9/68

Lemma There is an optimum solution in which box 1 contains thelargest item it can hold.

Proof.

Let j = largest item that box 1 can hold.

Take any optimum solution S. If j is put into Box 1 in S, done.

Otherwise, assume this is what happens in S:

box 1

item j

· · · · · ·S′:

item j′

sj′ ≤ sj, and swapping gives another solution S ′

S ′ is also an optimum solution. In S ′, j is put into Box 1.

Page 33: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

9/68

Lemma There is an optimum solution in which box 1 contains thelargest item it can hold.

Proof.

Let j = largest item that box 1 can hold.

Take any optimum solution S. If j is put into Box 1 in S, done.

Otherwise, assume this is what happens in S:

box 1

item j

· · · · · ·S′:

item j′

sj′ ≤ sj, and swapping gives another solution S ′

S ′ is also an optimum solution. In S ′, j is put into Box 1.

Page 34: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

10/68

Notice that the exchanging operation is only for the sake ofanalysis; it is not a part of the algorithm.

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Trivial: we decided to put Item j into Box 1, and the remaininginstance is obtained by removing Item j and Box 1.

Page 35: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

10/68

Notice that the exchanging operation is only for the sake ofanalysis; it is not a part of the algorithm.

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Trivial: we decided to put Item j into Box 1, and the remaininginstance is obtained by removing Item j and Box 1.

Page 36: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

10/68

Notice that the exchanging operation is only for the sake ofanalysis; it is not a part of the algorithm.

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Trivial: we decided to put Item j into Box 1, and the remaininginstance is obtained by removing Item j and Box 1.

Page 37: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

11/68

Generic Greedy Algorithm

1 while the instance is non-trivial do

2 make the choice using the greedy strategy

3 reduce the instance

Greedy Algorithm for Box Packing

1 T ← {1, 2, 3, · · · ,m}2 for i← 1 to n do

3 if some item in T can be put into box i, then

4 j ← the largest item in T that can be put into box i

5 print(“put item j in box i”)

6 T ← T \ {j}

Page 38: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

12/68

Generic Greedy Algorithm

1 while the instance is non-trivial do

2 make the choice using the greedy strategy

3 reduce the instance

Lemma Generic algorithm is correct if and only if the greedystrategy is safe.

Greedy strategy is safe: we will not miss the optimum solution

Greedy stretegy is not safe: we will miss the optimum solutionfor some instance, since the choices we made are irrevocable.

Page 39: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

12/68

Generic Greedy Algorithm

1 while the instance is non-trivial do

2 make the choice using the greedy strategy

3 reduce the instance

Lemma Generic algorithm is correct if and only if the greedystrategy is safe.

Greedy strategy is safe: we will not miss the optimum solution

Greedy stretegy is not safe: we will miss the optimum solutionfor some instance, since the choices we made are irrevocable.

Page 40: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

12/68

Generic Greedy Algorithm

1 while the instance is non-trivial do

2 make the choice using the greedy strategy

3 reduce the instance

Lemma Generic algorithm is correct if and only if the greedystrategy is safe.

Greedy strategy is safe: we will not miss the optimum solution

Greedy stretegy is not safe: we will miss the optimum solutionfor some instance, since the choices we made are irrevocable.

Page 41: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

13/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Def. A strategy is “safe” if there is always an optimum solutionthat is “consistent” with the decision made according to thestrategy.

Page 42: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

13/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Def. A strategy is “safe” if there is always an optimum solutionthat is “consistent” with the decision made according to thestrategy.

Page 43: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

13/68

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe”

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem

Def. A strategy is “safe” if there is always an optimum solutionthat is “consistent” with the decision made according to thestrategy.

Page 44: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

14/68

Exchange argument: Proof of Safety of a Strategy

let S be an arbitrary optimum solution.

if S is consistent with the greedy choice, done.

otherwise, show that it can be modified to another optimumsolution S ′ that is consistent with the choice.

The procedure is not a part of the algorithm.

Page 45: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

14/68

Exchange argument: Proof of Safety of a Strategy

let S be an arbitrary optimum solution.

if S is consistent with the greedy choice, done.

otherwise, show that it can be modified to another optimumsolution S ′ that is consistent with the choice.

The procedure is not a part of the algorithm.

Page 46: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

15/68

Outline

1 Toy Example: Box Packing

2 Interval Scheduling

3 Offline Caching

4 Data Compression and Huffman Code

5 Summary

Page 47: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

16/68

Interval Scheduling

Input: n jobs, job i with start time si and finish time fi

i and j are compatible if [si, fi) and [sj, fj) are disjoint

Output: A maximum-size subset of mutually compatible jobs

0 1 2 3 4 5 6 7 8 9

Page 48: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

16/68

Interval Scheduling

Input: n jobs, job i with start time si and finish time fi

i and j are compatible if [si, fi) and [sj, fj) are disjoint

Output: A maximum-size subset of mutually compatible jobs

0 1 2 3 4 5 6 7 8 9

Page 49: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

17/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?

Schedule the job with the smallest size?

No!

Page 50: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

17/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?

Schedule the job with the smallest size?

No!

Page 51: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

17/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?

Schedule the job with the smallest size? No!

Page 52: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

17/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?

Schedule the job with the smallest size? No!

0 1 2 3 4 5 6 7 8 9

Page 53: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

17/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?

Schedule the job with the smallest size? No!

0 1 2 3 4 5 6 7 8 9

Page 54: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

17/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?

Schedule the job with the smallest size? No!

0 1 2 3 4 5 6 7 8 9

Page 55: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

18/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!

Schedule the job conflicting with smallest number of other jobs?

No!

Page 56: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

18/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?

No!

Page 57: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

18/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!

Page 58: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

18/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!

0 1 2 3 4 5 6 7 8 9

Page 59: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

18/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!

0 1 2 3 4 5 6 7 8 9

Page 60: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

18/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!

0 1 2 3 4 5 6 7 8 9

Page 61: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

18/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!

0 1 2 3 4 5 6 7 8 9

Page 62: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

19/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!

Schedule the job with the earliest finish time?

Yes!

Page 63: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

19/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!Schedule the job with the earliest finish time?

Yes!

Page 64: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

19/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!Schedule the job with the earliest finish time? Yes!

Page 65: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

19/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!Schedule the job with the earliest finish time? Yes!

0 1 2 3 4 5 6 7 8 9

Page 66: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

19/68

Greedy Algorithm for Interval Scheduling

Which of the following strategies are safe?Schedule the job with the smallest size? No!Schedule the job conflicting with smallest number of other jobs?No!Schedule the job with the earliest finish time? Yes!

0 1 2 3 4 5 6 7 8 9

Page 67: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

20/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

Proof.

Take an arbitrary optimum solution S

If it contains j, done

Otherwise, replace the first job in S with j to obtain anotheroptimum schedule S ′.

Page 68: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

20/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

Proof.

Take an arbitrary optimum solution S

If it contains j, done

Otherwise, replace the first job in S with j to obtain anotheroptimum schedule S ′.

S:

Page 69: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

20/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

Proof.

Take an arbitrary optimum solution S

If it contains j, done

Otherwise, replace the first job in S with j to obtain anotheroptimum schedule S ′.

S:

Page 70: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

20/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

Proof.

Take an arbitrary optimum solution S

If it contains j, done

Otherwise, replace the first job in S with j to obtain anotheroptimum schedule S ′.

S:

j:

Page 71: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

20/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

Proof.

Take an arbitrary optimum solution S

If it contains j, done

Otherwise, replace the first job in S with j to obtain anotheroptimum schedule S ′.

S:

j:

Page 72: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

20/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

Proof.

Take an arbitrary optimum solution S

If it contains j, done

Otherwise, replace the first job in S with j to obtain anotheroptimum schedule S ′.

S:

j:

S ′:

Page 73: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

21/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

What is the remaining task after we decided to schedule j?Is it another instance of interval scheduling problem?

Yes!

0 1 2 3 4 5 6 7 8 9

j

Page 74: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

21/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

What is the remaining task after we decided to schedule j?Is it another instance of interval scheduling problem? Yes!

0 1 2 3 4 5 6 7 8 9

j

Page 75: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

21/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

What is the remaining task after we decided to schedule j?Is it another instance of interval scheduling problem? Yes!

0 1 2 3 4 5 6 7 8 9

j

Page 76: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

21/68

Greedy Algorithm for Interval Scheduling

Lemma It is safe to schedule the job j with the earliest finish time:There is an optimum solution where the job j with the earliest finishtime is scheduled.

What is the remaining task after we decided to schedule j?Is it another instance of interval scheduling problem? Yes!

0 1 2 3 4 5 6 7 8 9

Page 77: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

22/68

Greedy Algorithm for Interval Scheduling

Schedule(s, f, n)

1 A← {1, 2, · · · , n}, S ← ∅2 while A 6= ∅3 j ← argminj′∈A fj′

4 S ← S ∪ {j}; A← {j′ ∈ A : sj′ ≥ fj}5 return S

Page 78: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

22/68

Greedy Algorithm for Interval Scheduling

Schedule(s, f, n)

1 A← {1, 2, · · · , n}, S ← ∅2 while A 6= ∅3 j ← argminj′∈A fj′

4 S ← S ∪ {j}; A← {j′ ∈ A : sj′ ≥ fj}5 return S

0 1 2 3 4 5 6 7 8 9

Page 79: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

22/68

Greedy Algorithm for Interval Scheduling

Schedule(s, f, n)

1 A← {1, 2, · · · , n}, S ← ∅2 while A 6= ∅3 j ← argminj′∈A fj′

4 S ← S ∪ {j}; A← {j′ ∈ A : sj′ ≥ fj}5 return S

0 1 2 3 4 5 6 7 8 9

Page 80: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

22/68

Greedy Algorithm for Interval Scheduling

Schedule(s, f, n)

1 A← {1, 2, · · · , n}, S ← ∅2 while A 6= ∅3 j ← argminj′∈A fj′

4 S ← S ∪ {j}; A← {j′ ∈ A : sj′ ≥ fj}5 return S

0 1 2 3 4 5 6 7 8 9

Page 81: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

22/68

Greedy Algorithm for Interval Scheduling

Schedule(s, f, n)

1 A← {1, 2, · · · , n}, S ← ∅2 while A 6= ∅3 j ← argminj′∈A fj′

4 S ← S ∪ {j}; A← {j′ ∈ A : sj′ ≥ fj}5 return S

0 1 2 3 4 5 6 7 8 9

Page 82: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

22/68

Greedy Algorithm for Interval Scheduling

Schedule(s, f, n)

1 A← {1, 2, · · · , n}, S ← ∅2 while A 6= ∅3 j ← argminj′∈A fj′

4 S ← S ∪ {j}; A← {j′ ∈ A : sj′ ≥ fj}5 return S

0 1 2 3 4 5 6 7 8 9

Page 83: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

23/68

Greedy Algorithm for Interval Scheduling

Schedule(s, f, n)

1 A← {1, 2, · · · , n}, S ← ∅2 while A 6= ∅3 j ← argminj′∈A fj′

4 S ← S ∪ {j}; A← {j′ ∈ A : sj′ ≥ fj}5 return S

Running time of algorithm?

Naive implementation: O(n2) time

Clever implementation: O(n lg n) time

Page 84: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

23/68

Greedy Algorithm for Interval Scheduling

Schedule(s, f, n)

1 A← {1, 2, · · · , n}, S ← ∅2 while A 6= ∅3 j ← argminj′∈A fj′

4 S ← S ∪ {j}; A← {j′ ∈ A : sj′ ≥ fj}5 return S

Running time of algorithm?

Naive implementation: O(n2) time

Clever implementation: O(n lg n) time

Page 85: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

23/68

Greedy Algorithm for Interval Scheduling

Schedule(s, f, n)

1 A← {1, 2, · · · , n}, S ← ∅2 while A 6= ∅3 j ← argminj′∈A fj′

4 S ← S ∪ {j}; A← {j′ ∈ A : sj′ ≥ fj}5 return S

Running time of algorithm?

Naive implementation: O(n2) time

Clever implementation: O(n lg n) time

Page 86: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 87: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 88: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 89: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 90: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 91: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 92: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 93: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 94: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 95: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 96: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 97: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 98: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

t

7

9

Page 99: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

7

t

9

Page 100: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

7

t

9

Page 101: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

7

t

9

Page 102: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

7

t

9

Page 103: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

24/68

Clever Implementation of Greedy Algorithm

Schedule(s, f, n)

1 sort jobs according to f values

2 t← 0, S ← ∅3 for every j ∈ [n] according to non-decreasing order of fj4 if sj ≥ t then

5 S ← S ∪ {j}6 t← fj7 return S

0 1 2 3 4 5 6 7 8 9

2

3

4

5

6

8

1

7

9

t

Page 104: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

25/68

Outline

1 Toy Example: Box Packing

2 Interval Scheduling

3 Offline Caching

4 Data Compression and Huffman Code

5 Summary

Page 105: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

26/68

Offline Caching: Example

Page 106: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

Page 107: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

Page 108: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

Page 109: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

Page 110: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

Page 111: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

Page 112: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

Page 113: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

Page 114: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

Page 115: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

Page 116: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

Page 117: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

Page 118: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

Page 119: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

Page 120: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

4 2 3

Page 121: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

4 2 3

Page 122: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

4 2 3

4 2 3

Page 123: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

4 2 3

4 2 3

Page 124: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

4 2 3

4 2 3

1 2 3

Page 125: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

4 2 3

4 2 3

1 2 3

misses = 7

Page 126: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

27/68

Offline Caching

Cache that can store k pages

Sequence of page requests

Cache miss happens ifrequested page not in cache.We need bring the page intocache, and evict some existingpage if necessary.

Cache hit happens ifrequested page already incache.

Goal: minimize the number ofcache misses.

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

4 2 3

4 2 3

1 2 3

misses = 7

Page 127: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

28/68

A Better Solution for Example

page

1

5

4

2

5

3

2

1

sequence

cache

1

5

4

4 2

4 2 5

4 2 3

4 2 3

1 2 3

misses = 7

1

5

5 4

5 4 2

5 24

5 23

5 23

1 23

misses = 6

cache

Page 128: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

29/68

Offline Caching Problem

Input: k : the size of cache

n : number of pages

ρ1, ρ2, ρ3, · · · , ρT ∈ [n]: sequence of requests

Output: i1, i2, i3, · · · , iT ∈ {hit, empty} ∪ [n]: indices of pages toevict (“hit” means evicting no page, “empty” meansevicting empty page)

We use [n] for {1, 2, 3, · · · , n}.

Offline Caching: we know the whole sequence ahead of time.

Online Caching: we have to make decisions on the fly, beforeseeing future requests.

Q: Which one is more realistic?

A: Online caching

Page 129: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

29/68

Offline Caching Problem

Input: k : the size of cache

n : number of pages

ρ1, ρ2, ρ3, · · · , ρT ∈ [n]: sequence of requests

Output: i1, i2, i3, · · · , iT ∈ {hit, empty} ∪ [n]: indices of pages toevict (“hit” means evicting no page, “empty” meansevicting empty page)

We use [n] for {1, 2, 3, · · · , n}.

Offline Caching: we know the whole sequence ahead of time.

Online Caching: we have to make decisions on the fly, beforeseeing future requests.

Q: Which one is more realistic?

A: Online caching

Page 130: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

29/68

Offline Caching Problem

Input: k : the size of cache

n : number of pages

ρ1, ρ2, ρ3, · · · , ρT ∈ [n]: sequence of requests

Output: i1, i2, i3, · · · , iT ∈ {hit, empty} ∪ [n]: indices of pages toevict (“hit” means evicting no page, “empty” meansevicting empty page)

We use [n] for {1, 2, 3, · · · , n}.

Offline Caching: we know the whole sequence ahead of time.

Online Caching: we have to make decisions on the fly, beforeseeing future requests.

Q: Which one is more realistic?

A: Online caching

Page 131: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

29/68

Offline Caching Problem

Input: k : the size of cache

n : number of pages

ρ1, ρ2, ρ3, · · · , ρT ∈ [n]: sequence of requests

Output: i1, i2, i3, · · · , iT ∈ {hit, empty} ∪ [n]: indices of pages toevict (“hit” means evicting no page, “empty” meansevicting empty page)

We use [n] for {1, 2, 3, · · · , n}.

Offline Caching: we know the whole sequence ahead of time.

Online Caching: we have to make decisions on the fly, beforeseeing future requests.

Q: Which one is more realistic?

A: Online caching

Page 132: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

30/68

Offline Caching: we know the whole sequence ahead of time.

Online Caching: we have to make decisions on the fly, beforeseeing future requests.

Q: Which one is more realistic?

A: Online caching

Q: Why do we study the offline caching problem?

A: Use the offline solution as a benchmark to measure the“competitive ratio” of online algorithms

Page 133: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

30/68

Offline Caching: we know the whole sequence ahead of time.

Online Caching: we have to make decisions on the fly, beforeseeing future requests.

Q: Which one is more realistic?

A: Online caching

Q: Why do we study the offline caching problem?

A: Use the offline solution as a benchmark to measure the“competitive ratio” of online algorithms

Page 134: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

31/68

Offline Caching: Potential Greedy Algorithms

FIFO(First-In-First-Out): always evict the first page in cache

LRU(Least-Recently-Used): Evict page whose most recentaccess was earliest

LFU(Least-Frequently-Used): Evict page that was leastfrequently requested

All the above algorithms are not optimum!

Indeed all the algorithms are “online”, i.e, the decisions can bemade without knowing future requests. Online algorithms cannot be optimum.

Page 135: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

31/68

Offline Caching: Potential Greedy Algorithms

FIFO(First-In-First-Out): always evict the first page in cache

LRU(Least-Recently-Used): Evict page whose most recentaccess was earliest

LFU(Least-Frequently-Used): Evict page that was leastfrequently requested

All the above algorithms are not optimum!

Indeed all the algorithms are “online”, i.e, the decisions can bemade without knowing future requests. Online algorithms cannot be optimum.

Page 136: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

31/68

Offline Caching: Potential Greedy Algorithms

FIFO(First-In-First-Out): always evict the first page in cache

LRU(Least-Recently-Used): Evict page whose most recentaccess was earliest

LFU(Least-Frequently-Used): Evict page that was leastfrequently requested

All the above algorithms are not optimum!

Indeed all the algorithms are “online”, i.e, the decisions can bemade without knowing future requests. Online algorithms cannot be optimum.

Page 137: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

31/68

Offline Caching: Potential Greedy Algorithms

FIFO(First-In-First-Out): always evict the first page in cache

LRU(Least-Recently-Used): Evict page whose most recentaccess was earliest

LFU(Least-Frequently-Used): Evict page that was leastfrequently requested

All the above algorithms are not optimum!

Indeed all the algorithms are “online”, i.e, the decisions can bemade without knowing future requests. Online algorithms cannot be optimum.

Page 138: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

Page 139: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

Page 140: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

Page 141: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

Page 142: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

1 2

Page 143: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

1 2

Page 144: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

1 2

1 2 3

Page 145: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

1 2

1 2 3

Page 146: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

1 2

1 2 3

4 2 3

Page 147: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

1 2

1 2 3

4 2 3

Page 148: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

1 2

1 2 3

4 2 3

4 1 3

Page 149: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

1 2

1 2 3

4 2 3

4 1 3

misses = 5

Page 150: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

32/68

FIFO is not optimum

requests

1

2

3

4

1

FIFO

1

1 2

1 2 3

4 2 3

4 1 3

misses = 5

1 4 3

1 4 3

misses = 4

1

1 2

1 2 3

Furthest-in-Future

Page 151: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

33/68

Optimum Offline Caching

Furthest-in-Future (FF)

Algorithm: every time, evict the item that is not requested untilfurthest in the future, if we need to evict one.

The algorithm is not an online algorithm, since the decision at astep depends on the request sequence in the future.

Page 152: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

34/68

Furthest-in-Future (FF)

requests

1

2

3

4

1

FIFO

1

1 2

1 2 3

4 2 3

4 1 3

misses = 5

1 4 3

1 4 3

misses = 4

1

1 2

1 2 3

Furthest-in-Future

Page 153: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5 3

Page 154: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

3

Page 155: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

3

Page 156: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

3

Page 157: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

3

Page 158: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

3

Page 159: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

3

Page 160: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

2

3

4

3

Page 161: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

2

3

4

3

Page 162: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

2

3

4

2

3

4

3

Page 163: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

2

3

4

2

3

4

2

3

4

3

Page 164: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

2

3

4

2

3

4

2

3

4

2

3

4

3

Page 165: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

2

3

4

2

3

4

2

3

4

2

3

4

3

Page 166: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

2

3

4

2

3

4

2

3

4

2

3

4

3

1

3

4

Page 167: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

2

3

4

2

3

4

2

3

4

2

3

4

3

1

3

4

5

3

4

Page 168: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

35/68

Example

requests

1 5 4 2 5 3 2 134 5

1 1

5

1

5

4

2

5

4

2

5

4

2

3

4

2

3

4

2

3

4

2

3

4

3

1

3

4

5

3

4

5

3

4

Page 169: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

36/68

Recall: Designing and Analyzing Greedy Algorithms

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Page 170: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

36/68

Recall: Designing and Analyzing Greedy Algorithms

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Page 171: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

37/68

Offline Caching Problem

Input: k : the size of cache

n : number of pages

ρ1, ρ2, ρ3, · · · , ρT ∈ [n]: sequence of requests

Output: i1, i2, i3, · · · , it ∈ {hit, empty} ∪ [n]

empty stands for an empty page“hit” means evicting no pages

Page 172: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

37/68

Offline Caching Problem

Input: k : the size of cache

n : number of pages

ρ1, ρ2, ρ3, · · · , ρT ∈ [n]: sequence of requests

p1, p2, · · · , pk ∈ {empty} ∪ [n]: initial set of pages incache

Output: i1, i2, i3, · · · , it ∈ {hit, empty} ∪ [n]

empty stands for an empty page“hit” means evicting no pages

Page 173: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

38/68

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Lemma Assume at time 1 a page fault happens and there are noempty pages in the cache. Let p∗ be the page in cache that is notrequested until furthest in the future. It is safe to evict p∗ at time 1.

Page 174: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

38/68

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Lemma Assume at time 1 a page fault happens and there are noempty pages in the cache. Let p∗ be the page in cache that is notrequested until furthest in the future. It is safe to evict p∗ at time 1.

Page 175: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

38/68

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Lemma Assume at time 1 a page fault happens and there are noempty pages in the cache. Let p∗ be the page in cache that is notrequested until furthest in the future. There is an optimum solutionin which p∗ is evicted at time 1.

Page 176: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

39/68

3

2

1

4 3

S :

21 ????

Proof.1 S: any optimum solution2 p∗: page in cache not requested until furthest in the future.

In the example, p∗ = 3.

3 Assume S evicts some p′ 6= p∗ at time 1; otherwise done.

In the example, p′ = 2.

Page 177: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

39/68

3

2

1

4 3

3

4

1

S :

21 ????

Proof.1 S: any optimum solution2 p∗: page in cache not requested until furthest in the future.

In the example, p∗ = 3.

3 Assume S evicts some p′ 6= p∗ at time 1; otherwise done.

In the example, p′ = 2.

Page 178: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

39/68

3

2

1

4 3

3

4

1

S :

2

Proof.1 S: any optimum solution2 p∗: page in cache not requested until furthest in the future.

In the example, p∗ = 3.

3 Assume S evicts some p′ 6= p∗ at time 1; otherwise done.

In the example, p′ = 2.

Page 179: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

2

Proof.

4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 180: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :1

2

4

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 181: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 182: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 183: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 184: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5 2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 185: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 186: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 187: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4 2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 188: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 189: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 190: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

6 2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 191: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

6

5

4

6

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 192: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

40/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

6

5

4

6

5

4

6

2

1

Proof.4 Create S ′. S ′ evicts p∗(=3) instead of p′(=2) at time 1.

5 After time 1, cache status of S and that of S ′ differ by only 1page. S contains p′(=2) and S contains p∗(=3).

6 From now on, S ′ will “copy” S.

Page 193: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

41/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

6

5

4

6

5

4

6

2

1

Proof.

7 If S evicted the page p′, S ′ will evict the page p∗. Then, thecache status of S and that of S ′ will be the same. S and S ′ willbe exactly the same from now on.

8 Assume S did not evict p′(=2) before we see p′(=2).

Page 194: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

41/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

6

5

4

6

5

4

6

2

1

Proof.7 If S evicted the page p′, S ′ will evict the page p∗. Then, the

cache status of S and that of S ′ will be the same. S and S ′ willbe exactly the same from now on.

8 Assume S did not evict p′(=2) before we see p′(=2).

Page 195: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

41/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

2

1

Proof.7 If S evicted the page p′, S ′ will evict the page p∗. Then, the

cache status of S and that of S ′ will be the same. S and S ′ willbe exactly the same from now on.

8 Assume S did not evict p′(=2) before we see p′(=2).

Page 196: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

41/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

1

Proof.7 If S evicted the page p′, S ′ will evict the page p∗. Then, the

cache status of S and that of S ′ will be the same. S and S ′ willbe exactly the same from now on.

8 Assume S did not evict p′(=2) before we see p′(=2).

Page 197: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

42/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

1

Proof.

9 If S evicts p∗(=3) for p′(=2), then S won’t be optimum.Assume otherwise.

10 So far, S ′ has 1 less page-miss than S does.

11 The status of S ′ and that of S only differ by 1 page.

Page 198: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

42/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

6

8

2

1

Proof.

9 If S evicts p∗(=3) for p′(=2), then S won’t be optimum.Assume otherwise.

10 So far, S ′ has 1 less page-miss than S does.

11 The status of S ′ and that of S only differ by 1 page.

Page 199: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

42/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

6

8

2

6

8

2

1

Proof.

9 If S evicts p∗(=3) for p′(=2), then S won’t be optimum.Assume otherwise.

10 So far, S ′ has 1 less page-miss than S does.

11 The status of S ′ and that of S only differ by 1 page.

Page 200: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

42/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

6

8

2

6

8

2

1

Proof.9 If S evicts p∗(=3) for p′(=2), then S won’t be optimum.

Assume otherwise.

10 So far, S ′ has 1 less page-miss than S does.

11 The status of S ′ and that of S only differ by 1 page.

Page 201: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

42/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

1

Proof.9 If S evicts p∗(=3) for p′(=2), then S won’t be optimum.

Assume otherwise.

10 So far, S ′ has 1 less page-miss than S does.

11 The status of S ′ and that of S only differ by 1 page.

Page 202: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

42/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

2

8

3

1

Proof.9 If S evicts p∗(=3) for p′(=2), then S won’t be optimum.

Assume otherwise.

10 So far, S ′ has 1 less page-miss than S does.

11 The status of S ′ and that of S only differ by 1 page.

Page 203: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

42/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

2

8

3

6

8

2

1

Proof.9 If S evicts p∗(=3) for p′(=2), then S won’t be optimum.

Assume otherwise.

10 So far, S ′ has 1 less page-miss than S does.

11 The status of S ′ and that of S only differ by 1 page.

Page 204: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

42/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

2

8

3

6

8

2

1

Proof.9 If S evicts p∗(=3) for p′(=2), then S won’t be optimum.

Assume otherwise.

10 So far, S ′ has 1 less page-miss than S does.

11 The status of S ′ and that of S only differ by 1 page.

Page 205: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

42/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

2

8

3

6

8

2

1

Proof.9 If S evicts p∗(=3) for p′(=2), then S won’t be optimum.

Assume otherwise.

10 So far, S ′ has 1 less page-miss than S does.

11 The status of S ′ and that of S only differ by 1 page.

Page 206: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

43/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

2

8

3

6

8

2

1

Proof.

12 We can then guarantee that S ′ make at most the same numberof page-misses as S does.

Idea: if S has a page-hit and S′ has a page-miss, we use theopportunity to make the status of S′ the same as that of S.

Page 207: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

43/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

2

8

3

6

8

2

1

Proof.12 We can then guarantee that S ′ make at most the same number

of page-misses as S does.

Idea: if S has a page-hit and S′ has a page-miss, we use theopportunity to make the status of S′ the same as that of S.

Page 208: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

43/68

3

2

1

4 3

3

4

1

S :

3

2

1

S′ :2

4

5

5

4

3

5

4

2

4

5

4

3

5

4

2

· · ·

6

8

3· · ·

6

8

2· · ·

· · ·

· · ·

2

2

8

3

6

8

2

1

Proof.12 We can then guarantee that S ′ make at most the same number

of page-misses as S does.

Idea: if S has a page-hit and S′ has a page-miss, we use theopportunity to make the status of S′ the same as that of S.

Page 209: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

44/68

Thus, we have shown how to create another solution S ′ with thesame number of page-misses as that of the optimum solution S.Thus, we proved

Lemma Assume at time 1 a page fault happens and there are noempty pages in the cache. Let p∗ be the page in cache that is notrequested until furthest in the future. There is an optimum solutionin which p∗ is evicted at time 1.

Theorem The furthest-in-future strategy is optimum.

Page 210: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

44/68

Thus, we have shown how to create another solution S ′ with thesame number of page-misses as that of the optimum solution S.Thus, we proved

Lemma Assume at time 1 a page fault happens and there are noempty pages in the cache. Let p∗ be the page in cache that is notrequested until furthest in the future. It is safe to evict p∗ at time 1.

Theorem The furthest-in-future strategy is optimum.

Page 211: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

44/68

Thus, we have shown how to create another solution S ′ with thesame number of page-misses as that of the optimum solution S.Thus, we proved

Lemma Assume at time 1 a page fault happens and there are noempty pages in the cache. Let p∗ be the page in cache that is notrequested until furthest in the future. It is safe to evict p∗ at time 1.

Theorem The furthest-in-future strategy is optimum.

Page 212: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

45/68

1 for t← 1 to T do

2 if ρt is in cache, then do nothing

3 else if there is an empty page in cache, then

4 evict the empty page and load ρt in cache

5 else

6 p∗ ← the page in cache that is not used furthest in thefuture

7 evict p∗ and load ρt in cache

Page 213: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

46/68

Q: How can we make the algorithm as fast as possible?

A:

The running time can be made to be O(n+ T log k).

For each page p, use a linked list to store the time steps in whichp is requested.

We can find the next time a page is requested easily.

Use a priority queue data structure to hold all the pages incache, so that we can easily find the page that is requestedfurthest in the future.

Page 214: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

46/68

Q: How can we make the algorithm as fast as possible?

A:

The running time can be made to be O(n+ T log k).

For each page p, use a linked list to store the time steps in whichp is requested.

We can find the next time a page is requested easily.

Use a priority queue data structure to hold all the pages incache, so that we can easily find the page that is requestedfurthest in the future.

Page 215: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

46/68

Q: How can we make the algorithm as fast as possible?

A:

The running time can be made to be O(n+ T log k).

For each page p, use a linked list to store the time steps in whichp is requested.

We can find the next time a page is requested easily.

Use a priority queue data structure to hold all the pages incache, so that we can easily find the page that is requestedfurthest in the future.

Page 216: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

46/68

Q: How can we make the algorithm as fast as possible?

A:

The running time can be made to be O(n+ T log k).

For each page p, use a linked list to store the time steps in whichp is requested.

We can find the next time a page is requested easily.

Use a priority queue data structure to hold all the pages incache, so that we can easily find the page that is requestedfurthest in the future.

Page 217: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

46/68

Q: How can we make the algorithm as fast as possible?

A:

The running time can be made to be O(n+ T log k).

For each page p, use a linked list to store the time steps in whichp is requested.

We can find the next time a page is requested easily.

Use a priority queue data structure to hold all the pages incache, so that we can easily find the page that is requestedfurthest in the future.

Page 218: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

47/68

1 for every p← 1 to n do

2 lists[p]← linked list of times in which p is requested, inincreasing order \\put ∞ at the end of the list

3 pointer[p]← head of lists[p]

4 nexttime[p]← value pointed by pointer[p]

5 Q← empty priority queue

6 for every t← 1 to T do

7 move pointer[ρt] to right by one position

8 nexttime[ρt]← value pointed by pointer[ρt]

9 if ρt ∈ Q then Q.update-priority(ρt, nexttime[ρt]), continue

10 if Q has size k then p← Q.extract-max() and evict p

11 load ρt12 add ρt to Q with priority value nexttime[ρt]

Page 219: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

48/68

Outline

1 Toy Example: Box Packing

2 Interval Scheduling

3 Offline Caching

4 Data Compression and Huffman Code

5 Summary

Page 220: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

49/68

Encoding Letters Using Bits

8 letters a, b, c, d, e, f, g, h in a language

need to encode a message using bits

idea: use 3 bits per letter

a b c d e f g h000 001 010 011 100 101 110 111

deacfg → 011100000010101110

Q: Can we have a better encoding scheme?

Seems unlikely: must use 3 bits per letter

Q: What if some letters appear more frequently than the others?

Page 221: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

50/68

Q: If some letters appear more frequently than the others, can wehave a better encoding scheme?

A: Using variable-length encoding scheme might be more efficient.

Idea

using fewer bits for letters that are more frequently used, andmore bits for letters that are less frequently used.

Page 222: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

51/68

Q: What is the issue with the following encoding scheme?

a: 0 b: 1 c: 00

A: Can not guarantee a unique decoding. For example, 00 can bedecoded to aa or c.

Solution

Use prefix codes to guarantee a unique decoding.

Page 223: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

51/68

Q: What is the issue with the following encoding scheme?

a: 0 b: 1 c: 00

A: Can not guarantee a unique decoding. For example, 00 can bedecoded to aa or c.

Solution

Use prefix codes to guarantee a unique decoding.

Page 224: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

51/68

Q: What is the issue with the following encoding scheme?

a: 0 b: 1 c: 00

A: Can not guarantee a unique decoding. For example, 00 can bedecoded to aa or c.

Solution

Use prefix codes to guarantee a unique decoding.

Page 225: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

52/68

Prefix Codes

Def. A prefix code for a set S of letters is a functionγ : S → {0, 1}∗ such that for two distinct x, y ∈ S, γ(x) is not aprefix of γ(y).

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

Page 226: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

52/68

Prefix Codes

Def. A prefix code for a set S of letters is a functionγ : S → {0, 1}∗ such that for two distinct x, y ∈ S, γ(x) is not aprefix of γ(y).

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

Page 227: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001001100000001011110100001001

cadbhhefca

Page 228: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001001100000001011110100001001

cadbhhefca

Page 229: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001001100000001011110100001001

cadbhhefca

Page 230: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001100000001011110100001001

c

adbhhefca

Page 231: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001/100000001011110100001001

ca

dbhhefca

Page 232: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001/100/000001011110100001001

cad

bhhefca

Page 233: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001/100/0000/01011110100001001

cadb

hhefca

Page 234: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001/100/0000/01/011110100001001

cadbh

hefca

Page 235: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001/100/0000/01/01/1110100001001

cadbhh

efca

Page 236: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001/100/0000/01/01/11/10100001001

cadbhhe

fca

Page 237: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001/100/0000/01/01/11/1010/0001001

cadbhhef

ca

Page 238: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001/100/0000/01/01/11/1010/0001/001

cadbhhefc

a

Page 239: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

53/68

Prefix Codes Guarantee Unique Decoding

Reason: there is only one way to cut the first code.

a b c d001 0000 0001 100

e f g h11 1010 1011 01

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

0001/001/100/0000/01/01/11/1010/0001/001/

cadbhhefca

Page 240: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

54/68

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

Properties of Encoding Tree

Rooted binary tree

Left edges labelled 0 and rightedges labelled 1

A leaf corresponds to a codefor some letter

If coding scheme is notwasteful: a non-leaf hasexactly two children

Best Prefix Codes

Input: frequencies of letters in a message

Output: prefix coding scheme with the shortest encoding for themessage

Page 241: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

54/68

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

Properties of Encoding Tree

Rooted binary tree

Left edges labelled 0 and rightedges labelled 1

A leaf corresponds to a codefor some letter

If coding scheme is notwasteful: a non-leaf hasexactly two children

Best Prefix Codes

Input: frequencies of letters in a message

Output: prefix coding scheme with the shortest encoding for themessage

Page 242: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

54/68

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

Properties of Encoding Tree

Rooted binary tree

Left edges labelled 0 and rightedges labelled 1

A leaf corresponds to a codefor some letter

If coding scheme is notwasteful: a non-leaf hasexactly two children

Best Prefix Codes

Input: frequencies of letters in a message

Output: prefix coding scheme with the shortest encoding for themessage

Page 243: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

54/68

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

Properties of Encoding Tree

Rooted binary tree

Left edges labelled 0 and rightedges labelled 1

A leaf corresponds to a codefor some letter

If coding scheme is notwasteful: a non-leaf hasexactly two children

Best Prefix Codes

Input: frequencies of letters in a message

Output: prefix coding scheme with the shortest encoding for themessage

Page 244: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

54/68

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

Properties of Encoding Tree

Rooted binary tree

Left edges labelled 0 and rightedges labelled 1

A leaf corresponds to a codefor some letter

If coding scheme is notwasteful: a non-leaf hasexactly two children

Best Prefix Codes

Input: frequencies of letters in a message

Output: prefix coding scheme with the shortest encoding for themessage

Page 245: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

54/68

0

0 1

1

b c

a

h

d

f

0 1

0 1

0 1

0 1

0 1

g

e

Properties of Encoding Tree

Rooted binary tree

Left edges labelled 0 and rightedges labelled 1

A leaf corresponds to a codefor some letter

If coding scheme is notwasteful: a non-leaf hasexactly two children

Best Prefix Codes

Input: frequencies of letters in a message

Output: prefix coding scheme with the shortest encoding for themessage

Page 246: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

55/68

example

letters a b c d efrequencies 18 3 4 6 10

scheme 1 length 2 3 3 2 2 total = 89scheme 2 length 1 3 3 3 3 total = 87scheme 3 length 1 4 4 3 2 total = 84

a d e

b c b c d e

a

b c

d

e

a

scheme 1 scheme 2 scheme 3

Page 247: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

55/68

example

letters a b c d efrequencies 18 3 4 6 10

scheme 1 length 2 3 3 2 2 total = 89scheme 2 length 1 3 3 3 3 total = 87scheme 3 length 1 4 4 3 2 total = 84

a d e

b c b c d e

a

b c

d

e

a

scheme 1 scheme 2 scheme 3

Page 248: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

56/68

Example Input: (a: 18, b: 3, c: 4, d: 6, e: 10)

Q: What types of decisions should we make?

Can we directly give a code for some letter?

Hard to design a strategy; residual problem is complicated.

Can we partition the letters into left and right sub-trees?

Not clear how to design the greedy algorithm

A: We can choose two letters and make them brothers in the tree.

Page 249: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

56/68

Example Input: (a: 18, b: 3, c: 4, d: 6, e: 10)

Q: What types of decisions should we make?

Can we directly give a code for some letter?

Hard to design a strategy; residual problem is complicated.

Can we partition the letters into left and right sub-trees?

Not clear how to design the greedy algorithm

A: We can choose two letters and make them brothers in the tree.

Page 250: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

56/68

Example Input: (a: 18, b: 3, c: 4, d: 6, e: 10)

Q: What types of decisions should we make?

Can we directly give a code for some letter?

Hard to design a strategy; residual problem is complicated.

Can we partition the letters into left and right sub-trees?

Not clear how to design the greedy algorithm

A: We can choose two letters and make them brothers in the tree.

Page 251: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

56/68

Example Input: (a: 18, b: 3, c: 4, d: 6, e: 10)

Q: What types of decisions should we make?

Can we directly give a code for some letter?

Hard to design a strategy; residual problem is complicated.

Can we partition the letters into left and right sub-trees?

Not clear how to design the greedy algorithm

A: We can choose two letters and make them brothers in the tree.

Page 252: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

56/68

Example Input: (a: 18, b: 3, c: 4, d: 6, e: 10)

Q: What types of decisions should we make?

Can we directly give a code for some letter?

Hard to design a strategy; residual problem is complicated.

Can we partition the letters into left and right sub-trees?

Not clear how to design the greedy algorithm

A: We can choose two letters and make them brothers in the tree.

Page 253: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

56/68

Example Input: (a: 18, b: 3, c: 4, d: 6, e: 10)

Q: What types of decisions should we make?

Can we directly give a code for some letter?

Hard to design a strategy; residual problem is complicated.

Can we partition the letters into left and right sub-trees?

Not clear how to design the greedy algorithm

A: We can choose two letters and make them brothers in the tree.

Page 254: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

56/68

Example Input: (a: 18, b: 3, c: 4, d: 6, e: 10)

Q: What types of decisions should we make?

Can we directly give a code for some letter?

Hard to design a strategy; residual problem is complicated.

Can we partition the letters into left and right sub-trees?

Not clear how to design the greedy algorithm

A: We can choose two letters and make them brothers in the tree.

Page 255: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

57/68

Which Two Letters Can Be Safely Put Together

As Brothers?

Focus on the “structure” of the optimum encoding tree

There are two deepest leaves that are brothers

Lemma It is safe to make the two least frequent letters brothers.

Page 256: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

57/68

Which Two Letters Can Be Safely Put Together

As Brothers?

Focus on the “structure” of the optimum encoding tree

There are two deepest leaves that are brothers

Lemma It is safe to make the two least frequent letters brothers.

Page 257: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

57/68

Which Two Letters Can Be Safely Put Together

As Brothers?

Focus on the “structure” of the optimum encoding tree

There are two deepest leaves that are brothers

best to put the two least

frenquent symbols here!

Lemma It is safe to make the two least frequent letters brothers.

Page 258: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

57/68

Which Two Letters Can Be Safely Put Together

As Brothers?

Focus on the “structure” of the optimum encoding tree

There are two deepest leaves that are brothers

best to put the two least

frenquent symbols here!

Lemma It is safe to make the two least frequent letters brothers.

Page 259: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

58/68

Lemma There is an optimum encoding tree, where the two leastfrequent letters are brothers.

So we can irrevocably decide to make the two least frequentletters brothers.

Q: Is the residual problem another instance of the best prefix codesproblem?

A: Yes, though it is not immediate to see why.

Page 260: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

58/68

Lemma There is an optimum encoding tree, where the two leastfrequent letters are brothers.

So we can irrevocably decide to make the two least frequentletters brothers.

Q: Is the residual problem another instance of the best prefix codesproblem?

A: Yes, though it is not immediate to see why.

Page 261: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

58/68

Lemma There is an optimum encoding tree, where the two leastfrequent letters are brothers.

So we can irrevocably decide to make the two least frequentletters brothers.

Q: Is the residual problem another instance of the best prefix codesproblem?

A: Yes, though it is not immediate to see why.

Page 262: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

58/68

Lemma There is an optimum encoding tree, where the two leastfrequent letters are brothers.

So we can irrevocably decide to make the two least frequentletters brothers.

Q: Is the residual problem another instance of the best prefix codesproblem?

A: Yes, though it is not immediate to see why.

Page 263: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

59/68

fx: the frequency of the letter x in the support.

x1 and x2: the two letters we decided to put together.

dx the depth of letter x in our output encoding tree.

x1 x2

Def: fx′ = fx1 + fx2

∑x∈S

fxdx

=∑

x∈S\{x1,x2}

fxdx + fx1dx1 + fx2dx2

=∑

x∈S\{x1,x2}

fxdx + (fx1 + fx2)dx1

=∑

x∈S\{x1,x2}

fxdx + fx′(dx′ + 1)

=∑

x∈S\{x1,x2}∪{x′}

fxdx + fx′

Page 264: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

59/68

fx: the frequency of the letter x in the support.

x1 and x2: the two letters we decided to put together.

dx the depth of letter x in our output encoding tree.

x1 x2

x′

Def: fx′ = fx1 + fx2

∑x∈S

fxdx

=∑

x∈S\{x1,x2}

fxdx + fx1dx1 + fx2dx2

=∑

x∈S\{x1,x2}

fxdx + (fx1 + fx2)dx1

=∑

x∈S\{x1,x2}

fxdx + fx′(dx′ + 1)

=∑

x∈S\{x1,x2}∪{x′}

fxdx + fx′

Page 265: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

59/68

fx: the frequency of the letter x in the support.

x1 and x2: the two letters we decided to put together.

dx the depth of letter x in our output encoding tree.

x1 x2

x′

Def: fx′ = fx1 + fx2

∑x∈S

fxdx

=∑

x∈S\{x1,x2}

fxdx + fx1dx1 + fx2dx2

=∑

x∈S\{x1,x2}

fxdx + (fx1 + fx2)dx1

=∑

x∈S\{x1,x2}

fxdx + fx′(dx′ + 1)

=∑

x∈S\{x1,x2}∪{x′}

fxdx + fx′

Page 266: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

59/68

fx: the frequency of the letter x in the support.

x1 and x2: the two letters we decided to put together.

dx the depth of letter x in our output encoding tree.

x1 x2

x′

Def: fx′ = fx1 + fx2

∑x∈S

fxdx

=∑

x∈S\{x1,x2}

fxdx + fx1dx1 + fx2dx2

=∑

x∈S\{x1,x2}

fxdx + (fx1 + fx2)dx1

=∑

x∈S\{x1,x2}

fxdx + fx′(dx′ + 1)

=∑

x∈S\{x1,x2}∪{x′}

fxdx + fx′

Page 267: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

59/68

fx: the frequency of the letter x in the support.

x1 and x2: the two letters we decided to put together.

dx the depth of letter x in our output encoding tree.

x1 x2

x′

Def: fx′ = fx1 + fx2

∑x∈S

fxdx

=∑

x∈S\{x1,x2}

fxdx + fx1dx1 + fx2dx2

=∑

x∈S\{x1,x2}

fxdx + (fx1 + fx2)dx1

=∑

x∈S\{x1,x2}

fxdx + fx′(dx′ + 1)

=∑

x∈S\{x1,x2}∪{x′}

fxdx + fx′

Page 268: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

59/68

fx: the frequency of the letter x in the support.

x1 and x2: the two letters we decided to put together.

dx the depth of letter x in our output encoding tree.

x1 x2

encoding tree for

S \ {x1, x2} ∪ {x′}

x′

Def: fx′ = fx1 + fx2

∑x∈S

fxdx

=∑

x∈S\{x1,x2}

fxdx + fx1dx1 + fx2dx2

=∑

x∈S\{x1,x2}

fxdx + (fx1 + fx2)dx1

=∑

x∈S\{x1,x2}

fxdx + fx′(dx′ + 1)

=∑

x∈S\{x1,x2}∪{x′}

fxdx + fx′

Page 269: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

60/68

In order to minimize ∑x∈S

fxdx,

we need to minimize ∑x∈S\{x1,x2}∪{x′}

fxdx,

subject to that d is the depth function for an encoding tree ofS \ {x1, x2}.

This is exactly the best prefix codes problem, with lettersS \ {x1, x2} ∪ {x′} and frequency vector f !

Page 270: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

61/68

Example

A B C D E F589111527

Page 271: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

61/68

Example

A B C D E F589111527

13

Page 272: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

61/68

Example

A B C D E F589111527

1320

Page 273: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

61/68

Example

A B C D E F589111527

1320

28

Page 274: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

61/68

Example

A B C D E F589111527

1320

2847

Page 275: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

61/68

Example

A B C D E F589111527

1320

2847

75

Page 276: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

61/68

Example

A B C D E F589111527

1320

2847

75

0

0

0

0 0

1

11

1 1

Page 277: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

61/68

Example

A B C D E F589111527

1320

2847

75

0

0

0

0 0

1

11

1 1

A : 00

B : 10

C : 010

D : 011

E : 110

F : 111

Page 278: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

62/68

Def. The codes given the greedy algorithm is called the Huffmancodes.

Huffman(S, f)

1 while |S| > 1 do

2 let x1, x2 be the two letters with the smallest f values

3 introduce a new letter x′ and let fx′ = fx1 + fx2

4 let x1 and x2 be the two children of x′

5 S ← S \ {x1, x2} ∪ {x′}6 return the tree constructed

Page 279: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

62/68

Def. The codes given the greedy algorithm is called the Huffmancodes.

Huffman(S, f)

1 while |S| > 1 do

2 let x1, x2 be the two letters with the smallest f values

3 introduce a new letter x′ and let fx′ = fx1 + fx2

4 let x1 and x2 be the two children of x′

5 S ← S \ {x1, x2} ∪ {x′}6 return the tree constructed

Page 280: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

63/68

Algorithm using Priority Queue

Huffman(S, f)

1 Q← build-priority-queue(S)

2 while Q.size > 1 do

3 x1 ← Q.extract-min()

4 x2 ← Q.extract-min()

5 introduce a new letter x′ and let fx′ = fx1 + fx2

6 let x1 and x2 be the two children of x′

7 Q.insert(x′)

8 return the tree constructed

Page 281: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

64/68

Outline

1 Toy Example: Box Packing

2 Interval Scheduling

3 Offline Caching

4 Data Compression and Huffman Code

5 Summary

Page 282: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

65/68

Summary for Greedy Algorithms

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Interval scheduling problem: schedule the job j∗ with the earliestdeadline

Offline Caching: evict the page that is used furthest in the future

Huffman codes: make the two least frequent letters brothers

Page 283: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

65/68

Summary for Greedy Algorithms

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Interval scheduling problem: schedule the job j∗ with the earliestdeadline

Offline Caching: evict the page that is used furthest in the future

Huffman codes: make the two least frequent letters brothers

Page 284: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

65/68

Summary for Greedy Algorithms

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Interval scheduling problem: schedule the job j∗ with the earliestdeadline

Offline Caching: evict the page that is used furthest in the future

Huffman codes: make the two least frequent letters brothers

Page 285: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

65/68

Summary for Greedy Algorithms

Greedy Algorithm

Build up the solutions in steps

At each step, make an irrevocable decision using a “reasonable”strategy

Interval scheduling problem: schedule the job j∗ with the earliestdeadline

Offline Caching: evict the page that is used furthest in the future

Huffman codes: make the two least frequent letters brothers

Page 286: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

66/68

Summary for Greedy Algorithms

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Def. A strategy is “safe” if there is always an optimum solutionthat “agrees with” the decision made according to the strategy.

Page 287: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

66/68

Summary for Greedy Algorithms

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Def. A strategy is “safe” if there is always an optimum solutionthat “agrees with” the decision made according to the strategy.

Page 288: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

67/68

Proving a Strategy is Safe

Take an arbitrary optimum solution S

If S agrees with the decision made according to the strategy,done

So assume S does not agree with decision

Change S slightly to another optimum solution S ′ that agreeswith the decision

Interval scheduling problem: exchange j∗ with the first job in anoptimal solutionOffline caching: a complicated “copying” algorithmHuffman codes: move the two least frequent letters to the deepestleaves.

Page 289: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

67/68

Proving a Strategy is Safe

Take an arbitrary optimum solution S

If S agrees with the decision made according to the strategy,done

So assume S does not agree with decision

Change S slightly to another optimum solution S ′ that agreeswith the decision

Interval scheduling problem: exchange j∗ with the first job in anoptimal solutionOffline caching: a complicated “copying” algorithmHuffman codes: move the two least frequent letters to the deepestleaves.

Page 290: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

67/68

Proving a Strategy is Safe

Take an arbitrary optimum solution S

If S agrees with the decision made according to the strategy,done

So assume S does not agree with decision

Change S slightly to another optimum solution S ′ that agreeswith the decision

Interval scheduling problem: exchange j∗ with the first job in anoptimal solutionOffline caching: a complicated “copying” algorithmHuffman codes: move the two least frequent letters to the deepestleaves.

Page 291: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

67/68

Proving a Strategy is Safe

Take an arbitrary optimum solution S

If S agrees with the decision made according to the strategy,done

So assume S does not agree with decision

Change S slightly to another optimum solution S ′ that agreeswith the decision

Interval scheduling problem: exchange j∗ with the first job in anoptimal solutionOffline caching: a complicated “copying” algorithmHuffman codes: move the two least frequent letters to the deepestleaves.

Page 292: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

67/68

Proving a Strategy is Safe

Take an arbitrary optimum solution S

If S agrees with the decision made according to the strategy,done

So assume S does not agree with decision

Change S slightly to another optimum solution S ′ that agreeswith the decision

Interval scheduling problem: exchange j∗ with the first job in anoptimal solution

Offline caching: a complicated “copying” algorithmHuffman codes: move the two least frequent letters to the deepestleaves.

Page 293: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

67/68

Proving a Strategy is Safe

Take an arbitrary optimum solution S

If S agrees with the decision made according to the strategy,done

So assume S does not agree with decision

Change S slightly to another optimum solution S ′ that agreeswith the decision

Interval scheduling problem: exchange j∗ with the first job in anoptimal solutionOffline caching: a complicated “copying” algorithm

Huffman codes: move the two least frequent letters to the deepestleaves.

Page 294: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

67/68

Proving a Strategy is Safe

Take an arbitrary optimum solution S

If S agrees with the decision made according to the strategy,done

So assume S does not agree with decision

Change S slightly to another optimum solution S ′ that agreeswith the decision

Interval scheduling problem: exchange j∗ with the first job in anoptimal solutionOffline caching: a complicated “copying” algorithmHuffman codes: move the two least frequent letters to the deepestleaves.

Page 295: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

68/68

Summary for Greedy Algorithms

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Interval scheduling problem: remove j∗ and the jobs it conflictswith

Offline caching: trivial

Huffman codes: merge two letters into one

Page 296: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

68/68

Summary for Greedy Algorithms

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Interval scheduling problem: remove j∗ and the jobs it conflictswith

Offline caching: trivial

Huffman codes: merge two letters into one

Page 297: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

68/68

Summary for Greedy Algorithms

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Interval scheduling problem: remove j∗ and the jobs it conflictswith

Offline caching: trivial

Huffman codes: merge two letters into one

Page 298: Department of Computer Science and Engineering University ...shil/courses/CSE531-Spring2020/Slides/Greedy.pdfCSE 431/531: Algorithm Analysis and Design (Spring 2020) Greedy Algorithms

68/68

Summary for Greedy Algorithms

Analysis of Greedy Algorithm

Prove that the reasonable strategy is “safe” (key)

Show that the remaining task after applying the strategy is tosolve a (many) smaller instance(s) of the same problem (usuallyeasy)

Interval scheduling problem: remove j∗ and the jobs it conflictswith

Offline caching: trivial

Huffman codes: merge two letters into one