Uploaded by Burner Account

MaxIndependentSetTrees

advertisement
Dynamic Programming Algorithms
Maximum Independent Set
CS 336: Design and Analysis of Algorithms
Instructor: Abhratanu Dutta
© Konstantin Makarychev
Independent Sets in Graphs
β–ͺ 𝐼 is an independent set in a graph 𝐺 if I doesn’t contain
adjacent vertices.
Independent Set
Independent Sets in Graphs
β–ͺ 𝐼 is an independent set in a graph 𝐺 if 𝐼 doesn’t contain
adjacent vertices.
Independent Set
Independent Sets in Graphs
β–ͺ 𝐼 is an independent set in a graph 𝐺 if 𝐼 doesn’t contain
adjacent vertices.
Not an
Independent Set
Maximum Independent Sets
Problem: Given a graph 𝐺 = (𝑉, 𝐸), find a maximum
independent set 𝐼 ⊂ 𝑉.
➒ Bad news: The problem is very hard to solve (NP-hard).
➒ Good news: The problem is easy on trees.
Maximum Independent Set in Trees
Maximum Independent Set in Trees
Greedy Algorithm
for Trees
β–ͺ Sort vertices by depth
in decreasing order.
β–ͺ For each node π‘₯:
if children of π‘₯ are
not in 𝑰, add π‘₯.
Else: discard π‘₯.
Maximum Independent Set in Weighted Trees
β–ͺ Suppose vertices have
weights.
β–ͺ Goal: Find maximum
weight independent set.
Maximum Independent Set in Weighted Trees
Very Valuable
Vertex
1,000
β–ͺ Suppose vertices have
weights.
β–ͺ Goal: Find maximum
weight independent set.
β–ͺ Very expensive vertices
must belong to 𝑰.
Recursive Algorithm for Binary Trees
function Max_Independent_Set (π‘₯)
Let 𝑦 and 𝑧 be the children of π‘₯.
Find optimal solutions for subtrees rooted in 𝑦 and 𝑧.
𝐼𝑦 = Max_Independent_Set(𝑦)
𝐼𝑧 = Max_Independent_Set(𝑧)
How to combine solutions 𝐼𝑦 and 𝐼𝑧 ? A better subproblem?
Maximum Independent Set in Weighted Trees
β–ͺ Should we add π‘₯ to 𝐼?
β–ͺ It depends…
𝒙
π’š
𝒛
Maximum Independent Set in Weighted Trees
Consider two cases.
• If π‘₯ ∉ 𝐼, then
𝒙
π’š
𝒛
we can pick arbitrary
independent sets
𝐼𝑦 and 𝐼𝑧 in subtrees 𝑇𝑦
and 𝑇𝑧 rooted at 𝑦 and
𝑧.
Maximum Independent Set in Weighted Trees
Consider two cases.
• If π‘₯ ∉ 𝐼, then
𝒙
π’š
𝒛
independent sets 𝐼𝑦 and
𝐼𝑧 in subtrees 𝑇𝑦 and 𝑇𝑧
rooted at 𝑦 and z are
not restricted.
Maximum Independent Set in Weighted Trees
Consider two cases.
• If π‘₯ ∈ 𝐼, then
𝒙
π’š
𝒛
independent sets 𝐼𝑦𝑅
and 𝐼𝑧𝑅 in subtrees 𝑇𝑦
and 𝑇𝑧 rooted at y and 𝑧
cannot contain
𝑦 and 𝑧.
Two Subproblems
a. Maximum Independent Set in Subtree 𝑇π‘₯ :
β–ͺ
The cost of the maximum weight independent set 𝐼π‘₯ in 𝑇π‘₯ .
b. Restricted Maximum Independent Set in Subtree 𝑇π‘₯ :
β–ͺ
The maximum weight independent set 𝐼π‘₯𝑅 in 𝑇π‘₯ that
does not contain π‘₯.
Maximum Independent Set in 𝑻𝒙
function Max_Independent_Set (π‘₯)
Let 𝑦 and 𝑧 be children of π‘₯:
𝐼𝑦 = Max_Independent_Set (𝑦)
𝐼𝑧 = Max_Independent_Set (𝑧)
𝐼𝑦𝑅 = Restricted_Max_Independent_Set (𝑦)
𝐼𝑦𝑅 = Restricted_Max_Independent_Set (𝑧)
Return the best of two solutions 𝐼𝑦 ∪ 𝐼𝑧 and π‘₯ ∪ 𝐼𝑦𝑅 ∪ 𝐼𝑧𝑅
Restricted Maximum Independent Set
We have π‘₯ ∉ 𝐼π‘₯𝑅 , hence
𝒙
π’š
𝒛
independent sets 𝐼𝑦 and
𝐼𝑧 in subtrees 𝑇𝑦 and 𝑇𝑧
rooted at 𝑦 and 𝑧 are
not restricted.
Restricted Maximum Independent Set in 𝑇π‘₯
function Restricted_Max_Independent_Set (π‘₯)
Let 𝑦 and 𝑧 be children of π‘₯:
𝐼𝑦 = Max_Independent_Set (𝑦)
𝐼𝑧 = Max_Independent_Set (𝑧)
Return set 𝐼𝑦 ∪ 𝐼𝑧
Dynamic Programming
function Max_Independent_Set (π‘₯)
➒ Lookup result for π‘₯ in the cache or DP table.
Let 𝑦 and 𝑧 be children of π‘₯:
𝐼𝑦 = Max_Independent_Set (𝑦)
𝐼𝑧 = Max_Independent_Set (𝑧)
𝐼𝑦𝑅 = Restricted_Max_Independent_Set (𝑦)
𝐼𝑦𝑅 = Restricted_Max_Independent_Set (𝑧)
Store the result in the cache or DP table.
Return the best of two solutions 𝐼𝑦 ∪ 𝐼𝑧 and π‘₯ ∪ 𝐼𝑦𝑅 ∪ 𝐼𝑧𝑅
Dynamic Programming
function Restricted_Max_Independent_Set (π‘₯)
➒ Lookup result for π‘₯ in the cache or DP table.
Let 𝑦 and 𝑧 be children of π‘₯:
𝐼𝑦 = Max_Independent_Set (𝑦)
𝐼𝑧 = Max_Independent_Set (𝑧)
➒ Store the result (𝐼𝑦 ∪ 𝐼𝑧 ) in the cache or DP table.
Return set 𝐼𝑦 ∪ 𝐼𝑧
Filling values in DP Table
We can
❖ Recursively call Max_Independent_Set and
Restricted_Max_Independent_Set as discussed above.
or
❖ Fill DP table bottom up.
Bottom-up Approach
β–ͺ Start with values in
leaves.
Bottom-up Approach
β–ͺ Start with values in
leaves.
5; 0
7; 0
9; 0
3; 0
1;0
Bottom-up Approach
β–ͺ Start with values in
leaves.
β–ͺ Fill in DP table level by
level.
5; 0
7; 0
β–ͺ When Alg needs a
result, it is already
available.
9; 0
3; 0
1;0
Running Time
β–ͺ We perform a constant number of operations for every node in
the binary tree.
β–ͺ Hence, the running time is 𝑂(𝑛).
Questions?
Download