Greedy Algorithms Lecture 17 March 19, 2015 Chandra & Lenny - - PowerPoint PPT Presentation

greedy algorithms
SMART_READER_LITE
LIVE PREVIEW

Greedy Algorithms Lecture 17 March 19, 2015 Chandra & Lenny - - PowerPoint PPT Presentation

CS 374: Algorithms & Models of Computation, Spring 2015 Greedy Algorithms Lecture 17 March 19, 2015 Chandra & Lenny (UIUC) CS374 1 Spring 2015 1 / 46 Part I Problems and Terminology Chandra & Lenny (UIUC) CS374 2 Spring


slide-1
SLIDE 1

CS 374: Algorithms & Models of Computation, Spring 2015

Greedy Algorithms

Lecture 17

March 19, 2015

Chandra & Lenny (UIUC) CS374 1 Spring 2015 1 / 46

slide-2
SLIDE 2

Part I Problems and Terminology

Chandra & Lenny (UIUC) CS374 2 Spring 2015 2 / 46

slide-3
SLIDE 3

Problem Types

1

Decision Problem: Is the input a YES or NO input? Example: Given graph G, nodes s, t, is there a path from s to t in G?

2

Search Problem: Find a solution if input is a YES input. Example: Given graph G, nodes s, t, find an s-t path.

3

Optimization Problem: Find a best solution among all solutions for the input. Example: Given graph G, nodes s, t, find a shortest s-t path.

Chandra & Lenny (UIUC) CS374 3 Spring 2015 3 / 46

slide-4
SLIDE 4

Terminology

1

A problem Π consists of an infinite collection of inputs {I1, I2, . . . , }. Each input is referred to as an instance.

2

The size of an instance I is the number of bits in its representation.

3

For an instance I, sol(I) is a set of feasible solutions to I. Typical implicit assumption: given instance I and y ∈ Σ∗, there is a way to check (efficiently!) if y ∈ sol(I). In other words, problem is in NP.

4

For optimization problems each solution s ∈ sol(I) has an associated value. Typical implicit assumption: given s, can compute value efficiently.

Chandra & Lenny (UIUC) CS374 4 Spring 2015 4 / 46

slide-5
SLIDE 5

Problem Types

1

Decision Problem: Given I output whether sol(I) = ∅ or not.

2

Search Problem: Given I, find a solution s ∈ sol(I) if sol(I) = ∅.

3

Optimization Problem: Given I,

1

Minimization problem. Find a solution s ∈ sol(I) of minimum value

2

Maximization problem. Find a solution s ∈ sol(I) of maximum value

3

Notation: opt(I): interchangeably (when there is no confusion) used to denote the value of an optimum solution or some fixed

  • ptimum solution.

Chandra & Lenny (UIUC) CS374 5 Spring 2015 5 / 46

slide-6
SLIDE 6

Part II Greedy Algorithms: Tools and Techniques

Chandra & Lenny (UIUC) CS374 6 Spring 2015 6 / 46

slide-7
SLIDE 7

What is a Greedy Algorithm?

Chandra & Lenny (UIUC) CS374 7 Spring 2015 7 / 46

slide-8
SLIDE 8

What is a Greedy Algorithm?

No real consensus on a universal definition.

Chandra & Lenny (UIUC) CS374 7 Spring 2015 7 / 46

slide-9
SLIDE 9

What is a Greedy Algorithm?

No real consensus on a universal definition. Greedy algorithms:

1

make decision incrementally in small steps without backtracking

2

decision at each step is based on improving local or current state in a myopic fashion without paying attention to the global situation

3

decisions often based on some fixed and simple priority rules

Chandra & Lenny (UIUC) CS374 7 Spring 2015 7 / 46

slide-10
SLIDE 10

Pros and Cons of Greedy Algorithms

Pros:

1

Usually (too) easy to design greedy algorithms

2

Easy to implement and often run fast since they are simple

3

Several important cases where they are effective/optimal

4

Lead to a first-cut heuristic when problem not well understood

Chandra & Lenny (UIUC) CS374 8 Spring 2015 8 / 46

slide-11
SLIDE 11

Pros and Cons of Greedy Algorithms

Pros:

1

Usually (too) easy to design greedy algorithms

2

Easy to implement and often run fast since they are simple

3

Several important cases where they are effective/optimal

4

Lead to a first-cut heuristic when problem not well understood Cons:

1

Very often greedy algorithms don’t work. Easy to lull oneself into believing they work

2

Many greedy algorithms possible for a problem and no structured way to find effective ones

Chandra & Lenny (UIUC) CS374 8 Spring 2015 8 / 46

slide-12
SLIDE 12

Pros and Cons of Greedy Algorithms

Pros:

1

Usually (too) easy to design greedy algorithms

2

Easy to implement and often run fast since they are simple

3

Several important cases where they are effective/optimal

4

Lead to a first-cut heuristic when problem not well understood Cons:

1

Very often greedy algorithms don’t work. Easy to lull oneself into believing they work

2

Many greedy algorithms possible for a problem and no structured way to find effective ones CS 374: Every greedy algorithm needs a proof of correctness

Chandra & Lenny (UIUC) CS374 8 Spring 2015 8 / 46

slide-13
SLIDE 13

Greedy Algorithm Types

Crude classification:

1

Non-adaptive: fix some ordering of decisions a priori and stick with the order

2

Adaptive: make decisions adaptively but greedily/locally at each step

Chandra & Lenny (UIUC) CS374 9 Spring 2015 9 / 46

slide-14
SLIDE 14

Greedy Algorithm Types

Crude classification:

1

Non-adaptive: fix some ordering of decisions a priori and stick with the order

2

Adaptive: make decisions adaptively but greedily/locally at each step Plan:

1

See several examples

2

Pick up some proof techniques

Chandra & Lenny (UIUC) CS374 9 Spring 2015 9 / 46

slide-15
SLIDE 15

Interval Scheduling

Problem (Interval Scheduling)

Input: A set of jobs with start and finish times to be scheduled on a resource (example: classes and class rooms). Goal: Schedule as many jobs as possible

Chandra & Lenny (UIUC) CS374 10 Spring 2015 10 / 46

slide-16
SLIDE 16

Interval Scheduling

Problem (Interval Scheduling)

Input: A set of jobs with start and finish times to be scheduled on a resource (example: classes and class rooms). Goal: Schedule as many jobs as possible

1

Two jobs with overlapping intervals cannot both be scheduled!

Chandra & Lenny (UIUC) CS374 10 Spring 2015 10 / 46

slide-17
SLIDE 17

Greedy Template

R is the set of all requests X is empty (* X will store all the jobs that will be scheduled

while R is not empty do

choose i ∈ R add i to X remove from R all requests that overlap with i

return the set X

Chandra & Lenny (UIUC) CS374 11 Spring 2015 11 / 46

slide-18
SLIDE 18

Greedy Template

R is the set of all requests X is empty (* X will store all the jobs that will be scheduled

while R is not empty do

choose i ∈ R add i to X remove from R all requests that overlap with i

return the set X

Main task: Decide the order in which to process requests in R

ES SP FC EF Chandra & Lenny (UIUC) CS374 11 Spring 2015 11 / 46

slide-19
SLIDE 19

Earliest Start Time

Process jobs in the order of their starting times, beginning with those that start earliest.

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 46

slide-20
SLIDE 20

Earliest Start Time

Process jobs in the order of their starting times, beginning with those that start earliest.

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 46

slide-21
SLIDE 21

Earliest Start Time

Process jobs in the order of their starting times, beginning with those that start earliest.

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 46

slide-22
SLIDE 22

Earliest Start Time

Process jobs in the order of their starting times, beginning with those that start earliest.

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 46

slide-23
SLIDE 23

Earliest Start Time

Process jobs in the order of their starting times, beginning with those that start earliest.

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 46

slide-24
SLIDE 24

Earliest Start Time

Process jobs in the order of their starting times, beginning with those that start earliest.

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 46

slide-25
SLIDE 25

Earliest Start Time

Process jobs in the order of their starting times, beginning with those that start earliest.

Figure : Counter example for earliest start time

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 46

slide-26
SLIDE 26

Earliest Start Time

Process jobs in the order of their starting times, beginning with those that start earliest.

Figure : Counter example for earliest start time

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 46

slide-27
SLIDE 27

Earliest Start Time

Process jobs in the order of their starting times, beginning with those that start earliest.

Figure : Counter example for earliest start time

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 46

slide-28
SLIDE 28

Smallest Processing Time

Process jobs in the order of processing time, starting with jobs that require the shortest processing.

Back Counter Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 46

slide-29
SLIDE 29

Smallest Processing Time

Process jobs in the order of processing time, starting with jobs that require the shortest processing.

Back Counter Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 46

slide-30
SLIDE 30

Smallest Processing Time

Process jobs in the order of processing time, starting with jobs that require the shortest processing.

Back Counter Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 46

slide-31
SLIDE 31

Smallest Processing Time

Process jobs in the order of processing time, starting with jobs that require the shortest processing.

Back Counter Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 46

slide-32
SLIDE 32

Smallest Processing Time

Process jobs in the order of processing time, starting with jobs that require the shortest processing.

Back Counter Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 46

slide-33
SLIDE 33

Smallest Processing Time

Process jobs in the order of processing time, starting with jobs that require the shortest processing.

Figure : Counter example for smallest processing time

Back Counter Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 46

slide-34
SLIDE 34

Smallest Processing Time

Process jobs in the order of processing time, starting with jobs that require the shortest processing.

Figure : Counter example for smallest processing time

Back Counter Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 46

slide-35
SLIDE 35

Smallest Processing Time

Process jobs in the order of processing time, starting with jobs that require the shortest processing.

Figure : Counter example for smallest processing time

Back Counter Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 46

slide-36
SLIDE 36

Fewest Conflicts

Process jobs in that have the fewest “conflicts” first.

Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

slide-37
SLIDE 37

Fewest Conflicts

Process jobs in that have the fewest “conflicts” first.

Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

slide-38
SLIDE 38

Fewest Conflicts

Process jobs in that have the fewest “conflicts” first.

Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

slide-39
SLIDE 39

Fewest Conflicts

Process jobs in that have the fewest “conflicts” first.

Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

slide-40
SLIDE 40

Fewest Conflicts

Process jobs in that have the fewest “conflicts” first.

Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

slide-41
SLIDE 41

Fewest Conflicts

Process jobs in that have the fewest “conflicts” first.

Figure : Counter example for fewest conflicts

Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

slide-42
SLIDE 42

Fewest Conflicts

Process jobs in that have the fewest “conflicts” first.

Figure : Counter example for fewest conflicts

Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

slide-43
SLIDE 43

Fewest Conflicts

Process jobs in that have the fewest “conflicts” first.

Figure : Counter example for fewest conflicts

Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

slide-44
SLIDE 44

Fewest Conflicts

Process jobs in that have the fewest “conflicts” first.

Figure : Counter example for fewest conflicts

Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

slide-45
SLIDE 45

Earliest Finish Time

Process jobs in the order of their finishing times, beginning with those that finish earliest.

Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

slide-46
SLIDE 46

Earliest Finish Time

Process jobs in the order of their finishing times, beginning with those that finish earliest.

Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

slide-47
SLIDE 47

Earliest Finish Time

Process jobs in the order of their finishing times, beginning with those that finish earliest.

Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

slide-48
SLIDE 48

Earliest Finish Time

Process jobs in the order of their finishing times, beginning with those that finish earliest.

Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

slide-49
SLIDE 49

Earliest Finish Time

Process jobs in the order of their finishing times, beginning with those that finish earliest.

Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

slide-50
SLIDE 50

Earliest Finish Time

Process jobs in the order of their finishing times, beginning with those that finish earliest.

Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

slide-51
SLIDE 51

Earliest Finish Time

Process jobs in the order of their finishing times, beginning with those that finish earliest.

Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

slide-52
SLIDE 52

Optimal Greedy Algorithm

R is the set of all requests X is empty (* X will store all the jobs that will be scheduled

while R is not empty

choose i ∈ R such that finishing time of i is least add i to X remove from R all requests that overlap with i

return X

Theorem

The greedy algorithm that picks jobs in the order of their finishing times is optimal.

Chandra & Lenny (UIUC) CS374 16 Spring 2015 16 / 46

slide-53
SLIDE 53

Proving Optimality

1

Correctness: Clearly the algorithm returns a set of jobs that does not have any conflicts

Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

slide-54
SLIDE 54

Proving Optimality

1

Correctness: Clearly the algorithm returns a set of jobs that does not have any conflicts

2

For a set of requests R, let O be an optimal set and let X be the set returned by the greedy algorithm. Then O = X?

Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

slide-55
SLIDE 55

Proving Optimality

1

Correctness: Clearly the algorithm returns a set of jobs that does not have any conflicts

2

For a set of requests R, let O be an optimal set and let X be the set returned by the greedy algorithm. Then O = X?Not likely!

Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

slide-56
SLIDE 56

Proving Optimality

1

Correctness: Clearly the algorithm returns a set of jobs that does not have any conflicts

2

For a set of requests R, let O be an optimal set and let X be the set returned by the greedy algorithm. Then O = X?Not likely!

Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

slide-57
SLIDE 57

Proving Optimality

1

Correctness: Clearly the algorithm returns a set of jobs that does not have any conflicts

2

For a set of requests R, let O be an optimal set and let X be the set returned by the greedy algorithm. Then O = X?Not likely!

Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

slide-58
SLIDE 58

Proving Optimality

1

Correctness: Clearly the algorithm returns a set of jobs that does not have any conflicts

2

For a set of requests R, let O be an optimal set and let X be the set returned by the greedy algorithm. Then O = X?Not likely! Instead we will show that |O| = |X|

Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

slide-59
SLIDE 59

Proof of Optimality: Key Lemma

Lemma

Let i1 be first interval picked by Greedy. There exists an optimum solution that contains i1.

Proof.

Let O be an arbitrary optimum solution. If i1 ∈ O we are done.

Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 46

slide-60
SLIDE 60

Proof of Optimality: Key Lemma

Lemma

Let i1 be first interval picked by Greedy. There exists an optimum solution that contains i1.

Proof.

Let O be an arbitrary optimum solution. If i1 ∈ O we are done. Claim: If i1 ∈ O then there is exactly one interval j1 ∈ O that conflicts with i1. (proof later)

Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 46

slide-61
SLIDE 61

Proof of Optimality: Key Lemma

Lemma

Let i1 be first interval picked by Greedy. There exists an optimum solution that contains i1.

Proof.

Let O be an arbitrary optimum solution. If i1 ∈ O we are done. Claim: If i1 ∈ O then there is exactly one interval j1 ∈ O that conflicts with i1. (proof later)

1

Form a new set O′ by removing j1 from O and adding i1, that is O′ = (O − {j1}) ∪ {i1}.

2

From claim, O′ is a feasible solution (no conflicts).

3

Since |O′| = |O|, O′ is also an optimum solution and it contains i1.

Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 46

slide-62
SLIDE 62

Proof of Claim

Claim

If i1 ∈ O, there is exactly one interval j1 ∈ O that conflicts with i1.

Proof.

1

If no j ∈ O conflicts with i1 then O is not optimal!

2

Suppose j1, j2 ∈ O such that j1 = j2 and both j1 and j2 conflict with i1.

3

Since i1 has earliest finish time, j1 and i1 overlap at f(i1).

4

For same reason j2 also overlaps with i1 at f(i1).

5

Implies that j1, j2 overlap at f(i1) but intervals in O cannot

  • verlap.

See figure in next slide.

Chandra & Lenny (UIUC) CS374 19 Spring 2015 19 / 46

slide-63
SLIDE 63

Figure for proof of Claim

f(i1) f(j1)

i1 j1 j2

f(j2)

time

Figure : Since i1 has the earliest finish time, any interval that conflicts with it does so at f(i1). This implies j1 and j2 conflict.

Chandra & Lenny (UIUC) CS374 20 Spring 2015 20 / 46

slide-64
SLIDE 64

Proof of Optimality of Earliest Finish Time First

Proof by Induction on number of intervals.

Base Case: n = 1. Trivial since Greedy picks one interval. Induction Step: Assume theorem holds for i < n. Let I be an instance with n intervals I′: I with i1 and all intervals that overlap with i1 removed G(I), G(I′): Solution produced by Greedy on I and I′ From Lemma, there is an optimum solution O to I and i1 ∈ O. Let O′ = O − {i1}. O′ is a solution to I′. |G(I)| = 1 + |G(I′)| (from Greedy description) ≥ 1 + |O′| (By induction, G(I′) is optimum for I′) = |O|

Chandra & Lenny (UIUC) CS374 21 Spring 2015 21 / 46

slide-65
SLIDE 65

Implementation and Running Time

Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R

return the set X

Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

slide-66
SLIDE 66

Implementation and Running Time

Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R

return the set X

Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

slide-67
SLIDE 67

Implementation and Running Time

Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R

return the set X

1

Presort all requests based on finishing time. O(n log n) time

2

Now choosing least finishing time is O(1)

Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

slide-68
SLIDE 68

Implementation and Running Time

Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R

return the set X

1

Presort all requests based on finishing time. O(n log n) time

2

Now choosing least finishing time is O(1)

3

Keep track of the finishing time of the last request added to A. Then check if starting time of i later than that

4

Thus, checking non-overlapping is O(1)

Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

slide-69
SLIDE 69

Implementation and Running Time

Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R

return the set X

1

Presort all requests based on finishing time. O(n log n) time

2

Now choosing least finishing time is O(1)

3

Keep track of the finishing time of the last request added to A. Then check if starting time of i later than that

4

Thus, checking non-overlapping is O(1)

5

Total time O(n log n + n) = O(n log n)

Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

slide-70
SLIDE 70

Comments

1

Interesting Exercise: smallest interval first picks at least half the

  • ptimum number of intervals.

2

All requests need not be known at the beginning. Such online algorithms are a subject of research

Chandra & Lenny (UIUC) CS374 23 Spring 2015 23 / 46

slide-71
SLIDE 71

Weighted Interval Scheduling

Suppose we are given n jobs. Each job i has a start time si, a finish time fi, and a weight wi. We would like to find a set S of compatible jobs whose total weight is maximized. Which of the following greedy algorithms finds the optimum schedule? (A) Earliest start time first. (B) Earliest finish time fist. (C) Highest weight first. (D) None of the above. (E) IDK.

Chandra & Lenny (UIUC) CS374 24 Spring 2015 24 / 46

slide-72
SLIDE 72

Scheduling all Requests

Input A set of lectures, with start and end times Goal Find the minimum number of classrooms, needed to schedule all the lectures such two lectures do not occur at the same time in the same room. a b c d e f g h i j

Figure : A schedule requiring 4 classrooms Figure : A schedule requiring 3 classrooms

Chandra & Lenny (UIUC) CS374 25 Spring 2015 25 / 46

slide-73
SLIDE 73

Scheduling all Requests

Input A set of lectures, with start and end times Goal Find the minimum number of classrooms, needed to schedule all the lectures such two lectures do not occur at the same time in the same room. a b c d e f g h i j

Figure : A schedule requiring 4 classrooms

a b c d e f g h i j

Figure : A schedule requiring 3 classrooms

Chandra & Lenny (UIUC) CS374 25 Spring 2015 25 / 46

slide-74
SLIDE 74

Scheduling all Requests

Input A set of lectures, with start and end times Goal Find the minimum number of classrooms, needed to schedule all the lectures such two lectures do not occur at the same time in the same room. a b c d e f g h i j

Figure : A schedule requiring 4 classrooms

a b c d e f g h i j

Figure : A schedule requiring 3 classrooms

Chandra & Lenny (UIUC) CS374 25 Spring 2015 25 / 46

slide-75
SLIDE 75

Greedy Algorithm

Initially R is the set of all requests d = 0 (* number of classrooms *)

while R is not empty do

choose i ∈ R

if i can be scheduled in some class-room k ≤ d

schedule lecture i in class-room k

else

allocate a new class-room d + 1 and schedule lecture i in d + 1 d = d + 1

What order should we process requests in?

Chandra & Lenny (UIUC) CS374 26 Spring 2015 26 / 46

slide-76
SLIDE 76

Greedy Algorithm

Initially R is the set of all requests d = 0 (* number of classrooms *)

while R is not empty do

choose i ∈ R such that start time of i is earliest

if i can be scheduled in some class-room k ≤ d

schedule lecture i in class-room k

else

allocate a new class-room d + 1 and schedule lecture i in d + 1 d = d + 1

What order should we process requests in? According to start times (breaking ties arbitrarily)

Chandra & Lenny (UIUC) CS374 26 Spring 2015 26 / 46

slide-77
SLIDE 77

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b c d e f g h i j

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-78
SLIDE 78

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-79
SLIDE 79

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-80
SLIDE 80

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

b a

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-81
SLIDE 81

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-82
SLIDE 82

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a c a b

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-83
SLIDE 83

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b c

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-84
SLIDE 84

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

d a b c

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-85
SLIDE 85

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b c d

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-86
SLIDE 86

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

e a b c d

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-87
SLIDE 87

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b c d e

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-88
SLIDE 88

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

f a b c d e

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-89
SLIDE 89

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b c d e f

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-90
SLIDE 90

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

g a b c d e f

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-91
SLIDE 91

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b c d e f g

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-92
SLIDE 92

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

h a b c d e f g

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-93
SLIDE 93

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b c d e f g h

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-94
SLIDE 94

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

i a b c d e f g h

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-95
SLIDE 95

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b c d e f g h i

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-96
SLIDE 96

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

j a b c d e f g h i

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-97
SLIDE 97

Example of algorithm execution

“Few things are harder to put up with than a good example.” – Mark Twain

a b c d e f g h i j

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

slide-98
SLIDE 98

Depth of Lectures

Definition

1

For a set of lectures R, k are said to be in conflict if there is some time t such that there are k lectures going on at time t.

Chandra & Lenny (UIUC) CS374 28 Spring 2015 28 / 46

slide-99
SLIDE 99

Depth of Lectures

Definition

1

For a set of lectures R, k are said to be in conflict if there is some time t such that there are k lectures going on at time t.

2

The depth of a set of lectures R is the maximum number of lectures in conflict at any time.

Chandra & Lenny (UIUC) CS374 28 Spring 2015 28 / 46

slide-100
SLIDE 100

Depth of Lectures

Definition

1

For a set of lectures R, k are said to be in conflict if there is some time t such that there are k lectures going on at time t.

2

The depth of a set of lectures R is the maximum number of lectures in conflict at any time. a b c d e f g h i j

Chandra & Lenny (UIUC) CS374 28 Spring 2015 28 / 46

slide-101
SLIDE 101

Depth and Number of Class-rooms

Lemma

For any set R of lectures, the number of class-rooms required is at least the depth of R.

Chandra & Lenny (UIUC) CS374 29 Spring 2015 29 / 46

slide-102
SLIDE 102

Depth and Number of Class-rooms

Lemma

For any set R of lectures, the number of class-rooms required is at least the depth of R.

Proof.

All lectures that are in conflict must be scheduled in different rooms.

Chandra & Lenny (UIUC) CS374 29 Spring 2015 29 / 46

slide-103
SLIDE 103

Number of Class-rooms used by Greedy Algorithm

Lemma

Let d be the depth of the set of lectures R. The number of class-rooms used by the greedy algorithm is d.

Proof.

1

Suppose the greedy algorithm uses more that d rooms. Let j be the first lecture that is scheduled in room d + 1.

Chandra & Lenny (UIUC) CS374 30 Spring 2015 30 / 46

slide-104
SLIDE 104

Number of Class-rooms used by Greedy Algorithm

Lemma

Let d be the depth of the set of lectures R. The number of class-rooms used by the greedy algorithm is d.

Proof.

1

Suppose the greedy algorithm uses more that d rooms. Let j be the first lecture that is scheduled in room d + 1.

2

Since we process lectures according to start times, there are d lectures that start (at or) before j and conflict with j.

Chandra & Lenny (UIUC) CS374 30 Spring 2015 30 / 46

slide-105
SLIDE 105

Number of Class-rooms used by Greedy Algorithm

Lemma

Let d be the depth of the set of lectures R. The number of class-rooms used by the greedy algorithm is d.

Proof.

1

Suppose the greedy algorithm uses more that d rooms. Let j be the first lecture that is scheduled in room d + 1.

2

Since we process lectures according to start times, there are d lectures that start (at or) before j and conflict with j.

3

Thus, at the start time of j, there are at least d + 1 lectures in conflict, which contradicts the fact that the depth is d.

Chandra & Lenny (UIUC) CS374 30 Spring 2015 30 / 46

slide-106
SLIDE 106

Figure

s(j) j

no such job scheduled before j

Chandra & Lenny (UIUC) CS374 31 Spring 2015 31 / 46

slide-107
SLIDE 107

Correctness

Observation

The greedy algorithm does not schedule two overlapping lectures in the same room.

Theorem

The greedy algorithm is correct and uses the optimal number of class-rooms.

Chandra & Lenny (UIUC) CS374 32 Spring 2015 32 / 46

slide-108
SLIDE 108

Implementation and Running Time

Initially R is the set of all requests d = 0 (* number of classrooms *)

while R is not empty

choose i ∈ R such that start time of i is earliest

if i can be scheduled in some class-room k ≤ d

schedule lecture i in class-room k

else

allocate a new class-room d + 1 and schedule lecture i in d = d + 1

Chandra & Lenny (UIUC) CS374 33 Spring 2015 33 / 46

slide-109
SLIDE 109

Implementation and Running Time

Initially R is the set of all requests d = 0 (* number of classrooms *)

while R is not empty

choose i ∈ R such that start time of i is earliest

if i can be scheduled in some class-room k ≤ d

schedule lecture i in class-room k

else

allocate a new class-room d + 1 and schedule lecture i in d = d + 1

1

Presort according to start times. Picking lecture with earliest start time can be done in O(1) time.

Chandra & Lenny (UIUC) CS374 33 Spring 2015 33 / 46

slide-110
SLIDE 110

Implementation and Running Time

Initially R is the set of all requests d = 0 (* number of classrooms *)

while R is not empty

choose i ∈ R such that start time of i is earliest

if i can be scheduled in some class-room k ≤ d

schedule lecture i in class-room k

else

allocate a new class-room d + 1 and schedule lecture i in d = d + 1

1

Presort according to start times. Picking lecture with earliest start time can be done in O(1) time.

Chandra & Lenny (UIUC) CS374 33 Spring 2015 33 / 46

slide-111
SLIDE 111

Implementation and Running Time

Initially R is the set of all requests d = 0 (* number of classrooms *)

while R is not empty

choose i ∈ R such that start time of i is earliest

if i can be scheduled in some class-room k ≤ d

schedule lecture i in class-room k

else

allocate a new class-room d + 1 and schedule lecture i in d = d + 1

1

Presort according to start times. Picking lecture with earliest start time can be done in O(1) time.

2

Keep track of the finish time of last lecture in each room.

3

Checking conflict takes O(d) time.

4

Total time = O(n log n + nd)

Chandra & Lenny (UIUC) CS374 33 Spring 2015 33 / 46

slide-112
SLIDE 112

Implementation and Running Time

Initially R is the set of all requests d = 0 (* number of classrooms *)

while R is not empty

choose i ∈ R such that start time of i is earliest

if i can be scheduled in some class-room k ≤ d

schedule lecture i in class-room k

else

allocate a new class-room d + 1 and schedule lecture i in d = d + 1

1

Presort according to start times. Picking lecture with earliest start time can be done in O(1) time.

2

Keep track of the finish time of last lecture in each room.

3

With priority queues, checking conflict takes O(log d) time.

4

Total time = O(n log n + n log d) = O(n log n)

Chandra & Lenny (UIUC) CS374 33 Spring 2015 33 / 46

slide-113
SLIDE 113

Scheduling to Minimize Lateness

1

Given jobs with deadlines and processing times to be scheduled

  • n a single resource.

2

If a job i starts at time si then it will finish at time fi = si + ti, where ti is its processing time. di: deadline.

3

The lateness of a job is li = max(0, fi − di).

4

Schedule all jobs such that L = max li is minimized.

Chandra & Lenny (UIUC) CS374 34 Spring 2015 34 / 46

slide-114
SLIDE 114

Scheduling to Minimize Lateness

1

Given jobs with deadlines and processing times to be scheduled

  • n a single resource.

2

If a job i starts at time si then it will finish at time fi = si + ti, where ti is its processing time. di: deadline.

3

The lateness of a job is li = max(0, fi − di).

4

Schedule all jobs such that L = max li is minimized. 1 2 3 4 5 6 ti 3 2 1 4 3 2 di 6 8 9 9 14 15

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 3 2 6 1 5 4 l1 = 2 l5 = 0 l4 = 6

Chandra & Lenny (UIUC) CS374 34 Spring 2015 34 / 46

slide-115
SLIDE 115

A Simpler Feasibility Problem

1

Given jobs with deadlines and processing times to be scheduled

  • n a single resource.

2

If a job i starts at time si then it will finish at time fi = si + ti, where ti is its processing time.

3

Schedule all jobs such that each of them completes before its deadline (in other words L = maxi li = 0).

Definition

A schedule is feasible if all jobs finish before their deadline.

Chandra & Lenny (UIUC) CS374 35 Spring 2015 35 / 46

slide-116
SLIDE 116

Greedy Template

Initially R is the set of all requests curr time = 0

while R is not empty do

choose i ∈ R curr time = curr time + ti

if (curr time > di) then return ‘‘no feasible schedule’’ return ‘‘found feasible schedule’’

Chandra & Lenny (UIUC) CS374 36 Spring 2015 36 / 46

slide-117
SLIDE 117

Greedy Template

Initially R is the set of all requests curr time = 0

while R is not empty do

choose i ∈ R curr time = curr time + ti

if (curr time > di) then return ‘‘no feasible schedule’’ return ‘‘found feasible schedule’’

Main task: Decide the order in which to process jobs in R

Chandra & Lenny (UIUC) CS374 36 Spring 2015 36 / 46

slide-118
SLIDE 118

Three Algorithms

1

Shortest job first — sort according to ti.

2

Shortest slack first — sort according to di − ti.

3

EDF = Earliest deadline first — sort according to di.

Chandra & Lenny (UIUC) CS374 37 Spring 2015 37 / 46

slide-119
SLIDE 119

Three Algorithms

1

Shortest job first — sort according to ti.

2

Shortest slack first — sort according to di − ti.

3

EDF = Earliest deadline first — sort according to di. Counter examples for first two: exercise

Chandra & Lenny (UIUC) CS374 37 Spring 2015 37 / 46

slide-120
SLIDE 120

Earliest Deadline First

Theorem

Greedy with EDF rule for picking requests correctly decides if there is a feasible schedule.

Chandra & Lenny (UIUC) CS374 38 Spring 2015 38 / 46

slide-121
SLIDE 121

Earliest Deadline First

Theorem

Greedy with EDF rule for picking requests correctly decides if there is a feasible schedule. Proof via an exchange argument.

Chandra & Lenny (UIUC) CS374 38 Spring 2015 38 / 46

slide-122
SLIDE 122

Earliest Deadline First

Theorem

Greedy with EDF rule for picking requests correctly decides if there is a feasible schedule. Proof via an exchange argument. Idle time: time during which machine is not working.

Chandra & Lenny (UIUC) CS374 38 Spring 2015 38 / 46

slide-123
SLIDE 123

Earliest Deadline First

Theorem

Greedy with EDF rule for picking requests correctly decides if there is a feasible schedule. Proof via an exchange argument. Idle time: time during which machine is not working.

Lemma

If there is a feasible schedule then there is one with no idle time before all jobs are finished.

Chandra & Lenny (UIUC) CS374 38 Spring 2015 38 / 46

slide-124
SLIDE 124

Inversions

Definition

A schedule S is said to have an inversion if there are jobs i and j such that S schedules i before j, but di > dj.

Chandra & Lenny (UIUC) CS374 39 Spring 2015 39 / 46

slide-125
SLIDE 125

Inversions

Definition

A schedule S is said to have an inversion if there are jobs i and j such that S schedules i before j, but di > dj.

Claim

If a schedule S has an inversion then there is an inversion between two adjacently scheduled jobs. Proof: exercise.

Chandra & Lenny (UIUC) CS374 39 Spring 2015 39 / 46

slide-126
SLIDE 126

Main Lemma

Lemma

If there is a feasible schedule, then there is one with no inversions.

Proof Sketch.

Let S be a schedule with minimum number of inversions.

1

If S has 0 inversions, done.

2

Suppose S has one or more inversions. By claim there are two adjacent jobs i and j that define an inversion.

3

Swap positions of i and j.

4

New schedule is still feasible. (Why?)

5

New schedule has one fewer inversion — contradiction!

Chandra & Lenny (UIUC) CS374 40 Spring 2015 40 / 46

slide-127
SLIDE 127

Back to Minimizing Lateness

Goal: schedule to minimize L = maxi li.

Chandra & Lenny (UIUC) CS374 41 Spring 2015 41 / 46

slide-128
SLIDE 128

Back to Minimizing Lateness

Goal: schedule to minimize L = maxi li. How can we use algorithm for simpler feasibility problem?

Chandra & Lenny (UIUC) CS374 41 Spring 2015 41 / 46

slide-129
SLIDE 129

Back to Minimizing Lateness

Goal: schedule to minimize L = maxi li. How can we use algorithm for simpler feasibility problem? Given a lateness bound L, can we check if there is a schedule such that maxi li ≤ L?

Chandra & Lenny (UIUC) CS374 41 Spring 2015 41 / 46

slide-130
SLIDE 130

Back to Minimizing Lateness

Goal: schedule to minimize L = maxi li. How can we use algorithm for simpler feasibility problem? Given a lateness bound L, can we check if there is a schedule such that maxi li ≤ L? Yes! Set d′

i = di + L for each job i. Use feasibility algorithm with

new deadlines.

Chandra & Lenny (UIUC) CS374 41 Spring 2015 41 / 46

slide-131
SLIDE 131

Back to Minimizing Lateness

Goal: schedule to minimize L = maxi li. How can we use algorithm for simpler feasibility problem? Given a lateness bound L, can we check if there is a schedule such that maxi li ≤ L? Yes! Set d′

i = di + L for each job i. Use feasibility algorithm with

new deadlines. How can we find minimum L?

Chandra & Lenny (UIUC) CS374 41 Spring 2015 41 / 46

slide-132
SLIDE 132

Back to Minimizing Lateness

Goal: schedule to minimize L = maxi li. How can we use algorithm for simpler feasibility problem? Given a lateness bound L, can we check if there is a schedule such that maxi li ≤ L? Yes! Set d′

i = di + L for each job i. Use feasibility algorithm with

new deadlines. How can we find minimum L? Binary search!

Chandra & Lenny (UIUC) CS374 41 Spring 2015 41 / 46

slide-133
SLIDE 133

Binary search for finding minimum lateness

L = Lmin = 0 Lmax =

i ti // why is this sufficient?

While Lmin < Lmax do L = ⌊(Lmax + Lmin)/2⌋ check if there is a feasible schedule with lateness L if ‘‘yes’’ then Lmax = L else Lmin = L + 1 end while return L

Chandra & Lenny (UIUC) CS374 42 Spring 2015 42 / 46

slide-134
SLIDE 134

Binary search for finding minimum lateness

L = Lmin = 0 Lmax =

i ti // why is this sufficient?

While Lmin < Lmax do L = ⌊(Lmax + Lmin)/2⌋ check if there is a feasible schedule with lateness L if ‘‘yes’’ then Lmax = L else Lmin = L + 1 end while return L

Running time: O(n log n · log T) where T =

i ti

1

O(n log n) for feasibility test (sort by deadlines)

2

O(log T) calls to feasibility test in binary search

Chandra & Lenny (UIUC) CS374 42 Spring 2015 42 / 46

slide-135
SLIDE 135

Do we need binary search?

What happens in each call? EDF algorithm with deadlines d′

i = di + L.

Chandra & Lenny (UIUC) CS374 43 Spring 2015 43 / 46

slide-136
SLIDE 136

Do we need binary search?

What happens in each call? EDF algorithm with deadlines d′

i = di + L.

Greedy with EDF schedules the jobs in the same order for all L!!!

Chandra & Lenny (UIUC) CS374 43 Spring 2015 43 / 46

slide-137
SLIDE 137

Do we need binary search?

What happens in each call? EDF algorithm with deadlines d′

i = di + L.

Greedy with EDF schedules the jobs in the same order for all L!!! Maybe there is a direct greedy algorithm for minimizing maximum lateness?

Chandra & Lenny (UIUC) CS374 43 Spring 2015 43 / 46

slide-138
SLIDE 138

Greedy Algorithm for Minimizing Lateness

Initially R is the set of all requests curr time = 0 curr late = 0

while R is not empty

choose i ∈ R with earliest deadline curr time = curr time + ti late = curr time − di curr late = max(late, curr late) return curr late

Chandra & Lenny (UIUC) CS374 44 Spring 2015 44 / 46

slide-139
SLIDE 139

Greedy Algorithm for Minimizing Lateness

Initially R is the set of all requests curr time = 0 curr late = 0

while R is not empty

choose i ∈ R with earliest deadline curr time = curr time + ti late = curr time − di curr late = max(late, curr late) return curr late

Exercise: argue directly that above algorithm is correct

Chandra & Lenny (UIUC) CS374 44 Spring 2015 44 / 46

slide-140
SLIDE 140

Greedy Algorithm for Minimizing Lateness

Initially R is the set of all requests curr time = 0 curr late = 0

while R is not empty

choose i ∈ R with earliest deadline curr time = curr time + ti late = curr time − di curr late = max(late, curr late) return curr late

Exercise: argue directly that above algorithm is correct Can be easily implemented in O(n log n) time after sorting jobs.

Chandra & Lenny (UIUC) CS374 44 Spring 2015 44 / 46

slide-141
SLIDE 141

Greedy Analysis: Overview

1

Greedy’s first step leads to an optimum solution. Show that there is an optimum solution leading from the first step of Greedy and then use induction. Example, Interval Scheduling.

2

Greedy algorithm stays ahead. Show that after each step the solution of the greedy algorithm is at least as good as the solution of any other algorithm. Example, Interval scheduling.

3

Structural property of solution. Observe some structural bound

  • f every solution to the problem, and show that greedy algorithm

achieves this bound. Example, Interval Partitioning.

4

Exchange argument. Gradually transform any optimal solution to the one produced by the greedy algorithm, without hurting its

  • ptimality. Example, Minimizing lateness.

Chandra & Lenny (UIUC) CS374 45 Spring 2015 45 / 46

slide-142
SLIDE 142

Takeaway Points

1

Greedy algorithms come naturally but often are incorrect. A proof of correctness is an absolute necessity.

2

Exchange arguments are often the key proof ingredient. Focus

  • n why the first step of the algorithm is correct: need to show

that there is an optimum/correct solution with the first step of the algorithm.

3

Thinking about correctness is also a good way to figure out which of the many greedy strategies is likely to work.

Chandra & Lenny (UIUC) CS374 46 Spring 2015 46 / 46