Download presentation
Presentation is loading. Please wait.
Published byAda Richards Modified over 6 years ago
1
Greedy Algorithms / Interval Scheduling Yin Tat Lee
CSE 421 Greedy Algorithms / Interval Scheduling Yin Tat Lee
2
Summary for last two classes
Terminology: vertices, edges, paths, connected component, tree, bipartiteβ¦ Vertices vs Edges: π=π( π 2 ) in general, π=πβ1 for trees BFS: Layers, queue, shortest paths, all edges go to same or adjacent layer DFS: recursion/stack; all edges ancestor/descendant Algorithms: Connected Comp, bipartiteness, topological sort Techniques: Induction on vertices/layers Sorry that the algorithm 2 for topological sort is wrong. See the for the fix
3
Tentative Schedule Today β Apr 16: Greedy Method Apr 18 β Apr 27: Divide and Conquer Apr 30: Midterm May 2 β May 9: Dynamic Programming May 11 β May 21: Network Flow Rest: NP completeness
4
Greedy Algorithms Hard to define exactly but can give general properties Solution is built in small steps Decisions on how to build the solution are made to maximize some criterion without looking to the future Want the βbestβ current partial solution as if the current step were the last step May be more than one greedy algorithm using different criteria to solve a given problem
5
Greedy Strategy Goal: Given currency denominations: 1, 5, 10, 25, 100, give change to customer using fewest number of coins. Ex: 34Β’. Cashier's algorithm: At each iteration, give the largest coin valued β€ the amount to be paid. Ex: $2.89.
6
Greedy is not always Optimal
Observation: Greedy algorithm is sub-optimal for US postal denominations: 1, 10, 21, 34, 70, 100, 350, 1225, 1500. Counterexample. 140Β’. Greedy: 100, 34, 1, 1, 1, 1, 1, 1. Optimal: 70, 70. Lesson: Greedy is short-sighted. Always chooses the most attractive choice at the moment. But this may lead to a dead-end later.
7
Greedy Algorithms Greedy algorithms
Easy to produce Fast running times Work only on certain classes of problems Hard part is showing that they are correct Two methods for proving that greedy algorithms do work Greedy algorithm stays ahead At each step any other algorithm will have a worse value for some criterion that eventually implies optimality Exchange Argument Can transform any other solution to the greedy solution at no loss in quality
8
Interval Scheduling Time 1 2 3 4 5 6 7 8 9 10 11 f g h e a b c d
9
Interval Scheduling Job j starts at π (π) and finishes at π(π).
Two jobs compatible if they donβt overlap. Goal: find maximum subset of mutually compatible jobs. a h e b b c d e f g h Time 1 2 3 4 5 6 7 8 9 10 11
10
Greedy Strategy Sort the jobs in some order. Go over the jobs and take as much as possible provided it is compatible with the jobs already taken. Main question: What order? Does it give the Optimum answer? Why?
11
Possible Approaches for Inter Sched
Sort the jobs in some order. Go over the jobs and take as much as possible provided it is compatible with the jobs already taken. [Shortest interval] Consider jobs in ascending order of interval length π π βπ (π). [Earliest start time] Consider jobs in ascending order of start time π (π). [Earliest finish time] Consider jobs in ascending order of finish time π(π).
12
Greedy Alg: Earliest Finish Time
Consider jobs in increasing order of finish time. Take each job provided itβs compatible with the ones already taken. Implementation. O(n log n). Remember job π β that was added last to A. Job π is compatible with A if π π β₯π( π β ). Sort jobs by finish times so that f(1) ο£ f(2) ο£ ... ο£ f(n). π¨ββ
for j = 1 to n { if (job j compatible with π¨) π¨βπ¨βͺ{π} } return π¨
13
Greedy Alg: Example B C A E D F G H A B C D E F G H Time 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11 A B C D E F G H 1 2 3 4 5 6 7 8 9 10 11
14
How to prove it gives maximum number of jobs?
Correctness The output is compatible. (This is by construction.) How to prove it gives maximum number of jobs? Let π 1 , π 2 , π 3 ,β― be jobs picked by greedy (ordered by finish time) Let π 1 , π 2 , π 3 ,β― be an optimal solution (ordered by finish time) How about proving π π = π π for all π? No, there can be multiple optimal solutions. Idea: Prove that greedy outputs the βbestβ optimal solution. Given two compatible orders, which is better? The one finish earlier. How to prove greedy gives the βbestβ? Induction: it gives the βbestβ during every iteration.
15
Correctness Theorem: Greedy algorithm is optimal.
Proof: (technique: βGreedy stays aheadβ) Let π 1 , π 2 , π 3 ,β―, π π be jobs picked by greedy, π 1 , π 2 , π 3 ,β―, π π those in some optimal solution in order. We show π( π π ) ο£ π( π π ) for all π, by induction on π. Base Case: π 1 chosen to have min finish time, so π(π1) ο£ π(π1). IH: π( π π ) ο£ π π π for some r IS: Since π π π β€π π π β€π ( π π+1 ), π π+1 is among the candidates considered by greedy when it picked π π+1 , & it picks min finish, so π π π+1 β€π( π π+1 ) Observe that we must have πβ₯π, else π π+1 is among (nonempty) set of candidates for π π+1 .
16
Interval Partitioning Technique: Structural
17
Interval Partitioning
Lecture π starts at π (π) and finishes at π(π). Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Room 1 Room 2 Room 3 Room 4 e j c d g b h a f i 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time
18
Interval Partitioning
Note: graph coloring is very hard in general, but graphs corresponding to interval intersections are simpler. Room 1 Room 2 Room 3 Room 4 E J C D G H B A F I e j c d g b h a f i 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time
19
A Better Schedule This one uses only 3 classrooms c d f j b g i a e h
9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time
20
A Greedy Algorithm Greedy algorithm: Consider lectures in increasing order of finish time: assign lecture to any compatible classroom. Correctness: This is wrong! Sort intervals by finish time so that f1 ο£ f2 ο£ ... ο£ fn. d ο¬ 0 for j = 1 to n { if (lect j is compatible with some classroom k, πβ€πβ€π
) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d ο¬ d + 1 }
21
Greedy by finish time gives:
Example a b c d Time 1 2 3 4 5 6 OPT: Greedy by finish time gives: a d a c b c b d Time Time 1 2 3 4 5 6 1 2 3 4 5 6
22
A Greedy Algorithm Greedy algorithm: Consider lectures in increasing order of start time: assign lecture to any compatible classroom. Implementation: Exercise! Sort intervals by starting time so that s1 ο£ s2 ο£ ... ο£ sn. d ο¬ 0 for j = 1 to n { if (lect j is compatible with some classroom k, πβ€πβ€π
) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d ο¬ d + 1 }
23
A Structural Lower-Bound on OPT
Def. The depth of a set of open intervals is the maximum number that contain any given time. Key observation. Number of classrooms needed ο³ depth. Ex: Depth of schedule below = 3 ο schedule below is optimal. Q. Does there always exist a schedule equal to depth of intervals? c d f j b g i a e h 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time
24
Correctness Observation: Greedy algorithm never schedules two incompatible lectures in the same classroom. Theorem: Greedy algorithm is optimal. Proof (exploit structural property). Let π= number of classrooms that the greedy algorithm allocates. Classroom π is opened because we needed to schedule a job, say π, that is incompatible with all πβ1 previously used classrooms. Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than π (π). Thus, we have π lectures overlapping at time π π +π, i.e. depth β₯π βOPT Observationβ ο all schedules use ο³ depth classrooms, so π= depth and greedy is optimal βͺ
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.