NeFut Logo NeFut
Admin Login

State Compression Dynamic Programming: Efficient Solution to the Traveling Salesman Problem

Published at: 2026-05-27 09:17 Last updated: 2026-06-06 13:04
#algorithm #Dynamic Programming #DP

Core Logic and Mathematical Principles

The essence of State Compression Dynamic Programming (State Compression DP) is to losslessly map a one-dimensional combinatorial set with strong discrete characteristics into a low-dimensional integer scalar through numeral system conversion (usually binary). This dimensionality reduction allows us to physically transfer states and check conflicts in $O(1)$ time using bitwise operations.

The Traveling Salesman Problem (TSP) is a textbook model for State Compression DP: given a graph and the distances between pairs of nodes, the goal is to find a circuit that visits all vertices exactly once with the minimum total weight (this section discusses its variant: finding the minimum weight path from the starting point 0, visiting all vertices exactly once, to the endpoint $N-1$).

1. Discrete Mapping of State Space

In the TSP problem, we need to know not only which city we are currently in, but also which cities have already been visited and which cities remain unvisited.

2. Bitwise Operations Control Matrix and Operator-based State Transition

Using the underlying bitwise operations of computers, operations such as intersection, union, complement, and membership of sets can be highly abstracted into the following operators:

Set Operation Bitwise Operation Mathematical Expression Physical Meaning
Membership Check (S >> i) & 1 Check if city $i$ has been visited (i.e., if it is 1)
Union S | (1 << i) Force mark city $i$ as visited
Elimination S & ~(1 << i) Remove city $i$ from the visited set
Universal Set Expression (1 << N) - 1 Obtain a full selection state containing $N$ ones

State Design and Algorithm Derivation

1. State Space Definition

Let state $f[S][i]$ represent: the current visited city set is $S$ (the corresponding decimal integer of its binary mask), and currently standing at city $i$, the minimum total distance incurred.

2. Derivation of State Transition Equation

We implement decisions by looking back to find the predecessors of the current state. When we are at city $i$ with set $S$, we must have been at a different city $j$ in the previous step.
Since we stepped from $j$ to $i$, city $i$ must have been in the unvisited state in the previous step. Therefore, the predecessor state’s city set must be $S - \{i\}$, expressed in bitwise operations as S ^ (1 << i).

$$\forall j \in S \setminus \{i\}, \quad f[S][i] = \min \left( f[S][i], f[S \setminus \{i\}][j] + \text{weight}(j, i) \right)$$

Substituting in the bitwise operation operator:

f[S][i] = min(f[S][i], f[S ^ (1 << i)][j] + weight[j][i]);

3. Control of Topological Order

Since the states of large sets are always derived from the states of small sets (sets with one fewer element), in the underlying double loop, the outer loop must strictly enumerate state numbers $S$ from small to large (from 1 to $(1<


NOIP Practical Pitfall Guide

1. Confusion in Bitwise Operation Precedence Leading to Logical Breakdown

2. Incorrect Space Dimension Allocation Leading to Memory Overflow (MLE)


Classic NOIP/Luogu Problems

1. Luogu P1433 Cheese Eating

2. Luogu P1879 [USACO06NOV] Corn Fields G

Original Source: local://17.1

[h] Back to Home