![Page 1: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/1.jpg)
Greedy AlgorithmsPart One
![Page 2: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/2.jpg)
Announcements
● Problem Set Three due right now if using a late period.
● Solutions will be released at end of lecture.
![Page 3: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/3.jpg)
Outline for Today
● Greedy Algorithms● Can myopic, shortsighted decisions lead to
an optimal solution?
● Lilypad Jumping● Helping our amphibious friends home!
● Activity Selection● Planning your weekend!
![Page 4: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/4.jpg)
Frog Jumping
0 1 32 4 5 6 7 8 9 10
![Page 5: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/5.jpg)
Frog Jumping
0 1 32 4 5 6 7 8 9 10
![Page 6: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/6.jpg)
Frog Jumping
0 1 32 4 5 6 7 8 9 10
Max jump size: 3
![Page 7: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/7.jpg)
Frog Jumping
0 1 32 4 5 6 7 8 9 10
Max jump size: 3
![Page 8: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/8.jpg)
Frog Jumping
● The frog begins at position 0 in the river. Its goal is to get to position n.
● There are lilypads at various positions. There is always a lilypad at position 0 and position n.
● The frog can jump at most r units at a time.
● Goal: Find the path the frog should take to minimize jumps, assuming a solution exists.
![Page 9: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/9.jpg)
Frog Jumping
0 1 32 4 5 6 7 8 9 10
Max jump size: 3
![Page 10: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/10.jpg)
Frog Jumping
0 1 32 4 5 6 7 8 9 10
Max jump size: 3
![Page 11: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/11.jpg)
As a Graph
0 1 32 4 5 6 7 8 9 10
Max jump size: 3
![Page 12: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/12.jpg)
A Leap of Faith
0 1 32 4 5 6 7 8 9 10
Max jump size: 2 Algorithm: Always jump as far forward as possible.
Algorithm: Always jump as far forward as possible.
![Page 13: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/13.jpg)
A Leap of Faith
0 1 32 4 5 6 7 8 9 10
Max jump size: 4 Algorithm: Always jump as far forward as possible.
Algorithm: Always jump as far forward as possible.
![Page 14: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/14.jpg)
Formalizing the Algorithm
● Let J be an empty series of jumps.● Let our current position x = 0.● While x < n:
● Find the furthest lilypad l reachable from x that is not after position n.
● Add a jump to J from x to l's location.● Set x to l's location.
● Return J.
![Page 15: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/15.jpg)
Greedy Algorithms
● A greedy algorithm is an algorithm that constructs an object X one step at a time, at each step choosing the locally best option.
● In some cases, greedy algorithms construct the globally best object by repeatedly choosing the locally best option.
![Page 16: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/16.jpg)
Greedy Advantages
● Greedy algorithms have several advantages over other algorithmic approaches:● Simplicity: Greedy algorithms are often
easier to describe and code up than other algorithms.
● Efficiency: Greedy algorithms can often be implemented more efficiently than other algorithms.
![Page 17: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/17.jpg)
Greedy Challenges
● Greedy algorithms have several drawbacks:● Hard to design: Once you have found the
right greedy approach, designing greedy algorithms can be easy. However, finding the right approach can be hard.
● Hard to verify: Showing a greedy algorithm is correct often requires a nuanced argument.
![Page 18: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/18.jpg)
Back to Frog Jumping
● We now have a simple greedy algorithm for routing the frog home: jump as far forward as possible at each step.
● We need to prove two properties:
● The algorithm will find a legal series of jumps (i.e. it doesn't “get stuck”).
● The algorithm finds an optimal series of jumps (i.e. there isn't a better path available).
![Page 19: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/19.jpg)
Back to Frog Jumping
We now have a simple greedy algorithm for routing the frog home: jump as far forward as possible at each step.
We need to prove two properties:
● The algorithm will find a legal series of jumps (i.e. it doesn't “get stuck”).
The algorithm finds an optimal series of jumps (i.e. there isn't a better path available).
![Page 20: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/20.jpg)
0 1 32 4 5 6 7 8 9 10
![Page 21: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/21.jpg)
0 1 32 4 5 6 7 8 9 10
![Page 22: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/22.jpg)
0 1 32 4 5 6 7 8 9 10
![Page 23: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/23.jpg)
0 1 32 4 5 6 7 8 9 10
![Page 24: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/24.jpg)
0 1 32 4 5 6 7 8 9 10
![Page 25: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/25.jpg)
0 1 32 4 5 6 7 8 9 10
If there is any path at all, each lilypad must be at most r
distance ahead of the lilypad before it.
If there is any path at all, each lilypad must be at most r
distance ahead of the lilypad before it.
![Page 26: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/26.jpg)
Lemma 1: The greedy algorithm always finds a pathfrom the start lilypad to the destination lilypad.
Proof: By contradiction; suppose it did not. Let thepositions of the lilypads be x₁ < x₂ < … < xₘ. Sinceour algorithm didn't find a path, it must have stoppedat some lilypad xₖ and not been able to jump to afuture lilypad. In particular, this means it could notjump to lilypad k + 1, so xₖ + r < xₖ₊₁.
Since there is a path from lilypad 1 to the lilypad m, there must be some jump in that path that starts before lilypad k + 1 and ends at or after lilypad k + 1. This jump can't be made from lilypad k, so it must have been made from lilypad s for some s < k. But then we have xₛ + r < xₖ + r < xₖ₊₁, so this jump is illegal.
We have reached a contradiction, so our assumption was wrong and our algorithm always finds a path. ■
![Page 27: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/27.jpg)
Lemma 1: The greedy algorithm always finds a pathfrom the start lilypad to the destination lilypad.
Proof: By contradiction; suppose it did not. Let thepositions of the lilypads be x₁ < x₂ < … < xₘ. Sinceour algorithm didn't find a path, it must have stoppedat some lilypad xₖ and not been able to jump to afuture lilypad. In particular, this means it could notjump to lilypad k + 1, so xₖ + r < xₖ₊₁.
Since there is a path from lilypad 1 to the lilypad m, there must be some jump in that path that starts before lilypad k + 1 and ends at or after lilypad k + 1. This jump can't be made from lilypad k, so it must have been made from lilypad s for some s < k. But then we have xₛ + r < xₖ + r < xₖ₊₁, so this jump is illegal.
We have reached a contradiction, so our assumption was wrong and our algorithm always finds a path. ■
![Page 28: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/28.jpg)
Lemma 1: The greedy algorithm always finds a pathfrom the start lilypad to the destination lilypad.
Proof: By contradiction; suppose it did not. Let thepositions of the lilypads be x₁ < x₂ < … < xₘ. Sinceour algorithm didn't find a path, it must have stoppedat some lilypad xₖ and not been able to jump to afuture lilypad. In particular, this means it could notjump to lilypad k + 1, so xₖ + r < xₖ₊₁.
Since there is a path from lilypad 1 to the lilypad m, there must be some jump in that path that starts before lilypad k + 1 and ends at or after lilypad k + 1. This jump can't be made from lilypad k, so it must have been made from lilypad s for some s < k. But then we have xₛ + r < xₖ + r < xₖ₊₁, so this jump is illegal.
We have reached a contradiction, so our assumption was wrong and our algorithm always finds a path. ■
![Page 29: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/29.jpg)
Lemma 1: The greedy algorithm always finds a pathfrom the start lilypad to the destination lilypad.
Proof: By contradiction; suppose it did not. Let thepositions of the lilypads be x₁ < x₂ < … < xₘ. Sinceour algorithm didn't find a path, it must have stoppedat some lilypad xₖ and not been able to jump to afuture lilypad. In particular, this means it could notjump to lilypad k + 1, so xₖ + r < xₖ₊₁.
Since there is a path from lilypad 1 to the lilypad m, there must be some jump in that path that starts before lilypad k + 1 and ends at or after lilypad k + 1. This jump can't be made from lilypad k, so it must have been made from lilypad s for some s < k. But then we have xₛ + r < xₖ + r < xₖ₊₁, so this jump is illegal.
We have reached a contradiction, so our assumption was wrong and our algorithm always finds a path. ■
![Page 30: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/30.jpg)
Lemma 1: The greedy algorithm always finds a pathfrom the start lilypad to the destination lilypad.
Proof: By contradiction; suppose it did not. Let thepositions of the lilypads be x₁ < x₂ < … < xₘ. Sinceour algorithm didn't find a path, it must have stoppedat some lilypad xₖ and not been able to jump to afuture lilypad. In particular, this means it could notjump to lilypad k + 1, so xₖ + r < xₖ₊₁.
Since there is a path from lilypad 1 to the lilypad m, there must be some jump in that path that starts before lilypad k + 1 and ends at or after lilypad k + 1. This jump can't be made from lilypad k, so it must have been made from lilypad s for some s < k. But then we have xₛ + r < xₖ + r < xₖ₊₁, so this jump is illegal.
We have reached a contradiction, so our assumption was wrong and our algorithm always finds a path. ■
![Page 31: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/31.jpg)
Lemma 1: The greedy algorithm always finds a pathfrom the start lilypad to the destination lilypad.
Proof: By contradiction; suppose it did not. Let thepositions of the lilypads be x₁ < x₂ < … < xₘ. Sinceour algorithm didn't find a path, it must have stoppedat some lilypad xₖ and not been able to jump to afuture lilypad. In particular, this means it could notjump to lilypad k + 1, so xₖ + r < xₖ₊₁.
Since there is a path from lilypad 1 to the lilypad m, there must be some jump in that path that starts before lilypad k + 1 and ends at or after lilypad k + 1. This jump can't be made from lilypad k, so it must have been made from lilypad s for some s < k. But then we have xₛ + r < xₖ + r < xₖ₊₁, so this jump is illegal.
We have reached a contradiction, so our assumption was wrong and our algorithm always finds a path. ■
![Page 32: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/32.jpg)
Lemma 1: The greedy algorithm always finds a pathfrom the start lilypad to the destination lilypad.
Proof: By contradiction; suppose it did not. Let thepositions of the lilypads be x₁ < x₂ < … < xₘ. Sinceour algorithm didn't find a path, it must have stoppedat some lilypad xₖ and not been able to jump to afuture lilypad. In particular, this means it could notjump to lilypad k + 1, so xₖ + r < xₖ₊₁.
Since there is a path from lilypad 1 to the lilypad m, there must be some jump in that path that starts before lilypad k + 1 and ends at or after lilypad k + 1. This jump can't be made from lilypad k, so it must have been made from lilypad s for some s < k. But then we have xₛ + r < xₖ + r < xₖ₊₁, so this jump is illegal.
We have reached a contradiction, so our assumption was wrong and our algorithm always finds a path. ■
![Page 33: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/33.jpg)
Lemma 1: The greedy algorithm always finds a pathfrom the start lilypad to the destination lilypad.
Proof: By contradiction; suppose it did not. Let thepositions of the lilypads be x₁ < x₂ < … < xₘ. Sinceour algorithm didn't find a path, it must have stoppedat some lilypad xₖ and not been able to jump to afuture lilypad. In particular, this means it could notjump to lilypad k + 1, so xₖ + r < xₖ₊₁.
Since there is a path from lilypad 1 to the lilypad m, there must be some jump in that path that starts before lilypad k + 1 and ends at or after lilypad k + 1. This jump can't be made from lilypad k, so it must have been made from lilypad s for some s < k. But then we have xₛ + r < xₖ + r < xₖ₊₁, so this jump is illegal.
We have reached a contradiction, so our assumption was wrong and our algorithm always finds a path. ■
![Page 34: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/34.jpg)
Lemma 1: The greedy algorithm always finds a pathfrom the start lilypad to the destination lilypad.
Proof: By contradiction; suppose it did not. Let thepositions of the lilypads be x₁ < x₂ < … < xₘ. Sinceour algorithm didn't find a path, it must have stoppedat some lilypad xₖ and not been able to jump to afuture lilypad. In particular, this means it could notjump to lilypad k + 1, so xₖ + r < xₖ₊₁.
Since there is a path from lilypad 1 to the lilypad m, there must be some jump in that path that starts before lilypad k + 1 and ends at or after lilypad k + 1. This jump can't be made from lilypad k, so it must have been made from lilypad s for some s < k. But then we have xₛ + r < xₖ + r < xₖ₊₁, so this jump is illegal.
We have reached a contradiction, so our assumption was wrong and our algorithm always finds a path. ■
![Page 35: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/35.jpg)
Proving Optimality
● How can we prove this algorithm finds an optimal series of jumps?
● Key Proof Idea: Consider an arbitrary optimal series of jumps J*, then show that our greedy algorithm produces a series of jumps no worse than J*.● We don't know what J* is or that our
algorithm is necessarily optimal. However, we can still use the existence of J* in our proof.
![Page 36: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/36.jpg)
Some Notation
● Let J be the series of jumps produced by our algorithm and let J* be an optimal series of jumps.● Note that there might be multiple different
optimal jump patterns.
● Let | J| and | J*| denote the number of jumps in J and J*, respectively.
● Note that | J| ≥ | J*|. (Why?)
![Page 37: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/37.jpg)
0 1 32 4 5 6 7 8 9 10
Max jump size: 3
![Page 38: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/38.jpg)
0 1 32 4 5 6 7 8 9 10
Max jump size: 3
0 1 32 4 5 6 7 8 9 10
![Page 39: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/39.jpg)
0 1 32 4 5 6 7 8 9 10
Max jump size: 3
0 1 32 4 5 6 7 8 9 10
![Page 40: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/40.jpg)
The Key Lemma
● Let p(i, J) denote the frog's position after taking the first i jumps from jump series J.
● Lemma: For any i in 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).● After taking i jumps according to the greedy
algorithm, the frog will be at least as far forward as if she took i jumps according to the optimal solution.
● We can formalize this using induction.
![Page 41: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/41.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 42: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/42.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 43: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/43.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 44: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/44.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 45: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/45.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 46: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/46.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 47: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/47.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 48: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/48.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 49: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/49.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 50: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/50.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 51: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/51.jpg)
Lemma 2: For all 0 ≤ i ≤ | J*|, we have p(i, J) ≥ p(i, J*).
Proof: By induction. As a base case, if i = 0, thenp(0, J) = 0 ≥ 0 = p(0, J*) since the frog hasn't moved.
For the inductive step, assume that the claim holds for some 0 ≤ i < | J*|. We will prove the claim holds for i + 1 by considering two cases:
Case 1: p(i, J) ≥ p(i + 1, J*). Since each jump movesforward, we have p(i + 1, J) ≥ p(i, J), so we havep(i + 1, J) ≥ p(i + 1, J*).
Case 2: p(i, J) < p(i + 1, J*). Each jump is of size atmost r, so p(i + 1, J*) ≤ p(i, J*) + r. By our IH, weknow p(i, J) ≥ p(i, J*), so p(i + 1, J*) ≤ p(i, J) + r. Therefore, the greedy algorithm can jump toposition at least p(i + 1, J*). Therefore,p(i + 1, J) ≥ p(i + 1, J*).
So p(i + 1, J) ≥ p(i + 1, J*), completing the induction. ■
![Page 52: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/52.jpg)
Theorem: Let J be the series of jumps produced bythe greedy algorithm and J* be any optimal seriesof jumps. Then | J| = | J*|.
Proof: Since J* is an optimal solution, we know that| J*| ≤ | J|. We will prove | J*| ≥ | J|.
Suppose for contradiction that | J*| < | J|. Letk = | J*|. By Lemma 2, we have p(k, J*) ≤ p(k, J). Because the frog arrives at position n after k jumps along series J*, we know n ≤ p(k, J). Because the greedy algorithm never jumps past position n, we know p(k, J) ≤ n, so n = p(k, J). Since | J*| < | J|, the greedy algorithm must have taken another jump after its kth jump, contradicting that the algorithm stops after reaching position n.
We have reached a contradiction, so our assumption was wrong and | J*| = | J|, so the greedy algorithm produces an optimal solution. ■
![Page 53: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/53.jpg)
Theorem: Let J be the series of jumps produced bythe greedy algorithm and J* be any optimal seriesof jumps. Then | J| = | J*|.
Proof: Since J* is an optimal solution, we know that| J*| ≤ | J|. We will prove | J*| ≥ | J|.
Suppose for contradiction that | J*| < | J|. Letk = | J*|. By Lemma 2, we have p(k, J*) ≤ p(k, J). Because the frog arrives at position n after k jumps along series J*, we know n ≤ p(k, J). Because the greedy algorithm never jumps past position n, we know p(k, J) ≤ n, so n = p(k, J). Since | J*| < | J|, the greedy algorithm must have taken another jump after its kth jump, contradicting that the algorithm stops after reaching position n.
We have reached a contradiction, so our assumption was wrong and | J*| = | J|, so the greedy algorithm produces an optimal solution. ■
![Page 54: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/54.jpg)
Theorem: Let J be the series of jumps produced bythe greedy algorithm and J* be any optimal seriesof jumps. Then | J| = | J*|.
Proof: Since J* is an optimal solution, we know that| J*| ≤ | J|. We will prove | J*| ≥ | J|.
Suppose for contradiction that | J*| < | J|. Letk = | J*|. By Lemma 2, we have p(k, J*) ≤ p(k, J). Because the frog arrives at position n after k jumps along series J*, we know n ≤ p(k, J). Because the greedy algorithm never jumps past position n, we know p(k, J) ≤ n, so n = p(k, J). Since | J*| < | J|, the greedy algorithm must have taken another jump after its kth jump, contradicting that the algorithm stops after reaching position n.
We have reached a contradiction, so our assumption was wrong and | J*| = | J|, so the greedy algorithm produces an optimal solution. ■
![Page 55: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/55.jpg)
Theorem: Let J be the series of jumps produced bythe greedy algorithm and J* be any optimal seriesof jumps. Then | J| = | J*|.
Proof: Since J* is an optimal solution, we know that| J*| ≤ | J|. We will prove | J*| ≥ | J|.
Suppose for contradiction that | J*| < | J|. Letk = | J*|. By Lemma 2, we have p(k, J*) ≤ p(k, J). Because the frog arrives at position n after k jumps along series J*, we know n ≤ p(k, J). Because the greedy algorithm never jumps past position n, we know p(k, J) ≤ n, so n = p(k, J). Since | J*| < | J|, the greedy algorithm must have taken another jump after its kth jump, contradicting that the algorithm stops after reaching position n.
We have reached a contradiction, so our assumption was wrong and | J*| = | J|, so the greedy algorithm produces an optimal solution. ■
![Page 56: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/56.jpg)
Theorem: Let J be the series of jumps produced bythe greedy algorithm and J* be any optimal seriesof jumps. Then | J| = | J*|.
Proof: Since J* is an optimal solution, we know that| J*| ≤ | J|. We will prove | J*| ≥ | J|.
Suppose for contradiction that | J*| < | J|. Letk = | J*|. By Lemma 2, we have p(k, J*) ≤ p(k, J). Because the frog arrives at position n after k jumps along series J*, we know n ≤ p(k, J). Because the greedy algorithm never jumps past position n, we know p(k, J) ≤ n, so n = p(k, J). Since | J*| < | J|, the greedy algorithm must have taken another jump after its kth jump, contradicting that the algorithm stops after reaching position n.
We have reached a contradiction, so our assumption was wrong and | J*| = | J|, so the greedy algorithm produces an optimal solution. ■
![Page 57: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/57.jpg)
Theorem: Let J be the series of jumps produced bythe greedy algorithm and J* be any optimal seriesof jumps. Then | J| = | J*|.
Proof: Since J* is an optimal solution, we know that| J*| ≤ | J|. We will prove | J*| ≥ | J|.
Suppose for contradiction that | J*| < | J|. Letk = | J*|. By Lemma 2, we have p(k, J*) ≤ p(k, J). Because the frog arrives at position n after k jumps along series J*, we know n ≤ p(k, J). Because the greedy algorithm never jumps past position n, we know p(k, J) ≤ n, so n = p(k, J). Since | J*| < | J|, the greedy algorithm must have taken another jump after its kth jump, contradicting that the algorithm stops after reaching position n.
We have reached a contradiction, so our assumption was wrong and | J*| = | J|, so the greedy algorithm produces an optimal solution. ■
![Page 58: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/58.jpg)
Theorem: Let J be the series of jumps produced bythe greedy algorithm and J* be any optimal seriesof jumps. Then | J| = | J*|.
Proof: Since J* is an optimal solution, we know that| J*| ≤ | J|. We will prove | J*| ≥ | J|.
Suppose for contradiction that | J*| < | J|. Letk = | J*|. By Lemma 2, we have p(k, J*) ≤ p(k, J). Because the frog arrives at position n after k jumps along series J*, we know n ≤ p(k, J). Because the greedy algorithm never jumps past position n, we know p(k, J) ≤ n, so n = p(k, J). Since | J*| < | J|, the greedy algorithm must have taken another jump after its kth jump, contradicting that the algorithm stops after reaching position n.
We have reached a contradiction, so our assumption was wrong and | J*| = | J|, so the greedy algorithm produces an optimal solution. ■
![Page 59: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/59.jpg)
Theorem: Let J be the series of jumps produced bythe greedy algorithm and J* be any optimal seriesof jumps. Then | J| = | J*|.
Proof: Since J* is an optimal solution, we know that| J*| ≤ | J|. We will prove | J*| ≥ | J|.
Suppose for contradiction that | J*| < | J|. Letk = | J*|. By Lemma 2, we have p(k, J*) ≤ p(k, J). Because the frog arrives at position n after k jumps along series J*, we know n ≤ p(k, J). Because the greedy algorithm never jumps past position n, we know p(k, J) ≤ n, so n = p(k, J). Since | J*| < | J|, the greedy algorithm must have taken another jump after its kth jump, contradicting that the algorithm stops after reaching position n.
We have reached a contradiction, so our assumption was wrong and | J*| = | J|, so the greedy algorithm produces an optimal solution. ■
![Page 60: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/60.jpg)
Greedy Stays Ahead
● The style of proof we just wrote is an example of a greedy stays ahead proof.
● The general proof structure is the following:● Find a series of measurements M₁, M₂, …, Mₖ
you can apply to any solution.● Show that the greedy algorithm's measures are
at least as good as any solution's measures. (This usually involves induction.)
● Prove that because the greedy solution's measures are at least as good as any solution's measures, the greedy solution must be optimal. (This is usually a proof by contradiction.)
![Page 61: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/61.jpg)
Another Problem:Activity Scheduling
![Page 62: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/62.jpg)
Activity Scheduling3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 63: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/63.jpg)
Activity Scheduling3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 64: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/64.jpg)
Activity Scheduling3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 65: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/65.jpg)
Activity Scheduling
● You are given a list of activities (s₁, e₁), (s₂ , e₂), …, (sₙ, eₙ) denoted by their start and end times.
● All activities are equally attractive to you, and you want to maximize the number of activities you do.
● Goal: Choose the largest number of non-overlapping activities possible.
![Page 66: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/66.jpg)
Thinking Greedily
● If we want to try solving this using a greedy approach, we should think about different ways of picking activities greedily.
● A few options:● Be Impulsive: Choose activities in ascending
order of start times.● Avoid Commitment: Choose activities in
ascending order of length.● Finish Fast: Choose activities in ascending
order of end times.
![Page 67: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/67.jpg)
Be Impulsive3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 68: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/68.jpg)
Be Impulsive3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 69: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/69.jpg)
Be Impulsive3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 70: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/70.jpg)
Be Impulsive3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Jazz Concert
![Page 71: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/71.jpg)
Be Impulsive3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Jazz Concert
![Page 72: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/72.jpg)
Be Impulsive3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Navel Gazing
Bonfire
Jazz Concert
![Page 73: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/73.jpg)
Be Impulsive3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Navel Gazing
Bonfire
Jazz Concert
![Page 74: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/74.jpg)
Be Impulsive3 4 5 6 7 8 9 10 11 12 1
Llama Hugging
Navel Gazing
Bonfire
![Page 75: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/75.jpg)
Impulse Control3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
Day Trip
![Page 76: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/76.jpg)
Impulse Control3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
Day Trip
![Page 77: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/77.jpg)
Impulse Control3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
Day Trip
![Page 78: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/78.jpg)
Impulse Control3 4 5 6 7 8 9 10 11 12 1
Day Trip
![Page 79: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/79.jpg)
Impulse Control3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
Day Trip
![Page 80: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/80.jpg)
Impulse Control3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
![Page 81: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/81.jpg)
Thinking Greedily
● If we want to try solving this using a greedy approach, we should think about different ways of picking activities greedily.
● A few options:● Be Impulsive: Choose activities in ascending
order of start times.● Avoid Commitment: Choose activities in
ascending order of length.● Finish Fast: Choose activities in ascending
order of end times.
![Page 82: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/82.jpg)
Thinking Greedily
● If we want to try solving this using a greedy approach, we should think about different ways of picking activities greedily.
● A few options:● Be Impulsive: Choose activities in ascending
order of start times.● Avoid Commitment: Choose activities in
ascending order of length.● Finish Fast: Choose activities in ascending
order of end times.
![Page 83: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/83.jpg)
Avoid Commitment3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 84: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/84.jpg)
Avoid Commitment3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 85: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/85.jpg)
Avoid Commitment3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 86: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/86.jpg)
Avoid Commitment3 4 5 6 7 8 9 10 11 12 1
Llama Hugging
Gardening
Skydiving
Fancy Dinner
Tree Climbing
![Page 87: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/87.jpg)
Avoid Commitment3 4 5 6 7 8 9 10 11 12 1
Llama Hugging
Gardening
Skydiving
Fancy Dinner
Tree Climbing
![Page 88: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/88.jpg)
Avoid Commitment3 4 5 6 7 8 9 10 11 12 1
Gardening Fancy Dinner
![Page 89: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/89.jpg)
Thinking Greedily
● If we want to try solving this using a greedy approach, we should think about different ways of picking activities greedily.
● A few options:● Be Impulsive: Choose activities in ascending
order of start times.● Avoid Commitment: Choose activities in
ascending order of length.● Finish Fast: Choose activities in ascending
order of end times.
![Page 90: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/90.jpg)
Thinking Greedily
● If we want to try solving this using a greedy approach, we should think about different ways of picking activities greedily.
● A few options:● Be Impulsive: Choose activities in ascending
order of start times.● Avoid Commitment: Choose activities in
ascending order of length.● Finish Fast: Choose activities in ascending
order of end times.
![Page 91: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/91.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 92: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/92.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 93: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/93.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging
Gardening
Skydiving
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Tree Climbing
Jazz Concert
Evening Hike
![Page 94: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/94.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Gardening
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Jazz Concert
![Page 95: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/95.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Gardening
Navel Gazing
Fancy Dinner
Salsa Dancing
Bar Crawling
Bonfire
Jazz Concert
![Page 96: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/96.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Gardening
Navel Gazing
Bonfire
Jazz Concert
![Page 97: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/97.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Gardening
Navel Gazing
Bonfire
Jazz Concert
![Page 98: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/98.jpg)
Finish Fast3 4 5 6 7 8 9 10 11 12 1
Gardening
Navel Gazing
Bonfire
![Page 99: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/99.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
Day Trip
![Page 100: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/100.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
Day Trip
![Page 101: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/101.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
Day Trip
![Page 102: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/102.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
![Page 103: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/103.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
![Page 104: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/104.jpg)
Finish Fast3 4 5 6 7 8 9 10
Night Snorkeling
11 12 1
Llama Hugging Salsa Dancing
![Page 105: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/105.jpg)
Thinking Greedily
● Of the three options we saw, only the third one seems to work:
Choose activities in ascending order of finishing times.
● More formally:● Sort the activities into ascending order by
finishing time and add them to a set U.● While U is not empty:
– Choose any activity with the earliest finishing time.– Add that activity to S.– Remove from U all activities that overlap S.
![Page 106: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/106.jpg)
Proving Legality
● Lemma: The schedule produced this way is a legal schedule.
● Proof Idea: Use induction to show that at each step, the set U only contains activities that don't conflict with activities picked from S.
![Page 107: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/107.jpg)
Proving Optimality
● To prove that the schedule S produced by the algorithm is optimal, we will use another “greedy stays ahead” argument:● Find some measures by which the algorithm
is at least as good as any other solution.● Show that those measures mean that the
algorithm must produce an optimal solution.
![Page 108: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/108.jpg)
Comparing Solutions3 4 5 6 7 8 9 10
Cupcake Baking
11 12 1
Muffin Collecting
Gallivanting
Pondering
Wandering
Fancy Dinner
Basket Weaving
Meandering
Clubbing
Movies
![Page 109: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/109.jpg)
Comparing Solutions3 4 5 6 7 8 9 10
Cupcake Baking
11 12 1
Muffin Collecting
Gallivanting
Pondering
Wandering
Fancy Dinner
Basket Weaving
Meandering
Clubbing
Movies
![Page 110: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/110.jpg)
Comparing Solutions3 4 5 6 7 8 9 10
Cupcake Baking
11 12 1
Muffin Collecting
Gallivanting
Pondering
Wandering
Fancy Dinner
Basket Weaving
Meandering
Clubbing
Movies
![Page 111: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/111.jpg)
Comparing Solutions3 4 5 6 7 8 9 10
Cupcake Baking
11 12 1
Muffin Collecting
Gallivanting
Pondering
Wandering
Fancy Dinner
Basket Weaving
Meandering
Clubbing
Movies
![Page 112: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/112.jpg)
Comparing Solutions3 4 5 6 7 8 9 10
Cupcake Baking
11 12 1
Muffin Collecting
Gallivanting
Pondering
Wandering
Fancy Dinner
Basket Weaving
Meandering
Clubbing
Movies
![Page 113: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/113.jpg)
Comparing Solutions3 4 5 6 7 8 9 10
Cupcake Baking
11 12 1
Muffin Collecting
Gallivanting
Pondering
Wandering
Fancy Dinner
Basket Weaving
Meandering
Clubbing
Movies
![Page 114: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/114.jpg)
Comparing Solutions3 4 5 6 7 8 9 10
Cupcake Baking
11 12 1
Muffin Collecting
Gallivanting
Pondering
Wandering
Fancy Dinner
Basket Weaving
Meandering
Clubbing
Movies
![Page 115: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/115.jpg)
Greedy Stays Ahead
● Observation: The kth activity chosen by the greedy algorithm finishes no later than the kth activity chosen in any legal schedule.
● We need to● Prove that this is actually true, and● Show that, if it's true, the algorithm is
optimal.
● We'll do this out of order.
![Page 116: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/116.jpg)
Some Notation
● Let S be the schedule our algorithm produces and S* be any optimal schedule.
● Note that |S| ≤ |S*|.● Let f(i, S) denote the time that the ith
activity finishes in schedule S.● Lemma: For any 1 ≤ i ≤ |S|, we have
f(i, S) ≤ f(i, S*).
![Page 117: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/117.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 118: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/118.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 119: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/119.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 120: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/120.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 121: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/121.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 122: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/122.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 123: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/123.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 124: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/124.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 125: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/125.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 126: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/126.jpg)
Theorem: The greedy algorithm for activity selectionproduces an optimal schedule.
Proof: Let S be the schedule the algorithm producedand S* be any optimal schedule. Since S* isoptimal, we have |S| ≤ |S*|. We will prove |S| ≥ |S*|.
Assume for contradiction that |S| < |S*|. Let k = |S|. By our lemma, we know f(k, S) ≤ f(k, S*), so the kth activity in S finishes no later than the kth activity in S*. Since |S| < |S*|, there is a (k + 1)st activity in S*, and its start time must be after f(k, S*) and therefore after f(k, S). Thus after the greedy algorithm added its kth activity to S, the (k + 1)st activity from S* would still belong to U. But the greedy algorithm ended after k activities, so U must have been empty.
We have reached a contradiction, so our assumption must have been wrong. Thus the greedy algorithm must be optimal. ■
![Page 127: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/127.jpg)
Lemma: If S is a schedule produced by the greedyalgorithm and S* is an optimal schedule, then forany 1 ≤ i ≤ |S|, we have f(i, S) ≤ f(i, S*).
Proof: By induction. For our base case, we provef(1, S) ≤ f(1, S*). The first activity the greedyalgorithm selects must be an activity that ends nolater than any other activity, so f(1, S) ≤ f(1, S*).
For the inductive step, assume the claim holds for some i in 1 ≤ i < |S|. Since f(i, S) ≤ f(i, S*), the ith activity in S finishes before the ith activity in S*. Since the (i+1)st activity in S* must start after the ith activity in S* ends, the (i + 1)st activity in S* must start after the ith activity in S ends. Therefore, the (i+1)st activity in S* must be in U when the greedy algorithm selects its (i+1)st activity. Since the greedy algorithm selects the activity in U with the lowest end time, we have f(i + 1, S) ≤ f(i, S*), completing the induction. ■
![Page 128: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/128.jpg)
Lemma: If S is a schedule produced by the greedyalgorithm and S* is an optimal schedule, then forany 1 ≤ i ≤ |S|, we have f(i, S) ≤ f(i, S*).
Proof: By induction. For our base case, we provef(1, S) ≤ f(1, S*). The first activity the greedyalgorithm selects must be an activity that ends nolater than any other activity, so f(1, S) ≤ f(1, S*).
For the inductive step, assume the claim holds for some i in 1 ≤ i < |S|. Since f(i, S) ≤ f(i, S*), the ith activity in S finishes before the ith activity in S*. Since the (i+1)st activity in S* must start after the ith activity in S* ends, the (i + 1)st activity in S* must start after the ith activity in S ends. Therefore, the (i+1)st activity in S* must be in U when the greedy algorithm selects its (i+1)st activity. Since the greedy algorithm selects the activity in U with the lowest end time, we have f(i + 1, S) ≤ f(i, S*), completing the induction. ■
![Page 129: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/129.jpg)
Lemma: If S is a schedule produced by the greedyalgorithm and S* is an optimal schedule, then forany 1 ≤ i ≤ |S|, we have f(i, S) ≤ f(i, S*).
Proof: By induction. For our base case, we provef(1, S) ≤ f(1, S*). The first activity the greedyalgorithm selects must be an activity that ends nolater than any other activity, so f(1, S) ≤ f(1, S*).
For the inductive step, assume the claim holds for some i in 1 ≤ i < |S|. Since f(i, S) ≤ f(i, S*), the ith activity in S finishes before the ith activity in S*. Since the (i+1)st activity in S* must start after the ith activity in S* ends, the (i + 1)st activity in S* must start after the ith activity in S ends. Therefore, the (i+1)st activity in S* must be in U when the greedy algorithm selects its (i+1)st activity. Since the greedy algorithm selects the activity in U with the lowest end time, we have f(i + 1, S) ≤ f(i, S*), completing the induction. ■
![Page 130: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/130.jpg)
Lemma: If S is a schedule produced by the greedyalgorithm and S* is an optimal schedule, then forany 1 ≤ i ≤ |S|, we have f(i, S) ≤ f(i, S*).
Proof: By induction. For our base case, we provef(1, S) ≤ f(1, S*). The first activity the greedyalgorithm selects must be an activity that ends nolater than any other activity, so f(1, S) ≤ f(1, S*).
For the inductive step, assume the claim holds for some i in 1 ≤ i < |S|. Since f(i, S) ≤ f(i, S*), the ith activity in S finishes before the ith activity in S*. Since the (i+1)st activity in S* must start after the ith activity in S* ends, the (i + 1)st activity in S* must start after the ith activity in S ends. Therefore, the (i+1)st activity in S* must be in U when the greedy algorithm selects its (i+1)st activity. Since the greedy algorithm selects the activity in U with the lowest end time, we have f(i + 1, S) ≤ f(i, S*), completing the induction. ■
![Page 131: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/131.jpg)
Lemma: If S is a schedule produced by the greedyalgorithm and S* is an optimal schedule, then forany 1 ≤ i ≤ |S|, we have f(i, S) ≤ f(i, S*).
Proof: By induction. For our base case, we provef(1, S) ≤ f(1, S*). The first activity the greedyalgorithm selects must be an activity that ends nolater than any other activity, so f(1, S) ≤ f(1, S*).
For the inductive step, assume the claim holds for some i in 1 ≤ i < |S|. Since f(i, S) ≤ f(i, S*), the ith activity in S finishes before the ith activity in S*. Since the (i+1)st activity in S* must start after the ith activity in S* ends, the (i + 1)st activity in S* must start after the ith activity in S ends. Therefore, the (i+1)st activity in S* must be in U when the greedy algorithm selects its (i+1)st activity. Since the greedy algorithm selects the activity in U with the lowest end time, we have f(i + 1, S) ≤ f(i, S*), completing the induction. ■
![Page 132: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/132.jpg)
Lemma: If S is a schedule produced by the greedyalgorithm and S* is an optimal schedule, then forany 1 ≤ i ≤ |S|, we have f(i, S) ≤ f(i, S*).
Proof: By induction. For our base case, we provef(1, S) ≤ f(1, S*). The first activity the greedyalgorithm selects must be an activity that ends nolater than any other activity, so f(1, S) ≤ f(1, S*).
For the inductive step, assume the claim holds for some i in 1 ≤ i < |S|. Since f(i, S) ≤ f(i, S*), the ith activity in S finishes before the ith activity in S*. Since the (i+1)st activity in S* must start after the ith activity in S* ends, the (i + 1)st activity in S* must start after the ith activity in S ends. Therefore, the (i+1)st activity in S* must be in U when the greedy algorithm selects its (i+1)st activity. Since the greedy algorithm selects the activity in U with the lowest end time, we have f(i + 1, S) ≤ f(i, S*), completing the induction. ■
![Page 133: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/133.jpg)
Lemma: If S is a schedule produced by the greedyalgorithm and S* is an optimal schedule, then forany 1 ≤ i ≤ |S|, we have f(i, S) ≤ f(i, S*).
Proof: By induction. For our base case, we provef(1, S) ≤ f(1, S*). The first activity the greedyalgorithm selects must be an activity that ends nolater than any other activity, so f(1, S) ≤ f(1, S*).
For the inductive step, assume the claim holds for some i in 1 ≤ i < |S|. Since f(i, S) ≤ f(i, S*), the ith activity in S finishes before the ith activity in S*. Since the (i+1)st activity in S* must start after the ith activity in S* ends, the (i + 1)st activity in S* must start after the ith activity in S ends. Therefore, the (i+1)st activity in S* must be in U when the greedy algorithm selects its (i+1)st activity. Since the greedy algorithm selects the activity in U with the lowest end time, we have f(i + 1, S) ≤ f(i, S*), completing the induction. ■
![Page 134: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/134.jpg)
Lemma: If S is a schedule produced by the greedyalgorithm and S* is an optimal schedule, then forany 1 ≤ i ≤ |S|, we have f(i, S) ≤ f(i, S*).
Proof: By induction. For our base case, we provef(1, S) ≤ f(1, S*). The first activity the greedyalgorithm selects must be an activity that ends nolater than any other activity, so f(1, S) ≤ f(1, S*).
For the inductive step, assume the claim holds for some i in 1 ≤ i < |S|. Since f(i, S) ≤ f(i, S*), the ith activity in S finishes before the ith activity in S*. Since the (i+1)st activity in S* must start after the ith activity in S* ends, the (i + 1)st activity in S* must start after the ith activity in S ends. Therefore, the (i+1)st activity in S* must be in U when the greedy algorithm selects its (i+1)st activity. Since the greedy algorithm selects the activity in U with the lowest end time, we have f(i + 1, S) ≤ f(i, S*), completing the induction. ■
![Page 135: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/135.jpg)
Lemma: If S is a schedule produced by the greedyalgorithm and S* is an optimal schedule, then forany 1 ≤ i ≤ |S|, we have f(i, S) ≤ f(i, S*).
Proof: By induction. For our base case, we provef(1, S) ≤ f(1, S*). The first activity the greedyalgorithm selects must be an activity that ends nolater than any other activity, so f(1, S) ≤ f(1, S*).
For the inductive step, assume the claim holds for some i in 1 ≤ i < |S|. Since f(i, S) ≤ f(i, S*), the ith activity in S finishes before the ith activity in S*. Since the (i+1)st activity in S* must start after the ith activity in S* ends, the (i + 1)st activity in S* must start after the ith activity in S ends. Therefore, the (i+1)st activity in S* must be in U when the greedy algorithm selects its (i+1)st activity. Since the greedy algorithm selects the activity in U with the lowest end time, we havef(i + 1, S) ≤ f(i + 1, S*), completing the induction. ■
![Page 136: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/136.jpg)
Summary
● Greedy algorithms aim for global optimality by iteratively making a locally optimal decision.
● To show correctness, typically need to show● The algorithm produces a legal answer, and● The algorithm produces an optimal answer.
● Often use “greedy stays ahead” to show optimality.
![Page 137: Greedy Algorithms - web.stanford.edu€¦ · Frog Jumping The frog begins at position 0 in the river. Its goal is to get to position n. There are lilypads at various positions. There](https://reader035.vdocument.in/reader035/viewer/2022081421/5f0eadf07e708231d44067c3/html5/thumbnails/137.jpg)
Next Time
● Minimum Spanning Trees● Prim's Algorithm● Exchange Arguments