0% found this document useful (0 votes)
11 views

TSP-USING-THE-BRANCH (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

TSP-USING-THE-BRANCH (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

// TSP USING THE BRANCH & BOUND TECHNIQUE

#include <iostream>

#include <vector>

#include <algorithm>

#include <climits>

using namespace std;

// Function to reduce the matrix and return the reduction cost

int reduceMatrix(vector<vector<int>>& matrix) {

int reductionCost = 0;

// Row Reduction

for (int i = 0; i < matrix.size(); i++) {

int rowMin = INT_MAX;

for (int j = 0; j < matrix.size(); j++) {

if (matrix[i][j] < rowMin) {

rowMin = matrix[i][j];

if (rowMin != INT_MAX && rowMin != 0) {

reductionCost += rowMin;

for (int j = 0; j < matrix.size(); j++) {

if (matrix[i][j] != INT_MAX) {

matrix[i][j] -= rowMin;

}
}

// Column Reduction

for (int j = 0; j < matrix.size(); j++) {

int colMin = INT_MAX;

for (int i = 0; i < matrix.size(); i++) {

if (matrix[i][j] < colMin) {

colMin = matrix[i][j];

if (colMin != INT_MAX && colMin != 0) {

reductionCost += colMin;

for (int i = 0; i < matrix.size(); i++) {

if (matrix[i][j] != INT_MAX) {

matrix[i][j] -= colMin;

return reductionCost;

// Function to print a matrix

void printMatrix(const vector<vector<int>>& matrix) {

for (const auto& row : matrix) {


for (int val : row) {

if (val == INT_MAX) {

cout << "INF ";

} else {

cout << val << " ";

cout << endl;

// Structure to store the information of a node

struct Node {

vector<vector<int>> matrix;

int cost;

vector<int> path;

int vertex;

int level;

};

// Function to create a new node

Node* createNode(vector<vector<int>> matrix, vector<int> path, int level, int i, int j) {

Node* node = new Node;

node->path = path;

if (level != 0) {

node->path.push_back(j);

}
node->matrix = matrix;

if (level != 0) {

for (int k = 0; k < matrix.size(); k++) {

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;

// Function to solve the TSP problem

int solveTSP(const vector<vector<int>>& costMatrix) {

int n = costMatrix.size();

// Initialize the root node

vector<int> path;

path.push_back(0);

Node* root = createNode(costMatrix, path, 0, -1, 0);

root->cost = reduceMatrix(root->matrix);

// Create a priority queue to store live nodes of the search tree

vector<Node*> pq;
pq.push_back(root);

while (!pq.empty()) {

Node* minNode = *min_element(pq.begin(), pq.end(), [](Node* a, Node* b) {

return a->cost < b->cost;

});

// Remove the minimum cost node

pq.erase(remove(pq.begin(), pq.end(), minNode), pq.end());

int i = minNode->vertex;

if (minNode->level == n - 1) {

minNode->path.push_back(0);

cout << "Minimum cost path: ";

for (int city : minNode->path) {

cout << city + 1 << " "; // +1 to convert to 1-based index

cout << endl;

return minNode->cost;

for (int j = 0; j < n; j++) {

if (minNode->matrix[i][j] != INT_MAX) {

Node* child = createNode(minNode->matrix, minNode->path, minNode->level + 1, i, j);

child->cost = minNode->cost + minNode->matrix[i][j] + reduceMatrix(child->matrix);

pq.push_back(child);
cout << "Matrix at level " << minNode->level + 1 << " after including edge (" << i + 1 << ", "
<< j + 1 << "):" << endl;

printMatrix(child->matrix);

cout << "Cost: " << child->cost << endl;

return INT_MAX;

int main() {

int n;

cout << "Enter the number of cities: ";

cin >> n;

vector<vector<int>> costMatrix(n, vector<int>(n));

cout << "Enter the cost matrix (use -1 to represent INF):\n";

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

cin >> costMatrix[i][j];

if (costMatrix[i][j] == -1) {

costMatrix[i][j] = INT_MAX;

}
int minCost = solveTSP(costMatrix);

cout << "The minimum cost is " << minCost << endl;

return 0;

You might also like