Anda di halaman 1dari 11

Greedy Algorithms

Interval Scheduling: Section 4.1

Greedy Algorithms
Optimization problems involve profit maximization (or cost minimization) under resource constraints p Greedy Algorithms build solutions step-bystep: each step uses locally optimal (short-sighted?) rules to optimize the objective function
p

1 PDF created with pdfFactory trial version www.pdffactory.com

Greed
p

Greed is good. Greed is right. Greed works. -- Gordon Gekko (Michael Douglas) in the movie Wall Street

Knapsack Problem

Bag of fixed size

Chocolates of different sizes

How to maximize the amount of chocolate packed?

2 PDF created with pdfFactory trial version www.pdffactory.com

Knapsack Problem: greedy

Knapsack Problem: optimal

3 PDF created with pdfFactory trial version www.pdffactory.com

Interval scheduling
p

Set of jobs {1, 2, , n} p Job i starts at time si and ends at time fi p Two jobs compatible if they do not overlap p Goal: find the maximum subset of mutually compatible jobs

Interval scheduling

4 PDF created with pdfFactory trial version www.pdffactory.com

Interval scheduling: greed algorithms


p

Greedy approach
n n n

Consider jobs in a specific order Choose the current best job Delete all other jobs that are not compatible with this job

Interval scheduling: greedy algorithms


[Earliest start time] Consider jobs in the ascending order of their start times si p [Earliest finish time] Consider jobs in the ascending order of their finish times fi p [Shortest interval] Consider jobs in the ascending order of their lengths fi si p [Fewest conflicts] For each job i, count the number of conflicting jobs ci. Consider jobs in the ascending order of ci
p

5 PDF created with pdfFactory trial version www.pdffactory.com

Interval scheduling: greedy algorithms

Earliest finish time

6 PDF created with pdfFactory trial version www.pdffactory.com

Earliest finish time

Analysis
Running time = O(n log n) p Let A be the solution of the greedy algorithm
p
n

A is a compatible set of requests

7 PDF created with pdfFactory trial version www.pdffactory.com

Analysis
Running time = O(n log n) p Let A be the solution of the greedy algorithm (earliest finish time algo.)
p
n n

A is a compatible set of requests A is optimal

Greedy stays ahead


p

Let A = i1, i2 , , ik p Let OPT be the optimal solution


n

OPT = j1,j2,,jm

Both A and OPT are arranged in the natural interval order p Need to show that k m
p

8 PDF created with pdfFactory trial version www.pdffactory.com

Greedy stays ahead


p

[4.2] For all indices r k we have f(ir) f(jr)

Greedy stays ahead


p p

[4.2] For all indices r k we have f(ir) f(jr) Proof by induction


n n n

Base case: r = 1. Greedy picks the earliest finish time Assume f(ir-1) f(jr-1) Can the greedy algorithms rth job really finish later than OPTs?

9 PDF created with pdfFactory trial version www.pdffactory.com

Greedy stays ahead


p p

[4.2] For all indices r k we have f(ir) f(jr) Proof by induction


n n

Base case: r = 1. Greedy picks the earliest finish time Assume f(ir-1) f(jr-1)

Greedy stays ahead


[4.2] For all indices r k we have f(ir) f(jr) p Proof by induction
p
n n n

Base case: r = 1. Greedy picks the earliest finish time Hypothesis: Let r > 1; assume f(ir-1) f(jr-1) Induction step
p p

f(ir-1) f(jr-1) s(jr) Job jr is available after jr-1 finishes for greedy algo; greedy goes for one with least finish time Hence f(ir) f(jr)

10 PDF created with pdfFactory trial version www.pdffactory.com

Greedy is optimal
p

[4.3] The greedy algorithm returns an optimal set A


n n n n

Proof by contradiction: Assume A is not optimal; lets see what happens Let |A| = k and |OPT| = m > k f(ik) f(jk) s(jk+1) After finishing job jk, job jk+1 is available for greedy contradicts termination condition for greedy

Scheduling all intervals: Interval partitioning problem


Set of lectures {1, 2, , n} p Lecture i starts at time si and ends at fi p We need to schedule all lectures
p
n

What is the minimum n

11 PDF created with pdfFactory trial version www.pdffactory.com

Anda mungkin juga menyukai