Given:
Set of
intervals
, each having weight 
Want:
Maximum weight subset of compatible intervals
Means:
we have:
-
requests labelled 
- with each
request specifying
- a start time

- a finish time

- a weight

- Two intervals are compatible if they do not overlap
- The goal is to find a subset
that maximize the sum of the weights of selected intervals.
- a start time
Solve:
Assume all requests are sorted in order of finish time: 
- We say a request
comes before a request
if 
- define
, for an interval
, to be the largest index
such that intervals
and
are disjoint - means
is the leftmost interval that ends before
begins, we define
if no request
is disjoint from
.
Prove:
Let
be an optimal solution, ignoring for now we have no idea what
is.
Claim:
Either interval
(the last one) belongs to
, or it doesn’t.
- Claim 1:
belongs to
, then no interval indexed strictly between
and
can belong to
, and
must include an optimal solution to the problem consisting of requests
. - Thus
is optimal for sub-instance
of intervals that finish 
- Claim 2:
does NOT belong to
, then
is equal to the optimal solution to the problem consisting of requests 
- Thus
is optimal for sub-instance 
Now computes:
MWIS(n)
if n=0
return 0 //optimum over empty set
else
return max{ MWIS(n-1), MWIS(p(n))+w(n) }
Just like Fibonacci numbers, it runs in 
Reusing computation:
MWIS(n)
T //array 1,...,n
T[0]=0
for i = 1,…,n
T[i]= max{T[i],T[p(i)]+w(i)}
return T[n]
Algorithm runs in
time
Took
to sort intervals and to compute
.
How to find optimal solution:
call MWIS(n) to compute array T
S //empty set
i //n
while i > 0
if T[i]=T[p(i)]+w(i)
add i to S
i←p(n)
else
i←i-1
Note: array T contains the value of the optimal solution on the full instance.
Note: each state of DP takes
time, the algorithm runs
iterations → 
