04-dynamic-programming
04-dynamic-programming
Dynamic Programming on
Trees
Lecture 4
Feb 4, 2021
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
Why?
Why?
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
Input Graph G = (V , E )
Goal Find maximum sized independent set in G
B
C
A
F
E D
5
B 15
2 C
A
F 2
E D
10 20
Brute-force algorithm:
Brute-force algorithm:
Try all subsets of vertices.
Running time:
T (n) = T (n − 1) + T n − 1 − deg (v ) + O(1 + deg (v ))
Running time:
T (n) = T (n − 1) + T n − 1 − deg (v ) + O(1 + deg (v ))
T (n) = 2T (n − 1) + O(1)
How many are they if G has n nodes to start with? A.: Exponential.
r 10
5 a
b 8
4 c d 4 e f g 11
9 3
2 h i 7 j 8
r 10
5 a
b 8
4 c d 4 e f g 11
9 3
2 h i 7 j 8
OPT (u) =
(P
v child of u OPT (v ),
OPT (u) = max P
w (u) + v grandchild of u OPT (v )
r 10
5 a
b 8
4 c d 4 e f g 11
9 3
2 h i 7 j 8
r 10
5 a
b 8
4 c d 4 e f g 11
9 3
2 h i 7 j 8
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
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.
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.
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.
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
5
B 15
2 C
A
F 2
E D
10 20
5
B 15
2 C
A
F 2
E D
10 20
NP-Hard problem
r 10
5 a
b 8
4 c d 4 e f g 11
9 3
2 h i 7 j 8
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.
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.
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.
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.
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.
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.
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.
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).
This subproblem does not make sense since if u’s parent is included
then u does not need to cover it.