Skip to content

tsp problem #694 #742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Dynamic_Programming/tsp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>

using namespace std;

struct Point {
int x, y;
};

double calculateDistance(const Point& p1, const Point& p2) {
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
}

double tspDynamicProgramming(const vector<Point>& cities) {
int numCities = cities.size();

// dp[mask][i] represents the minimum distance to visit all cities in the set represented by the bitmask 'mask', ending at city 'i'
vector<vector<double>> dp(1 << numCities, vector<double>(numCities, numeric_limits<double>::max()));

// Initialize base case: starting from the first city
dp[1][0] = 0;

// Iterate through all subsets of cities
for (int mask = 1; mask < (1 << numCities); ++mask) {
for (int i = 0; i < numCities; ++i) {
// Check if city 'i' is in the subset represented by the bitmask
if ((mask & (1 << i)) != 0) {
for (int j = 0; j < numCities; ++j) {
// Check if city 'j' is in the subset and different from 'i'
if ((mask & (1 << j)) != 0 && i != j) {
dp[mask][i] = min(dp[mask][i], dp[mask ^ (1 << i)][j] + calculateDistance(cities[j], cities[i]));
}
}
}
}
}

// Calculate the minimum distance to return to the starting city
double minDistance = numeric_limits<double>::max();
for (int i = 1; i < numCities; ++i) {
minDistance = min(minDistance, dp[(1 << numCities) - 1][i] + calculateDistance(cities[i], cities[0]));
}

return minDistance;
}

int main() {
// Example usage
vector<Point> cities = {{0, 0}, {1, 2}, {2, 4}, {3, 1}};

double minDistance = tspDynamicProgramming(cities);

cout << "Minimum distance: " << minDistance << endl;

return 0;
}