3. Greedy Approach
Problem Solving Paradigm
− Follows the problem solving heuristic of making the locally
optimal choose at each stage
− You should prove that greedy approach guarantees the global
optimal
Very difficult normally
− Finding a solution is very difficult, but
− Code is very simple because it choose locally optimal usually
POSCAT Seminar 1-3
4 July 2014
yougatup
4. Greedy Approach
Starting at A, our greedy algorithm should find ‘M’, not ‘m’.
Therefore, we must prove that greedy algorithm always find ‘M’,
although we always choose locally optimal path
POSCAT Seminar 1-4
4 July 2014
yougatup
6. Greedy Approach
Problem
Find a path which has largest sum
Greedy approach : Choose what appears to be the optimal
POSCAT Seminar 1-6
4 July 2014
yougatup
3
7
9 2 1 2
6
7. Greedy Approach
Problem
Find a path which has largest sum
Greedy approach : Choose what appears to be the optimal
POSCAT Seminar 1-7
4 July 2014
yougatup
3
7
9 2 1 2
6
8. Greedy Approach
Problem
Find a path which has largest sum
Greedy approach : Choose what appears to be the optimal
POSCAT Seminar 1-8
4 July 2014
yougatup
3
7
9 2 1 2
6
9. Greedy Approach
Problem
Find a path which has largest sum
Greedy approach : Choose what appears to be the optimal
Is it our solution ?
POSCAT Seminar 1-9
4 July 2014
yougatup
3
7
9 2 1 2
6
10. Greedy Approach
Problem
Find a path which has largest sum
Greedy approach : Choose what appears to be the optimal
Is it our solution ?
POSCAT Seminar 1-10
4 July 2014
yougatup
3
7
9 2 1 2
6
11. Greedy Approach
Problem
Find a path which has largest sum
Greedy approach : Choose what appears to be the optimal
We must prove that our choice guarantees global optimal value
POSCAT Seminar 1-11
4 July 2014
yougatup
3
7
9 2 1 2
6
12. Greedy Approach
Problem
Find a path which has largest sum
Greedy approach : Choose what appears to be the optimal
We must prove that our choice guarantees global optimal value
POSCAT Seminar 1-12
4 July 2014
yougatup
3
7
9 2 1 2
6
Completely Wrong !
13. Interval Scheduling
Problem
− Job 𝑗 starts at 𝑠𝑗 and finishes at 𝑓𝑗
− Two jobs compatible if they don’t overlap
− Find maximum subset of mutually compatible jobs
POSCAT Seminar 1-13
4 July 2014
yougatup
Time
14. Interval Scheduling
Problem
− Job 𝑗 starts at 𝑠𝑗 and finishes at 𝑓𝑗
− Two jobs compatible if they don’t overlap
− Find maximum subset of mutually compatible jobs
POSCAT Seminar 1-14
4 July 2014
yougatup
Time
They don’t overlap each other.
16. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-16
4 July 2014
yougatup
Time
Which one is better ? Why ?
17. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-17
4 July 2014
yougatup
Time
Which one is better ? Why ?
Small 𝑓𝑖 is always good !
18. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-18
4 July 2014
yougatup
Time
It must be the first one definitely !
19. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-19
4 July 2014
yougatup
Time
We can’t choose grey interval remove it
20. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-20
4 July 2014
yougatup
Time
We can’t choose grey interval remove it
21. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-21
4 July 2014
yougatup
Time
Which one is better ?
22. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-22
4 July 2014
yougatup
Time
Which one is better ?
23. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-23
4 July 2014
yougatup
Time
Which one is better ?
24. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-24
4 July 2014
yougatup
Time
Which one is better ?
25. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-25
4 July 2014
yougatup
Time
Which one is better ?
26. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-26
4 July 2014
yougatup
Time
Done ! You should know why greedy algorithm guarantees the global optimal
27. Interval Scheduling
Problem
Prove that this algorithm is optimal
POSCAT Seminar 1-27
4 July 2014
yougatup
Done ! You should know why greedy algorithm guarantees the global optimal
28. Interval Scheduling
Problem
Prove that this algorithm is optimal
Suppose that there is a solution 𝑠1 ≤ 𝑓1 ≤ 𝑠2 ≤ 𝑓2 ≤ … ≤ 𝑠 𝑘 ≤ 𝑓𝑘,
and we found 𝑠1
′
≤ 𝑓1
′
≤ 𝑠2
′
≤ 𝑓2
′
≤ … ≤ 𝑠 𝑜𝑝𝑡
′
≤ 𝑓𝑜𝑝𝑡′
POSCAT Seminar 1-28
4 July 2014
yougatup
Done ! You should know why greedy algorithm guarantees the global optimal
29. Interval Scheduling
Problem
Prove that this algorithm is optimal
Suppose that there is a solution 𝑠1 ≤ 𝑓1 ≤ 𝑠2 ≤ 𝑓2 ≤ … ≤ 𝑠 𝑘 ≤ 𝑓𝑘,
and we found 𝑠1
′
≤ 𝑓1
′
≤ 𝑠2
′
≤ 𝑓2
′
≤ … ≤ 𝑠 𝑜𝑝𝑡
′
≤ 𝑓𝑜𝑝𝑡′
We select the interval whose finish time is minimal.
∴ 𝑓1
′
≤ 𝑓1.
POSCAT Seminar 1-29
4 July 2014
yougatup
Done ! You should know why greedy algorithm guarantees the global optimal
30. Interval Scheduling
Problem
Prove that this algorithm is optimal
Suppose that there is a solution 𝑠1 ≤ 𝑓1 ≤ 𝑠2 ≤ 𝑓2 ≤ … ≤ 𝑠 𝑘 ≤ 𝑓𝑘,
and we found 𝑠1
′
≤ 𝑓1
′
≤ 𝑠2
′
≤ 𝑓2
′
≤ … ≤ 𝑠 𝑜𝑝𝑡
′
≤ 𝑓𝑜𝑝𝑡′
Also, we choose the second interval whose finish time is smallest
among intervals compatible with the first one.
∴ 𝑓2
′
≤ 𝑓2
POSCAT Seminar 1-30
4 July 2014
yougatup
Done ! You should know why greedy algorithm guarantees the global optimal
31. Interval Scheduling
Problem
Prove that this algorithm is optimal
Suppose that there is a solution 𝑠1 ≤ 𝑓1 ≤ 𝑠2 ≤ 𝑓2 ≤ … ≤ 𝑠 𝑘 ≤ 𝑓𝑘,
and we found 𝑠1
′
≤ 𝑓1
′
≤ 𝑠2
′
≤ 𝑓2
′
≤ … ≤ 𝑠 𝑜𝑝𝑡
′
≤ 𝑓𝑜𝑝𝑡′
Like this, we can always guarantee that 𝑓𝑘 ≤ 𝑓𝑘
′
. ∴ 𝑜𝑝𝑡 ≥ 𝑘
Also, 𝑜𝑝𝑡 ≤ 𝑘 because 𝑘 is solution
POSCAT Seminar 1-31
4 July 2014
yougatup
Done ! You should know why greedy algorithm guarantees the global optimal
32. Interval Scheduling
Problem
Prove that this algorithm is optimal
Suppose that there is a solution 𝑠1 ≤ 𝑓1 ≤ 𝑠2 ≤ 𝑓2 ≤ … ≤ 𝑠 𝑘 ≤ 𝑓𝑘,
and we found 𝑠1
′
≤ 𝑓1
′
≤ 𝑠2
′
≤ 𝑓2
′
≤ … ≤ 𝑠 𝑜𝑝𝑡
′
≤ 𝑓𝑜𝑝𝑡′
Like this, we can always guarantee that 𝑓𝑘 ≤ 𝑓𝑘
′
. ∴ 𝑜𝑝𝑡 ≥ 𝑘
Also, 𝑜𝑝𝑡 ≤ 𝑘 because 𝑘 is solution
Therefore, 𝑜𝑝𝑡 = 𝑘.
POSCAT Seminar 1-32
4 July 2014
yougatup
Done ! You should know why greedy algorithm guarantees the global optimal
33. Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-33
4 July 2014
yougatup
Time
Time complexity : O(𝒏 𝒍𝒐𝒈 𝒏) for sorting
34. Interval Partitioning
Problem
− Lecture 𝑗 starts at 𝑠𝑗 and finishes at 𝑓𝑗
− Find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room
POSCAT Seminar 1-34
4 July 2014
yougatup
Time
35. Interval Partitioning
Problem
− Lecture 𝑗 starts at 𝑠𝑗 and finishes at 𝑓𝑗
− Find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room
POSCAT Seminar 1-35
4 July 2014
yougatup
Time
36. Interval Partitioning
Problem
− Lecture 𝑗 starts at 𝑠𝑗 and finishes at 𝑓𝑗
− Find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room
POSCAT Seminar 1-36
4 July 2014
yougatup
Time
37. Interval Partitioning
Problem
− Lecture 𝑗 starts at 𝑠𝑗 and finishes at 𝑓𝑗
− Find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room
POSCAT Seminar 1-37
4 July 2014
yougatup
Time
38. Interval Partitioning
Problem
− Lecture 𝑗 starts at 𝑠𝑗 and finishes at 𝑓𝑗
− Find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room
POSCAT Seminar 1-38
4 July 2014
yougatup
Time
39. Interval Partitioning
Problem
− Lecture 𝑗 starts at 𝑠𝑗 and finishes at 𝑓𝑗
− Find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room
POSCAT Seminar 1-39
4 July 2014
yougatup
Time
40. Interval Partitioning
Problem
− Lecture 𝑗 starts at 𝑠𝑗 and finishes at 𝑓𝑗
− Find minimum number of classrooms to schedule all lectures
so that no two occur at the same time in the same room
POSCAT Seminar 1-40
4 July 2014
yougatup
Time
We need 5 classrooms !
42. Interval Partitioning
Problem
Idea : What is the minimum number of classrooms ?
POSCAT Seminar 1-42
4 July 2014
yougatup
TimeWe don’t know the solution, but we need at least 4 classrooms!
Is it sufficient ?
43. Interval Partitioning
Problem
Idea : What is the minimum number of classrooms ?
POSCAT Seminar 1-43
4 July 2014
yougatup
TimeWe don’t know the solution, but we need at least 5 classrooms!
44. Interval Partitioning
Problem
Idea : What is the minimum number of classrooms ?
∴ We need classrooms at least as many as the maximum number of
overlap
POSCAT Seminar 1-44
4 July 2014
yougatup
TimeWe don’t know the solution, but we need at least 5 classrooms!
51. Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-51
4 July 2014
yougatup
Time
I can reuse this color !
52. Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-52
4 July 2014
yougatup
Time
I can reuse this color !
53. Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-53
4 July 2014
yougatup
Time
I can reuse this color !
54. Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-54
4 July 2014
yougatup
Time
I can reuse this color !
56. Interval Partitioning
POSCAT Seminar 1-56
4 July 2014
yougatup
Prove that this algorithm needs classrooms no more than the maximum number of overlap
57. Interval Partitioning
POSCAT Seminar 1-57
4 July 2014
yougatup
Prove that this algorithm needs classrooms no more than the maximum number of overlap
Let 𝑀 be maximum number of overlap.
Suppose that our algorithm needs 𝑀 + 1 classrooms
58. Interval Partitioning
POSCAT Seminar 1-58
4 July 2014
yougatup
Prove that this algorithm needs classrooms no more than the maximum number of overlap
Consider the situation that we need last classrooms when we consider lecture 𝑗.
In other words, we need 1 more classroom although we already have 𝑀 classrooms
59. Interval Partitioning
POSCAT Seminar 1-59
4 July 2014
yougatup
Prove that this algorithm needs classrooms no more than the maximum number of overlap
To allocate another classroom, we have to go to “else” part
It means that lecture 𝑗 have no compatible classroom
60. Interval Partitioning
POSCAT Seminar 1-60
4 July 2014
yougatup
Prove that this algorithm needs classrooms no more than the maximum number of overlap
To allocate another classroom, we have to go to “else” part
It means that lecture 𝑗 have no compatible classroom
Therefore, the maximum number of overlap have to be 𝑀 + 1. Contradiction.
64. Other problems
POSCAT Seminar 1-64
4 July 2014
yougatup
Problem
Given N points, find a maximum value of 𝑚 such that 𝑚 = |
∆𝑦
∆𝑥
|
65. Other problems
POSCAT Seminar 1-65
4 July 2014
yougatup
Problem
Given N points, find a maximum value of 𝑚 such that 𝑚 = |
∆𝑦
∆𝑥
|
Naïve approach O(𝑛2)
66. Other problems
POSCAT Seminar 1-66
4 July 2014
yougatup
Problem
Given N points, find a maximum value of 𝑚 such that 𝑚 = |
∆𝑦
∆𝑥
|
We can prove that two points have to be adjacent
It can’t be optimal !
67. Other problems
POSCAT Seminar 1-67
4 July 2014
yougatup
Problem
Given N points, find a maximum value of 𝑚 such that 𝑚 = |
∆𝑦
∆𝑥
|
We can prove that two points have to be adjacent O(𝑛 log 𝑛)
It can’t be optimal !
68. Other problems
POSCAT Seminar 1-68
4 July 2014
yougatup
Problem
You have 2N cards, and each card have two numbers on both sides.
There are 2N locations to put your card on the table. For each
location, it has specific sign which is changed alternatively (+ or -).
Find the maximum value of the result of your calculation.
1 2 9 -1 -9 -8 7 4 4 -3 2 -1Cards (front/back)
Locations : + + +- - - = ?
69. Other problems
POSCAT Seminar 1-69
4 July 2014
yougatup
Problem
You have 2N cards, and each card have two numbers on both sides.
There are 2N locations to put your card on the table. For each
location, it has specific sign which is changed alternatively (+ or -).
Find the maximum value of the result of your calculation.
1 2 9 -1 -9 -8 7 4 4 -3 2 -1Cards (front/back)
Locations : + + +- - - = ?
70. Other problems
POSCAT Seminar 1-70
4 July 2014
yougatup
Problem
You have 2N cards, and each card have two numbers on both sides.
There are 2N locations to put your card on the table. For each
location, it has specific sign which is changed alternatively (+ or -).
Find the maximum value of the result of your calculation.
1
2 9
-1
-9
-8
7
4 4
-3
2
-1
Cards (front/back)
Locations : + + +- - - = 31
71. Other problems
POSCAT Seminar 1-71
4 July 2014
yougatup
Solution
Let 𝑎𝑖 be the value of bigger side of card 𝑖.
𝑏𝑖 be the value of smaller side of card 𝑖.
If card 𝑖 which is located with (+) should have the value 𝑎𝑖
Otherwise, it should have the value 𝑏𝑖
72. Other problems
POSCAT Seminar 1-72
4 July 2014
yougatup
Solution
Let 𝑎𝑖 be the value of bigger side of card 𝑖.
𝑏𝑖 be the value of smaller side of card 𝑖.
If card 𝑖 which is located with (+) should have the value 𝑎𝑖
Otherwise, it should have the value 𝑏𝑖
Let 𝑆 = { 𝑖 | card 𝑖 is located in (+) side }
Let 𝑆′ = { 𝑖 | card 𝑖 is located in (-) side }
73. Other problems
POSCAT Seminar 1-73
4 July 2014
yougatup
Solution
Let 𝑎𝑖 be the value of bigger side of card 𝑖.
𝑏𝑖 be the value of smaller side of card 𝑖.
Then our result is represented by
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗
74. Other problems
POSCAT Seminar 1-74
4 July 2014
yougatup
Solution
Let 𝑎𝑖 be the value of bigger side of card 𝑖.
𝑏𝑖 be the value of smaller side of card 𝑖.
Then our result is represented by
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗 =
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗 + (
𝑖∈𝑆
𝑏𝑖 −
𝑖∈𝑆
𝑏𝑖 )
75. Other problems
POSCAT Seminar 1-75
4 July 2014
yougatup
Solution
Let 𝑎𝑖 be the value of bigger side of card 𝑖.
𝑏𝑖 be the value of smaller side of card 𝑖.
Then our result is represented by
𝑖∈𝑆
𝑎𝑖 +
𝑗∈𝑆′
𝑏𝑗 =
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗 + (
𝑖∈𝑆
𝑏𝑖 −
𝑖∈𝑆
𝑏𝑖 )
𝑖∈𝑆
𝑎𝑖 +
𝑖∈𝑆
𝑏𝑖 − (
𝑗∈𝑆′
𝑏𝑗 +
𝑖∈𝑆
𝑏𝑖 )
76. Other problems
POSCAT Seminar 1-76
4 July 2014
yougatup
Solution
Let 𝑎𝑖 be the value of bigger side of card 𝑖.
𝑏𝑖 be the value of smaller side of card 𝑖.
Then our result is represented by
𝑖∈𝑆
𝑎𝑖 +
𝑗∈𝑆′
𝑏𝑗 =
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗 + (
𝑖∈𝑆
𝑏𝑖 −
𝑖∈𝑆
𝑏𝑖 )
𝑖∈𝑆
(𝑎𝑖 + 𝑏𝑖) − (𝑆𝑢𝑚 𝑜𝑓 𝑏)
77. Other problems
POSCAT Seminar 1-77
4 July 2014
yougatup
Solution
Let 𝑎𝑖 be the value of bigger side of card 𝑖.
𝑏𝑖 be the value of smaller side of card 𝑖.
𝑖∈𝑆
(𝑎𝑖 + 𝑏𝑖) − (𝑆𝑢𝑚 𝑜𝑓 𝑏)
(Sum of 𝑏) is constant because 𝑏 is the smaller value of each card.
78. Other problems
POSCAT Seminar 1-78
4 July 2014
yougatup
Solution
Let 𝑎𝑖 be the value of bigger side of card 𝑖.
𝑏𝑖 be the value of smaller side of card 𝑖.
𝑖∈𝑆
(𝑎𝑖 + 𝑏𝑖) − (𝑆𝑢𝑚 𝑜𝑓 𝑏)
(Sum of 𝑏) is constant because 𝑏 is the smaller value of each card.
Therefore, we have to choose 𝑖 by considering the value of 𝑎 + 𝑏
It means that a card whose 𝑎 + 𝑏 value is larger should be located in (+)
79. Other problems
POSCAT Seminar 1-79
4 July 2014
yougatup
Solution
1. Sort cards with respect to the value of a + b
2. N cards whose value is larger should be located in (+)
3. Remaining N cards should be located in (-)
𝑶(𝒏 𝐥𝐨𝐠 𝒏)