Download presentation
Presentation is loading. Please wait.
Published byAsher Harrell Modified over 9 years ago
1
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 8
2
Greedy Algorithms Solve problems with the simplest possible algorithm The hard part: showing that something simple actually works Pseudo-definition –An algorithm is Greedy if it builds its solution by adding elements one at a time using a simple rule
3
Interval Scheduling Interval scheduling –Job j starts at s j and finishes at f j –Two jobs compatible if they don't overlap –Goal: find maximum subset of mutually compatible jobs Time f g h e a b c d
4
What is the Largest Solution?
5
Greedy Template Let T be the set of jobs, construct a set of independent jobs A H is the rule (heuristic) determining the greedy algorithm A = { } While (T is not empty) Select a job t from T by a rule H Add t to A Remove t and all jobs incompatible with t from T
6
Heuristics [Earliest start time] Consider jobs in ascending order of start time s j [Earliest finish time] Consider jobs in ascending order of finish time f j [Shortest interval] Consider jobs in ascending order of interval length f j – s j [Fewest conflicts] For each job, count the number of conflicting jobs c j. Schedule in ascending order of conflicts c j
7
Simulate greedy algorithm for each of these heuristics Schedule earliest starting job Schedule shortest available job Schedule job with fewest conflicting jobs
8
Greedy solution based on earliest finishing time Example 1 Example 2 Example 3
9
Heuristics that don’t work breaks earliest start time breaks shortest interval breaks fewest conflicts
10
Greedy Algorithm for Interval Scheduling Earliest Finish Algorithm (EFA): 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 j* that was added last to A –Job j is compatible with A if s j f j* Sort jobs by finish times so that f 1 f 2 ... f n A for j = 1 to n { if (job j compatible with A) A A {j} } return A
11
Theorem: EFA is Optimal Key idea: EFA stays ahead Let A = {i 1,..., i k } be the set of jobs found by EFA in increasing order of finish times Let B = {j 1,..., j m } be the set of jobs found by a different algorithm in increasing order of finish times Show that for r<= min(k, m), f(i r ) <= f(j r )
12
Stay Ahead Lemma A always stays ahead of B, f(i r ) <= f(j r ) Induction argument –f(i 1 ) <= f(j 1 ) –If f(i r-1 ) <= f(j r-1 ) then f(i r ) <= f(j r )
13
Completing the Proof Let A = {i 1,..., i k } be the set of jobs found by EFA in increasing order of finish times Let O = {j 1,..., j m } be the set of jobs found by an optimal algorithm in increasing order of finish times If k < m, then the Earliest Finish Algorithm stopped before it ran out of jobs
14
Interval Partitioning Interval partitioning –Lecture j starts at s j and finishes at f j –Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room –Ex: This schedule uses 4 classrooms to schedule 10 lectures Time 99:301010:301111:301212:3011:3022:30 h c b a e d g f i j 33:3044:30
15
Interval Partitioning –Ex: This schedule uses only 3 Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b
16
How many processors are needed for this example?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.