简体   繁体   中英

Dynamic Programming Recurrence to Solution

I'm trying to solve the weighted interval scheduling problem. Basically, I came up with the following recurrence to obtain the length of the optimum solution:

optimum[i] = max(duration(intervals[i]) + opt[prior[i]], opt[i - 1])

where prior[i] = the latest non-overlapping schedule that finishes before current interval starts.

The recurrence works well and I get the correct solution. However, I want to get the actual schedule and not just the length. How can I do that? I tried by starting from the largest p[i] value and following the pointer until I reach None / -1 / Null but that doesn't always work. I assume I need to keep track of which intervals to keep and which to discard as I solve the recurrence above. I tried by doing something like:

if (duration(intervals[i]) + optimum[prior[i]] >= optimum[i - 1]) {
  optimum[i] = duration(intervals[i]) + optimum[p[i]];
  retain[i] = true;
}
else {
  optimum[i] = optimum[i - 1];
  retain[i] = false;
  retain[i - 1] = true;
}

But this didn't work well.

You can use prior[i] as well as optimum[i] to construct the path. Concretely, you start from the i with the optimal solution. Then the path can be got as follows.

queue<int> path;
int st = i;
while (st > 0) {
  if (op[st] == op[st-1]) st = st -1;
  else {
    path.push(st);
    st = prior[st];
  }
}
pop each item from queue<int> path, you get the intervals you selected.  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM