1
+ // C++ program to solve Traveling Salesman Problem
2
+ // using Branch and Bound.
3
+ #include < bits/stdc++.h>
4
+ using namespace std ;
5
+ const int N = 4 ;
6
+
7
+ // final_path[] stores the final solution ie, the
8
+ // path of the salesman.
9
+ int final_path[N+1 ];
10
+
11
+ // visited[] keeps track of the already visited nodes
12
+ // in a particular path
13
+ bool visited[N];
14
+
15
+ // Stores the final minimum weight of shortest tour.
16
+ int final_res = INT_MAX;
17
+
18
+ // Function to copy temporary solution to
19
+ // the final solution
20
+ void copyToFinal (int curr_path[])
21
+ {
22
+ for (int i=0 ; i<N; i++)
23
+ final_path[i] = curr_path[i];
24
+ final_path[N] = curr_path[0 ];
25
+ }
26
+
27
+ // Function to find the minimum edge cost
28
+ // having an end at the vertex i
29
+ int firstMin (int adj[N][N], int i)
30
+ {
31
+ int min = INT_MAX;
32
+ for (int k=0 ; k<N; k++)
33
+ if (adj[i][k]<min && i != k)
34
+ min = adj[i][k];
35
+ return min;
36
+ }
37
+
38
+ // function to find the second minimum edge cost
39
+ // having an end at the vertex i
40
+ int secondMin (int adj[N][N], int i)
41
+ {
42
+ int first = INT_MAX, second = INT_MAX;
43
+ for (int j=0 ; j<N; j++)
44
+ {
45
+ if (i == j)
46
+ continue ;
47
+
48
+ if (adj[i][j] <= first)
49
+ {
50
+ second = first;
51
+ first = adj[i][j];
52
+ }
53
+ else if (adj[i][j] <= second &&
54
+ adj[i][j] != first)
55
+ second = adj[i][j];
56
+ }
57
+ return second;
58
+ }
59
+
60
+ // function that takes as arguments:
61
+ // curr_bound -> lower bound of the root node
62
+ // curr_weight-> stores the weight of the path so far
63
+ // level-> current level while moving in the search
64
+ // space tree
65
+ // curr_path[] -> where the solution is being stored which
66
+ // would later be copied to final_path[]
67
+ void TSPRec (int adj[N][N], int curr_bound, int curr_weight,
68
+ int level, int curr_path[])
69
+ {
70
+ // base case is when we have reached level N which
71
+ // means we have covered all the nodes once
72
+ if (level==N)
73
+ {
74
+ // check if there is an edge from last vertex in
75
+ // path back to the first vertex
76
+ if (adj[curr_path[level-1 ]][curr_path[0 ]] != 0 )
77
+ {
78
+ // curr_res has the total weight of the
79
+ // solution we got
80
+ int curr_res = curr_weight +
81
+ adj[curr_path[level-1 ]][curr_path[0 ]];
82
+
83
+ // Update final result and final path if
84
+ // current result is better.
85
+ if (curr_res < final_res)
86
+ {
87
+ copyToFinal (curr_path);
88
+ final_res = curr_res;
89
+ }
90
+ }
91
+ return ;
92
+ }
93
+
94
+ // for any other level iterate for all vertices to
95
+ // build the search space tree recursively
96
+ for (int i=0 ; i<N; i++)
97
+ {
98
+ // Consider next vertex if it is not same (diagonal
99
+ // entry in adjacency matrix and not visited
100
+ // already)
101
+ if (adj[curr_path[level-1 ]][i] != 0 &&
102
+ visited[i] == false )
103
+ {
104
+ int temp = curr_bound;
105
+ curr_weight += adj[curr_path[level-1 ]][i];
106
+
107
+ // different computation of curr_bound for
108
+ // level 2 from the other levels
109
+ if (level==1 )
110
+ curr_bound -= ((firstMin (adj, curr_path[level-1 ]) +
111
+ firstMin (adj, i))/2 );
112
+ else
113
+ curr_bound -= ((secondMin (adj, curr_path[level-1 ]) +
114
+ firstMin (adj, i))/2 );
115
+
116
+ // curr_bound + curr_weight is the actual lower bound
117
+ // for the node that we have arrived on
118
+ // If current lower bound < final_res, we need to explore
119
+ // the node further
120
+ if (curr_bound + curr_weight < final_res)
121
+ {
122
+ curr_path[level] = i;
123
+ visited[i] = true ;
124
+
125
+ // call TSPRec for the next level
126
+ TSPRec (adj, curr_bound, curr_weight, level+1 ,
127
+ curr_path);
128
+ }
129
+
130
+ // Else we have to prune the node by resetting
131
+ // all changes to curr_weight and curr_bound
132
+ curr_weight -= adj[curr_path[level-1 ]][i];
133
+ curr_bound = temp;
134
+
135
+ // Also reset the visited array
136
+ memset (visited, false , sizeof (visited));
137
+ for (int j=0 ; j<=level-1 ; j++)
138
+ visited[curr_path[j]] = true ;
139
+ }
140
+ }
141
+ }
142
+
143
+ // This function sets up final_path[]
144
+ void TSP (int adj[N][N])
145
+ {
146
+ int curr_path[N+1 ];
147
+
148
+ // Calculate initial lower bound for the root node
149
+ // using the formula 1/2 * (sum of first min +
150
+ // second min) for all edges.
151
+ // Also initialize the curr_path and visited array
152
+ int curr_bound = 0 ;
153
+ memset (curr_path, -1 , sizeof (curr_path));
154
+ memset (visited, 0 , sizeof (curr_path));
155
+
156
+ // Compute initial bound
157
+ for (int i=0 ; i<N; i++)
158
+ curr_bound += (firstMin (adj, i) +
159
+ secondMin (adj, i));
160
+
161
+ // Rounding off the lower bound to an integer
162
+ curr_bound = (curr_bound&1 )? curr_bound/2 + 1 :
163
+ curr_bound/2 ;
164
+
165
+ // We start at vertex 1 so the first vertex
166
+ // in curr_path[] is 0
167
+ visited[0 ] = true ;
168
+ curr_path[0 ] = 0 ;
169
+
170
+ // Call to TSPRec for curr_weight equal to
171
+ // 0 and level 1
172
+ TSPRec (adj, curr_bound, 0 , 1 , curr_path);
173
+ }
174
+
175
+ // Driver code
176
+ int main ()
177
+ {
178
+ // Adjacency matrix for the given graph
179
+ int adj[N][N] = { {0 , 10 , 15 , 20 },
180
+ {10 , 0 , 35 , 25 },
181
+ {15 , 35 , 0 , 30 },
182
+ {20 , 25 , 30 , 0 }
183
+ };
184
+
185
+ TSP (adj);
186
+
187
+ printf (" Minimum cost : %d\n " , final_res);
188
+ printf (" Path Taken : " );
189
+ for (int i=0 ; i<=N; i++)
190
+ printf (" %d " , final_path[i]);
191
+
192
+ return 0 ;
193
+ }
0 commit comments