Download presentation
Presentation is loading. Please wait.
Published byJocelin Robinson Modified over 9 years ago
1
CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some by Iker Gondra
2
Greedy Algorithms Solve problems with the simplest possible algorithm – easy to find greedy algorithms The hard part: finding cases where they work well and proving that they work well Pseudo-definition –An algorithm is Greedy if it builds its solution by small steps making the decision about what to do at each step “myopically” to optimize some underlying criterion.
3
Greedy Algorithms Two general methods to prove that a greedy algorithm produces an optimal solution to a problem: –Establish that the greedy algorithm stays ahead If we measure the greedy algorithm’s progress in a step-by- step fashion, one can see it does better than any other algorithm at each step – The exchange argument Consider any possible solution and gradually transform it to the solution found by the greedy algorithm without hurting its quality
4
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
5
Interval Scheduling Idea: use a simple rule upon which to select the first job; once this is selected reject all jobs not compatible with it and use the rule to select a second job etc., etc. What is a simple rule upon which to base a selection?? Call such a rule a heuristic.
6
Greedy Template Let T be the set of jobs, construct a set of compatible 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
7
Possible 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
8
Simulate greedy algorithm for each of these heuristics Schedule earliest starting job -- minimal start time s(i) Schedule shortest available job -- minimal f(i) – s(i) Schedule job with fewest conflicting jobs
9
Heuristics that don’t give optimal results 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* –Take the compatible one with earliest finish time 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
Implementation Analysis Sort the n requests in order of finishing time; label them in this order i 1, i 2, … - O( n log n) Construct the array S[1,2,..n] with the property that S[r] contains the value s(i r ) –O(n) Select requests by processing the intervals in order of increasing f(i r ). Start by selecting the first interval; then iterate through the intervals in order until reaching the first interval i j for which s(i j ) > f(i 1 ); select this one as well and continue – i.e., we now know f(i j ) so we continue down the list of intervals until we find the first interval k such that s(i k ) > f(i j ) and select that one – NOTE we make one pass through the intervals spending constant time per interval –O(n) time –Total time O(n log n) ( use additive property: 2.5 in text)
12
Theorem: EFA finds an Optimal Solution 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 O = {j 1,..., j m } be an optimal set of jobs found by a different algorithm in increasing order of finish times Must show that #A = #O Idea: Show that for r ≤ min(k, m), f(i r ) ≤ f(j r ) --- i.e EFA “stays ahead”
13
Stay Ahead Lemma Induction argument –f(i 1 ) ≤ f(j 1 ) –Assume f(i r-1 ) ≤ f(j r-1 ) ; show f(i r ) ≤ f(j r ) In O, f(j r-1 ) < s(j r ) (jobs in O are compatible) By induction hypothesis we know f(i r-1 ) ≤ f(j r-1 ) so f(i r-1 ) < s(j r ) So interval j r is in the set of available intervals for greedy algorithm to pick when it picks the next interval i r But greedy will pick the interval with smallest finish time so f(i r ) < f(j r )
14
Completing the Proof Recall A = {i 1,..., i k } is the set of jobs found by EFA in increasing order of finish times and O = {j 1,..., j m } is the set of jobs found by an optimal algorithm in increasing order of finish times Assume k < m, then O has more jobs than A; But we have shown that for all r, f(i r ) ≤ f(j r ) Thus f(i k ) ≤ f(j k ) Since k < m, there is a request j k+1 in O; this request is available for EFA since it is compatible with jobs in A Algorithm stopped before it picked all available requests Contradiction as algorithm doesn’t stop until it selects all compatible jobs! So assumption is not true!
15
Related problems Weighted Interval Scheduling Problem –Each job has different value –Wish to find the set of jobs that maximize the sum of these values –Interesting fact : we will see in a few weeks that there is no natural greedy algorithm for this closely related problem Interval Partitioning Problem –Interval scheduling – single resource and many jobs –Interval partitioning – many identical resources and wish to schedule all jobs using as few resources as possible.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.