Independent Set on Bounded Treewidth
MAXIMUM WEIGHT INDEPENDENT SET is NP-hard and, parameterized by the solution size, W[1]-hard. Parameterized by treewidth it is easy: given a tree decomposition of width , a dynamic program finds the optimum in time. This lesson builds that program on the nice tree decompositions of Nice Tree Decompositions, checks it against brute force, and explains why the base 2 cannot be lowered under SETH.
The table
Every vertex has a weight . For a node of a nice tree decomposition with bag , recall that is the union of the bags in the subtree of . For every define
or if itself is not independent. The bag vertices are the "interface" of the partial solution: what lies below the bag and is already forgotten cannot interact with anything above, by the separation property, so only matters for the future. The answer is .
The four rules
In the introduce rule, has no neighbour in (it appears nowhere below), so checking inside the bag is enough. In the join rule, the two subtrees share only the bag, so their solutions combine freely once they agree on ; the weight of was counted in both. Every rule costs or per entry, there are at most entries per node and nodes, so the total time is .
Implementation
The program builds a path decomposition of a "grid with diagonals" (a sliding window of vertices in row-major order), turns it into a nice path decomposition on the fly, runs the four rules (a path has no join nodes) and compares with a brute-force optimum. It also counts table entries.
import random
def grid_with_diagonals(r, c, rng):
n = r * c
adj = {v: set() for v in range(n)}
def add(a, b):
adj[a].add(b); adj[b].add(a)
for i in range(r):
for j in range(c):
v = i * c + j
if j + 1 < c and rng.random() < 0.8: add(v, v + 1)
if i + 1 < r and rng.random() < 0.8: add(v, v + c)
if i + 1 < r and j + 1 < c and rng.random() < 0.3: add(v, v + c + 1)
return adj
def mis_path_dp(adj, w, order, window):
table = {frozenset(): 0} # leaf
bag, entries = [], 1
for v in order:
if len(bag) == window: # forget the oldest bag vertex
old = bag.pop(0)
new = {}
for S, val in table.items():
T = S - {old}
new[T] = max(new.get(T, -1), val)
table = new
entries += len(table)
new = {} # introduce v
for S, val in table.items():
new[S] = val
if not adj[v] & S:
new[S | {v}] = val + w[v]
table = new
bag.append(v)
entries += len(table)
return max(table.values()), entries # forgetting the rest = taking the max
def mis_brute(adj, w, verts):
if not verts:
return 0
v = verts[0]
skip = mis_brute(adj, w, verts[1:])
take = w[v] + mis_brute(adj, w, [u for u in verts[1:] if u not in adj[v]])
return max(skip, take)
rng = random.Random(8)
print("grid n width DP optimum brute force table entries bound 2n*2^(t+1)")
for r, c in [(4, 4), (5, 4), (6, 3), (4, 5)]:
adj = grid_with_diagonals(r, c, rng)
w = [rng.randint(1, 9) for _ in range(r * c)]
best, entries = mis_path_dp(adj, w, list(range(r * c)), c + 2)
t = c + 1
print(f"{r}x{c} {r * c:4} {t:6} {best:11} {mis_brute(adj, w, list(range(r * c))):12} {entries:14} {2 * r * c * 2 ** (t + 1):17}")grid n width DP optimum brute force table entries bound 2n*2^(t+1)
4x4 16 5 46 46 446 2048
5x4 20 5 52 52 598 2560
6x3 18 4 60 60 335 1152
4x5 20 6 55 55 1094 5120
There are introduce and forget nodes, each with at most entries. The counts stay well below that bound because only independent subsets of each bag are stored. That is typical: problem-specific pruning of states helps in practice, but the worst case still grows like .
In C++
On grids the same DP is the classical profile (or "broken line") DP: the bag is the last vertices in row-major order, and the state is a bit mask saying which of them are in the set. Introducing checks its left neighbour (bit 0) and the neighbour above (bit ); shifting the mask forgets the oldest vertex. The program solves a grid, too large for brute force, and checks a grid against brute force over all subsets.
#include <algorithm>
#include <cstdio>
#include <vector>
long solve(int r, int c, const std::vector<int>& w, long& entries) {
// state: bit b = vertex (v - 1 - b) is in the set, for the last c vertices
std::vector<long> dp(1 << c, -1), nd(1 << c);
dp[0] = 0;
for (int v = 0; v < r * c; v++) {
std::fill(nd.begin(), nd.end(), -1);
for (int m = 0; m < (1 << c); m++) {
if (dp[m] < 0) continue;
int shifted = (m << 1) & ((1 << c) - 1); // forget the oldest, make room for v
nd[shifted] = std::max(nd[shifted], dp[m]); // v not in the set
bool left = (v % c != 0) && (m & 1);
bool up = (v >= c) && (m >> (c - 1) & 1);
if (!left && !up) nd[shifted | 1] = std::max(nd[shifted | 1], dp[m] + w[v]);
}
dp.swap(nd);
for (long x : dp) entries += (x >= 0);
}
return *std::max_element(dp.begin(), dp.end());
}
int main() {
unsigned s = 12;
auto rnd = [&](int m) { s = s * 1103515245u + 12345u; return int((s >> 8) % m); };
{
int r = 4, c = 4, n = 16;
std::vector<int> w(n);
for (int& x : w) x = 1 + rnd(9);
long entries = 0, best = solve(r, c, w, entries), brute = 0;
for (int S = 0; S < (1 << n); S++) {
bool ok = true;
long sum = 0;
for (int v = 0; v < n && ok; v++) {
if (!(S >> v & 1)) continue;
if (v % c && (S >> (v - 1) & 1)) ok = false;
if (v >= c && (S >> (v - c) & 1)) ok = false;
sum += w[v];
}
if (ok) brute = std::max(brute, sum);
}
std::printf("4 x 4: DP %ld, brute force %ld, %ld table entries\n", best, brute, entries);
}
{
int r = 40, c = 12;
std::vector<int> w(r * c);
for (int& x : w) x = 1 + rnd(9);
long entries = 0, best = solve(r, c, w, entries);
std::printf("40 x 12: DP %ld, %ld table entries (2^12 = 4096 masks per step, 480 steps)\n", best, entries);
}
}4 x 4: DP 51, brute force 51, 129 table entries
40 x 12: DP 1277, 206436 table entries (2^12 = 4096 masks per step, 480 steps)
The grid has 480 vertices, so brute force would face subsets; the DP touches a few hundred thousand table entries.
Beyond independent set
The same scheme gives, for a decomposition of width :
| Problem | States per bag vertex | Time (stated) |
|---|---|---|
| independent set, vertex cover | in / out | |
| dominating set | in / dominated / not yet dominated | with fast join (van Rooij, Bodlaender and Rossmanith, 2009) |
| -coloring | one of colors | |
| Hamiltonian cycle, Steiner tree, feedback vertex set | connectivity patterns | naively, by Cut and Count (randomized) or rank-based methods |
Under SETH these bases are tight: independent set has no algorithm, dominating set no , -coloring no , even given a path decomposition of width (Lokshtanov, Marx and Saurabh, 2011; see Tight Lower Bounds from ETH and SETH).
What the DP needs as input is a decomposition. Finding an optimal one is NP-hard, but approximations and heuristics work well; that is the subject of Computing Treewidth and Elimination Orderings.
Test yourself
Why does the DP table at a bag only need to record which bag vertices are in the independent set?
- Because forgotten vertices have weight 0
- Because the bag separates the forgotten vertices below from everything above, so only the bag part of a partial solution affects future choices
- Because the bag is a clique
- Because the DP is greedy
In the join rule , why is subtracted?
- To penalize large sets
- Because lies in both subtrees and its weight is counted in both children
- Because is not independent
- It is a mistake
What is the running time of the independent set DP on a nice tree decomposition of width with nodes?
Under which hypothesis is the base 2 in for independent set optimal?
- P NP
- SETH
- The Unique Games Conjecture
- It is proven unconditionally