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?