TExcelTAD
Home › Parameterized & Fine-Grained Complexity › Independent Set on Bounded Treewidth

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 tt, a dynamic program finds the optimum in O(2t⋅tO(1)⋅n)O(2^{t} \cdot t^{O(1)} \cdot n) 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 vv has a weight w(v)≥0w(v) \ge 0. For a node ii of a nice tree decomposition with bag XiX_i, recall that ViV_i is the union of the bags in the subtree of ii. For every S⊆XiS \subseteq X_i define

c[i,S]=max⁡{w(I):I independent in G[Vi], I∩Xi=S},c[i, S] = \max \{ w(I) : I \text{ independent in } G[V_i], \ I \cap X_i = S \},

or −∞-\infty if SS 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 SS matters for the future. The answer is c[root,∅]c[\text{root}, \emptyset].

The four rules

leaf:c[i,∅]=0introduce v (child j):c[i,S]=c[j,S] if v∉S;c[i,S]=c[j,S∖{v}]+w(v) if v∈S and N(v)∩S=∅forget v (child j):c[i,S]=max⁡(c[j,S], c[j,S∪{v}])join (children j1,j2):c[i,S]=c[j1,S]+c[j2,S]−w(S)\begin{aligned} &\text{leaf:} && c[i, \emptyset] = 0 \\ &\text{introduce } v \text{ (child } j\text{):} && c[i, S] = c[j, S] \ \text{if } v \notin S; \quad c[i, S] = c[j, S \setminus \{v\}] + w(v) \ \text{if } v \in S \text{ and } N(v) \cap S = \emptyset \\ &\text{forget } v \text{ (child } j\text{):} && c[i, S] = \max\left( c[j, S], \ c[j, S \cup \{v\}] \right) \\ &\text{join (children } j_1, j_2\text{):} && c[i, S] = c[j_1, S] + c[j_2, S] - w(S) \end{aligned}

In the introduce rule, vv has no neighbour in Vj∖XjV_j \setminus X_j (it appears nowhere below), so checking N(v)∩SN(v) \cap S 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 SS; the weight of SS was counted in both. Every rule costs O(1)O(1) or O(t)O(t) per entry, there are at most 2t+12^{t+1} entries per node and O(tn)O(tn) nodes, so the total time is O(2t⋅t2⋅n)O(2^t \cdot t^2 \cdot n).

Implementation

The program builds a path decomposition of a "grid with diagonals" (a sliding window of c+2c + 2 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.

python · try it yourself
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 2n2n introduce and forget nodes, each with at most 2t+12^{t+1} 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 2t2^t.

In C++

On grids the same DP is the classical profile (or "broken line") DP: the bag is the last cc vertices in row-major order, and the state is a bit mask saying which of them are in the set. Introducing vv checks its left neighbour (bit 0) and the neighbour above (bit c−1c - 1); shifting the mask forgets the oldest vertex. The program solves a 40×1240 \times 12 grid, too large for brute force, and checks a 4×44 \times 4 grid against brute force over all 2162^{16} subsets.

cpp · try it yourself
#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 40×1240 \times 12 grid has 480 vertices, so brute force would face 24802^{480} subsets; the DP touches a few hundred thousand table entries.

Beyond independent set

The same scheme gives, for a decomposition of width tt:

Problem States per bag vertex Time (stated)
independent set, vertex cover in / out O∗(2t)O^*(2^t)
dominating set in / dominated / not yet dominated O∗(3t)O^*(3^t) with fast join (van Rooij, Bodlaender and Rossmanith, 2009)
qq-coloring one of qq colors O∗(qt)O^*(q^t)
Hamiltonian cycle, Steiner tree, feedback vertex set connectivity patterns O∗(tO(t))O^*(t^{O(t)}) naively, 2O(t)2^{O(t)} by Cut and Count (randomized) or rank-based methods

Under SETH these bases are tight: independent set has no O∗((2−ε)t)O^*((2 - \varepsilon)^{t}) algorithm, dominating set no O∗((3−ε)t)O^*((3 - \varepsilon)^t), qq-coloring no O∗((q−ε)t)O^*((q - \varepsilon)^t), even given a path decomposition of width tt (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?

  1. Because forgotten vertices have weight 0
  2. Because the bag separates the forgotten vertices below from everything above, so only the bag part of a partial solution affects future choices
  3. Because the bag is a clique
  4. Because the DP is greedy

In the join rule c[i,S]=c[j1,S]+c[j2,S]−w(S)c[i, S] = c[j_1, S] + c[j_2, S] - w(S), why is w(S)w(S) subtracted?

  1. To penalize large sets
  2. Because SS lies in both subtrees and its weight is counted in both children
  3. Because SS is not independent
  4. It is a mistake

What is the running time of the independent set DP on a nice tree decomposition of width tt with O(tn)O(tn) nodes?

  1. O(nt)O(n^t)
  2. O(2t⋅tO(1)⋅n)O(2^t \cdot t^{O(1)} \cdot n)
  3. O(tn)O(t^n)
  4. O(2n)O(2^n)

Under which hypothesis is the base 2 in O∗(2t)O^*(2^t) for independent set optimal?

  1. P ≠\ne NP
  2. SETH
  3. The Unique Games Conjecture
  4. It is proven unconditionally