Detailing Held-Karp TSP with memory considerations
The plan is to implement the classical Held-Karp DP for TSP using bitmasks in C++, storing dp as a flattened 1D vector for O(n·2ⁿ) memory (160MB for n=20). The approach entails a dp array indexed by mask and last node, iterating over subsets to update minimal path costs and finally outputting the minimal tour cost. Memory optimizations and iteration order will be noted but the full dp table will be allocated to handle the problem up to n=20 reasonably.
A standard way to solve TSP exactly in (O(n^2\cdot 2^n)) is the Held–Karp dynamic program. The idea is to represent the set of visited vertices by a bitmask and keep track of the cheapest way to reach a given end–vertex having visited exactly that set.
Define dp[mask][i] = minimal cost of a path that starts at the fixed start vertex 0, visits exactly the vertices with bits set in mask (so mask always contains bit 0), and ends at vertex i. The recurrence is
dp[1<<0][0] = 0
dp[mask][i] = min_{j in mask, j!=i} dp[mask ^ (1<<i)][j] + dist[j][i]
and the optimum tour cost is (\min_i dp[(1<<n)-1][i] + dist[i][0]).
To fit (n=20) into reasonable memory, flatten the two–dimensional array into a single std::vector<long long> of length ((1<<n)\cdot n), which is about 20 million entries when (n=20), ~160 MB at 8 bytes/entry. If your edge weights are small you can use 32‑bit ints to halve that. The array index for state (mask, i) is mask * n + i, avoiding an extra level of indirection.
Here is a complete C++17 implementation that reads an n×n distance matrix and prints the minimal tour cost:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n)) return 0;
vector<vector<long long>> dist(n, vector<long long>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
cin >> dist[i][j];
const long long INF = 4e18;
int Nmask = 1 << n;
// Flattened dp: index = mask * n + last
vector<long long> dp((size_t)Nmask * n, INF);
// Start at vertex 0 with only vertex 0 visited
dp[(1 << 0) * n + 0] = 0;
for (int mask = 1; mask < Nmask; ++mask) {
if ((mask & 1) == 0) continue; // ensure start vertex 0 is included
for (int last = 0; last < n; ++last) {
if ((mask & (1 << last)) == 0) continue; // last not in mask
long long cur = dp[(size_t)mask * n + last];
if (cur == INF) continue;
// Try to extend to a new vertex nxt not yet visited
int remain = (~mask) & (Nmask - 1);
for (int nxt = 0; nxt < n; ++nxt) {
if (remain & (1 << nxt)) {
int nmask = mask | (1 << nxt);
long long &ref = dp[(size_t)nmask * n + nxt];
long long cand = cur + dist[last][nxt];
if (cand < ref) ref = cand;
}
}
}
}
long long ans = INF;
int full = Nmask - 1;
for (int last = 1; last < n; ++last) { // end at last, return to 0
long long tour = dp[(size_t)full * n + last] + dist[last][0];
if (tour < ans) ans = tour;
}
cout << ans << "\n";
return 0;
}
This runs in (O(n^2 2^n)) time. The flattened dp array uses (n\cdot 2^n) storage but avoids extra vectors per mask. For (n=20), the array length is (20\cdot 2^{20}\approx 21) million; with 8‑byte long long entries that's about 160 MiB. If you know your edge weights fit in 32 bits you can change long long to int to save memory.