feat: Add Bitmask DP TSP implementation#7505
Open
YashKumar-404 wants to merge 4 commits into
Open
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7505 +/- ##
============================================
- Coverage 80.23% 80.16% -0.07%
+ Complexity 7354 7352 -2
============================================
Files 810 811 +1
Lines 23785 23807 +22
Branches 4678 4686 +8
============================================
+ Hits 19083 19084 +1
- Misses 3942 3963 +21
Partials 760 760 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DenizAltunkapan
requested changes
Jul 2, 2026
DenizAltunkapan
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR! The TSP logic looks correct, but a few things should be fixed before this can be merged:
- Add a test class. New algorithms need a
BitmaskDPTest.javaundersrc/test/.... Right now there is no test, so the code is not covered. - Remove
BitmaskDP_README.mdfromsrc/main/java. Source folders should only contain.javafiles. Please move the explanation into the class Javadoc instead. - Return type / empty input.
tspreturnsintand usesInteger.MAX_VALUE. Forn == 1or disconnected costs the sum can overflow. Please guard against this (e.g. skip unreachable states) and document the expected input. - Naming.
distanceis fine, but please make the class name match the algorithm (e.g. keepBitmaskDPonly if it stays generic; since it only does TSP, a clearer name or a note in the Javadoc would help).
Also note the repo already has graph/TravelingSalesman.java — please mention in the description why this DP version is a useful addition.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bitmask Dynamic Programming: Traveling Salesperson Problem (TSP)
Overview
This implementation demonstrates how to use Bitmask Dynamic Programming to solve the Traveling Salesperson Problem (TSP).
The Traveling Salesperson Problem asks: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city?"
What is Bitmask DP?
When solving optimization problems that involve subsets or combinations of elements (where the order/inclusion matters), tracking the "visited" state using standard arrays or objects can be highly inefficient.
Bitmask DP optimizes this by representing a subset of elements as bits in an integer:
If the$i$ -th bit is 1, the $i$ -th element is included in the subset.
If the$i$ -th bit is 0, the $i$ -th element is excluded.
Core Bitwise Operations Used:
Add an element to the subset: mask | (1 << i)
Check if an element is in the subset: (mask & (1 << i)) != 0
Because an integer is typically 32 bits, this technique is incredibly fast and memory-efficient but is generally restricted to problem sizes where$N \le 20$ .
Algorithm Complexity
For a graph with$N$ vertices:
Time Complexity:$O(N^2 \cdot 2^N)$
There are$2^N$ possible subsets. For each subset, we iterate through $N$ currently visited nodes, and for each, we try to visit $N$ unvisited nodes. This is a massive improvement over the naive $O(N!)$ brute-force approach.
Space Complexity:$O(N \cdot 2^N)$
We use a 2D array dp[1 << N][N] to store the minimum cost for each subset and ending node.
Example
If we have 4 cities (0, 1, 2, 3), a subset containing cities 0, 2, and 3 is represented by the binary number 1101 (which is the integer 13).
The DP state dp[13][2] stores the minimum distance traveled to visit cities {0, 2, 3} where the last visited city is 2.
Transitions are made by checking unvisited cities (like city 1), adding them to the mask (1101 | 0010 = 1111), and updating the minimum cost.
How to Run & Test
If you have cloned the repository locally, you can run the JUnit tests for this specific implementation using Maven:
mvn test -Dtest=BitmaskDPTest
Resolves #7503