Presentation is loading. Please wait.

Presentation is loading. Please wait.

Weighted Interval Scheduling

Similar presentations


Presentation on theme: "Weighted Interval Scheduling"— Presentation transcript:

1 Weighted Interval Scheduling

2 Optimal Substructure Is the solution equal to the “sum” of smaller solutions? or Does the solution to the whole problem define solutions to the sub-problems?

3 Shortest Path: Optimal Substructure?
w v Shortest path from node u to node v goes through node w Is this the shortest path from w to v? Is this the shortest path from u to w? Yes – improving one improves the whole

4 Longest Path: Optimal Substructure?
w v Longest path from node u to node v goes through node w Is this the longest path from u to w?

5 The (u,v) path does define the longest path from u to w
True False 10

6 The (u,v) path does define the longest path from u to w
True False 10

7 Longest path from u to v u w v u v v Longest path from u to v is not composed of the longest path from u to w plus the longest path from w to v. u w Longest path from u to w?

8 Interval Scheduling Problem
Input: A list of intervals, each with a: Start time si Finish time fi Output: A subset of non-overlapping intervals Goal: The largest possible subset

9 Weighted Interval Scheduling Problem
Input: A list of intervals, each with a: Start time si Finish time fi Weight wi Output: A subset of non-overlapping intervals Goal: The largest possible weight sum

10 Weighted “Classroom” Scheduling Problem
Input: A list of classes, each with a: Start time si Finish time fi Enrollment wi Output: A set of (non-overlapping) classes to be scheduled in the same room Goal: The largest possible enrollment

11 (Unweighted) Interval Scheduling
Solution: Sort by finish time, keep taking next available

12 Weighted Interval Scheduling
6 1 3 100 8 2 16 100 1 Optimal weight: = 203

13 Notice: If we have already sorted by f, we know that p(i) < i
Notation Number the objects by finish time Consider object i p(i) = Largest j such that fj < si (Largest j such that j does not overlap i) Notice: If we have already sorted by f, we know that p(i) < i (fp(i) < si < fi )

14 Weighted Interval Scheduling
Note: we are numbering from 1, not 0 (It will be less confusing later on.) 7:6 2:1 5:3 9:100 1:8 4:2 8:16 3:100 6:1 p(6) = 4 What is p(9)? p(8) = 5 p(7) = Undefined

15 What is p(9)? 1 2 3 4 5 6 7 8 5:3 2:1 4:2 1:8 3:100 6:1 8:16 9:100 7:6 10

16 What is p(9)? 1 2 3 4 5 6 7 8 5:3 2:1 4:2 1:8 3:100 6:1 8:16 9:100 7:6 10

17 Dynamic Programming We will solve the (weighted) problem by dynamic programming Allows us to try a limited number of potential solutions at once More than one solution (as opposed to a greedy approach) But still a limited number (as opposed to brute force)

18 DP Hints We follow the “steps” that apply to all DP problems
Order objects Concentrate on optimal score (not solution) Use the optimal substructure property to create a recursion Use overlapping subproblems property to avoid a recursive encoding

19 Step 1: Order Objects A DP solution usually requires that we place some order on the objects In this case, we will still want to order by finish time. Assume this already has been done: so if i < j  fi  fj

20 Step 2: Concentrate on optimal score
We will start by looking for the optimal score only In this case: what is the highest weight total we can achieve? Define: opt(i) = Optimal score using only elements 1 to i Finding the optimal score will be easier than trying to find the optimal solution Finding the optimal solution will actually be an easy second step

21 Step 3: Exploit Optimal Substructure
Is the solution equal to the “sum” of smaller solutions? or Does the solution to the whole problem define solutions to the sub-problems?

22 Optimal Substructure opt(9) = 100+100+20+7 = 227 optimal = 100+100?
6 1 3 20 100 100 4 3 7 optimal = ? optimal = 7+20? Yes – since opt(n) = ( )+(7+20)

23 Optimal Substruture opt(9) = 100+3+4+5 = 112 opt(8) = 100+3+4 = 107?
6 1 3 5 8 2 4 100 1 opt(8) = = 107? Yes – so opt(9) = opt(8) + 5

24 Optimal Substruture opt(9) = 100+100+7 = 207 opt(8) = 100+100+7 = 207?
6 1 3 2 100 100 4 3 7 opt(8) = = 207? Yes – so opt(9) = opt(8)

25 Using Optimal Substructure
We want to find the value of opt(n) Interval n is used in the optimal solution Two possibilities: Interval n is not used in the optimal solution This is a tautology. (Or it isn’t.)

26 Using Optimal Substructure
We can make use of optimal substructure to solve the problem Compute best solution possible when using interval n Compute best solution possible when not using interval n use whichever is better

27 Case 1: interval n is used
If we know interval n is used, what else do we need to do? p(n) is the last interval we can still use (if j > p(n), then fj > sn) Claim: opt(n) = opt(p(n)) + wn

28 Case 1: interval n is used
Claim: opt(n) = opt(p(n)) + wn Must have opt(n)  opt(p(n)) + wn Take solution for opt(p(n)) Add interval n (must be possible) Resulting total: opt(p(n)) + wn Must have opt(n)  opt(p(n)) + wn Remove interval n from optimal New weight: opt(n) – wn Picked from {1, 2, …, p(n)} …Hence must be  opt(p(n)) Result: opt(n) – wn  opt(p(n)) Results comes from the optimal sub-structure property

29 Case 1: Illustrated opt(9) = 100+3+100 = 203, p(9) = 6
8 2 16 100 1 opt(9) = = 203, p(9) = 6 opt(6) = = 103 opt(9) = opt(6) + w9

30 If interval n is in the optimal solution: then opt(n) = opt(p(n)) + wn
Case 1: Recap If interval n is in the optimal solution: then opt(n) = opt(p(n)) + wn

31 If we know the interval item is not used, what else do we need to do?
Case 2: Item n is not used If we know the interval item is not used, what else do we need to do? Claim: opt(n) = opt(n-1) If item n is not used, then opt(n) and opt(n-1) must correspond to the same solution score

32 The optimal solution clearly doesn’t use interval 9
Case 2: Illustrated 7:6 2:1 5:3 9:2 1:8 4:2 8:1 3:100 6:100 opt(9) = = 203 The optimal solution clearly doesn’t use interval 9 opt(8) = = 203 opt(9) = opt(8) So we get the same solution if we remove 9, considering only intervals 1 to 8

33 Summing Up If n is in the optimal solution: opt(n) = opt(p(n))+wn
If n is not in the optimal solution: opt(n) = opt(n-1) Either we use item n, or we don’t. opt(p(n))+ wn opt(n) = max opt(n-1) opt(0) = 0

34 Recursive Formula int WIS(array W, array P, int n) { if n==0 return 0
int c1 = WIS(W, P, n-1) int c2 = WIS(W, P, P[n])+W[n] return max(c1, c2) Bad news: runtime is terrible Good news: Overlapping sub-problems

35 Step 5: Bottom-Up Calculation
opt(p(n))+ wn opt(n) = max opt(0) = 0 opt(n-1) You can definitely calculate opt(i) if you know opt(i) i < n Calculate opt(1), then opt(2), then …

36 Execution opt(7) = max{opt(0) + w7, opt(6)} = 103
8 8 100 100 103 103 103 119 203 opt(7) = max{opt(0) + w7, opt(6)} = 103 opt(8) = max{opt(5) + w8, opt(7)} = 119 opt(8) = max{opt(6) + w9, opt(8)} = 203 opt(6) = max{opt(4) + w6, opt(5)} = 103 opt(2) = max{opt(0) + w2, opt(1)} = 8 opt(1) = max{opt(0) + w1, opt(0)} = 8 opt(3) = max{opt(0) + w3, opt(2)} = 100 opt(4) = max{opt(2) + w4, opt(3)} = 100 opt(5) = max{opt(3) + w5, opt(4)} = 100

37 Trace-back 7:6 (p = 0) 5:3 (p = 3) 2:1 (p = 0) 9:100 (p = 6)
8 8 100 100 100 100 103 103 103 103 103 103 103 119 203 203 203

38 Maintains Solutions 7:6 (p = 0) 5:3 (p = 3) 2:1 (p = 0) 9:100 (p = 6) 1:8 (p = 0) 4:2 (p = 2) 8:16 (p = 5) 3:100 (p = 0) 6:1 (p = 4) 8 8 8 100 100 100 103 103 103 119 119 203 As we continue, we track up to n different solutions -- more than a greedy algorithm, less than brute force

39 Runtime We need to fill in values into n boxes
8 8 8 100 100 100 103 103 103 119 119 203 We need to fill in values into n boxes Each box requires O(1) time to fill (if we know p()) Total runtime: O(n)

40 Bottom-Up Code What is the runtime of the WIS algorithm?
int WIS(array W, array P, int n) array opt, opt[0] = 0 for i  1 to n c1  opt[P[i]] + W[i] c2  opt[i-1] opt[i]  max{c1,c2} return opt[n] array WIS_Sol(array opt, array P, int n) array solution i  n while (i > 0) if opt[i] == opt[i-1] i = i-1 else solution.push(i) i  p[i] return solution What is the runtime of the WIS algorithm?

41 Runtime of WIS? O(1) O(log n) O(n) O(n log n) O(n2) 9

42 Runtime of WIS? O(1) O(log n) O(n) O(n log n) O(n2) 9


Download ppt "Weighted Interval Scheduling"

Similar presentations


Ads by Google