2Brute2Best
🔥0

Travelling Salesman (bitmask DP)

Hard·GeeksforGeeks Pro OnlyInterview-Level
GeeksforGeeks
01

Problem Statement

You have n cities numbered 0 to n − 1. cost[i][j] is the price of travelling directly from city i to city j. The two directions can have different prices.

Start at city 0. Visit every other city exactly once, then come back to city 0. Return the smallest total cost of such a tour.

With only one city there is no trip to make, so the answer is 0.

Input

A square matrix cost of size n × n.

Output

One integer: the least total cost of a tour that starts at city 0, visits each city once, and returns to city 0. The function is int tsp(vector<vector<int>>& cost).

Example 1
Input: cost = [[0, 111], [112, 0]]
Output: 223

There are only two cities. The tour 0 → 1 → 0 costs 111 + 112 = 223.

Example 2
Input: cost = [[0, 1000, 5000], [5000, 0, 1000], [1000, 5000, 0]]
Output: 3000

The tour 0 → 1 → 2 → 0 costs 1000 + 1000 + 1000 = 3000. The other way round, 0 → 2 → 1 → 0, costs 15000.

Constraints
  • • 1 ≤ n ≤ 15
  • • 0 ≤ cost[i][j] ≤ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“visit every city exactly once”
→ The visited set is the state, so one bit per city is enough.
“the least total cost”
→ Each state keeps the minimum cost, so the DP combines moves with min.
“n ≤ 15”
→ 2^15 · 15 states is small enough to store every mask.
03

Approach Complexity Ladder

Brute Force — try every order of the other citiesO(N!)O(N)

Try all (n − 1)! orders of cities 1 … n−1 and add the trip back to 0

Optimal — Held–Karp DP over (visited set, last city)O(2^N · N^2)O(2^N · N)

dp[mask][last] = cheapest walk from 0 that visits mask and ends at last; extend by one city

Brute Force Intuition

A tour is city 0, then some order of the other cities, then back to 0. Write every order with next_permutation, add up the cost of each tour, and keep the smallest. There are (n − 1)! orders, so this only works for small n.

Recognition rule for this sub-pattern:

Represent a set of chosen items as bits of an integer, then iterate or DP over masks.
Remember this · Bit Manipulation
Trigger
"Every element appears twice except…", power of two, counting set bits, subsets as bitmasks, O(1) space tricks.
Think
What happens to this problem if I look at each bit separately?
Key question
Which bit operation (XOR, AND, OR, shift) cancels or isolates what I need?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.