Appearance
Boundary State
Tree DP
A local choice fixes the boundary, then child subtrees become independent DP pieces.
postorderMISFPT
01Rootorient tree
02Takeblock child
03Skipmerge child
04Bagtreewidth k
DP Problem on Trees
Maximize Independent Set on Trees 最大独立集
Input: an undirected tree
Output: an independent set with maximum cardinality, namely the largest possible number of selected vertices.
Independent Set: a set
最大独立集在一般图上是 NP-hard 的;但是当图是一棵树时,结构足够简单,可以用 DP 或者等价的 greedy idea 在线性时间解决。
Recursive View
Root the tree at an arbitrary vertex
对于某个节点
- Choose
. - Do not choose
.
如果选择
如果不选择
Subproblem Definition
先定义一个单状态 DP:
Then:
含义:
- 第一项:不选
,所以每个 child 的 subtree 都可以取最优。 - 第二项:选
,拿到 ,但是不能选 children,只能从 grandchildren 的 subtree 继续取最优。
The topological order is bottom-up, usually postorder traversal.
A Cleaner Two-State DP
实际写代码时,更常用 two-state DP:
Answer:
这里:
: max independent set size in subtree when is selected. : max independent set size in subtree when is not selected.
This avoids explicitly iterating over grandchildren, but it is the same idea.
Complexity
每个 vertex 只会被处理一次,每条 parent-child 关系也只参与常数次转移。
Greedy View on Trees
这个 tree DP 也可以从 greedy 的角度理解。
Greedy idea:
- Choose all current leaves.
- Remove all leaves' parents.
- Repeat on the remaining forest.
为什么可以这样做?
If a leaf
The process is equivalent to solving the DP bottom-up:
- leaves are selected,
- their parents become blocked,
- then the remaining graph decomposes into smaller trees.
Implementation can be
text
queue = all leaves
while queue is not empty:
take all current leaves into answer
mark their parents as removed
update degrees of the remaining neighbors
newly created leaves enter queueThis greedy formulation is useful for intuition, while the DP formulation is safer to generalize.
Independent Set on General Graphs
一般图不像树,因为 cycle 和 clique 会让“父子子树独立”这个结构消失。
但是有些图看起来 almost like a tree。如果我们把一个小的 dense part, such as a triangle, 看成一个 super node,图可能重新变成 tree-like。
For a super node with
- there are at most
ways to choose vertices inside it; - each way creates different restrictions for adjacent super nodes;
- we can design a DP state like
.
If the largest super node has size
This motivates treewidth.
Treewidth Idea
Treewidth measures how close a graph is to a tree.
Informally:
- 把图拆成一棵 tree of bags;
- each bag contains several original vertices;
- smaller bags mean the graph is more tree-like.
If the largest bag has size
Examples:
- Tree: treewidth
. - Cycle: treewidth
. - Clique on
vertices: treewidth . - Series-parallel graphs: treewidth at most
.
Many optimization problems become:
when parameterized by treewidth
This is called Fixed-Parameter Tractable, or FPT:
Here
Tree Decomposition
A tree decomposition is a tree whose nodes are bags of vertices.
Let
Requirements:
- Every graph vertex appears in at least one bag.
- For every graph edge
, there is a bag containing both and . - For every graph vertex
, all bags containing form a connected subtree.
The treewidth of this decomposition is:
The treewidth of the graph is the minimum value over all possible decompositions.
Separation Property
Root the tree decomposition.
For a decomposition node
Intuition:
- vertices inside one child subtree can interact with the parent side only through
; - two different child subtrees are independent once
is fixed.
This is exactly why DP works: once we decide which vertices in the boundary bag are selected, each child subtree can be solved independently.
Notation:
: bag at decomposition node . : subtree of the decomposition rooted at . : union of all bags inside .
DP on Tree Decomposition for MIS
Goal: solve Maximum Independent Set on a graph with treewidth
Root the tree decomposition.
Define:
as the maximum size of an independent set
where:
If
Base Case
If
if
Transition
Suppose
The parent state fixes
that is compatible with
Then the best contribution from child
The subtraction avoids double counting the vertices in the overlap between parent and child bags.
Therefore:
The final answer at root
Complexity
If treewidth is
Number of states per bag:
For each parent state, checking compatible child states can cost another factor
Total running time, given a tree decomposition:
The important takeaway:
Tree DP works not only on trees, but also on graphs that can be decomposed into small bags arranged as a tree.
Network Flow
在多项式时间内精确求解问题的算法,不断迭代求解
问题描述
以轨道运输为例,对一个有向图

问题的formalize
Given a directed graph
a Flow is a function
where
For
Capacity Constraint
For each edge
Flow Conservation
For each vertex
Total Flow
The value of a flow is the total flow leaving the source:
Equivalently, for any feasible flow,
So the Maximum Flow Problem is:
subject to the capacity constraint and flow conservation.
Ford-Fulkerson Algorithm
一个自然的 greedy idea 是:
不断找一条从
到 的路径,然后沿着这条路径尽可能多地 push flow。
但是这个做法如果只在原图
所以我们需要允许算法 cancel 一部分已经送出去的 flow,也就是在残量网络里加入 backward edge。
Residual Network
Given a feasible flow
For each original edge
- If
, add a forward residual edge with residual capacity
- If
, add a backward residual edge with residual capacity
Intuition:
- forward edge means we can send more flow on the original edge;
- backward edge means we can cancel part of the flow already sent on the original edge.
Augmenting Path
An augmenting path is an
The amount of flow we can push through
Then for every residual edge on
- if it is a forward edge
, update
- if it is a backward edge
corresponding to original edge , update
Algorithm
text
FordFulkerson(G=(V,E), s, t, c):
initialize f(e) = 0 for every e in E
construct the residual network G_f
while there exists an s-t path P in G_f:
b = min residual capacity of edges on P
for each residual edge e on P:
if e is a forward edge (u,v):
f(u,v) = f(u,v) + b
if e is a backward edge (v,u) for original edge (u,v):
f(u,v) = f(u,v) - b
update G_f
return fThe algorithm stops when there is no
Small Bug: Anti-parallel Edges
If the original graph contains both
- a real forward edge;
- a backward edge used to cancel flow.
One standard fix is to transform the graph so that no anti-parallel edges exist, or explicitly tag each residual edge as forward/backward.
Termination
If all capacities are integers, then each augmentation increases
With a DFS/BFS search for each augmenting path, one iteration costs
For rational capacities, we can rescale them into integers. For irrational capacities, Ford-Fulkerson may not halt depending on how augmenting paths are chosen.