Docsity

CS 161 — Greedy Algorithms, Exams of Nursing

Toggle sidebar
bg1
CS 161 — Greedy Algorithms
Table of Contents
1. What Is a Greedy Algorithm?
2. When Greedy Works
3. How to Prove Correctness
4. Activity Selection
5. Job Scheduling
6. Huffman Coding
7. Minimum Spanning Trees
8. Greedy versus Dynamic Programming
9. Common Mistakes
10. Official CS 161 References
What Is a Greedy Algorithm?
A greedy algorithm:
1. Makes a choice that looks best at the current step.
2. Commits to that choice.
3. Never backtracks.
4. Repeats until the problem is solved.
Stanford describes the idea as: “Make choices one-at-a-time. Never look back.”
Example intuition
For scheduling jobs, a greedy algorithm might choose the job with the best immediate
ratio of importance to duration. For activity selection, it chooses the compatible activity
that finishes earliest.
The challenge is not making the greedy choice—it is proving that the choice is safe.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS 161 — Greedy Algorithms and more Exams Nursing in PDF only on Docsity!

CS 161 — Greedy Algorithms

Table of Contents

  1. What Is a Greedy Algorithm?
  2. When Greedy Works
  3. How to Prove Correctness
  4. Activity Selection
  5. Job Scheduling
  6. Huffman Coding
  7. Minimum Spanning Trees
  8. Greedy versus Dynamic Programming
  9. Common Mistakes
  10. Official CS 161 References

What Is a Greedy Algorithm?

A greedy algorithm:

  1. Makes a choice that looks best at the current step.
  2. Commits to that choice.
  3. Never backtracks.
  4. Repeats until the problem is solved.

Stanford describes the idea as: “Make choices one-at-a-time. Never look back.”

Example intuition

For scheduling jobs, a greedy algorithm might choose the job with the best immediate ratio of importance to duration. For activity selection, it chooses the compatible activity that finishes earliest.

The challenge is not making the greedy choice—it is proving that the choice is safe.

When Greedy Works

A greedy algorithm is guaranteed to be correct when the problem has:

Property Meaning

Optimal substructure

An optimal solution contains optimal solutions to subproblems

Greedy-choice property

A locally optimal choice can be extended to a globally optimal solution

Stanford’s notes state that proving a greedy algorithm correct requires identifying optimal substructure, then showing that at each step only one subproblem needs to be considered.

Important warning

Not every problem has a greedy solution. A greedy rule that works for one optimization problem may fail for another, even if the problems look similar. Stanford’s practice materials explicitly note that greedy algorithms may fail and require counterexamples.

How to Prove Correctness

The standard proof technique is an exchange argument.

Exchange-argument template

1. Let G G be the greedy solution.

2. Let O O be an optimal solution.

3. Assume G≠O G O.

  1. Find the first place where they differ.

5. Exchange part of O O with part of G G.

  1. Show the exchange does not worsen the objective.
  2. Conclude that there is an optimal solution consistent with the greedy choice.

8. Repeat until G=O G = O.

If an optimal solution uses some other first activity am am , replace am am with ak ak.

Since ak ak finishes no later than am am , every activity compatible with am am remains

compatible with ak ak. Therefore, the exchange does not reduce the number of selected

activities.

Runtime

 If activities are already sorted by finish time: O(n) O ( n )

 If sorting is required: O(nlogn) O ( n log n )

Stanford’s slides give exactly these bounds.

Job Scheduling

Problem

Given n n jobs that share one resource, each job i i has:

 Length ti ti , the time required to complete it

 Cost of delay ci ci , representing the penalty incurred while waiting

The goal is to order the jobs to minimize total delay cost.

Greedy rule

Schedule jobs in decreasing order of:

ri=citi ri = tici

That is, prioritize jobs with the largest cost-of-delay-to-duration ratio.

Exchange proof

Suppose job B B has the largest ratio ci/ti ci / ti , but is not first in an optimal schedule. Let

job A A be scheduled immediately before B B.

Swapping A A and B B changes only the delay costs of A A and B B. Stanford’s slides

show that the total cost does not increase. Repeating the exchange moves B B to the

front without loss of optimality.

Runtime

Sorting by ratio takes:

O(nlogn) O ( n log n )

After sorting, the schedule is produced directly.

Huffman Coding

Problem

Given characters and their frequencies, construct a prefix-free binary encoding that minimizes the total encoded length.

A prefix-free code ensures that no codeword is a prefix of another codeword. This allows unambiguous decoding.

Greedy rule

Repeatedly merge the two subtrees with the smallest total frequencies.

Algorithm

text HUFFMAN(frequencies): create one leaf node for each character

while more than one node remains: x = node with smallest frequency y = node with second-smallest frequency

create parent z left child of z = x right child of z = y frequency(z) = frequency(x) + frequency(y)

insert z into the collection

return the remaining node

Why the rule works

Kruskal’s Algorithm

Kruskal’s algorithm grows a forest of disjoint trees.

text KRUSKAL(G): T = empty set sort edges by increasing weight

for each edge (u, v) in sorted order: if u and v are in different components: add (u, v) to T union their components

return T

Kruskal considers edges in increasing weight order and adds an edge only if it does not create a cycle.

Correctness

Both algorithms rely on the cut property :

For any cut (S,V−S)( S , V − S ), if edge e e is the minimum-weight edge crossing the cut,

then some MST contains e e.

For Prim, the chosen edge is the lightest edge crossing the current tree-to-rest cut. For Kruskal, each accepted edge is the lightest edge crossing the cut between its two connected components.

Stanford’s notes state that the correctness proofs for both Prim and Kruskal use exchange arguments.

Greedy versus Dynamic Programming

Feature Greedy algorithm Dynamic programming

Decision

strategy Makes one locally optimal choice

Considers multiple subproblem outcomes

Backtracking Never revisits choices May combine many

Feature Greedy algorithm Dynamic programming

subproblem solutions

Proof requirement

Greedy-choice property and exchange argument

Optimal substructure and recurrence

Typical runtime

Often O(n) O ( n ) or

O(nlogn) O ( n log n )

Often polynomial in table size

Example Activity selection 0/1 Knapsack

Stanford describes greedy algorithms as a refinement of dynamic programming: a greedy algorithm is correct when only one prior table entry needs to be considered at each step.

Example contrast

Activity selection has a correct greedy rule: choose the earliest-finishing compatible activity.

0/1 Knapsack generally requires dynamic programming because choosing the highest- value or best-ratio item first can prevent an optimal solution.

Common Mistakes

Mistake 1: Assuming every greedy rule works

A greedy algorithm must be proved correct. Many plausible greedy rules fail.

For example, choosing the shortest job first, the highest-value job first, or the best-ratio job first may fail for certain optimization problems. Stanford’s practice materials emphasize constructing counterexamples when greedy fails.

Mistake 2: Confusing local and global optimality

A locally optimal choice is not automatically part of a globally optimal solution. The greedy-choice property must be proved.

Mistake 3: Omitting the exchange argument

Topic

Official Stanford resource

Topic

Official Stanford resource

slides

Quick Revision Card

Problem Greedy rule

Correctness tool Runtime

Activity

selection

Choose earliest- finishing compatible activity

Exchange

argument O(nlogn) O ( n log n )

Job

scheduling

Choose largest cost/time ratio first

Exchange

argument O(nlogn)^ O ( n log n )

Huffman

coding

Merge two least- frequent subtrees

Greedy-choice

property O(nlogn) O ( n log n )

Prim’s MST

Add lightest edge

crossing tree cut Cut property O(mlogn)^ O ( m log n )

Kruskal’s

MST

Add lightest non- cycle edge

Cut property and

union-find O(mlogn)^ O ( m log n )