csci 256 data structures and algorithm analysis lecture 6 some slides by kevin wayne copyright 2005,...

15
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

Upload: jocelin-robinson

Post on 05-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

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

Page 2: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

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.

Page 3: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

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

Page 4: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Interval Scheduling

• Interval scheduling– Job j starts at sj and finishes at fj– 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

Page 5: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

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.

Page 6: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

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

Page 7: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Possible Heuristics

• [Earliest start time] Consider jobs in ascending order of start time sj

• [Earliest finish time] Consider jobs in ascending order of finish time fj

• [Shortest interval] Consider jobs in ascending order of interval length fj – sj

• [Fewest conflicts] For each job, count the number of conflicting jobs cj. Schedule in ascending order of conflicts cj

Page 8: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

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

Page 9: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Heuristics that don’t give optimal results

breaks earliest start time

breaks shortest interval

breaks fewest conflicts

Page 10: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

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 sj fj*

– Take the compatible one with earliest finish time

Sort jobs by finish times so that f1 f2 ... fn

A for j = 1 to n { if (job j compatible with A) A A {j}}return A

Page 11: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Implementation Analysis

• Sort the n requests in order of finishing time; label them in this order i1, i2, …

- O( n log n)

• Construct the array S[1,2,..n] with the property that S[r] contains the value s(ir)

– O(n)

• Select requests by processing the intervals in order of increasing f(ir). Start by selecting the first interval; then iterate through the intervals in order until reaching the first interval ij for which s(ij) > f(i1); select this one as well and continue – i.e., we now know f(ij) so we continue down the list of intervals until we find the first interval k such that s(ik) > f(ij) 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)

Page 12: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Theorem: EFA finds an Optimal Solution

• Key idea: EFA stays ahead

• Let A = {i1, . . ., ik} be the set of jobs found by EFA in increasing order of finish times

• Let O = {j1, . . ., jm} 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(ir) ≤ f(jr) --- i.e EFA “stays ahead”

Page 13: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Stay Ahead Lemma

• Induction argument– f(i1) ≤ f(j1)

– Assume f(ir-1) ≤ f(jr-1) ; show f(ir) ≤ f(jr)

• In O, f(jr-1) < s(jr) (jobs in O are compatible)

• By induction hypothesis we know f(ir-1) ≤ f(jr-1) so f(ir-1) < s(jr)

• So interval jr is in the set of available intervals for greedy algorithm to pick when it picks the next interval ir

• But greedy will pick the interval with smallest finish time so f(ir) < f(jr)

Page 14: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Completing the Proof

• Recall A = {i1, . . ., ik} is the set of jobs found by EFA in increasing order of finish times and O = {j1, . . ., jm} 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(ir) ≤ f(jr)

• Thus f(ik) ≤ f(jk)

• Since k < m, there is a request jk+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!

Page 15: CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

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.