The Problem
- We have n n n requests labeled 1 , 2 , . . . , n 1, 2, ..., n 1,2,...,n, with each request i i i 起始时间: s i s_i si ,结束时间: f i f_i fi.
- Each interval i i i has a value, or weight, v i v_i vi.
- Two intervals are compatible if they do not overlap.
- GOAL: select a subset S ⊆ { 1 , 2 , . . . , n } S \subseteq \{1, 2, ..., n\} S⊆{1,2,...,n} of mutually compatible intervals to maximize the sum of the values of the selected intervals ∑ i ∈ S v i \sum_{i \in S}v_i ∑i∈Svi
Design Algorithm
将结束时间按从小到大排列, f 1 ≤ f 2 ≤ . . . ≤ f n f_1 \leq f_2 \leq ... \leq f_n f1≤f2≤...≤fn。我们称 a request i i i comes before a request j j j if f i < f j f_i < f_j fi<fj。
对于一个 request
j
j
j, 定义
p
(
j
)
p(j)
p(j):the largest index
i
<
j
i < j
i<j such that intervals
i
i
i and
j
j
j are disjoint.
Consider an optimal solution
O
\mathcal{O}
O.
Let
O
j
\mathcal{O}_j
Oj denote the optimal solutions of smaller problems of the form
{
1
,
2
,
.
.
.
,
j
}
\{1, 2, ..., j\}
{1,2,...,j}.
对于最后一个 request n n n 只有两种可能性:
- n ∈ O n n \in \mathcal{O}_n n∈On , index 在 p ( n ) p(n) p(n) ~ n n n 之间的 request 都不属于 O n \mathcal{O}_n On
- n ∉ O n n \notin \mathcal{O}_n n∈/On, 那么 O n = O n − 1 \mathcal{O}_n = \mathcal{O}_{n-1} On=On−1
Let O P T ( j ) OPT(j) OPT(j) denote the value of solution O j \mathcal{O}_j Oj (最优解集包含的 requests 的 values)
定理 (6.1): O P T ( j ) = max ( v j + O P T ( p ( j ) ) , O P T ( j − 1 ) ) OPT(j) = \max(v_j + OPT(p(j)), OPT(j-1)) OPT(j)=max(vj+OPT(p(j)),OPT(j−1)) .
定理 (6.2): Request
j
∈
O
j
j \in \mathcal{O}_j
j∈Oj if and only if
v
j
+
O
P
T
(
p
(
j
)
)
≥
O
P
T
(
j
−
1
)
v_j + OPT(p(j)) \geq OPT(j-1)
vj+OPT(p(j))≥OPT(j−1)
求最优解的值的算法伪代码:(简易)
Compute-Opt( j j j)
If
j
=
0
j=0
j=0 then
\quad
Return 0
Else
\quad
Return
max
(
v
j
+
Compute-Opt
(
p
(
j
)
)
,
Compute-Opt
(
j
−
1
)
)
\max(v_j+\text{Compute-Opt}(p(j)), \text{Compute-Opt}(j-1))
max(vj+Compute-Opt(p(j)),Compute-Opt(j−1))
EndIf
定理 (6.3): Compute-Opt(
j
j
j) correct compute OPT(
j
j
j) for each
j
=
1
,
2
,
.
.
.
,
n
j=1, 2, ..., n
j=1,2,...,n.
证明:
由定义,OPT
(
0
)
=
0
(0)= 0
(0)=0
对于
j
>
0
j > 0
j>0, 假设
∀
i
<
j
\forall i<j
∀i<j Compute-Opt
(
j
)
(j)
(j) = OPT(
i
i
i)
那么,可知 Compute-Opt
(
j
)
(j)
(j) = OPT(
p
(
j
)
p(j)
p(j)) 和 Compute-Opt
(
j
−
1
)
(j-1)
(j−1) = OPT(
j
−
1
j-1
j−1)
由 定义(6.1) 可得
OPT
(
j
)
=
max
(
v
j
+
Compute-Opt
(
p
(
j
)
)
,
Compute-Opt
(
j
−
1
)
)
\text{OPT}(j)=\max (v_j+\text{Compute-Opt}(p(j)), \text{Compute-Opt}(j-1))
OPT(j)=max(vj+Compute-Opt(p(j)),Compute-Opt(j−1))
=
Compute-Opt
(
j
)
\quad \quad \quad \quad \quad \quad\,\,\,\,\,= \text{Compute-Opt}(j)
=Compute-Opt(j)
对于上面图片中给出的 requests,recursion 如下:
Memorizing Recursion
可以发现,中间包含很多重复计算。
其实只需要 solve
n
+
1
n+1
n+1 subproblems:
Compute-Opt
(
1
)
\text{Compute-Opt}(1)
Compute-Opt(1), …,
Compute-Opt
(
n
)
\text{Compute-Opt}(n)
Compute-Opt(n).
用 Memorizing Recursion 来减少运算。
Maintain an array M [ 0... n ] M[0 ... n] M[0...n]
- M [ j ] M[j] M[j] will start with the value “empty”
- but will hold the value of Compute-Opt ( j ) \text{Compute-Opt}(j) Compute-Opt(j) as soon as it is first determined.
求最优解的值的算法伪代码:
M-Compute-Opt( j j j)
\quad
If
j
=
0
j=0
j=0 then
\quad
\quad
Return
0
0
0
\quad
Else if
M
[
j
]
M[j]
M[j] is not empty then
\quad
\quad
Return
M
[
j
]
M[j]
M[j]
\quad
Else
\quad
\quad
Define
M
[
j
]
=
max
(
v
j
+
M-Compute-Opt
(
p
(
j
)
)
,
M-Compute-Opt
(
j
−
1
)
)
M[j]=\max (v_j+\text{M-Compute-Opt}(p(j)), \text{M-Compute-Opt}(j-1))
M[j]=max(vj+M-Compute-Opt(p(j)),M-Compute-Opt(j−1))
\quad
\quad
Return
M
[
j
]
M[j]
M[j]
\quad
Endif
定理 (6.4): The running time of M-Compute-Opt( n n n) is O ( n ) O(n) O(n). (assume the intervals 按照结束时间从小到大排列)
Compute a Solution in Addition to Its Value
之前计算的是 the value of the optimal solution
还想直到 optimal solution 具体包含哪些 intervals
我们可以 maintain an array S S S, 其中 S [ i ] S[i] S[i] 包含 an optimal set of intervals among { 1 , 2 , . . . , i } \{1,2, ..., i\} {1,2,...,i}。但是,每次书写 S [ i ] S[i] S[i] 会 blow up the running time by an additional factor of O ( n ) O(n) O(n) time.
解决方法:可以通过计算得到的 array M M M 中的 value 来 recover the optimal solution。 j ∈ O n j \in \mathcal{O}_n j∈On if and only if v j + OPT ( p ( j ) ) ≥ OPT ( j − 1 ) v_j+\text{OPT}(p(j)) \geq \text{OPT}(j-1) vj+OPT(p(j))≥OPT(j−1)
求最优解集的算法伪代码:
Find-Solution( j j j)
\quad
If
j
=
0
j=0
j=0 then
\quad
\quad
Output nothing
\quad
Else
\quad
\quad
If
v
j
+
M
[
p
(
j
)
]
≥
M
[
j
−
1
]
v_j + M[p(j)] \geq M[j-1]
vj+M[p(j)]≥M[j−1] then
\quad
\quad
\quad
Output
j
j
j together with the result of
Find-Solution
(
p
(
j
)
)
\text{Find-Solution}(p(j))
Find-Solution(p(j))
\quad
\quad
Else
\quad
\quad
\quad
Output the result of
Find-Solution
(
j
−
1
)
\text{Find-Solution}(j-1)
Find-Solution(j−1)
\quad
\quad
Endif
\quad
Endif
定理 (6.5): Given the array M M M of the optimal values of the subproblems, Find-Solution \text{Find-Solution} Find-Solution returns an optimal solution in O ( n ) O(n) O(n) time.