



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Some possibly familiar examples:^ n^
Gale-Shapley stable matching algorithm. n^
Dijkstra’s shortest path algorithm. n^
Prim and Kruskal MST algorithms. n^
Huffman codes. n^
3
Minimizing breakpoints.^ n^
Truck driver going from Princeton to Palo Alto alongpredetermined route. n^
Refueling stations at certain points along the way. n^
Truck fuel capacity = C. Greedy algorithm.^ n^
Go as far as you can before refueling.
Princeton
Palo Alto
Sort breakpoints by
increasing
value:
0 = b
0
< b
1
b^2
b
.n
x = 0while (x
b
)n
let p
be largest
integer
such
that b
p^
x +
if (b
p^
x)
return "no solution" x^
b
p
{p}
return S
Greedy Breakpoint Selection Algorithm
S = breakpoints selected.
5
Selecting Breakpoints
Theorem: greedy algorithm is optimal.Proof (by contradiction):^ n^
Let 0 = g
0
< g
<... < g 1
p^
= L denote set of breakpoints chosen by
greedy and assume it is not optimal. n^
Let 0 = f
< f 0
<... < f 1
= L denote set of breakpoints in optimalq^
solution with f
= g 0
, f 0
= g 1
,... , f 1
= gr^
for largest possible value of r.r^
n^
Note: q < p.
r = 4
Greedy:OPT:
g^0
g^1
g^2
f^0
f^1
f^2
g^ p f^ q
g^ r f^ r
Selecting Breakpoints
Theorem: greedy algorithm is optimal.Proof (by contradiction):^ n^
Let 0 = g
0
< g
<... < g 1
p^
= L denote set of breakpoints chosen by
greedy and assume it is not optimal. n^
Let 0 = f
< f 0
<... < f 1
= L denote set of breakpoints in optimalq^
solution with f
= g 0
, f 0
= g 1
,... , f 1
= gr^
for largest possible value of r.r^
n^
Note: q < p.
Greedy:OPT:
r = 5
g^0
g^1
g^2
f^0
f^1
f^2
g^ p f^ q
r = 4
7
Selecting Breakpoints
Theorem: greedy algorithm is optimal.Proof (by contradiction):^ n^
Let 0 = g
0
< g
<... < g 1
p^
= L denote set of breakpoints chosen by
greedy and assume it is not optimal. n^
Let 0 = f
< f 0
<... < f 1
= L denote set of breakpoints in optimalq^
solution with f
= g 0
, f 0
= g 1
,... , f 1
= gr^
for largest possible value of r.r^
n^
Note: q < p. n^
Thus, f
= g 0
, f 0
= g 1
,... , f 1
q^
= g
q
Greedy:OPT:
r = q = 5
g^0
g^1
g^2
f^0
f^1
f^2
g^ q f^ q
g^ p
Activity Selection
Activity selection problem (CLR 17.1).^ n^
Job requests 1, 2, … , n. n^
Job j starts at s
and finishes at f j
. j
n^
Two jobs compatible if they don't overlap. n^
Goal: find maximum subset of mutually compatible jobs.
Time
Docsity.com
13
Activity Selection
Theorem: greedy algorithm is optimal.Proof (by contradiction):^ n^
Let g
, g 1
,... g 2
denote set of jobs selected by greedy and assumep^
it is not optimal. n^
Let f
, f 1
,... f 2
q^
denote set of jobs selected by optimal solution with
f^1
= g
, f^1
= g 2
,... , f 2
= gr
for largest possible value of r.r
n^
Note: r < q.
r = 3
p = 6 q = 7
Greedy:OPT:
f^1
= g
1
f^2
= g
2
f^3
= g
3
(^99) r = 4
Making Change
Given currency denominations: 1, 5, 10, 25, 100, devise a method topay amount to customer using fewest number of coins.^ n^
Ex. 34¢. Greedy algorithm.^ n^
At each iteration, add coin of the largest value that does not takeus past the amount to be paid. n^
Ex. $2.89.
15
Coin-Changing: Greedy Algorithm Sort
coins
denominations
by
increasing value:
c^1
c
2
< c
.n
φ while
(x
let p
be largest
integer
such
that c
p^
x
if
(p
return "no solution
found"
x^
x -
c
p
{p}
return S
Greedy Coin-Changing Algorithm
S = coins selected.
Is Greedy Optimal for Coin-Changing Problem?Yes, for U.S. coinage: {c
, c 1
, c 2
, c^3
, c 4
Ad hoc proof.^ n^
Consider optimal way to change amount c
≤k^
x < c
k+
n^
Greedy takes coin k. n^
Suppose optimal solution does not take coin k.^ –
it must take enough coins of type c
, c 1
,... , c 2
k-
to add up to x.
c^ k^11025100
Max # taken byoptimal solution
no limit
k 1 2 3 4 5
Max value of coins 1, 2,... , k in any OPT
no limit
2 dimes
no nickelsDocsity.com
17
Does Greedy Always Work?
US postal denominations: 1, 10, 21, 34, 70, 100, 350, 1225, 1500.^ n^
Ex. 140¢. n^
Greedy: 100, 34, 1, 1, 1, 1, 1, 1. n^
Optimal: 70, 70.
Characteristics of Greedy Algorithms
Greedy choice property.^ n^
Globally optimal solution can be arrived at by making locallyoptimal (greedy) choice. n^
At each step, choose most "promising" candidate, withoutworrying whether it will prove to be a sound decision in long run. Optimal substructure property.^ n^
Optimal solution to the problem contains optimal solutions to sub-problems.^ –
if best way to change 34¢ is {25, 5, 1, 1, 1, 1} then best way tochange 29¢ is {25, 1, 1, 1, 1}.
Objective function does not explicitly appear in greedy algorithm!Hard, if not impossible, to precisely define "greedy algorithm."^ n^
See matroids (CLR 17.4), greedoids for very general frameworks.
19
Minimizing Lateness
Minimizing lateness problem.^ n^
Single resource can process one job at a time. n^
n jobs to be processed.^ –
job j requires p
units of processing time.j
-^ job j has due date d
.j
n^
If we assign job j to start at time s
, it finishes at time fj
= sj
.j
n^
Lateness:
lj
= max { 0, f
}.j
n^
Goal: schedule all jobs to minimize maximum lateness L = max
lj
d = 11
d = 8
d = 15
d = 6
d = 9
d = 9
d = 11
d = 8
d = 2
d = 6
d = 9
d = 9
Lateness = 3
Minimizing Lateness: Greedy Algorithm
Sort jobs
by increasing
deadline
so
that
d^1
d
2
dn
t = 0for j = 1
to n
Assign
job
j to
interval
[t,
t +
p
]j
sj
t, f
j^
t +
p
j
t^
t +
p
j
output intervals [s
,j f
]j
Greedy Activity Selection Algorithm
max lateness = 2
d^5
d^2
d^6
d^1
d^4
d^3
= 9
Docsity.com