(Latest Revision:
Nov 20, 2022)
Solution to Weighted Intervals Problem
The Info You Had To E-Mail:
# strt-fnsh w p OPT
1 11-14 7 0 7
2 12-15 5 0 7
3 10-18 9 0 9
4 16-20 10 2 17
5 17-21 6 2 17
6 19-23 6 3 17
7 22-28 10 5 27
8 26-29 8 6 27
Backtracking finds that intervals 7, 4, and 1
form an optimal solution, with total weight 27.
DETAILS SHOWING HOW TO DO THE PROBLEM
The Assigned Set of Intervals and Weights:
start finish weight
10 18 9
22 28 10
12 15 5
26 29 8
11 14 7
19 23 6
17 21 6
16 20 10
To solve the problem, we sort the intervals by increasing finish time, and compute the p-values.
# start finish weight 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
1 11 14 7 --------------- p=0
2 12 15 5 --------------- p=0
3 10 18 9 ---------------------------------------- p=0
4 16 20 10 -------------------- p=2
5 17 21 6 -------------------- p=2
6 19 23 6 -------------------- p=3
7 22 28 10 ------------------------------ p=5
8 26 29 8 --------------- p=6
We then compute the OPT values from row 1 to row 8.
# strt-fnsh w p OPT
0 0-0 0 0 0
1 11-14 7 0 7 = max {OPT(0), w1+OPT(p(1))} = max { 0, 7 + 0} = 7
2 12-15 5 0 7 = max {OPT(1), w2+OPT(p(2))} = max { 7, 5 + 0} = 7
3 10-18 9 0 9 = max {OPT(2), w3+OPT(p(3))} = max { 7, 9 + 0} = 9
4 16-20 10 2 17 = max {OPT(3), w4+OPT(p(4))} = max { 9, 10 + 7} = 17
5 17-21 6 2 17 = max {OPT(4), w5+OPT(p(5))} = max { 17, 6 + 7} = 17
6 19-23 6 3 17 = max {OPT(5), w6+OPT(p(6))} = max { 17, 6 + 9} = 17
7 22-28 10 5 27 = max {OPT(6), w7+OPT(p(7))} = max { 17, 10 + 17} = 27
8 26-29 8 6 27 = max {OPT(7), w8+OPT(p(8))} = max { 27, 8 + 17} = 27
Then we do the backtracking.
- Since 27=OPT(8) = OPT(7)=27, we know we don't need interval 8 for an optimal solution, so we save time by not checking further, and resume backtracking one row above at interval 7, looking for an optimal set chosen from intervals 1-7.
- Since 27=OPT(7) > OPT(6)=17, we need interval 7 to get the optimal total of 27, so the solution set so far is {7}, and we resume backtracking at interval P(7), which is interval 5.
- Since 17=OPT(5) = OPT(4)=17, we know we don't need interval 5 for an optimal solution, so we save time by not checking further, and resume backtracking one row above at interval 4, looking to choose the rest of our optimal set from intervals 1-4.
- Since 17=OPT(4) > OPT(3)=9, we need interval 4 to get the optimal total of 27, so the solution set so far is {7,4}, and we resume backtracking at interval P(4), which is interval 2.
- Since 7=OPT(2) = OPT(1)=7, we know we don't need interval 2 for an optimal solution, so we save time by not checking further, and resume backtracking one row above at interval 1.
- Since 7=OPT(1) > OPT(0)=0, we need interval 1 to get the optimal total of 27, so the solution set so far is {7,4,1}, and we resume backtracking at interval P(1), which is interval 0, but going to interval 0 is the sentinel condition signaling the base case, so we stop.
Thus the solution set is now {7,4,1}. Checking the weights we see that w7+w4+w1 = 10+10+7=27 = the computed value of OPT(8).
Note:
As has been mentioned elsewhere, generally, when backtracking, if OPT(j) = OPT(j-1), it may or may not
be possible to find an optimal solution that contains interval j. However interval j is certainly not needed for an optimal solution, and it's easier just to resume backtracking at row j-1. We will always find an optimal solution this way.