🖨️ Printing Instructions: Press Ctrl/Cmd + P and select "Save as PDF".
1

The Greedy Paradigm

2

Learning Goals

3

Introduction to Greedy Strategy

4

The Core Idea

5

Pros and Cons

6

Activity Selection Problem

7

Interactive: Activity Selection

8

Problem Statement

9

Example

10

Possible Greedy Choices

11

The Correct Greedy Choice

12

Greedy-Activity-Selector(s, f)

Sort by finish times f[1] <= f[2] <= ... <= f[n]
A = {Activity 1}
k = 1  // Last selected index
for m = 2 to n
  if s[m] >= f[k]
    A = A U {Activity m}
    k = m
return A
13

Trace

14

Proof of Correctness

15

Greedy Choice Property

16

Exchange Step

17

Optimal Substructure

18

Knapsack Problems

19

Interactive: Knapsack Comparison

20

0/1 Knapsack

21

Fractional Knapsack

22

Fractional Knapsack Greedy

Calculate v[i]/w[i] for all i
Sort by density descending
Total = 0
for i = 1 to n
  if w[i] <= Capacity
    Take all of i
    Capacity -= w[i]
    Total += v[i]
  else
    Take Fraction (Capacity/w[i]) of i
    Total += Fraction * v[i]
    break
23

Coin Change Problem

24

Interactive: Coin Change

25

Problem

26

US Currency

27

General Systems

28

Canonical Coin Systems

29

Design Principles

30

Properties Required

31

Contrast with DP

32

Example: Minimum Spanning Tree

33

Preview

34

Practice Problems

35

Problem 1

36

Problem 2

37

Common Pitfalls & Anti-Patterns

38

Watch Out For...

39

Interactive Practice

40

Activity 1: Greedy Choice

41

Solution 1

42

Activity 2: Knapsack Analysis

43

Solution 2

44

Activity 3: Coin Change

45

Solution 3

46

Lecture Summary

47

All Interactive Demos

48

Supplementary Resources