Introduction, and WIS in Path Graphs
1. Problem Statement
1.1 Input: A path graph
G=(V,E)
G
=
(
V
,
E
)
with nonnegative weight on vertices.
1.2 Desired output: Subset of nonadajacent vertices -an independent set –of maximmum total weight.
Next: Iterate through our algorithm design principles.
Brute-force search: Requires exponential time.
2. A Greedy Approach
2.1 Greedy: Iteratively choose the max-weight vertex not adjacent to any previously chosen vertex.
2.2 A Divide & Conquer Approach
Idea: Recursively compute the max-weight IS of 1st half, ditto(同上) for 2nd half, then combine the solutions.
3. Optimal Substructure
3.1 Critical step: Reason about structure of an optimal solution.
[In terms of optimal solutions of smaller subproblems]
3.2 Motivation: This thought experiment narrows down the set of candidate for the optimal solution; can search through the small set using brute-force-search
Notation: Let
S⊆V
S
⊆
V
be a max-weight independent set(IS). Let
vn=
v
n
=
last vertex of path.
3.2 A Case Analysis
case1: Suppose
vn∉S
v
n
∉
S
. Let
G′=G
G
′
=
G
with
vn
v
n
deleted.
Note:
S
S
also an IS of .
Note:
S
S
must be a max-weight IS of .
Case2: Suppose
vn∈S
v
n
∈
S
.
Note: Previous vertex
vn−1∉S
v
n
−
1
∉
S
[by definition of an IS]. Let
G′′=G
G
″
=
G
with
vn−1,vn
v
n
−
1
,
v
n
deleted.
Note:
S−{vn}
S
−
{
v
n
}
is an IS of
G′′
G
″
.
Note: Must in fact be a max-weight IS of
G′′
G
″
.
4. Toward an Algorithm
Upshot: A max-weight IS must be either
(i) a max-weight IS of
G′
G
′
or.
(ii)
vn
v
n
+ a max-weight IS of
G′′
G
″
.
Corollary: f we knew whether or not
vn
v
n
was in the max-weight IS, could recursively compute the max-weight IS of
G′′
G
″
and be done.
(Crazy ?) idea: Try both possibilities + return the better solution.
5. The Story So far
5.1 Uprshot: If we know whether or not
vn
v
n
is in the max-weight IS, then could recursively compute the max-weight IS of
G′
G
′
or
G′′
G
″
and be done.
Proposed algorithm:
−
−
Recursively compute = max-weight IS of
G′
G
′
.
−
−
Recursively compute = max-weight IS of
G′′
G
″
.
−
−
Return or
S2∪{vn}
S
2
∪
{
v
n
}
,whichever is better.
Good news: Correct.
Bad news: Exponential time.
5.2 Eliminating Redundancy
Obvious fix: The first time you solve a subproblem cache its solution in a global table for
O(1)
O
(
1
)
-time lookup later on.[memoization]
Even better: Reformulate as a bottom-up iterative algorithm. Let
Gi
G
i
= 1st vertices of G.
Plan: Populate array A left to right with A[i] = value of max-weight IS of
Gi
G
i
.
Initialization: A[0] = 0, A[1] =
w1
w
1
Main loop: For
i=2,3,...,n
i
=
2
,
3
,
.
.
.
,
n
:
A[i]
A
[
i
]
= max
{A[i−1],A[i−2]+wi}
{
A
[
i
−
1
]
,
A
[
i
−
2
]
+
w
i
}
Run time: Obviously
O(n)
O
(
n
)
.
A Reconstruction Algorithm
Let A = filled-in array.
−
−
Let
−
−
While
−−
−
−
If
A[i−1]≥A[i−2]+wi
A
[
i
−
1
]
≥
A
[
i
−
2
]
+
w
i
−−−
−
−
−
Decrease
i
i
by 1
Else
−−−−
−
−
−
−
Add
vi
v
i
to
S
S
, decrease by 2
−
−
Return
Claim: Final output S is a max-weight IS of G.
Principles of Dynamic Programming
Fact: Our WIS algorithm is a dynamic programming algorithm!
Key ingredients of dynamix programming:
(1) Identify a small number of subroblems.
[e..g., compute the max-weight IS of
Gi
G
i
for
i=0,1,,,,n
i
=
0
,
1
,
,
,
,
n
]
(2) Can quickly + correctly solve “larger” subproblems given the solutions to “smaller subproblems”.
[Usually via a recurrence such as A[i] = max {A[i-1], A[i-2] +
Wi
W
i
}]
(3) After solving all subproblems, can quickly compute the final solution.
[Usually, it’s just the answer to the “biggest” subproblem].