Poscat seminar 6
-
Upload
hyungyu-shin -
Category
Education
-
view
58 -
download
0
Transcript of Poscat seminar 6
Topic
Topic today
− Greedy Approach
• Basic Concept
• Interval Scheduling
• Interval Partitioning
• Fractional Knapsack
• Huffman Encoding
• Other Problems
POSCAT Seminar 1-24 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-34 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-44 July 2014
yougatup
Greedy Approach
Problem
Find a path which has largest sum
POSCAT Seminar 1-54 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-64 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-74 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-84 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-94 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-104 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-114 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-124 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-134 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-144 July 2014
yougatup
TimeThey don’t overlap each other.
Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-154 July 2014
yougatup
Time
Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-164 July 2014
yougatup
Time
Which one is better ? Why ?
Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-174 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-184 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-194 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-204 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-214 July 2014
yougatup
Time
Which one is better ?
Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-224 July 2014
yougatup
Time
Which one is better ?
Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-234 July 2014
yougatup
Time
Which one is better ?
Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-244 July 2014
yougatup
Time
Which one is better ?
Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-254 July 2014
yougatup
Time
Which one is better ?
Interval Scheduling
Problem
Idea : What have to be the first interval ?
POSCAT Seminar 1-264 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-274 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-284 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-294 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-304 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-314 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-324 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-334 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-344 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-354 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-364 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-374 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-384 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-394 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-404 July 2014
yougatup
Time
We need 5 classrooms !
Interval Partitioning
Problem
Idea : What is the minimum number of classrooms ?
POSCAT Seminar 1-414 July 2014
yougatup
Time
Interval Partitioning
Problem
Idea : What is the minimum number of classrooms ?
POSCAT Seminar 1-424 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-434 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-444 July 2014
yougatup
TimeWe don’t know the solution, but we need at least 5 classrooms!
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-464 July 2014
yougatup
Time
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-474 July 2014
yougatup
Time
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-484 July 2014
yougatup
Time
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-494 July 2014
yougatup
Time
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-504 July 2014
yougatup
Time
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-514 July 2014
yougatup
TimeI can reuse this color !
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-524 July 2014
yougatup
TimeI can reuse this color !
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-534 July 2014
yougatup
TimeI can reuse this color !
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-544 July 2014
yougatup
TimeI can reuse this color !
Interval Partitioning
Problem
Is it sufficient ? Yes ! Consider this algorithm
POSCAT Seminar 1-554 July 2014
yougatup
TimeDone !
Interval Partitioning
POSCAT Seminar 1-564 July 2014
yougatup
Prove that this algorithm needs classrooms no more than the maximum number of overlap
Interval Partitioning
POSCAT Seminar 1-574 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-584 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-594 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” partIt means that lecture 𝑗 have no compatible classroom
Interval Partitioning
POSCAT Seminar 1-604 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” partIt means that lecture 𝑗 have no compatible classroomTherefore, the maximum number of overlap have to be 𝑀 + 1. Contradiction.
Interval Partitioning
POSCAT Seminar 1-624 July 2014
yougatup
Implementation ?
By using priority queue, we can make O(𝒏 𝐥𝐨𝐠𝒏) algorithm
Fractional Knapsack
POSCAT Seminar 1-634 July 2014
yougatup
Easy, just think about it
− I’ll provide this problem today
Other problems
POSCAT Seminar 1-644 July 2014
yougatup
Problem
Given N points, find a maximum value of 𝑚 such that 𝑚 = |∆𝑦
∆𝑥|
Other problems
POSCAT Seminar 1-654 July 2014
yougatup
Problem
Given N points, find a maximum value of 𝑚 such that 𝑚 = |∆𝑦
∆𝑥|
Naïve approach O(𝑛2)
Other problems
POSCAT Seminar 1-664 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-674 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-684 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-694 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-704 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-714 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-724 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-734 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-744 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-754 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-764 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-774 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-784 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 (+)