0% found this document useful (0 votes)
18 views4 pages

Travelling Salesperson Problem Using Branch and Bound

Uploaded by

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

Travelling Salesperson Problem Using Branch and Bound

Uploaded by

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

Travelling Salesperson Problem using Branch and Bound

#include <stdio.h>

#include <limits.h>

int n;

int findMinEdge(int cost[10][10], int city)

int min = INT_MAX;

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

if (cost[city][i] < min && cost[city][i] != 0)

min = cost[city][i];

return min;

int calculateBound(int cost[10][10], int visited[10])

int bound = 0;

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

if (visited[i] == 0)

bound += findMinEdge(cost, i);

return bound;

void tsp(int cost[10][10], int visited[10], int currBound, int currWeight, int level, int path[10], int
*minCost)

if (level == n)

int totalCost = currWeight + cost[path[level - 1]][path[0]];

if (totalCost < *minCost)


{

*minCost = totalCost;

return;

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

if (!visited[i] && cost[path[level - 1]][i] != 0)

int tempBound = currBound;

currWeight += cost[path[level - 1]][i];

currBound -= findMinEdge(cost, path[level - 1]);

if (currWeight + currBound < *minCost)

path[level] = i;

visited[i] = 1;

tsp(cost, visited, currBound, currWeight, level + 1, path, minCost);

currWeight -= cost[path[level - 1]][i];

currBound = tempBound;

visited[i] = 0;

int main()

int cost[10][10];

int visited[10];

visited[0] = 1;

int path[10];

path[0] = 0;

printf("Enter the number of vertices");

scanf("%d",&n);
for(int i=0;i<n;i++)

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

printf("Enter element of cost[%d][%d]:",i,j);

scanf("%d",&cost[i][j]);

int minCost = INT_MAX;

tsp(cost, visited, calculateBound(cost, visited), 0, 1, path, &minCost);

printf("Minimum cost: %d\n", minCost);

return 0;

OUTPUT:

Enter the number of vertices5

Enter element of cost[0][0]:0

Enter element of cost[0][1]:20

Enter element of cost[0][2]:30

Enter element of cost[0][3]:10

Enter element of cost[0][4]:11

Enter element of cost[1][0]:15

Enter element of cost[1][1]:0

Enter element of cost[1][2]:16

Enter element of cost[1][3]:4

Enter element of cost[1][4]:2

Enter element of cost[2][0]:3

Enter element of cost[2][1]:5

Enter element of cost[2][2]:0

Enter element of cost[2][3]:2

Enter element of cost[2][4]:4

Enter element of cost[3][0]:19

Enter element of cost[3][1]:6

Enter element of cost[3][2]:18

Enter element of cost[3][3]:0

Enter element of cost[3][4]:3


Enter element of cost[4][0]:16

Enter element of cost[4][1]:4

Enter element of cost[4][2]:7

Enter element of cost[4][3]:16

Enter element of cost[4][4]:0

Minimum cost: 28

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy