0% found this document useful (0 votes)
0 views

04-dynamic-programming

The document discusses dynamic programming on trees, focusing on the maximum weight independent set problem. It outlines a recursive algorithm for solving this problem, emphasizing the importance of memoization and the structure of subproblems. The lecture also highlights the efficiency of dynamic programming when applied to tree structures compared to arbitrary graphs.

Uploaded by

Tanmay Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

04-dynamic-programming

The document discusses dynamic programming on trees, focusing on the maximum weight independent set problem. It outlines a recursive algorithm for solving this problem, emphasizing the importance of memoization and the structure of subproblems. The lecture also highlights the efficiency of dynamic programming when applied to tree structures compared to arbitrary graphs.

Uploaded by

Tanmay Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

CS 473: Algorithms, Spring 2021

Dynamic Programming on
Trees
Lecture 4
Feb 4, 2021

Most slides are courtesy Prof. Chekuri


Ruta (UIUC) CS473 1 Spring 2021 1 / 39
What is Dynamic Programming?
Every recursion can be memoized. Automatic memoization does not
help us understand whether the resulting algorithm is efficient or not.

Dynamic Programming:
A recursion that when memoized leads to an efficient algorithm.

Key Questions:
Given a recursive algorithm, how do we analyze the complexity
when it is memoized?
How do we recognize whether a problem admits a (recursive)
dynamic programming based efficient algorithm?
How do we further optimize time and space of a dynamic
programming based algorithm?
Ruta (UIUC) CS473 2 Spring 2021 2 / 39
Dynamic Programming Template

1 Come up with a recursive algorithm to solve problem


2 Understand the structure/number of the subproblems generated
by recursion
3 Memoize the recursion
set up compact notation for subproblems
set up a data structure for storing subproblems

Ruta (UIUC) CS473 3 Spring 2021 3 / 39


Dynamic Programming Template

1 Come up with a recursive algorithm to solve problem


2 Understand the structure/number of the subproblems generated
by recursion
3 Memoize the recursion
set up compact notation for subproblems
set up a data structure for storing subproblems
4 Iterative algorithm
Understand dependency graph on subproblems
Pick an evaluation order (any topological sort of the
dependency DAG)
5 Analyze time and space
6 Optimize

Ruta (UIUC) CS473 3 Spring 2021 3 / 39


Dynamic Programming on Trees
Fact: Many graph optimization problems are NP-Hard

Fact: The same graph optimization problems are in P on trees.

Why?

Ruta (UIUC) CS473 4 Spring 2021 4 / 39


Dynamic Programming on Trees
Fact: Many graph optimization problems are NP-Hard

Fact: The same graph optimization problems are in P on trees.

Why?

A significant reason: DP algorithm based on decomposability

Powerful methodology for graph algorithms via a formal notion of


decomposability called treewidth (beyond the scope of this class)

Ruta (UIUC) CS473 4 Spring 2021 4 / 39


Maximum Independent Set in a Graph

Definition
Given undirected graph G = (V , E ) a subset of nodes S ⊆ V is an
independent set (also called a stable set) if for there are no edges
between nodes in S. That is, if u, v ∈ S then (u, v ) 6∈ E .

B
C
A
F

E D

Some independent sets in graph above: {D}, {A, C }, {B, E , F }

Ruta (UIUC) CS473 5 Spring 2021 5 / 39


Maximum Independent Set Problem

Input Graph G = (V , E )
Goal Find maximum sized independent set in G

B
C
A
F

E D

Ruta (UIUC) CS473 6 Spring 2021 6 / 39


Maximum Weight Independent Set Problem

Input Graph G = (V , E ), weights w (v ) ≥ 0 for v ∈ V


Goal Find maximum weight independent set in G

5
B 15
2 C
A
F 2
E D
10 20

Ruta (UIUC) CS473 7 Spring 2021 7 / 39


Maximum Weight Independent Set Problem

1 No one knows an efficient (polynomial time) algorithm for this


problem
2 Problem is NP-Hard and it is believed that there is no
polynomial time algorithm

Brute-force algorithm:

Ruta (UIUC) CS473 8 Spring 2021 8 / 39


Maximum Weight Independent Set Problem

1 No one knows an efficient (polynomial time) algorithm for this


problem
2 Problem is NP-Hard and it is believed that there is no
polynomial time algorithm

Brute-force algorithm:
Try all subsets of vertices.

Ruta (UIUC) CS473 8 Spring 2021 8 / 39


A Recursive Algorithm
Let V = {v1 , v2 , . . . , vn }.
For a vertex u let N(u) be its neighbors.

Ruta (UIUC) CS473 9 Spring 2021 9 / 39


A Recursive Algorithm
Let V = {v1 , v2 , . . . , vn }.
For a vertex u let N(u) be its neighbors.
Observation
v1 : vertex in the graph.
One of the following two cases is true
Case 1 v1 is in some maximum independent set.
Case 2 v1 is in no maximum independent set.
We can try both cases to “reduce” the size of the problem

Ruta (UIUC) CS473 9 Spring 2021 9 / 39


A Recursive Algorithm
Let V = {v1 , v2 , . . . , vn }.
For a vertex u let N(u) be its neighbors.
Observation
v1 : vertex in the graph.
One of the following two cases is true
Case 1 v1 is in some maximum independent set.
Case 2 v1 is in no maximum independent set.
We can try both cases to “reduce” the size of the problem
G1 = G − v1 obtained by removing v1 and incident edges from G
G2 = G − v1 − N(v1 ) obtained by removing N(v1 ) ∪ v1 from G

MIS(G ) = max{MIS(G1 ), MIS(G2 ) + w (v1 )}

Ruta (UIUC) CS473 9 Spring 2021 9 / 39


A Recursive Algorithm
RecursiveMIS(G ):
if G is empty then Output 0
v ← a vertex of G
a = RecursiveMIS(G − v )
b = w (v ) + RecursiveMIS(G − v − N(v ))
Output max(a, b)

Ruta (UIUC) CS473 10 Spring 2021 10 / 39


Example

Ruta (UIUC) CS473 11 Spring 2021 11 / 39


Recursive Algorithms
..for Maximum Independent Set

Running time:
 
T (n) = T (n − 1) + T n − 1 − deg (v ) + O(1 + deg (v ))

where deg (v ) is the degree of v . T (0) = T (1) = 1 is base case.

Ruta (UIUC) CS473 12 Spring 2021 12 / 39


Recursive Algorithms
..for Maximum Independent Set

Running time:
 
T (n) = T (n − 1) + T n − 1 − deg (v ) + O(1 + deg (v ))

where deg (v ) is the degree of v . T (0) = T (1) = 1 is base case.

Worst case is when deg (v ) = 0 when the recurrence becomes

T (n) = 2T (n − 1) + O(1)

Solution to this is T (n) = O(2n ).

Ruta (UIUC) CS473 12 Spring 2021 12 / 39


Memoization
We can memoize the recursive algorithm.

Question: Does it lead to an efficient algorithm?

Ruta (UIUC) CS473 13 Spring 2021 13 / 39


Memoization
We can memoize the recursive algorithm.

Question: Does it lead to an efficient algorithm?

What are the sub-problems?

Ruta (UIUC) CS473 13 Spring 2021 13 / 39


Memoization
We can memoize the recursive algorithm.

Question: Does it lead to an efficient algorithm?

What are the sub-problems? Ans.: Subgraphs (subsets of nodes).

How many are they if G has n nodes to start with?

Ruta (UIUC) CS473 13 Spring 2021 13 / 39


Memoization
We can memoize the recursive algorithm.

Question: Does it lead to an efficient algorithm?

What are the sub-problems? Ans.: Subgraphs (subsets of nodes).

How many are they if G has n nodes to start with? A.: Exponential.

Exercise: Show that even when G is a cycle the number of


subproblems is exponential in n.

Ruta (UIUC) CS473 13 Spring 2021 13 / 39


Part I

Maximum Weighted Independent Set


in Trees

Ruta (UIUC) CS473 14 Spring 2021 14 / 39


Maximum Weight Independent Set in a Tree

Input Tree T = (V , E ) and weights w (v ) ≥ 0 for each


v ∈V
Goal Find maximum weight independent set in T

r 10
5 a
b 8

4 c d 4 e f g 11
9 3
2 h i 7 j 8

Maximum weight independent set in above tree: ??

Ruta (UIUC) CS473 15 Spring 2021 15 / 39


A Recursive Algorithm
For an arbitrary graph G :
1 Number vertices as v1 , v2 , . . . , vn
2 Find recursively optimum solutions without vn (recurse on
G − vn ) and with vn (recurse on G − vn − N(vn ) & include
vn ).
3 Saw that if graph G is arbitrary there was no good ordering that
resulted in a small number of subproblems.

What about a tree?

Ruta (UIUC) CS473 16 Spring 2021 16 / 39


A Recursive Algorithm
For an arbitrary graph G :
1 Number vertices as v1 , v2 , . . . , vn
2 Find recursively optimum solutions without vn (recurse on
G − vn ) and with vn (recurse on G − vn − N(vn ) & include
vn ).
3 Saw that if graph G is arbitrary there was no good ordering that
resulted in a small number of subproblems.

What about a tree? Natural candidate for vn is root r of T ?

Ruta (UIUC) CS473 16 Spring 2021 16 / 39


Towards a Recursive Solution
Natural candidate for vn is root r of T ? Let O be an optimum
solution to the whole problem.
Case r 6∈ O :

Ruta (UIUC) CS473 17 Spring 2021 17 / 39


Towards a Recursive Solution
Natural candidate for vn is root r of T ? Let O be an optimum
solution to the whole problem.
Case r 6∈ O :Then O contains an optimum solution for each subtree
of T hanging at a child of r .

Ruta (UIUC) CS473 17 Spring 2021 17 / 39


Towards a Recursive Solution
Natural candidate for vn is root r of T ? Let O be an optimum
solution to the whole problem.
Case r 6∈ O :Then O contains an optimum solution for each subtree
of T hanging at a child of r .
Case r ∈ O :

Ruta (UIUC) CS473 17 Spring 2021 17 / 39


Towards a Recursive Solution
Natural candidate for vn is root r of T ? Let O be an optimum
solution to the whole problem.
Case r 6∈ O :Then O contains an optimum solution for each subtree
of T hanging at a child of r .
Case r ∈ O :None of the children of r can be in O. O − {r }
contains an optimum solution for each subtree of T
hanging at a grandchild of r .

Ruta (UIUC) CS473 17 Spring 2021 17 / 39


Towards a Recursive Solution
Natural candidate for vn is root r of T ? Let O be an optimum
solution to the whole problem.
Case r 6∈ O :Then O contains an optimum solution for each subtree
of T hanging at a child of r .
Case r ∈ O :None of the children of r can be in O. O − {r }
contains an optimum solution for each subtree of T
hanging at a grandchild of r .

Subproblems? Subtrees of T rooted at nodes in T .

Ruta (UIUC) CS473 17 Spring 2021 17 / 39


Towards a Recursive Solution
Natural candidate for vn is root r of T ? Let O be an optimum
solution to the whole problem.
Case r 6∈ O :Then O contains an optimum solution for each subtree
of T hanging at a child of r .
Case r ∈ O :None of the children of r can be in O. O − {r }
contains an optimum solution for each subtree of T
hanging at a grandchild of r .

Subproblems? Subtrees of T rooted at nodes in T .

How many of them?

Ruta (UIUC) CS473 17 Spring 2021 17 / 39


Towards a Recursive Solution
Natural candidate for vn is root r of T ? Let O be an optimum
solution to the whole problem.
Case r 6∈ O :Then O contains an optimum solution for each subtree
of T hanging at a child of r .
Case r ∈ O :None of the children of r can be in O. O − {r }
contains an optimum solution for each subtree of T
hanging at a grandchild of r .

Subproblems? Subtrees of T rooted at nodes in T .

How many of them?

Ruta (UIUC) CS473 17 Spring 2021 17 / 39


Towards a Recursive Solution
Natural candidate for vn is root r of T ? Let O be an optimum
solution to the whole problem.
Case r 6∈ O :Then O contains an optimum solution for each subtree
of T hanging at a child of r .
Case r ∈ O :None of the children of r can be in O. O − {r }
contains an optimum solution for each subtree of T
hanging at a grandchild of r .

Subproblems? Subtrees of T rooted at nodes in T .

How many of them?O(n)

Ruta (UIUC) CS473 17 Spring 2021 17 / 39


Example

r 10
5 a
b 8

4 c d 4 e f g 11
9 3
2 h i 7 j 8

Ruta (UIUC) CS473 18 Spring 2021 18 / 39


A Recursive Solution
T (u): subtree of T hanging at node u

OPT (u): max weighted independent set value in T (u)

OPT (u) =

Ruta (UIUC) CS473 19 Spring 2021 19 / 39


A Recursive Solution
T (u): subtree of T hanging at node u

OPT (u): max weighted independent set value in T (u)

(P
v child of u OPT (v ),
OPT (u) = max P
w (u) + v grandchild of u OPT (v )

Ruta (UIUC) CS473 19 Spring 2021 19 / 39


Iterative Algorithm
1 To evaluate OPT (u) need to have computed values of all
children and grandchildren of u. Compute OPT (u) bottom up.

Ruta (UIUC) CS473 20 Spring 2021 20 / 39


Iterative Algorithm
1 To evaluate OPT (u) need to have computed values of all
children and grandchildren of u. Compute OPT (u) bottom up.
2 What is an ordering of nodes of a tree T to achieve above?

r 10
5 a
b 8

4 c d 4 e f g 11
9 3
2 h i 7 j 8

Ruta (UIUC) CS473 20 Spring 2021 20 / 39


Iterative Algorithm
1 To evaluate OPT (u) need to have computed values of all
children and grandchildren of u. Compute OPT (u) bottom up.
2 What is an ordering of nodes of a tree T to achieve above?

r 10
5 a
b 8

4 c d 4 e f g 11
9 3
2 h i 7 j 8

Ans.: Post-order traversal of a tree.


Ruta (UIUC) CS473 20 Spring 2021 20 / 39
Iterative Algorithm
MIS-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
for i = 1 to n do P !
vj child of vi M[vj ],
M[vi ] = max P
w (vi ) + vj grandchild of vi M[vj ]
return M[vn ] (* Note: vn is the root of T *)

Ruta (UIUC) CS473 21 Spring 2021 21 / 39


Iterative Algorithm
MIS-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
for i = 1 to n do P !
vj child of vi M[vj ],
M[vi ] = max P
w (vi ) + vj grandchild of vi M[vj ]
return M[vn ] (* Note: vn is the root of T *)
Space:

Ruta (UIUC) CS473 21 Spring 2021 21 / 39


Iterative Algorithm
MIS-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
for i = 1 to n do P !
vj child of vi M[vj ],
M[vi ] = max P
w (vi ) + vj grandchild of vi M[vj ]
return M[vn ] (* Note: vn is the root of T *)
Space: O(n) to store the value at each node of T
Running time:

Ruta (UIUC) CS473 21 Spring 2021 21 / 39


Iterative Algorithm
MIS-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
for i = 1 to n do P !
vj child of vi M[vj ],
M[vi ] = max P
w (vi ) + vj grandchild of vi M[vj ]
return M[vn ] (* Note: vn is the root of T *)
Space: O(n) to store the value at each node of T
Running time:
1 Naive bound: Each M[Vi ] evaluation may take

Ruta (UIUC) CS473 21 Spring 2021 21 / 39


Iterative Algorithm
MIS-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
for i = 1 to n do P !
vj child of vi M[vj ],
M[vi ] = max P
w (vi ) + vj grandchild of vi M[vj ]
return M[vn ] (* Note: vn is the root of T *)
Space: O(n) to store the value at each node of T
Running time:
1 Naive bound: Each M[Vi ] evaluation may take O(n).

Ruta (UIUC) CS473 21 Spring 2021 21 / 39


Iterative Algorithm
MIS-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
for i = 1 to n do P !
vj child of vi M[vj ],
M[vi ] = max P
w (vi ) + vj grandchild of vi M[vj ]
return M[vn ] (* Note: vn is the root of T *)
Space: O(n) to store the value at each node of T
Running time:
1 Naive bound: Each M[Vi ] evaluation may take O(n).There are
n such evaluations – O(n2 ).

Ruta (UIUC) CS473 21 Spring 2021 21 / 39


Iterative Algorithm
MIS-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
for i = 1 to n do P !
vj child of vi M[vj ],
M[vi ] = max P
w (vi ) + vj grandchild of vi M[vj ]
return M[vn ] (* Note: vn is the root of T *)
Space: O(n) to store the value at each node of T
Running time:
1 Naive bound: Each M[Vi ] evaluation may take O(n).There are
n such evaluations – O(n2 ).
2 Better bound: Value M[vj ] is accessed by who all?

Ruta (UIUC) CS473 21 Spring 2021 21 / 39


Iterative Algorithm
MIS-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
for i = 1 to n do P !
vj child of vi M[vj ],
M[vi ] = max P
w (vi ) + vj grandchild of vi M[vj ]
return M[vn ] (* Note: vn is the root of T *)
Space: O(n) to store the value at each node of T
Running time:
1 Naive bound: Each M[Vi ] evaluation may take O(n).There are
n such evaluations – O(n2 ).
2 Better bound: Value M[vj ] is accessed by who all? Parent and
grand-parent. So in total

Ruta (UIUC) CS473 21 Spring 2021 21 / 39


Iterative Algorithm
MIS-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
for i = 1 to n do P !
vj child of vi M[vj ],
M[vi ] = max P
w (vi ) + vj grandchild of vi M[vj ]
return M[vn ] (* Note: vn is the root of T *)
Space: O(n) to store the value at each node of T
Running time:
1 Naive bound: Each M[Vi ] evaluation may take O(n).There are
n such evaluations – O(n2 ).
2 Better bound: Value M[vj ] is accessed by who all? Parent and
grand-parent. So in total O(n).

Ruta (UIUC) CS473 21 Spring 2021 21 / 39


Why did DP work on trees?
Each node (including the root) is a separator!

Definition
Given a graph G = (V , E ) a set of nodes S ⊂ V is a separator for
G if G − S has at least two connected components.

Ruta (UIUC) CS473 22 Spring 2021 22 / 39


Why did DP work on trees?
Each node (including the root) is a separator!

Definition
Given a graph G = (V , E ) a set of nodes S ⊂ V is a separator for
G if G − S has at least two connected components.

Definition
S is a balanced separator if each connected component of G − S
has at most 2|V (G )|/3 nodes.

Ruta (UIUC) CS473 22 Spring 2021 22 / 39


Why did DP work on trees?
Each node (including the root) is a separator!

Definition
Given a graph G = (V , E ) a set of nodes S ⊂ V is a separator for
G if G − S has at least two connected components.

Definition
S is a balanced separator if each connected component of G − S
has at most 2|V (G )|/3 nodes.

Exercise: Prove that every tree T has a balanced separator


consisting of a single node.

Ruta (UIUC) CS473 22 Spring 2021 22 / 39


Why did DP work on trees?
Each node (including the root) is a separator!

Definition
Given a graph G = (V , E ) a set of nodes S ⊂ V is a separator for
G if G − S has at least two connected components.

Definition
S is a balanced separator if each connected component of G − S
has at most 2|V (G )|/3 nodes.

Exercise: Prove that every tree T has a balanced separator


consisting of√a single node.
Aside: O(2 n ) algorithm to find MIS in planar graphs using, (i )
balanced-separators, (ii ) DP algorithm on trees.

Ruta (UIUC) CS473 22 Spring 2021 22 / 39


Part II

Minimum Dominating Set in Trees

Ruta (UIUC) CS473 23 Spring 2021 23 / 39


Minimum Dominating Set in a Graph

Definition
Given undirected graph G = (V , E ) a subset of nodes S ⊆ V is a
dominating set if for all v ∈ V , either v ∈ S or a neighbor of v is in
S.

B
C
A
F

E D

Some dominating sets in graph above: {A, B, C , D, E , F },

Ruta (UIUC) CS473 24 Spring 2021 24 / 39


Minimum Weight Dominating Set Problem

Input Graph G = (V , E ), weights w (v ) ≥ 0 for v ∈ V


Goal Find minimum weight dominating set in G

5
B 15
2 C
A
F 2
E D
10 20

Ruta (UIUC) CS473 25 Spring 2021 25 / 39


Minimum Weight Dominating Set Problem

Input Graph G = (V , E ), weights w (v ) ≥ 0 for v ∈ V


Goal Find minimum weight dominating set in G

5
B 15
2 C
A
F 2
E D
10 20

NP-Hard problem

Ruta (UIUC) CS473 25 Spring 2021 25 / 39


Minimum Weight Dominating Set in a Tree

Input Tree T = (V , E ) and weights w (v ) ≥ 0 for each


v ∈V
Goal Find minimum weight dominating set in T

r 10
5 a
b 8

4 c d 4 e f g 11
9 3
2 h i 7 j 8

Minimum weight dominating set in above tree: ??

Ruta (UIUC) CS473 26 Spring 2021 26 / 39


Recursive Algorithm
r is root of T . Let O be an optimum solution for T .
Case r 6∈ O : Then O must contain some child of r . Which one?

Ruta (UIUC) CS473 27 Spring 2021 27 / 39


Recursive Algorithm
r is root of T . Let O be an optimum solution for T .
Case r 6∈ O : Then O must contain some child of r . Which one?
Case r ∈ O : None of the children of r need to be in O because r
can dominate them. However, they may have to be.

Ruta (UIUC) CS473 27 Spring 2021 27 / 39


Recursive Algorithm
r is root of T . Let O be an optimum solution for T .
Case r 6∈ O : Then O must contain some child of r . Which one?
Case r ∈ O : None of the children of r need to be in O because r
can dominate them. However, they may have to be.

Issue 1: In both cases it is not feasible to express |O| easily as


optimum solution values of children or descendants of r .

Ruta (UIUC) CS473 28 Spring 2021 28 / 39


Recursive Algorithm
r is root of T . Let O be an optimum solution for T .
Case r 6∈ O : Then O must contain some child of r . Which one?
Case r ∈ O : None of the children of r need to be in O because r
can dominate them. However, they may have to be.

Issue 1: In both cases it is not feasible to express |O| easily as


optimum solution values of children or descendants of r .

Issue 2: Removing r decomposes T into subtrees rooted at children


of r . However, not easy to decompose problem structure recursively.
Problems at children of r are dependent.
Need to introduce additional variable(s).

Ruta (UIUC) CS473 28 Spring 2021 28 / 39


Recursive Algorithm: Understanding Dependence
Let u1 , u2 , . . . , uk be children of root r of T

What “information” do Tu1 , . . . , Tuk need to know about r ’s status


in an optimum solution in order to become “independent”

Ruta (UIUC) CS473 29 Spring 2021 29 / 39


Recursive Algorithm: Understanding Dependence
Let u1 , u2 , . . . , uk be children of root r of T

What “information” do Tu1 , . . . , Tuk need to know about r ’s status


in an optimum solution in order to become “independent”
Whether r is included in the solution
If r is not included then which of the children is going to cover
it. Equivalently, Tui needs to know whether it should cover r or
some other child will.

Ruta (UIUC) CS473 29 Spring 2021 29 / 39


Recursive Algorithm: Introducing Variables

u: node in tree
pi : boolean variable to indicate whether parent is in solution.
pi = 0 means parent is not included. pi = 1 means it is
included.
cp: boolean variable to indicate whether node needed to cover
parent. cp = 1 means parent needs to be covered. cp = 0
means not needed.

Ruta (UIUC) CS473 30 Spring 2021 30 / 39


Recursive Algorithm: Introducing Variables

u: node in tree
pi : boolean variable to indicate whether parent is in solution.
pi = 0 means parent is not included. pi = 1 means it is
included.
cp: boolean variable to indicate whether node needed to cover
parent. cp = 1 means parent needs to be covered. cp = 0
means not needed.

OPT (u, pi , cp): value of minimum dominating set in Tu with


booleans pi and cp with meaning above.

Ruta (UIUC) CS473 30 Spring 2021 30 / 39


Recursive Algorithm: Sub-problem

u: node in tree
pi indicates if the parent is included or not.
cp indicates if the parent needs to be covered or not.
OPT (u, pi , cp): opt value in Tu with pi and cp as above.

OPT (u, 0, 0): opt value in Tu when parent of u is not included


and u need not cover it.

Ruta (UIUC) CS473 31 Spring 2021 31 / 39


Recursive Algorithm: Sub-problem

u: node in tree
pi indicates if the parent is included or not.
cp indicates if the parent needs to be covered or not.
OPT (u, pi , cp): opt value in Tu with pi and cp as above.

OPT (u, 0, 0): opt value in Tu when parent of u is not included


and u need not cover it.
OPT (u, 0, 1): opt value in Tu when parent of u is not included
and u need to cover it.

Ruta (UIUC) CS473 31 Spring 2021 31 / 39


Recursive Algorithm: Sub-problem

u: node in tree
pi indicates if the parent is included or not.
cp indicates if the parent needs to be covered or not.
OPT (u, pi , cp): opt value in Tu with pi and cp as above.

OPT (u, 0, 0): opt value in Tu when parent of u is not included


and u need not cover it.
OPT (u, 0, 1): opt value in Tu when parent of u is not included
and u need to cover it.
OPT (u, 1, 0): opt value in Tu when parent of u is included and u
need not cover it.

Ruta (UIUC) CS473 31 Spring 2021 31 / 39


Recursive Algorithm: Sub-problem

u: node in tree
pi indicates if the parent is included or not.
cp indicates if the parent needs to be covered or not.
OPT (u, pi , cp): opt value in Tu with pi and cp as above.

OPT (u, 0, 0): opt value in Tu when parent of u is not included


and u need not cover it.
OPT (u, 0, 1): opt value in Tu when parent of u is not included
and u need to cover it.
OPT (u, 1, 0): opt value in Tu when parent of u is included and u
need not cover it.
OPT (u, 1, 1): NOT NEEDED!

Ruta (UIUC) CS473 31 Spring 2021 31 / 39


Recursive Algorithm: Sub-problem

u: node in tree
pi indicates if the parent is included or not.
cp indicates if the parent needs to be covered or not.
OPT (u, pi , cp): opt value in Tu with pi and cp as above.

OPT (u, 0, 0): opt value in Tu when parent of u is not included


and u need not cover it.
OPT (u, 0, 1): opt value in Tu when parent of u is not included
and u need to cover it.
OPT (u, 1, 0): opt value in Tu when parent of u is included and u
need not cover it.
OPT (u, 1, 1): NOT NEEDED!

OPT (r , 0, 0): value of minimum dominating set in T .


Ruta (UIUC) CS473 31 Spring 2021 31 / 39
Recursive Solution
Can we express OPT (u, pi , cp) recursively via children of u?

Ruta (UIUC) CS473 32 Spring 2021 32 / 39


Recursive Solution
Can we express OPT (u, pi , cp) recursively via children of u?
OPT (u, 0, 0): Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u does not need to cover
its parent.

Ruta (UIUC) CS473 32 Spring 2021 32 / 39


Recursive Solution
Can we express OPT (u, pi , cp) recursively via children of u?
OPT (u, 0, 0): Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u does not need to cover
its parent.Let Cu be children of u.
Case u is included: Then u does not need to be covered by any
child.

Ruta (UIUC) CS473 32 Spring 2021 32 / 39


Recursive Solution
Can we express OPT (u, pi , cp) recursively via children of u?
OPT (u, 0, 0): Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u does not need to cover
its parent.Let Cu be children of u.
Case u is included: Then u does not need to be covered by any
child. Include u and recurse.P
OPT (u, 0, 0) = w (u) + v ∈Cu

Ruta (UIUC) CS473 32 Spring 2021 32 / 39


Recursive Solution
Can we express OPT (u, pi , cp) recursively via children of u?
OPT (u, 0, 0): Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u does not need to cover
its parent.Let Cu be children of u.
Case u is included: Then u does not need to be covered by any
child. Include u and recurse.P
OPT (u, 0, 0) = w (u) + v ∈Cu OPT (v , 1, 0)
Case u is not included: Then u needs to be covered by some child.
We do a min over all children.
OPT (u, 0, 0) =
minv ∈Cu

Ruta (UIUC) CS473 32 Spring 2021 32 / 39


Recursive Solution
Can we express OPT (u, pi , cp) recursively via children of u?
OPT (u, 0, 0): Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u does not need to cover
its parent.Let Cu be children of u.
Case u is included: Then u does not need to be covered by any
child. Include u and recurse.P
OPT (u, 0, 0) = w (u) + v ∈Cu OPT (v , 1, 0)
Case u is not included: Then u needs to be covered by some child.
We do a min over all children.
OPT (u, 0, 0) =
minv ∈Cu (OPT (v , 0, 1) + v 0 ∈Cu −v OPT (v 0 , 0, 0))
P

Ruta (UIUC) CS473 32 Spring 2021 32 / 39


Recursive Solution
Can we express OPT (u, pi , cp) recursively via children of u?
OPT (u, 0, 0): Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u does not need to cover
its parent.Let Cu be children of u.
Case u is included: Then u does not need to be covered by any
child. Include u and recurse.P
OPT (u, 0, 0) = w (u) + v ∈Cu OPT (v , 1, 0)
Case u is not included: Then u needs to be covered by some child.
We do a min over all children.
OPT (u, 0, 0) =
minv ∈Cu (OPT (v , 0, 1) + v 0 ∈Cu −v OPT (v 0 , 0, 0))
P

Since one of these cases has to be true, we take the min of the values
in the above two cases to compute OPT (u, 0, 0).

Ruta (UIUC) CS473 32 Spring 2021 32 / 39


Recursive Solution
OPT (u, 0, 1) : Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u needs to cover its
parent. Let Cu be children of u.

Ruta (UIUC) CS473 33 Spring 2021 33 / 39


Recursive Solution
OPT (u, 0, 1) : Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u needs to cover its
parent. Let Cu be children of u.

Case u is included: Then u does not need to covered by any child.


Include u and recurse. P
OPT (u, 0, 1) = w (u) + v ∈Cu OPT (v , 1, 0)

Ruta (UIUC) CS473 33 Spring 2021 33 / 39


Recursive Solution
OPT (u, 0, 1) : Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u needs to cover its
parent. Let Cu be children of u.

Case u is included: Then u does not need to covered by any child.


Include u and recurse. P
OPT (u, 0, 1) = w (u) + v ∈Cu OPT (v , 1, 0)
Case u is not included:

Ruta (UIUC) CS473 33 Spring 2021 33 / 39


Recursive Solution
OPT (u, 0, 1) : Value of a minimum dominating set in Tu where we
assume that u’s parent is not included and u needs to cover its
parent. Let Cu be children of u.

Case u is included: Then u does not need to covered by any child.


Include u and recurse. P
OPT (u, 0, 1) = w (u) + v ∈Cu OPT (v , 1, 0)
Case u is not included: This does not arise because u has to cover
its parent.

Ruta (UIUC) CS473 33 Spring 2021 33 / 39


Recursive Solution
OPT (u, 1, 0) : Value of a minimum dominating set in Tu where we
assume that u’s parent is included and u does not need to cover its
parent. Let Cu be children of u.

Ruta (UIUC) CS473 34 Spring 2021 34 / 39


Recursive Solution
OPT (u, 1, 0) : Value of a minimum dominating set in Tu where we
assume that u’s parent is included and u does not need to cover its
parent. Let Cu be children of u.
Case u is included: Then u does not need to covered by any child.
Include u and recurse. P
OPT (u, 1, 0) = w (u) + v ∈Cu OPT (v , 1, 0)

Ruta (UIUC) CS473 34 Spring 2021 34 / 39


Recursive Solution
OPT (u, 1, 0) : Value of a minimum dominating set in Tu where we
assume that u’s parent is included and u does not need to cover its
parent. Let Cu be children of u.
Case u is included: Then u does not need to covered by any child.
Include u and recurse. P
OPT (u, 1, 0) = w (u) + v ∈Cu OPT (v , 1, 0)
Case u is not included: u’s parent is included. Now, does u need to
be covered by its children?

Ruta (UIUC) CS473 34 Spring 2021 34 / 39


Recursive Solution
OPT (u, 1, 0) : Value of a minimum dominating set in Tu where we
assume that u’s parent is included and u does not need to cover its
parent. Let Cu be children of u.
Case u is included: Then u does not need to covered by any child.
Include u and recurse. P
OPT (u, 1, 0) = w (u) + v ∈Cu OPT (v , 1, 0)
Case u is not included: u’s parent is included. Now, does u need to
be covered by its children?
P No. Thus we have,
OPT (u, 1, 0) = v ∈Cu OPT (v , 0, 0)
Take the min of the values in the above two cases to compute
OPT (u, 1, 0).

Ruta (UIUC) CS473 34 Spring 2021 34 / 39


Recursive Solution
OPT (u, 1, 0) : Value of a minimum dominating set in Tu where we
assume that u’s parent is included and u does not need to cover its
parent. Let Cu be children of u.
Case u is included: Then u does not need to covered by any child.
Include u and recurse. P
OPT (u, 1, 0) = w (u) + v ∈Cu OPT (v , 1, 0)
Case u is not included: u’s parent is included. Now, does u need to
be covered by its children?
P No. Thus we have,
OPT (u, 1, 0) = v ∈Cu OPT (v , 0, 0)
Take the min of the values in the above two cases to compute
OPT (u, 1, 0).
Caution: Not including u may appear to be always advantageous
but it is not true.

Ruta (UIUC) CS473 34 Spring 2021 34 / 39


Recursive Solution
OPT (u, 1, 1) : Value of a minimum dominating set in Tu where we
assume that u’s parent is included and u needs to cover its parent.

This subproblem does not make sense since if u’s parent is included
then u does not need to cover it.

Ruta (UIUC) CS473 35 Spring 2021 35 / 39


Base Cases
Leaves are base cases. If u is a leaf.
OPT (u, 0, 0) =

Ruta (UIUC) CS473 36 Spring 2021 36 / 39


Base Cases
Leaves are base cases. If u is a leaf.
OPT (u, 0, 0) = w (u)
OPT (u, 0, 1) =

Ruta (UIUC) CS473 36 Spring 2021 36 / 39


Base Cases
Leaves are base cases. If u is a leaf.
OPT (u, 0, 0) = w (u)
OPT (u, 0, 1) = w (u)
OPT (u, 1, 0) =

Ruta (UIUC) CS473 36 Spring 2021 36 / 39


Base Cases
Leaves are base cases. If u is a leaf.
OPT (u, 0, 0) = w (u)
OPT (u, 0, 1) = w (u)
OPT (u, 1, 0) = 0

Ruta (UIUC) CS473 36 Spring 2021 36 / 39


DP Algorithm

Minimum weight dominating set value in T is OPT (r , 0, 0)

Ruta (UIUC) CS473 37 Spring 2021 37 / 39


DP Algorithm

Minimum weight dominating set value in T is OPT (r , 0, 0)


To compute OPT (r , 0, 0) we need to compute recursively
OPT (u, 0, 0), OPT (u, 0, 1), OPT (u, 1, 0) for all u ∈ T .
Thus number of subproblems is O(n).

Ruta (UIUC) CS473 37 Spring 2021 37 / 39


DP Algorithm

Minimum weight dominating set value in T is OPT (r , 0, 0)


To compute OPT (r , 0, 0) we need to compute recursively
OPT (u, 0, 0), OPT (u, 0, 1), OPT (u, 1, 0) for all u ∈ T .
Thus number of subproblems is O(n).
Nodes should be traveresed in what order?

Ruta (UIUC) CS473 37 Spring 2021 37 / 39


DP Algorithm

Minimum weight dominating set value in T is OPT (r , 0, 0)


To compute OPT (r , 0, 0) we need to compute recursively
OPT (u, 0, 0), OPT (u, 0, 1), OPT (u, 1, 0) for all u ∈ T .
Thus number of subproblems is O(n).
Nodes should be traveresed in what order? Ans.: bottom up
from leaves to root.

Ruta (UIUC) CS473 37 Spring 2021 37 / 39


DP Algorithm

Minimum weight dominating set value in T is OPT (r , 0, 0)


To compute OPT (r , 0, 0) we need to compute recursively
OPT (u, 0, 0), OPT (u, 0, 1), OPT (u, 1, 0) for all u ∈ T .
Thus number of subproblems is O(n).
Nodes should be traveresed in what order? Ans.: bottom up
from leaves to root.
In particular?

Ruta (UIUC) CS473 37 Spring 2021 37 / 39


DP Algorithm

Minimum weight dominating set value in T is OPT (r , 0, 0)


To compute OPT (r , 0, 0) we need to compute recursively
OPT (u, 0, 0), OPT (u, 0, 1), OPT (u, 1, 0) for all u ∈ T .
Thus number of subproblems is O(n).
Nodes should be traveresed in what order? Ans.: bottom up
from leaves to root.
In particular? Ans.: post-order traversal.

Ruta (UIUC) CS473 37 Spring 2021 37 / 39


Iterative Algorithm
DominatingSet-Tree(T ):
Let v1 , v2 , . . . , vn be a post-order traversal of nodes of T
Allocate array M[1..n, 0..1, 0..1] to store OPT (vi , pi , cp) values
for i = 1 to n do
Compute OPT (vi , 0, 0), OPT (vi , 1, 0) and OPT (vi , 0, 1) using
values of children of vi stored in M,
or via base cases if vi is leaf

Store computed values in M for use by parent of vi .


return OPT (vn , 0, 0) (* Note: vn is the root of T *)
Exercise: Work out details and prove an O(n) time implementation.

Ruta (UIUC) CS473 38 Spring 2021 38 / 39


Recap

To obtain recursive solution we introduced additional variables


based on “information” needed to decompose
Decomposition depends both on structure (trees decompose via
separators) and objective function
Subproblems and recursion are almost defined hand in hand

Ruta (UIUC) CS473 39 Spring 2021 39 / 39

You might also like