dynamic programming( weighted interval scheduling)cs.indstate.edu/~arash/algolec12.pdf · dynamic...
TRANSCRIPT
Dynamic Programming( Weighted IntervalScheduling)
Arash Rafiey
17 November, 2016
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Dynamic Programming
1 Dynamic programming algorithms are used for optimization(for example, finding the shortest path between two points, orthe fastest way to multiply many matrices).
2 We use the basic idea of divide and conquer. Dividing theproblem into a number of subproblems.
3 There are polynomial number of subproblems (If the input isof size n then there are at most n, n2,.., n5 subproblems andnot 2
n4 sub-problems).
4 The solution to the original problem can be easily computedfrom the solution to the subproblems (e.g. sum the solutionsto the subproblems and get the solution to the original)
5 The idea is to solve each subproblem only once. Once thesolution to a given subproblem has been computed, it isstored or memorized: (the next time the same solution isneeded, it is simply looked up). This way we reduce thenumber of computations.
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Dynamic Programming
1 Dynamic programming algorithms are used for optimization(for example, finding the shortest path between two points, orthe fastest way to multiply many matrices).
2 We use the basic idea of divide and conquer. Dividing theproblem into a number of subproblems.
3 There are polynomial number of subproblems (If the input isof size n then there are at most n, n2,.., n5 subproblems andnot 2
n4 sub-problems).
4 The solution to the original problem can be easily computedfrom the solution to the subproblems (e.g. sum the solutionsto the subproblems and get the solution to the original)
5 The idea is to solve each subproblem only once. Once thesolution to a given subproblem has been computed, it isstored or memorized: (the next time the same solution isneeded, it is simply looked up). This way we reduce thenumber of computations.
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Dynamic Programming
1 Dynamic programming algorithms are used for optimization(for example, finding the shortest path between two points, orthe fastest way to multiply many matrices).
2 We use the basic idea of divide and conquer. Dividing theproblem into a number of subproblems.
3 There are polynomial number of subproblems (If the input isof size n then there are at most n, n2,.., n5 subproblems andnot 2
n4 sub-problems).
4 The solution to the original problem can be easily computedfrom the solution to the subproblems (e.g. sum the solutionsto the subproblems and get the solution to the original)
5 The idea is to solve each subproblem only once. Once thesolution to a given subproblem has been computed, it isstored or memorized: (the next time the same solution isneeded, it is simply looked up). This way we reduce thenumber of computations.
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Dynamic Programming
1 Dynamic programming algorithms are used for optimization(for example, finding the shortest path between two points, orthe fastest way to multiply many matrices).
2 We use the basic idea of divide and conquer. Dividing theproblem into a number of subproblems.
3 There are polynomial number of subproblems (If the input isof size n then there are at most n, n2,.., n5 subproblems andnot 2
n4 sub-problems).
4 The solution to the original problem can be easily computedfrom the solution to the subproblems (e.g. sum the solutionsto the subproblems and get the solution to the original)
5 The idea is to solve each subproblem only once. Once thesolution to a given subproblem has been computed, it isstored or memorized: (the next time the same solution isneeded, it is simply looked up). This way we reduce thenumber of computations.
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Dynamic Programming
1 Dynamic programming algorithms are used for optimization(for example, finding the shortest path between two points, orthe fastest way to multiply many matrices).
2 We use the basic idea of divide and conquer. Dividing theproblem into a number of subproblems.
3 There are polynomial number of subproblems (If the input isof size n then there are at most n, n2,.., n5 subproblems andnot 2
n4 sub-problems).
4 The solution to the original problem can be easily computedfrom the solution to the subproblems (e.g. sum the solutionsto the subproblems and get the solution to the original)
5 The idea is to solve each subproblem only once. Once thesolution to a given subproblem has been computed, it isstored or memorized: (the next time the same solution isneeded, it is simply looked up). This way we reduce thenumber of computations.
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Problem Statement:
1 We have a resource and many people request to use theresource for periods of time (an interval of time)
2 Each interval (request) i has a start time si and finishtime fi
3 Each interval (request) i , has a value vi
Conditions:
• the resource can be used by at most one person at a time.
• we can accept only compatible intervals (requests)(overlap-free).
Goal: a set of compatible intervals (requests) with a maximumtotal value .
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
What if all the values are the same ?
We start with the one which has the smallest finish time and add itinto our solution and remove the ones that have intersection withit and repeat!
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
What if all the values are the same ?
We start with the one which has the smallest finish time and add itinto our solution and remove the ones that have intersection withit and repeat!
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
1
2
3
4
5
6
v1 = 2
v2 = 4
v3 = 4
v4 = 7
v5 = 2
v6 = 1
1
2
3
4
5
6
v1 = 2
v2 = 4
v3 = 4
v4 = 7
v5 = 5
v6 = 1
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
1
2
3
4
5
6
v1 = 2
v2 = 4
v3 = 4
v4 = 7
v5 = 2
v6 = 1
1
2
3
4
5
6
v1 = 2
v2 = 4
v3 = 4
v4 = 7
v5 = 5
v6 = 1
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
We order the intervals (requests) based on their finishing time(non-increasing order of finishing time) :
f1 ≤ f2 ≤ · · · ≤ fn
For every interval j let p(j) be the largest index i < j such thatintervals i , j do not overlap (have more than one points incommon)
p(j) = 0 if there is no interval before j and disjoint from j .
1
2
3
4
5
6
v1 = 2
v2 = 4
v3 = 4
v4 = 7
v5 = 2
v6 = 1
p(1) = 0
p(2) = 0
p(3) = 0
p(4) = 0
p(5) = 3
p(6) = 4
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
We order the intervals (requests) based on their finishing time(non-increasing order of finishing time) :
f1 ≤ f2 ≤ · · · ≤ fn
For every interval j let p(j) be the largest index i < j such thatintervals i , j do not overlap (have more than one points incommon)
p(j) = 0 if there is no interval before j and disjoint from j .
1
2
3
4
5
6
v1 = 2
v2 = 4
v3 = 4
v4 = 7
v5 = 2
v6 = 1
p(1) = 0
p(2) = 0
p(3) = 0
p(4) = 0
p(5) = 3
p(6) = 4
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
We order the intervals (requests) based on their finishing time(non-increasing order of finishing time) :
f1 ≤ f2 ≤ · · · ≤ fn
For every interval j let p(j) be the largest index i < j such thatintervals i , j do not overlap (have more than one points incommon)
p(j) = 0 if there is no interval before j and disjoint from j .
1
2
3
4
5
6
v1 = 2
v2 = 4
v3 = 4
v4 = 7
v5 = 2
v6 = 1
p(1) = 0
p(2) = 0
p(3) = 0
p(4) = 0
p(5) = 3
p(6) = 4
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT (j) be the value of the optimal solution considering onlyintervals from 1 to j (according to their order).
We can write a recursive formula to compute OPT (j),
Either interval j is in the optimal solution or j is not in the solution.Therefore :
OPT (j) = max{OPT (j − 1), vj + OPT (p(j))}
Try not to implement using recursive call because the running timewould be exponential!
Recursive function is easy to implement but time consuming!
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT (j) be the value of the optimal solution considering onlyintervals from 1 to j (according to their order).
We can write a recursive formula to compute OPT (j),
Either interval j is in the optimal solution or j is not in the solution.
Therefore :
OPT (j) = max{OPT (j − 1), vj + OPT (p(j))}
Try not to implement using recursive call because the running timewould be exponential!
Recursive function is easy to implement but time consuming!
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT (j) be the value of the optimal solution considering onlyintervals from 1 to j (according to their order).
We can write a recursive formula to compute OPT (j),
Either interval j is in the optimal solution or j is not in the solution.Therefore :
OPT (j) = max{OPT (j − 1), vj + OPT (p(j))}
Try not to implement using recursive call because the running timewould be exponential!
Recursive function is easy to implement but time consuming!
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT (j) be the value of the optimal solution considering onlyintervals from 1 to j (according to their order).
We can write a recursive formula to compute OPT (j),
Either interval j is in the optimal solution or j is not in the solution.Therefore :
OPT (j) = max{OPT (j − 1), vj + OPT (p(j))}
Try not to implement using recursive call because the running timewould be exponential!
Recursive function is easy to implement but time consuming!
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Dynamic Programming
Iterative-Compute-Opt()1. M[0] := 0;2. for j = 1 to n3. M[j ] = max{vj + M[p(j)],M[j − 1]}
Find-Solution(j)1. if j = 0 then return2. else3. if (vj + M[p(j)] ≥ M[j − 1])4. print j and print ” ”5. Find-Solution(p(j))6. else7. Find-Solution(j-1)
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Dynamic Programming
Iterative-Compute-Opt()1. M[0] := 0;2. for j = 1 to n3. M[j ] = max{vj + M[p(j)],M[j − 1]}
Find-Solution(j)1. if j = 0 then return2. else3. if (vj + M[p(j)] ≥ M[j − 1])4. print j and print ” ”5. Find-Solution(p(j))6. else7. Find-Solution(j-1)
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Subset Sums and Knapsack Problem
Subset Sums (Problem Statement):
1 We are given n items {1, 2, . . . , n} and each item i hasweight wi
2 We are also given a bound W
Goal: select a subset S of the items so that :
1 Σi∈Swi ≤ W2 Σi∈Swi is maximized
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
A greedy approach won’t work if it is based on picking the biggestvalue first. Suppose we have a set {W /2 + 1,W /2,W /2} ofitems. If we choose W /2 + 1 then we can not choose anythingelse. However the optimal is W /2 + W /2.
A greedy approach won’t work if it is based on picking the smallestvalue first. Suppose we have a set {1,W /2,W /2} of items. If wechoose 1 then we can only choose W /2 and nothing more.However the optimal is W /2 + W /2.
Exhaustive search! Produce all the subset and check which onesatisfies the constraint (≤W ) and has maximum size. Runningtime O(2n).
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
A greedy approach won’t work if it is based on picking the biggestvalue first. Suppose we have a set {W /2 + 1,W /2,W /2} ofitems. If we choose W /2 + 1 then we can not choose anythingelse. However the optimal is W /2 + W /2.
A greedy approach won’t work if it is based on picking the smallestvalue first. Suppose we have a set {1,W /2,W /2} of items. If wechoose 1 then we can only choose W /2 and nothing more.However the optimal is W /2 + W /2.
Exhaustive search! Produce all the subset and check which onesatisfies the constraint (≤W ) and has maximum size. Runningtime O(2n).
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
A greedy approach won’t work if it is based on picking the biggestvalue first. Suppose we have a set {W /2 + 1,W /2,W /2} ofitems. If we choose W /2 + 1 then we can not choose anythingelse. However the optimal is W /2 + W /2.
A greedy approach won’t work if it is based on picking the smallestvalue first. Suppose we have a set {1,W /2,W /2} of items. If wechoose 1 then we can only choose W /2 and nothing more.However the optimal is W /2 + W /2.
Exhaustive search! Produce all the subset and check which onesatisfies the constraint (≤W ) and has maximum size. Runningtime O(2n).
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT [i ,w ] be the optimal maximum value of a setS ⊆ {1, 2, . . . , i} where the total value of the items in S is at mostw .
If wi > w then OPT [i ,w ] = OPT [i − 1,w ] otherwise
OPT [i ,w ] = max{OPT [i − 1,w ],wi + OPT [i − 1,w − wi ]}
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT [i ,w ] be the optimal maximum value of a setS ⊆ {1, 2, . . . , i} where the total value of the items in S is at mostw .
If wi > w then OPT [i ,w ] = OPT [i − 1,w ] otherwise
OPT [i ,w ] = max{OPT [i − 1,w ],wi + OPT [i − 1,w − wi ]}
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT [i ,w ] be the optimal maximum value of a setS ⊆ {1, 2, . . . , i} where the total value of the items in S is at mostw .
If wi > w then OPT [i ,w ] = OPT [i − 1,w ] otherwise
OPT [i ,w ] = max{OPT [i − 1,w ],wi + OPT [i − 1,w − wi ]}
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Subset-Sum(n,W)1. Array M[0, .., n, 0, ..,W ]
2. for ( i = 1 to W ) M[0, i ] = 0
3. for ( i = 1 to n )
4. for ( w = 1 to W )
5. if (w < wi ) then M[i ,w ] = M[i − 1,w ]
6. else
7. if (wi + M[i − 1,w − wi ] > M[i − 1,w ])
8. M[i ,w ] = wi + M[i − 1,w − wi ]
9. else M[i ,w ] = M[i − 1,w ]
Time complexity O(nW )
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Subset-Sum(n,W)1. Array M[0, .., n, 0, ..,W ]
2. for ( i = 1 to W ) M[0, i ] = 0
3. for ( i = 1 to n )
4. for ( w = 1 to W )
5. if (w < wi ) then M[i ,w ] = M[i − 1,w ]
6. else
7. if (wi + M[i − 1,w − wi ] > M[i − 1,w ])
8. M[i ,w ] = wi + M[i − 1,w − wi ]
9. else M[i ,w ] = M[i − 1,w ]
Time complexity O(nW )
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Exercise
Suppose W = 6 and n = 3 and the items of sizes w1 = w2 = 2and w3 = 3.Fill out matrix M.
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Problem : We are given n jobs {J1, J2, . . . , Jn} where each Ji has aprocessing time pi > 0 ( an integer).
We have two identical machines M1,M2 and we want to executeall the jobs.
Schedule the jobs so that the finish time is minimized.
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Knapsack Problem
Knapsack Problem :
1 We are given n items {1, 2, . . . , n} and each item i hasweight wi
2 Each item has value vi3 We are also given a bound W
Goal: select a subset S of the items so that :
1 Σi∈Swi ≤ W2 Σi∈Svi is maximized
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT [i ,w ] be the optimal maximum value of a setS ⊂ {1, 2, . . . , i} where the total value of the items in S is at mostw .
If wi > w then OPT [i ,w ] = OPT [i − 1,w ] otherwise
OPT [i ,w ] = max{OPT [i − 1,w ], vi + OPT [i − 1,w − wi ]}
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT [i ,w ] be the optimal maximum value of a setS ⊂ {1, 2, . . . , i} where the total value of the items in S is at mostw .
If wi > w then OPT [i ,w ] = OPT [i − 1,w ] otherwise
OPT [i ,w ] = max{OPT [i − 1,w ], vi + OPT [i − 1,w − wi ]}
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)
Let OPT [i ,w ] be the optimal maximum value of a setS ⊂ {1, 2, . . . , i} where the total value of the items in S is at mostw .
If wi > w then OPT [i ,w ] = OPT [i − 1,w ] otherwise
OPT [i ,w ] = max{OPT [i − 1,w ], vi + OPT [i − 1,w − wi ]}
Arash Rafiey Dynamic Programming( Weighted Interval Scheduling)