Skip to content

Conversation

@Erennn7
Copy link
Contributor

@Erennn7 Erennn7 commented Oct 18, 2025

PR: Traveling Salesman Problem (TSP) using Bitmask Dynamic Programming in R

This PR introduces a fully documented implementation of the Traveling Salesman Problem (TSP) in R using Bitmask Dynamic Programming (DP).
The algorithm efficiently finds the shortest possible route that visits each city exactly once and returns to the starting city, making it suitable for small-to-medium numbers of cities.

Overview

The tsp_bitmask_dp function applies Bitmask DP to track visited cities using binary masks.
Instead of enumerating all permutations (which is factorial in time complexity), the algorithm leverages memoization and bitwise operations to reduce redundant computations, achieving O(n² * 2^n) performance.

Key features include:

  • Recursive DP with memoization for minimum travel cost
  • Optional path reconstruction using a separate function (tsp_bitmask_with_path)
  • Handles arbitrary distance matrices
  • Demonstrates the optimal TSP route and total cost

This approach is ideal for applications such as:

  • Route optimization and logistics
  • Network and circuit design
  • Scheduling and planning problems
  • Microchip and genome sequencing tasks

Complexity

  • Time Complexity: O(n² * 2^n), where n = number of cities
  • Space Complexity: O(n * 2^n) for memoization table

Notes

  • Works efficiently for small n (typically n ≤ 20)
  • For larger instances, consider heuristic or approximation algorithms like Nearest Neighbor, 2-opt, Christofides algorithm, or metaheuristics (Genetic Algorithms, Simulated Annealing)

@Erennn7 Erennn7 requested a review from siriak as a code owner October 18, 2025 09:31
Copilot AI review requested due to automatic review settings October 18, 2025 09:31
@Erennn7 Erennn7 requested a review from acylam as a code owner October 18, 2025 09:31
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new implementation of the Traveling Salesman Problem (TSP) using Bitmask Dynamic Programming in R. The algorithm efficiently finds the shortest route visiting all cities exactly once and returning to the starting city, suitable for small-to-medium problem sizes (n ≤ 20).

Key Changes:

  • Implements TSP using bitmask DP with O(n² × 2^n) time complexity
  • Provides both cost-only and path reconstruction variants
  • Includes three example use cases demonstrating the algorithm

@Erennn7
Copy link
Contributor Author

Erennn7 commented Oct 20, 2025

@siriak please have a look

@siriak siriak requested a review from Copilot October 24, 2025 16:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.

@siriak siriak merged commit 35936ea into TheAlgorithms:master Oct 24, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants