Algorithms And Data Structures Codexery

Held–Karp algorithm

Dynamic programming algorithm for exact TSP solution.

The Held–Karp algorithm, also referred to as the Bellman–Held–Karp algorithm, is a dynamic programming method for solving the traveling salesman problem (TSP). It was independently proposed in 1962 by Richard Bellman and by Michael Held and Richard Karp. The algorithm takes as input a distance matrix between a set of cities and returns the exact minimum-length tour that visits each city exactly once and returns to the start. It also solves related problems, such as the Hamiltonian cycle problem, but does so in exponential time.

The algorithm works by numbering the cities and arbitrarily designating one as the starting city, since the TSP solution is a cycle. It first computes, for every subset of cities and every city not in that subset, the shortest one-way path from the start through all cities in the subset to that final city. These values are calculated starting with the smallest subsets and building up. For subsets of two or fewer cities, the calculation is straightforward, involving only one or two possible paths. For larger subsets, the algorithm exploits a key insight: if the shortest path through a set ends at a particular city, then removing that final edge yields the shortest path through the remaining set to the preceding city. This reduces the number of paths to examine to only one per possible second-to-last city, making the computation feasible despite the combinatorial explosion.

After computing all such distances, the algorithm enters a second stage. It adds the direct edge from each possible final city back to the start, producing candidate cycles, and selects the shortest among them. The actual shortest path can be reconstructed by storing, alongside each distance, the label of the second-to-last city, which increases space requirements only by a constant factor.

The algorithm’s time complexity is exponential, specifically of order \(O(n^2 2^n)\), which is a significant improvement over the superexponential \(O(n!)\) of brute-force enumeration. However, it requires \(O(n 2^n)\) space to store all computed values, whereas brute force needs only \(O(n^2)\) space for the graph. For undirected graphs, a constant-factor improvement is possible by stopping early and performing a bidirectional search, but this does not change the asymptotic complexity. If only the tour length is needed, space can be reduced by keeping only values for subsets o

field
Computer science, operations research
known_for
Held–Karp algorithm for the traveling salesman problem

Lore & Background

The algorithm numbers cities 1,2,…,n, with 1 designated arbitrarily as a starting city. It calculates, for each set of cities S ⊆ {2,…,n} and every city e ≠ 1 not contained in S, the shortest one-way path from 1 to e that passes through every city in S in some order. This distance is denoted g(S,e), with d(u,v) representing the length of the direct edge from u to v. Values of g(S,e) are computed starting with the smallest sets S and finishing with the largest.

Reader's Guide

The Held–Karp algorithm is significant as an exact solution method for the traveling salesman problem, a classic NP-hard problem. Its dynamic programming approach systematically computes optimal subpaths, reducing the number of paths that must be examined. For sets S with two or fewer elements, calculation requires looking at one or two possible shortest paths. For larger sets, only a few paths need examination because the optimal substructure property holds: if the shortest path from 1 through S to e has a particular second-to-last city, then removing the final edge gives the shortest path from 1 to that city through the remaining set. This algorithm also applies to the Hamiltonian cycle problem. Its legacy lies in demonstrating that exact exponential-time solutions are feasible for small instances, and it remains a foundational technique in combinatorial optimization.

Did You Know?

Problem Definition and Scope

The subgraph isomorphism problem asks whether a given graph G contains a subgraph that is structurally identical to another graph H. This is a foundational question in theoretical computer science, and it subsumes several well-known tasks: finding the largest clique in a graph and determining whether a graph possesses a Hamiltonian cycle are both special cases. Because of this generalization property, the problem inherits NP-completeness from those underlying questions. The terminology can be slightly confusing—some researchers call it "subgraph matching" when the emphasis is on actually identifying and returning the matching subgraph rather than merely answering a yes-or-no decision query. Despite its intractability in full generality, certain restricted instances can be resolved in polynomial time, making the problem a rich area where computational hardness and tractability coexist in interesting tension.

NP-Completeness and Reductions

To establish NP-completeness, the problem must be cast as a decision question: given graphs G and H, does there exist a subgraph of G isomorphic to H? The formal statement involves finding a subset of vertices and edges in G that, under some bijection, preserve exactly the adjacency structure of H. The standard proof reduces from the clique problem—simply set H to be the complete graph on k vertices, and the subgraph isomorphism question becomes equivalent to asking whether G contains a k-clique. An alternative reduction uses the Hamiltonian cycle problem, where H is a cycle with the same vertex count as G; since Hamiltonicity remains NP-complete even for planar graphs, this demonstrates that subgraph isomorphism is hard in the planar setting as well. Notably, subgraph isomorphism generalizes the graph isomorphism problem, whose exact placement in the complexity hierarchy is still unresolved.

Algorithmic Landscape

The algorithmic history of subgraph isomorphism spans nearly five decades of refinement. Ullmann's 1976 paper introduced a recursive backtracking method that, while exponential in the worst case, runs in polynomial time when H is held fixed. In 2004, Cordella proposed VF2, which refined Ullmann's approach with different heuristics and notably reduced memory consumption. It employs a constraint programming framework combined with bit-parallel data structures and specialized propagation techniques to achieve strong performance on hard cases.

Theoretical Boundaries and Open Questions

Beyond the standard NP-completeness result, subgraph isomorphism sits at the intersection of several deep theoretical questions. It is a strict generalization of graph isomorphism, yet the latter's placement within the complexity hierarchy remains one of the most prominent open problems in computer science. This query-complexity result provides a meaningful barrier even for randomized or adaptive strategies. Meanwhile, structural restrictions on the input—such as requiring G to be planar or more generally of bounded expansion—can collapse the running time to linear when H is fixed, illustrating how geometric constraints can tame otherwise intractable search.

Frequently Asked Questions

What problem does the Held–Karp algorithm actually solve?

It finds the shortest Hamiltonian cycle that visits every node exactly once, giving a guaranteed optimal answer to the traveling salesman problem. The same framework can be adapted to other related problems, such as the general Hamiltonian cycle problem.

How fast is the Held–Karp algorithm in practice?

It runs in exponential time, roughly O(n²·2ⁿ), so it is only tractable for relatively small instance sizes. Even so, it remains the standard method whenever a provably optimal TSP tour is required rather than a heuristic approximation.

Why does the Held–Karp algorithm still matter?

It was among the first rigorous exact methods for TSP and cemented dynamic programming as a core tool in combinatorial optimization. More than six decades later it continues to serve as the benchmark against which approximation and heuristic approaches are measured.

More in Algorithms And Data Structures 1-24

Spotted an error? Know more?

This is a living reference — every entry is fact-audited, and reader corrections feed straight into our audit queue. Suggest an edit · See this site's audit record

Comments

Loading…
Open in the interactive codex →