Lec4 GreedyAlgorithmsI
Lec4 GreedyAlgorithmsI
G REEDY A LGORITHMS I
‣ coin changin
‣ interval schedulin
‣ interval partitionin
‣ scheduling to minimize lateness
g
4. G REEDY A LGORITHMS I
‣ coin changin
‣ interval schedulin
‣ interval partitionin
‣ scheduling to minimize lateness
g
Coin changing
Ex. 34¢.
3
Coin changing
Ex. 34¢.
3
Coin changing
Ex. 34¢.
Cashier′s algorithm. At each iteration, add coin of the largest value that does not
take us past the amount to be paid.
3
Coin changing
Ex. 34¢.
Cashier′s algorithm. At each iteration, add coin of the largest value that does not
take us past the amount to be paid.
Ex. $2.89.
3
Coin changing
Ex. 34¢.
Cashier′s algorithm. At each iteration, add coin of the largest value that does not
take us past the amount to be paid.
Ex. $2.89.
3
Cashier′s algorithm
At each iteration, add coin of the largest value that does not take us past the amount
to be paid.
WHILE (x > 0)
k ← largest coin denomination ck such that ck ≤ x.
IF (no such k)
RETURN “no solution.
ELSE
x ← x – ck.
S ← S ∪ { k }.
RETURN S.
________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
4
”
B. Yes, for any set of coin denominations c1 < c2 < … < cn provided c1 = 1.
D. No.
5
?
B. Yes, for any set of coin denominations c1 < c2 < … < cn provided c1 = 1.
D. No.
5
?
6
Cashier′s algorithm (for arbitrary coin denominations)
A. No. Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500.
6
Cashier′s algorithm (for arbitrary coin denominations)
A. No. Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500.
・Cashier’s algorithm: 140¢ = 100 + 34 + 1 + 1 + 1 + 1 + 1 + 1.
6
Cashier′s algorithm (for arbitrary coin denominations)
A. No. Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500.
・Cashier’s algorithm: 140¢ = 100 + 34 + 1 + 1 + 1 + 1 + 1 + 1.
・Optimal: 140¢ = 70 + 70.
6
Cashier′s algorithm (for arbitrary coin denominations)
A. No. Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500.
・Cashier’s algorithm: 140¢ = 100 + 34 + 1 + 1 + 1 + 1 + 1 + 1.
・Optimal: 140¢ = 70 + 70.
6
Cashier′s algorithm (for arbitrary coin denominations)
A. No. Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500.
・Cashier’s algorithm: 140¢ = 100 + 34 + 1 + 1 + 1 + 1 + 1 + 1.
・Optimal: 140¢ = 70 + 70.
6
Cashier′s algorithm (for arbitrary coin denominations)
A. No. Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500.
・Cashier’s algorithm: 140¢ = 100 + 34 + 1 + 1 + 1 + 1 + 1 + 1.
・Optimal: 140¢ = 70 + 70.
Theorem. Cashier’s algorithm is optimal for U.S. coins { 1, 5, 10, 25, 100 }
Pf. [ by induction on amount to be paid x
・Consider optimal way to change ck ≤ x < ck+1 : greedy takes coin k
・We claim that any optimal solution must take coin k
- if not, it needs enough coins of type c1, …, ck–1 to add up to x
- table below indicates no optimal solution can do thi
・Problem reduces to coin-changing x – ck cents, which, by induction,
is optimally solved by cashier’s algorithm. ▪
1 1 P ≤ 4 –
2 5 N ≤ 1 4
3 10 N+D ≤ 2 4+5=9
4 25 Q ≤ 3 20 + 4 = 24
5 100 no limit 75 + 24 = 99
8
s
4. G REEDY A LGORITHMS I
‣ coin changin
‣ interval schedulin
‣ interval partitionin
‣ scheduling to minimize lateness
SECTION 4.1
g
Interval scheduling
h
time
0 1 2 3 4 5 6 7 8 9 10 11
10
fi
Interval scheduling
d
jobs d and
e
are incompatible
h
time
0 1 2 3 4 5 6 7 8 9 10 11
10
g
fi
Interval scheduling
d
jobs d and
e
are incompatible
h
time
0 1 2 3 4 5 6 7 8 9 10 11
10
fi
g
fi
Greedy algorithms I: quiz 2
Consider jobs in some order, taking each job provided it′s compatible
with the ones already taken. Which rule is optimal
11
fi
?
Consider jobs in some order, taking each job provided it′s compatible
with the ones already taken. Which rule is optimal
11
fi
?
Consider jobs in some order, taking each job provided it′s compatible
with the ones already taken. Which rule is optimal
11
fi
?
Consider jobs in some order, taking each job provided it′s compatible
with the ones already taken. Which rule is optimal
11
fi
?
FOR j = 1 TO
fi
.
fi
.
fi
fi
_
13
fi
fi
fi
fi
Interval scheduling: analysis of earliest- nish-time- rst algorithm
13
fi
fi
fi
fi
Interval scheduling: analysis of earliest- nish-time- rst algorithm
13
fi
fi
fi
fi
Interval scheduling: analysis of earliest- nish-time- rst algorithm
13
fi
fi
fi
fi
Interval scheduling: analysis of earliest- nish-time- rst algorithm
13
fi
fi
fi
fi
Interval scheduling: analysis of earliest- nish-time- rst algorithm
13
s
fi
fi
fi
fi
Interval scheduling: analysis of earliest- nish-time- rst algorithm
13
s
fi
fi
fi
fi
fi
Interval scheduling: analysis of earliest- nish-time- rst algorithm
13
s
fi
fi
fi
fi
fi
Interval scheduling: analysis of earliest- nish-time- rst algorithm
14
fi
]
fi
fi
fi
.
fi
Greedy algorithms I: quiz 3
Suppose that each job also has a positive weight and the goal is to
nd a maximum weight subset of mutually compatible intervals.
Is the earliest- nish-time- rst algorithm still optimal?
D. No, because you could assign a huge weight to a job that overlaps
the job with the earliest nish time.
weight = 100
weight = 1
15
fi
fi
fi
fi
fi
.
Suppose that each job also has a positive weight and the goal is to
nd a maximum weight subset of mutually compatible intervals.
Is the earliest- nish-time- rst algorithm still optimal?
D. No, because you could assign a huge weight to a job that overlaps
the job with the earliest nish time.
weight = 100
weight = 1
15
fi
fi
fi
fi
fi
.
4. G REEDY A LGORITHMS I
‣ coin changin
‣ interval schedulin
‣ interval partitionin
‣ scheduling to minimize lateness
SECTION 4.1
g
Interval partitioning
jobs e and
are incompatible
4 e j
3 c d g
2 b h
1 a f i
fi
.
Interval partitioning
3 c d f j
2 b g i
1 a e h
fi
.
19
fi
?
fi
Greedy algorithms I: quiz 4
19
fi
?
fi
Greedy algorithms I: quiz 4
3
2
1
19
fi
fi
?
fi
Greedy algorithms I: quiz 4
3
2
1
19
fi
?
fi
Interval partitioning: earliest-start-time- rst algorithm
FOR j = 1 TO
IF (lecture j is compatible with some classroom)
Schedule lecture j in any such classroom k
ELSE
Allocate a new classroom d + 1
Schedule lecture j in classroom d + 1
d ← d + 1.
RETURN schedule
________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
20
n
fi
_
Pf. Store classrooms in a priority queue (key = nish time of its last lecture).
・To determine whether lecture j is compatible with some classroom,
compare sj to key of min classroom k in priority queue
・To add lecture j to classroom k, increase key of classroom k to fj.
・Total number of priority queue operations is O(n)
・Sorting by start times takes O(n log n) time. ▪
21
fi
fi
fi
.
fi
Interval partitioning: lower bound on optimal solution
Def. The depth of a set of open intervals is the maximum number of intervals that
contain any given point.
depth = 3
3 c d f j
2 b g i
1 a e h
Def. The depth of a set of open intervals is the maximum number of intervals that
contain any given point.
depth = 3
3 c d f j
2 b g i
1 a e h
Def. The depth of a set of open intervals is the maximum number of intervals that
contain any given point.
depth = 3
3 c d f j
2 b g i
1 a e h
Def. The depth of a set of open intervals is the maximum number of intervals that
contain any given point.
depth = 3
3 c d f j
2 b g i
1 a e h
23
fi
fi
.
fi
.
4. G REEDY A LGORITHMS I
‣ coin changin
‣ interval schedulin
‣ interval partitionin
‣ scheduling to minimize lateness
SECTION 4.2
g
25
Scheduling to minimizing lateness
25
Scheduling to minimizing lateness
1 2 3 4 5 6
tj 3 2 1 4 3 2
dj 6 8 9 9 14 15
25
Scheduling to minimizing lateness
1 2 3 4 5 6
tj 3 2 1 4 3 2
dj 6 8 9 9 14 15
d3 = 9 d2 = 8 d6 = 15 d1 = 6 d5 = 14 d4 = 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
25
fi
Scheduling to minimizing lateness
1 2 3 4 5 6
tj 3 2 1 4 3 2
dj 6 8 9 9 14 15
lateness = 2 lateness = 0
d3 = 9 d2 = 8 d6 = 15 d1 = 6 d5 = 14 d4 = 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
25
fi
Scheduling to minimizing lateness
1 2 3 4 5 6
tj 3 2 1 4 3 2
dj 6 8 9 9 14 15
d3 = 9 d2 = 8 d6 = 15 d1 = 6 d5 = 14 d4 = 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
25
fi
Greedy algorithms I: quiz 5
26
fi
?
26
fi
?
1 2
L =
tj 9 10
L* = 0
dj 100 10
26
9
fi
?
1 2
L =
tj 1 10
L* = 1
dj 2 10
26
9
fi
?
max lateness L = 1
d1 = 6 d2 = 8 d3 = 9 d4 = 9 d5 = 14 d6 = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
27
.
fi
_
28
Minimizing lateness: no idle time
28
Minimizing lateness: no idle time
28
Minimizing lateness: no idle time
28
e
28
e
fi
Minimizing lateness: inversions
a schedule wit
j i
an inversion
29
h
a schedule wit
j i
an inversion
1 2 3 4 5 6 … n
29
h
fi
Minimizing lateness: inversions
a schedule wit
j i
an inversion
j k i
30
.
befor
exchange
j i
afte
exchange i j
f ʹj
Key claim. Exchanging two adjacent, inverted jobs i and j reduces the number of
inversions by 1 and does not increase the max lateness
Pf. Let ℓ be the lateness before the swap, and let ℓʹ be it afterwards
・ℓʹk = ℓk for all k ≠ i, j.
・ℓʹi ≤ ℓi.
・If job j is late, ℓʹj = f ʹj – dj de nition
fi
e
optimal schedule ca
Pf. [by contradiction] have inversions
32
fi
n
fi
s
fi
.
fi
.
Greedy algorithm stays ahead. Show that after each step of the greedy algorithm,
its solution is at least as good as any other algorithm’s.
Exchange argument. Gradually transform any solution to the one found by the
greedy algorithm without hurting its quality.
33