Docsity

Greedy Algorithms: A Comprehensive Guide with Examples and Proofs, Lecture notes of Algorithms and Programming

Partial preview of the text

Download Greedy Algorithms: A Comprehensive Guide with Examples and Proofs and more Lecture notes Algorithms and Programming in PDF only on Docsity!

Greedy Algorithms

Part One

Announcements

●

Problem Set Three due right now if using

a late period.

●

Solutions will be released at end of

lecture.

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.

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.

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.

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.

Lemma 1: The greedy algorithm always finds a path from the start lilypad to the destination lilypad. Proof: By contradiction; suppose it did not. Let the positions of the lilypads be x ₁ < x ₂ < … < xₘ. Since our algorithm didn't find a path, it must have stopped at some lilypad xₖ and not been able to jump to a future lilypad. In particular, this means it could not jump 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. ■

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.

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.

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, then p (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 moves forward, we have p ( i + 1, J ) ≥ p ( i , J ), so we have p ( i + 1, J ) ≥ p ( i + 1, J* ). Case 2: p ( i , J ) < p ( i + 1, J* ). Each jump is of size at most r , so p ( i + 1, J* ) ≤ p ( i , J* ) + r. By our IH, we know p ( i , J ) ≥ p ( i , J ), so p ( i + 1, _J_ ) ≤ p ( i , J ) + r. Therefore, the greedy algorithm can jump to position 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. ■

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.)

Another Problem: Activity Scheduling

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.

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.