$ \def\opt{ {\mathrm{opt}} } $
Input:
Output. A collection of intervals from $R$ that is
Note: equivalent to (unweighted) interval scheduling when all weights are the same
MaxWeightSchedule(w, p, n):
if n = 0 then return 0
opt-n <- w[n] + MaxWeightSchedule(w, p, p[n])
opt-no-n <- MaxWeightSchedule(w, p, n-1)
return Max(opt-n, opt-no-n)
Idea. Store array max
:
max[i]
is maximum weight of schedule consisting of intervals $r_1, r_2, \ldots, r_i$Question. How to initialize/update max
values?
IMaxWeightSchedule(w, p)
max <- new array of size n+1
max[0] <- 0
for i = 1 up to n do
max[i] <- Max(w[i] + max[p[i]], max[i-1])
endfor
return max[n]
Correctness:
Running Time?
Steps: (assume $n$ intervals)
Sort intervals by end time
Compute array p
Run IMaxWeightSchedule(w, p)
Total?
Update IMaxWeightSchedule
to return the actual schedule of maximum weight, not just the weight itself.
In weighted interval scheduling each request had:
Goal: to service set of non-overlapping requests to maximize total value
Relaxation. Requests have
Each request can be scheduled at any time $\leq B$
Input:
Output: A set $S$ of requests to service with
Constrained Optimization Problem
Previous technique. Express relationship between optimal solutions that
Question. How can we express this relationship for the knapsack problem?
Subtlety. Must keep track of remaining budget
Definition. For $j = 0, 1, \ldots, n$, $\opt(j, C)$ is optimal value of set of requests from $1, 2, \ldots, j$ with budget $C$.
Question. Recursion relation for $\opt(n, B)$ depending on whether or not we include request $r_n$?
Assume. All durations $b_i$ are integers at most $B$.
Compute. To compute $\opt(n, B)$:
max
where max[j, C]
stores the value $\opt(j, C)$Questions.
max
?max
? FindMax(R, n, B):
max <- new 2d array of dimensions n+1, B+1
set max[0, C] <- 0 for C = 0 to B
for j from 1 to n
(b, v) <- i-th request in R
for C from 0 to B
if b <= C then
max[j, C] <- Max(v + max[j-1, C-b], max[j-1,C])
else
max[j, C] <- max[j-1, C]
return max[n, B]
Claim. For all $j$ and $C$, $\max[j, C] = \opt(j, C)$
Proof. Induction on $j$.
Base case $j = 0$. Optimal subset of size $0$ has value $0$.
Inductive step $j \implies j+1$.
request $j+1$ is in optimal subset $S$
$\opt(j+1, C) = v_{j+1} + \opt(j, C - b_{j+1}) = v_{j+1} + \max[j, C - b_{j+1}]$
request $j+1$ is not in optimal subset $S$
$\opt(j+1, C) = \opt(j, C) = \max[j, C]$
FindMax(R, n, B):
max <- new 2d array of dimensions n+1, B+1
set max[0, C] <- 0 for C = 0 to B
for j from 1 to n
(b, v) <- i-th request in R
for C from 0 to B
if b <= C then
max[j, C] <- Max(v + max[j-1, C-b], max[j-1,C])
else
max[j, C] <- max[j-1, C]
return max[n, B]
For the knapsack problem with $n$ requests and budget $B$, we can find compute $\opt(n, B)$ in $O(B n)$ time.
Lecture 25 Ticket. Modify FindMax
procedure to return the subset $S$ whose value is $\opt(n, B)$.