Algorithm Problem Solving (APS):
Greedy Method
Niema Moshiri
UC San Diego SPIS 2021
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Example: The Change Problem (USA Currency)
Optimization Problems
Optimization Problems
Optimization Problems
Optimization Problems
Optimization Problems
Revisiting the Change Problem (USA Currency)
Revisiting the Change Problem (USA Currency)
Revisiting the Change Problem (USA Currency)
Revisiting the Change Problem (USA Currency)
Multiple Optimal Solutions
Multiple Optimal Solutions
Multiple Optimal Solutions
Multiple Optimal Solutions
Multiple Optimal Solutions
Revisiting the Change Problem (USA Currency)
Let’s solve the problem!
Revisiting the Change Problem (USA Currency)
Algorithm change_USA(x,C):
change ← empty list
For each coin c in C (descending order):
While x >= c:
Add c to change
x ← x - c
Return change
Revisiting the Change Problem (USA Currency)
Algorithm change_USA(x,C):
change ← empty list
For each coin c in C (descending order):
While x >= c:
Add c to change
x ← x - c
Return change
Does this work for any arbitrary currency?
Global vs. Local Search
Global vs. Local Search
Global vs. Local Search
Global vs. Local Search
Global vs. Local Search
Local Search: The Greedy Method
Local Search: The Greedy Method
Local Search: The Greedy Method
Local Search: The Greedy Method
Local Search: The Greedy Method
Revisiting the Change Problem
Revisiting the Change Problem
Revisiting the Change Problem
Revisiting the Change Problem
Revisiting the Change Problem
Immediate Benefit vs. Opportunity Cost
Immediate Benefit vs. Opportunity Cost
Immediate Benefit vs. Opportunity Cost
Immediate Benefit vs. Opportunity Cost
Immediate Benefit vs. Opportunity Cost
Example: The Event Scheduling Problem
Example: The Event Scheduling Problem
Example: The Event Scheduling Problem
Example: The Event Scheduling Problem
Example: The Event Scheduling Problem
Example: The Event Scheduling Problem
Example: The Event Scheduling Problem
Example: The Event Scheduling Problem
Counterexample: Shortest Duration
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Shortest Duration
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Shortest Duration
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Shortest Duration
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Shortest Duration
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Earliest Start Time
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Earliest Start Time
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Earliest Start Time
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Earliest Start Time
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Earliest Start Time
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Fewest Conflicts
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Fewest Conflicts
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Fewest Conflicts
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Fewest Conflicts
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Fewest Conflicts
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Fewest Conflicts
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Fewest Conflicts
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Fewest Conflicts
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Fewest Conflicts
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Earliest End Time
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Counterexample: Earliest End Time
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
I can’t think of one!
Counterexample: Earliest End Time
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
I can’t think of one!
We still need to prove it’s correct!!!
Example: The Event Scheduling Problem
Algorithm schedule(E):
Sort E in ascending order of end time
curr_time ← negative infinity
events ← empty list
For each event (start,end) in E:
If start ≥ curr_time:
Add (start,end) to events
curr_time ← end
Return events
Proofs: The Exchange Argument
Proofs: The Exchange Argument
Proofs: The Exchange Argument
Proofs: The Exchange Argument
Proofs: The Exchange Argument
Proofs: The Exchange Argument
Proofs: The Exchange Argument
Let’s try to prove our algorithm!