CSE 421 Section 3
Greedy Algorithms
How to Approach Writing an Algorithm
Writing an Algorithm
The Strategy
1. Read and Understand the Problem
No really, we mean it!
Ask Yourself some Questions:
Problem 1 – Line Covering
Problem 1.1 – Line Covering
Work reading through this problem with the people around you and answering the four questions, and then we’ll go over it together!
Problem 1.1 – Line Covering
Problem 1.1 – Line Covering
“cover” means “place a truck at most 3 miles from”
Problem 1.1 – Line Covering
“cover” means “place a truck at most 3 miles from”
A list of integers
Problem 1.1 – Line Covering
“cover” means “place a truck at most 3 miles from”
A list of integers
A list of integers
Problem 1.1 – Line Covering
“cover” means “place a truck at most 3 miles from”
A list of integers
A list of integers
list
2. Generate Examples
Examples Help us Understand!
Problem 1.2 – Line Covering
Generate two examples with their associated outputs. Put some effort into these! The more different from each other they are, the more likely you are to catch mistakes later.
Work through reading this problem with the people around you and answering the four questions, and then we’ll go over it together!
Problem 1.2 – Line Covering
Generate two examples with their associated outputs. Put some effort into these! The more different from each other they are, the more likely you are to catch mistakes later.
Problem 1.2 – Line Covering
Generate two examples with their associated outputs. Put some effort into these! The more different from each other they are, the more likely you are to catch mistakes later.
3. Come Up with a Baseline
Inefficient but Effective First Attempt
4. Brainstorm and Analyze �Possible Algorithms
Think about Algorithm Possibilities
Remember: Your First Guess Might Not Be Right!
Problem 1.4 – Line Covering
For this problem today, you should use a greedy algorithm.
Come up with at least two greedy ideas (which may or may not work). Run each of your ideas through the examples you generated in part 2.
Think of some greedy ideas with the people around you, and then we’ll go over it together!
Problem 1.4 – Line Covering
Come up with at least two greedy ideas (which may or may not work). Run each of your ideas through the examples you generated in part 2.
Problem 1.4 – Line Covering
Come up with at least two greedy ideas (which may or may not work). Run each of your ideas through the examples you generated in part 2.
Try your Algorithm Ideas on your Examples
Problem 1.4 – Line Covering
Come up with at least two greedy ideas (which may or may not work). Run each of your ideas through the examples you generated in part 2.
5. Write an Algorithm
Write That Pseudocode!
Problem 1.5 – Line Covering
function Placements(A[1..n])
uncoveredIndex ← 1
sites ← empty list
while uncoveredIndex ≤ n do
newSite ← A[uncoveredIndex] + 3
append newSite to sites.
while uncoveredIndex ≤ n and |A[uncoveredIndex] − newSite| ≤ 3 do
uncoveredIndex++
return sites
6. Show Your Algorithm is Correct
Writing the Algo isn’t Enough… �We Need to Prove that it Works!
Problem 1.6 – Line Covering
Write a proof of correctness.
Try to use one of those greedy proof techniques with the people around you, and then we’ll go over it together!
Problem 1.6 – Line Covering
Problem 1.6 – Line Covering
7. Optimize and Analyze the Run Time
Just Like Back in 332…
Problem 1.7 – Line Covering
Write the big-O of your code and justify the running time with a few sentences.
Problem 1.7 – Line Covering
Write the big-O of your code and justify the running time with a few sentences.
That’s All, Folks!
Thanks for coming to section this week!
Any questions?