SlideShare a Scribd company logo
1 of 79
Download to read offline
POSCAT Seminar 6 :
Greedy Approach
yougatup @ POSCAT
1
Topic
 Topic today
− Greedy Approach
• Basic Concept
• Interval Scheduling
• Interval Partitioning
• Fractional Knapsack
• Huffman Encoding
• Other Problems
POSCAT Seminar 1-2
4 July 2014
yougatup
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
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
Greedy Approach
 Problem
Find a path which has largest sum
POSCAT Seminar 1-5
4 July 2014
yougatup
3
7
9 2 1 2
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
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
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
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
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
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
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 !
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
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.
Interval Scheduling
 Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-15
4 July 2014
yougatup
Time
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 ?
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 !
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 !
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
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
Interval Scheduling
 Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-21
4 July 2014
yougatup
Time
Which one is better ?
Interval Scheduling
 Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-22
4 July 2014
yougatup
Time
Which one is better ?
Interval Scheduling
 Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-23
4 July 2014
yougatup
Time
Which one is better ?
Interval Scheduling
 Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-24
4 July 2014
yougatup
Time
Which one is better ?
Interval Scheduling
 Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-25
4 July 2014
yougatup
Time
Which one is better ?
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 !
Interval Partitioning
 Problem
Idea : What is the minimum number of classrooms ?
POSCAT Seminar 1-41
4 July 2014
yougatup
Time
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 ?
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!
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!
Interval Partitioning
 Problem
Is it sufficient ?
POSCAT Seminar 1-45
4 July 2014
yougatup
Time
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-46
4 July 2014
yougatup
Time
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-47
4 July 2014
yougatup
Time
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-48
4 July 2014
yougatup
Time
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-49
4 July 2014
yougatup
Time
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-50
4 July 2014
yougatup
Time
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-51
4 July 2014
yougatup
Time
I can reuse this color !
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-52
4 July 2014
yougatup
Time
I can reuse this color !
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-53
4 July 2014
yougatup
Time
I can reuse this color !
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-54
4 July 2014
yougatup
Time
I can reuse this color !
Interval Partitioning
 Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-55
4 July 2014
yougatup
TimeDone !
Interval Partitioning
POSCAT Seminar 1-56
4 July 2014
yougatup
Prove that this algorithm needs classrooms no more than the maximum number of overlap
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
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
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
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.
Interval Partitioning
POSCAT Seminar 1-61
4 July 2014
yougatup
Implementation ?
Interval Partitioning
POSCAT Seminar 1-62
4 July 2014
yougatup
Implementation ?
By using priority queue, we can make O(𝒏 𝐥𝐨𝐠 𝒏) algorithm
Fractional Knapsack
POSCAT Seminar 1-63
4 July 2014
yougatup
 Easy, just think about it
− I’ll provide this problem today
Other problems
POSCAT Seminar 1-64
4 July 2014
yougatup
 Problem
Given N points, find a maximum value of 𝑚 such that 𝑚 = |
∆𝑦
∆𝑥
|
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)
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 !
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 !
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 : + + +- - - = ?
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 : + + +- - - = ?
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
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 𝑏𝑖
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 }
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
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗
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
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗 =
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗 + (
𝑖∈𝑆
𝑏𝑖 −
𝑖∈𝑆
𝑏𝑖 )
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
𝑖∈𝑆
𝑎𝑖 +
𝑗∈𝑆′
𝑏𝑗 =
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗 + (
𝑖∈𝑆
𝑏𝑖 −
𝑖∈𝑆
𝑏𝑖 )
𝑖∈𝑆
𝑎𝑖 +
𝑖∈𝑆
𝑏𝑖 − (
𝑗∈𝑆′
𝑏𝑗 +
𝑖∈𝑆
𝑏𝑖 )
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
𝑖∈𝑆
𝑎𝑖 +
𝑗∈𝑆′
𝑏𝑗 =
𝑖∈𝑆
𝑎𝑖 −
𝑗∈𝑆′
𝑏𝑗 + (
𝑖∈𝑆
𝑏𝑖 −
𝑖∈𝑆
𝑏𝑖 )
𝑖∈𝑆
(𝑎𝑖 + 𝑏𝑖) − (𝑆𝑢𝑚 𝑜𝑓 𝑏)
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.
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 (+)
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 (-)
𝑶(𝒏 𝐥𝐨𝐠 𝒏)

More Related Content

Similar to Poscat seminar 6

Sprint, How to solve big problems and test new ideas in 5 days
Sprint, How to solve big problems and test new ideas in 5 daysSprint, How to solve big problems and test new ideas in 5 days
Sprint, How to solve big problems and test new ideas in 5 daysZouaghi HouSSeM
 
SANDBOX LEARNING: Stop wasting your time studying!
SANDBOX LEARNING: Stop wasting your time studying!SANDBOX LEARNING: Stop wasting your time studying!
SANDBOX LEARNING: Stop wasting your time studying!ElizabethNugent8
 
Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)
Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)
Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)Mansi Tyagi
 
It's Not About the Standards
It's Not About the StandardsIt's Not About the Standards
It's Not About the StandardsMarlena Hebern
 
HSC: How to Stop Wasting Your Time Studying
HSC: How to Stop Wasting Your Time StudyingHSC: How to Stop Wasting Your Time Studying
HSC: How to Stop Wasting Your Time StudyingElizabethNugent8
 
The 4-hour Tester Experiment
The 4-hour Tester ExperimentThe 4-hour Tester Experiment
The 4-hour Tester ExperimentHelena Jeret-Mäe
 
Aug 17, 2016
Aug 17, 2016Aug 17, 2016
Aug 17, 2016khyps13
 
It's Not About the Standards
It's Not About the StandardsIt's Not About the Standards
It's Not About the StandardsMarlena Hebern
 
ACPET e Mentor Online Session 7
ACPET e Mentor Online Session 7ACPET e Mentor Online Session 7
ACPET e Mentor Online Session 7Yum Studio
 
Lean Startup Case Studies
Lean Startup Case StudiesLean Startup Case Studies
Lean Startup Case StudiesGrace Ng
 
Practice: Design "Swarms"
Practice: Design "Swarms"Practice: Design "Swarms"
Practice: Design "Swarms"DesignMap
 
User Testing: Adapt to Fit Your Needs
User Testing: Adapt to Fit Your NeedsUser Testing: Adapt to Fit Your Needs
User Testing: Adapt to Fit Your NeedsEffective
 
Teaching Kids Programming using the Intentional Method
Teaching Kids Programming using the Intentional MethodTeaching Kids Programming using the Intentional Method
Teaching Kids Programming using the Intentional MethodLynn Langit
 
Module 4 lesson 6
Module 4 lesson 6Module 4 lesson 6
Module 4 lesson 6mlabuski
 

Similar to Poscat seminar 6 (20)

Sprint, How to solve big problems and test new ideas in 5 days
Sprint, How to solve big problems and test new ideas in 5 daysSprint, How to solve big problems and test new ideas in 5 days
Sprint, How to solve big problems and test new ideas in 5 days
 
Poscat seminar 7
Poscat seminar 7Poscat seminar 7
Poscat seminar 7
 
Poscat seminar 8
Poscat seminar 8Poscat seminar 8
Poscat seminar 8
 
SANDBOX LEARNING: Stop wasting your time studying!
SANDBOX LEARNING: Stop wasting your time studying!SANDBOX LEARNING: Stop wasting your time studying!
SANDBOX LEARNING: Stop wasting your time studying!
 
Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)
Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)
Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)
 
It's Not About the Standards
It's Not About the StandardsIt's Not About the Standards
It's Not About the Standards
 
HSC: How to Stop Wasting Your Time Studying
HSC: How to Stop Wasting Your Time StudyingHSC: How to Stop Wasting Your Time Studying
HSC: How to Stop Wasting Your Time Studying
 
The 4-hour Tester Experiment
The 4-hour Tester ExperimentThe 4-hour Tester Experiment
The 4-hour Tester Experiment
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Aug 17, 2016
Aug 17, 2016Aug 17, 2016
Aug 17, 2016
 
It's Not About the Standards
It's Not About the StandardsIt's Not About the Standards
It's Not About the Standards
 
ACPET e Mentor Online Session 7
ACPET e Mentor Online Session 7ACPET e Mentor Online Session 7
ACPET e Mentor Online Session 7
 
Lean Startup Case Studies
Lean Startup Case StudiesLean Startup Case Studies
Lean Startup Case Studies
 
Design Sprints
Design SprintsDesign Sprints
Design Sprints
 
Practice: Design "Swarms"
Practice: Design "Swarms"Practice: Design "Swarms"
Practice: Design "Swarms"
 
OTIS4
OTIS4OTIS4
OTIS4
 
User Testing: Adapt to Fit Your Needs
User Testing: Adapt to Fit Your NeedsUser Testing: Adapt to Fit Your Needs
User Testing: Adapt to Fit Your Needs
 
Teaching Kids Programming using the Intentional Method
Teaching Kids Programming using the Intentional MethodTeaching Kids Programming using the Intentional Method
Teaching Kids Programming using the Intentional Method
 
Lesson 11 preparation session 2
Lesson 11  preparation session 2Lesson 11  preparation session 2
Lesson 11 preparation session 2
 
Module 4 lesson 6
Module 4 lesson 6Module 4 lesson 6
Module 4 lesson 6
 

More from Hyungyu Shin

More from Hyungyu Shin (6)

Poscat seminar 9
Poscat seminar 9Poscat seminar 9
Poscat seminar 9
 
Poscat seminar 10
Poscat seminar 10Poscat seminar 10
Poscat seminar 10
 
Poscat seminar 11
Poscat seminar 11Poscat seminar 11
Poscat seminar 11
 
Poscat seminar 4
Poscat seminar 4Poscat seminar 4
Poscat seminar 4
 
Poscat seminar 3-2
Poscat seminar 3-2Poscat seminar 3-2
Poscat seminar 3-2
 
Poscat seminar 1
Poscat seminar 1Poscat seminar 1
Poscat seminar 1
 

Recently uploaded

An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 

Recently uploaded (20)

An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 

Poscat seminar 6

  • 1. POSCAT Seminar 6 : Greedy Approach yougatup @ POSCAT 1
  • 2. Topic  Topic today − Greedy Approach • Basic Concept • Interval Scheduling • Interval Partitioning • Fractional Knapsack • Huffman Encoding • Other Problems POSCAT Seminar 1-2 4 July 2014 yougatup
  • 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
  • 5. Greedy Approach  Problem Find a path which has largest sum POSCAT Seminar 1-5 4 July 2014 yougatup 3 7 9 2 1 2 6
  • 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.
  • 15. Interval Scheduling  Problem Idea : What have to be the first interval ? POSCAT Seminar 1-15 4 July 2014 yougatup Time
  • 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 !
  • 41. Interval Partitioning  Problem Idea : What is the minimum number of classrooms ? POSCAT Seminar 1-41 4 July 2014 yougatup Time
  • 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!
  • 45. Interval Partitioning  Problem Is it sufficient ? POSCAT Seminar 1-45 4 July 2014 yougatup Time
  • 46. Interval Partitioning  Problem Is it sufficient ? Yes ! Consider this algorithm POSCAT Seminar 1-46 4 July 2014 yougatup Time
  • 47. Interval Partitioning  Problem Is it sufficient ? Yes ! Consider this algorithm POSCAT Seminar 1-47 4 July 2014 yougatup Time
  • 48. Interval Partitioning  Problem Is it sufficient ? Yes ! Consider this algorithm POSCAT Seminar 1-48 4 July 2014 yougatup Time
  • 49. Interval Partitioning  Problem Is it sufficient ? Yes ! Consider this algorithm POSCAT Seminar 1-49 4 July 2014 yougatup Time
  • 50. Interval Partitioning  Problem Is it sufficient ? Yes ! Consider this algorithm POSCAT Seminar 1-50 4 July 2014 yougatup Time
  • 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 !
  • 55. Interval Partitioning  Problem Is it sufficient ? Yes ! Consider this algorithm POSCAT Seminar 1-55 4 July 2014 yougatup TimeDone !
  • 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.
  • 61. Interval Partitioning POSCAT Seminar 1-61 4 July 2014 yougatup Implementation ?
  • 62. Interval Partitioning POSCAT Seminar 1-62 4 July 2014 yougatup Implementation ? By using priority queue, we can make O(𝒏 𝐥𝐨𝐠 𝒏) algorithm
  • 63. Fractional Knapsack POSCAT Seminar 1-63 4 July 2014 yougatup  Easy, just think about it − I’ll provide this problem today
  • 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 (-) 𝑶(𝒏 𝐥𝐨𝐠 𝒏)