TSP-USING-THE-BRANCH (1)
TSP-USING-THE-BRANCH (1)
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
int reductionCost = 0;
// Row Reduction
rowMin = matrix[i][j];
reductionCost += rowMin;
if (matrix[i][j] != INT_MAX) {
matrix[i][j] -= rowMin;
}
}
// Column Reduction
colMin = matrix[i][j];
reductionCost += colMin;
if (matrix[i][j] != INT_MAX) {
matrix[i][j] -= colMin;
return reductionCost;
if (val == INT_MAX) {
} else {
struct Node {
vector<vector<int>> matrix;
int cost;
vector<int> path;
int vertex;
int level;
};
node->path = path;
if (level != 0) {
node->path.push_back(j);
}
node->matrix = matrix;
if (level != 0) {
node->matrix[i][k] = INT_MAX;
node->matrix[k][j] = INT_MAX;
node->matrix[j][0] = INT_MAX;
node->level = level;
node->vertex = j;
node->cost = 0;
return node;
int n = costMatrix.size();
vector<int> path;
path.push_back(0);
root->cost = reduceMatrix(root->matrix);
vector<Node*> pq;
pq.push_back(root);
while (!pq.empty()) {
});
int i = minNode->vertex;
if (minNode->level == n - 1) {
minNode->path.push_back(0);
return minNode->cost;
if (minNode->matrix[i][j] != INT_MAX) {
pq.push_back(child);
cout << "Matrix at level " << minNode->level + 1 << " after including edge (" << i + 1 << ", "
<< j + 1 << "):" << endl;
printMatrix(child->matrix);
return INT_MAX;
int main() {
int n;
cin >> n;
if (costMatrix[i][j] == -1) {
costMatrix[i][j] = INT_MAX;
}
int minCost = solveTSP(costMatrix);
cout << "The minimum cost is " << minCost << endl;
return 0;