Skip to content

Commit 6955b23

Browse files
authored
Create Travelling_Salesman_Problem.cpp
1 parent ce58a90 commit 6955b23

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

C++/Travelling_Salesman_Problem.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define int long long
5+
#define F(a,b,i) for (int i = a; i < b; i++)
6+
#define all(v) v.begin(), v.end()
7+
8+
typedef long double ld;
9+
typedef vector<int> vi;
10+
11+
//------------------------------------------------------
12+
13+
/*
14+
Travelling Salesman Problem...
15+
16+
A weighted Graph is Given, you have to traverse through each vertex exactly once and return to the
17+
starting vertex such that the cost of travelling is least. (Also Known as Hamiltonian Cycle..)
18+
19+
Brute Force -->
20+
Find the all premutation of the Paths and choose min cost path by iteratin them all
21+
T.C --> O(n!)
22+
23+
D.P --> ( Top - Down )
24+
Take 1 node as root node.
25+
26+
Traverse through the node and build rec tree(it will have 2^n vertices)...
27+
28+
From bottom of tree calculate the min cost req to reach par node...
29+
30+
int mask = bit-masking [1->0001(represent A is vis), 3->(0011)...] is used to keep track of visited nodes while building rec tree..
31+
32+
I have taken the max_constraint for 2d matrix but-->
33+
34+
DP size = [2^n][n] [ row->keep track of visitness || col ->keep track of nodes ]
35+
36+
T.C --> O(n^2 * 2^n) ..
37+
38+
*/
39+
40+
41+
/*
42+
43+
**********************************************************************************
44+
| Better Aprroach is to use map , instead of 2D matrix for memoization.. |
45+
| as u can see the output --> most of the dp entries in unutilized... |
46+
**********************************************************************************
47+
48+
*/
49+
50+
51+
int dist[10001][10001] ; // Adjacency Matrix of Graph..
52+
53+
int dp[10001][10001] ;
54+
55+
int VISITED_ALL, n;
56+
57+
int TSP(int mask, int pos){
58+
if(mask==VISITED_ALL){
59+
return dist[pos][0]; // agar sabhi node visit kar liya to uss node se direct root node(i.e 1) tak ka cost add kr k return...
60+
}
61+
62+
if(dp[mask][pos]!=-1){
63+
return dp[mask][pos];
64+
}
65+
66+
int ans = INT_MAX;
67+
68+
F(0,n,city){
69+
if((mask&(1<<city))==0){ //[chcking no city is visited more than once, as 1&1=1 always]
70+
int newAns = dist[pos][city] + TSP( mask|(1<<city), city); // or operation is used to mark the visted city ..
71+
ans = min(ans,newAns);
72+
}
73+
74+
}
75+
76+
return dp[mask][pos] = ans;
77+
78+
}
79+
80+
81+
int32_t main()
82+
{
83+
cin>>n;
84+
85+
memset(dp, -1, sizeof(dp));
86+
87+
F(0,n,i){
88+
F(0,n,j){
89+
cin>>dist[i][j];
90+
}
91+
}
92+
93+
VISITED_ALL = (1<<n) - 1;
94+
95+
cout<<TSP(1,0)<<" "<<dp[1][0]<<"\n";
96+
97+
F(0,16,i){
98+
F(0,4,j){
99+
cout<<dp[i][j]<<" ";
100+
}
101+
cout<<"\n";
102+
}
103+
104+
}
105+
106+
/*
107+
gfg e.g->
108+
4
109+
0 10 15 20
110+
10 0 25 25
111+
15 25 0 30
112+
20 25 30 0
113+
4
114+
0 20 42 25
115+
20 0 30 34
116+
42 30 0 10
117+
25 34 10 0
118+
119+
*/

0 commit comments

Comments
 (0)