Lecture 14
Greedy algorithms!
Lecture 14 Greedy algorithms! Announcements HW6 Due Friday! TONS - - PowerPoint PPT Presentation
Lecture 14 Greedy algorithms! Announcements HW6 Due Friday! TONS OF PRACTICE ON DYNAMIC PROGRAMMING Sometimes I have hidden slides in PowerPoint These are usually rough drafts or ways I was thinking of presenting things that I
Greedy algorithms!
presenting things that I didn’t end up going with.
slides show up with no distinction from normal slides.
like it doesn’t belong or is full of garbage, let me know.
Frisbee Practice Orchestra CS161 study group Sleep CS110 Class Theory Lunch Theory Seminar Combinatorics Seminar Underwater basket weaving class Math 51 Class CS 161 Class CS 166 Class CS 161 Section CS 161 Office Hours Swimming lessons Programming team meeting Social activity time
You can only do one activity at a time, and you want to maximize the number of activities that you do.
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
globally optimal solution.
the optimal solution.
info so that the algorithm from Step 3 can find the actual solution.
person.
Activity i finishes and before Activity j starts a5 a2 a7 a6
time
a4 a1 a3
Activity i finishes and before Activity j starts
a5 a2 a7 a6
time
a4 a1 a3
the optimal solution.
info so that the algorithm from Step 3 can find the actual solution.
person.
between Activities i and j.
time
A[i,j] = number of activities you can squeeze in after Activity i finishes and before Activity j starts
between Activities i and j.
time
A[i,j] = number of activities you can squeeze in after Activity i finishes and before Activity j starts
possiblities for k.
O(nlog(n)), if it’s correct…
Try it! It builds character!
subproblem to look at.
after i with the smallest finishing time.
that extends the optimal solution to A[k..n+1].
Let’s add an additional activity an+1 that starts “tomorrow”.
A[i,j] = number of activities you can squeeze in after Activity i finishes and before Activity j starts
This is abusing notation…technically A[i..n+1] is a value, not a problem.
after i with the smallest finishing time.
that extends the optimal solution to A[k..n+1].
time
A[i,j] = number of activities you can squeeze in after Activity i finishes and before Activity j starts
after i with the smallest finishing time.
that extends the optimal solution to A[k..n+1].
time
A[i,j] = number of activities you can squeeze in after Activity i finishes and before Activity j starts
after i with the smallest finishing time.
that extends the optimal solution to A[k..n+1].
time
time
after i with the smallest finishing time.
that extends the optimal solution to A[k..n+1].
time
time in that solution.
after i with the smallest finishing time.
that extends the optimal solution to A[k..n+1].
time
time in that solution.
after i with the smallest finishing time.
that extends the optimal solution to A[k..n+1].
time
time in that solution.
after i with the smallest finishing time.
that extends the optimal solution to A[k..n+1].
time
time in that solution.
after i with the smallest finishing time.
that extends the optimal solution to A[k..n+1].
A[i,j] i j n+1 n+1
A[i,j] i j n+1 n+1 A[0,n+1] is the return value we wanted.
A[i,j] i j n+1 n+1 A[0,n+1] is the return value we wanted. We should know ahead
A[i,j] i j n+1 n+1 A[0,n+1] is the return value we wanted. We should know ahead
etc.
A[i,j] i j n+1 n+1 A[0,n+1] is the return value we wanted. We should know ahead
etc.
A[i,j] i j n+1 n+1 A[0,n+1] is the return value we wanted. We should know ahead
etc.
A[i,j] i j n+1 n+1 A[0,n+1] is the return value we wanted. We should know ahead
etc. There’s no reason we have to look at the whole table!
This is the same thing we saw before
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
a3 a1 a4 a2 a5 a7 a6
time
contains A[k..n+1].
In particular, when each big problem depends on only one sub-problem.
Big problem sub-problem sub-problem sub-sub- problem sub-sub- problem sub-sub- problem sub-sub- problem sub-sub- problem
Big problem sub-problem sub-problem sub-sub- problem sub-sub- problem sub-sub- problem sub-sub- problem sub-problem
Big problem sub-sub- problem sub-problem
Big problem sub-sub- problem sub-problem
from optimal solutions of sub-problems
sub-problem.
that we really only care about one sub-problem, then maybe we can use a greedy algorithm.
solution.
Overcommitted Stanford Student CS161 HW! Call your parents! Math HW! Econ HW! Practice musical instrument! Read CLRS! Have a social life! Sleep! Administrative stuff for your student club! Do laundry! Meditate!
CS161 HW! Sleep! 10 hours 8 hours Cost: 2 units per hour until it’s done. Cost: 3 units per hour until it’s done.
Job A Job B Job C Job D Suppose this is the optimal schedule:
Then this must be the optimal schedule on just jobs A and B.
Job A Job B Job C Job D Of all these jobs, which one(s) is it safe to choose first? Which won’t rule out an optimal solution?
Job A Job B x hours y hours Cost: z units per hour until it’s done. Cost: w units per hour until it’s done. A then B is better than B then A when: 𝑦𝑨 + 𝑦 + 𝑧 𝑥 ≤ 𝑧𝑥 + 𝑦 + 𝑧 𝑨 𝑦𝑨 + 𝑦𝑥 + 𝑧𝑥 ≤ 𝑧𝑥 + 𝑦𝑨 + 𝑧𝑨 𝑥𝑦 ≤ 𝑧𝑨 𝑥 𝑧 ≤ 𝑨 𝑦 What matters is the ratio: cos cost of
delay ti time it it ta takes Do the job with the biggest ratio first.
the previous slide that the cost won’t increase.
Job A Job B cA/tA >= cB/tB Job C Job D Job A Job B Job C Job D
𝒖𝒋 =
co cost of
delayin ing jo job i tim ime jo job i tak takes es to to co complete
The running time is O(nlog(n))
to prove this works
sorted_JOBS[1..t].
the first 0 jobs are []”
sorted_JOBS[t+1..n] so that sorted_JOBS[t] is first.
the first n jobs are sorted_JOBS.”
greedy algorithm works.
Job A Job B Job C Job D
01111001 00100000 01100101 01101110 01100111 01101100 01101001 01110011 01101000 00100000 01110011 01100101 01101110 01110100 01100101 01101110 01100011 01100101
01101001 01011111 01101111 01110000 01100001 01110011 01100100 01100110 01100111 00101011 01101000 01101010 01101011 01101100 01111010 01111000 01100011 01110110
01111001 00100000 01100101 01101110 01100111 01101100 01101001 01110011 01101000 00100000 01110011 01100101 01101110 01110100 01100101 01101110 01100011 01100101
01101001 01011111 01101111 01110000 01100001 01110011 01100100 01100110 01100111 00101011 01101000 01101010 01101011 01101100 01111010 01111000 01100011 01110110 ASCII is pretty wasteful. If e shows up so often, we should have a more parsimonious way
A B C D E F Percentage Letter 45 13 12 16 9 5 For simplicity, let’s go with this made-up example
How to encode them as efficiently as possible?
A B C D E F Percentage Letter 45 13 12 16 9 5 1 00 01 10 11
shorter strings.
A B C D E F Percentage Letter 45 13 12 16 9 5 01 00 101 110 111 100
10010101
Confusingly, “prefix-free codes” are also sometimes called “prefix codes” (including in CLRS).
A B C D E F Percentage Letter 45 13 12 16 9 5 01 00 101 110 111 100
10010101 F
Confusingly, “prefix-free codes” are also sometimes called “prefix codes” (including in CLRS).
A B C D E F Percentage Letter 45 13 12 16 9 5 01 00 101 110 111 100
10010101 FA
Confusingly, “prefix-free codes” are also sometimes called “prefix codes” (including in CLRS).
A B C D E F Percentage Letter 45 13 12 16 9 5 01 00 101 110 111 100
10010101 FAB
Question: What is the most efficient way to do prefix-free coding? (This isn’t it). Confusingly, “prefix-free codes” are also sometimes called “prefix codes” (including in CLRS).
D: 16 A: 45 B:13 F:5 C:12 E:9
1 1 1 1 1
00 01 100 101 110 111 As long as all the letters show up as leaves, this code is prefix-free. B:13 below means that ‘B’ makes up 13% of the characters that ever appear.
D: 16 A: 45 B:13 F:5 C:12 E:9
1 1 1 1 1
00 01 100 101 110 111
Expected cost of encoding a letter with this tree: 𝟑 𝟏. 𝟓𝟔 + 𝟏. 𝟐𝟕 + 𝟒 𝟏. 𝟏𝟔 + 𝟏. 𝟐𝟒 + 𝟏. 𝟐𝟑 + 𝟏. 𝟏𝟘 = 𝟑. 𝟒𝟘 Question: What is lowest-cost tree for this distribution? (This isn’t it). Cost = I 𝑄 𝑦 ⋅ depth(𝑦)
P(x) is the probability
The depth in the tree is the length
1
Then this is an
fewer letters.
Otherwise, we could change this sub-tree and end up with a better overall tree.
1
What’s a safe choice to make for these lower sub-trees? Infrequent elements!
We want them as low down as possible.
greedily build subtrees, starting with the infrequent letters
D: 16 A: 45 B:13 F:5 C:12 E:9 14
1
greedily build subtrees, starting with the infrequent letters
D: 16 A: 45 B:13 F:5 C:12 E:9 14
1
25
1
greedily build subtrees, starting with the infrequent letters
D: 16 A: 45 B:13 F:5 C:12 E:9 14
1
25
1
30
1
greedily build subtrees, starting with the infrequent letters
D: 16 A: 45 B:13 F:5 C:12 E:9 14
1
25
1
30
1
55
1
greedily build subtrees, starting with the infrequent letters
D: 16 A: 45 B:13 F:5 C:12 E:9 14
1
25
1
30
1
55
1
100
1
greedily build subtrees, starting with the infrequent letters
D: 16 A: 45 B:13 F:5 C:12 E:9 14
1
25
1
30
1
55
1
100
1
100 101 110 1110 1111 Expected cost of encoding a letter: 𝟐 ⋅ 𝟏. 𝟓𝟔 + 𝟒 ⋅ 𝟏. 𝟓𝟐 + 𝟓 ⋅ 𝟏. 𝟐𝟓
= 𝟑. 𝟑𝟓
D: 16 F:5 E:9 14
1
Y Z X
D: 16 A: 45 B:13 C:12
won’t rule out an optimal solution.
Then there is an optimal tree where x and y are siblings.
D: 16 A: 45 B:13 F:5 C:12 E:9 14
1
we just made its encoding shorter.
and y as siblings.
If x and y are the two least-frequent letters, there is an optimal subtree where x and y are siblings. x a Lowest-level sibling nodes: at least one of them is neither x nor y
we just made its encoding shorter.
and y as siblings.
x y Lowest-level sibling nodes: at least one of them is neither x nor y If x and y are the two least-frequent letters, there is an optimal subtree where x and y are siblings.
won’t rule out an optimal solution.
Then there is an optimal tree where x and y are siblings.
D: 16 A: 45 B:13 F:5 C:12 E:9 14
1
won’t rule out an optimal solution.
Then there is an optimal tree where x and y are siblings.
D: 16 A: 45 B:13 F:5 C:12 E:9
1
25
1 1
14 30 Our argument before just showed that we made the right choice at the first step, when everything was a leaf. What about
D: 16 F:5 E:9 14
1
25
1
30
1
55
1
100
1
C:12 B:13 A: 45 A: 45 55
1
100
1
G: 25 H: 30 The first thing is an optimal tree on {A,B,C,D,E,F} if and only if the second thing is an
Ollie the over-achieving ostrich Getting all the details isn’t that important, but you should convince yourself that this is true.
Then there is an optimal tree where x and y are siblings.
leaves.
tree.
After the t’th step, we’ve got a bunch of current sub-trees:
Inductive hyp. asserts that our subtrees can be assembled into an
current sub-trees as “leaves.”
current sub-trees as leaves.
there is an optimal subtree where x and y are siblings.
containing as a subtree. Then we may as well replace it with a new letter with frequency
We’ve got a bunch of current sub-trees: x y say that x and y are the two smallest. a a w z
current sub-trees as “leaves”.
We’ve got a bunch of current sub-trees: x y w say that x and y are the two smallest. a a y x w z z
current sub-trees as “leaves.”
correspond to optimal trees on the original alphabet.
We’ve got a bunch of current sub-trees: x y w say that x and y are the two smallest. a a z w y x z
current sub-trees as “leaves.”
We’ve got a bunch of current sub-trees: x y w say that x and y are the two smallest. x y w a a = x+y z z w y x z
current sub-trees as “leaves.”
We’ve got a bunch of current sub-trees: x y w say that x and y are the two smallest. z w a y x x y w a a = x+y Lemma 1 implies that there’s an optimal sub-tree that looks like this; aka, what our algorithm did okay. z z
current sub-trees as “leaves.”
We’ve got a bunch of current sub-trees: x y w say that x and y are the two smallest. w a x y w a a = x+y Lemma 2 again says that there’s an optimal tree that looks like this z y x z z
current sub-trees as “leaves.”
We’ve got a bunch of current sub-trees: x y w say that x and y are the two smallest. w a x y w a a = x+y Lemma 2 again says that there’s an optimal tree that looks like this z y x z This is what we wanted to show for the inductive step. z
After the t’th step, we’ve got a bunch of current sub-trees:
Inductive hyp. asserts that our subtrees can be assembled into an
the distribution on letters isn’t uniform.
language efficiently, we can use a greedy algorithm.
correct
algorithm if: