0% found this document useful (0 votes)
39 views7 pages

Ada 10-11

The document discusses the traveling salesman problem algorithm. It starts with a set of cities and distances between each pair of cities. It generates all possible permutations of the cities and calculates the total distance for each permutation. It selects the permutation with the minimum total distance as the solution.

Uploaded by

MS Gauhar
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)
39 views7 pages

Ada 10-11

The document discusses the traveling salesman problem algorithm. It starts with a set of cities and distances between each pair of cities. It generates all possible permutations of the cities and calculates the total distance for each permutation. It selects the permutation with the minimum total distance as the solution.

Uploaded by

MS Gauhar
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/ 7

FACULTYOFTECHNOLO

GY
Information

Experiment 10

49
MD SARFUZZAMAN GAUHAR 4ED1( page
FACULTYOFTECHNOLO
GY
Information

10. Write a program for Floyd-Warshall algorithm.

Algorithm:

Step 1: Initialize the solution matrix ame as the


input graph as a first step.
Step 2: Then update the solution matrix by
considering all vertices as an intermediate vertex.
Step 3: The idea is to pick all vertices one by one
and updates all shortest paths which include the
picked vertex as an intermediate vertex in the
shortest path. Step 4: When we pick vertex number k
as an intermediate vertex, we already have considered
vertices{0, 1, 2,
.. k-1} as intermediate vertices.
Step 5: For every pair(I, j) of the source and
destination vertices respectively, there are two
possible cases.
 K is not intermediate vertex in shortest path
from I to j. we keep the value of dist[i][j] as
it is.
 K is an intermediate vertex in shortest path
from I to j. we update the value of dis[i][j] as
dist[i][k] + dis[k][j], if dist[i][j]
>dist[i][k] + dist[k][j]
Pseudocode:

For k = 0 to n – 1
For i = 0 to n – 1
For j = 0 to n – 1
Distance[i, j] = min(Distance[i, j], Distance[i,
k] + Distance[k, j])
where i = source Node, j = Destination Node, k =
Intermediate Node

Code:

// C++ Program for Floyd Warshall Algorithm


#include <bits/stdc++.h>
using namespace std;

// Number of vertices in the graph


#define V 4

50
MD SARFUZZAMAN GAUHAR 4ED1( page
FACULTYOFTECHNOLO
GY
Information

/* Define Infinite as a large enough


value.This value will be used for
vertices not connected to each other */
#define INF 99999

// A function to print the solution matrix


void printSolution(int dist[][V]);

// Solves the all-pairs shortest path


// problem using Floyd Warshall algorithm
void floydWarshall(int dist[][V])
{

int i, j, k;

/* Add all vertices one by one to


the set of intermediate vertices.
---> Before start of an iteration,
we have shortest distances between all
pairs of vertices such that the
shortest distances consider only the
vertices in set {0, 1, 2, .. k-1} as
intermediate vertices.
----> After the end of an iteration,
vertex no. k is added to the set of
intermediate vertices and the set becomes {0, 1,
2, ..
k} */
for (k = 0; k < V; k++) {
// Pick all vertices as source one by one
for (i = 0; i< V; i++) {
// Pick all vertices as destination for the
// above picked source
for (j = 0; j < V; j++)
{
// If vertex k is on the shortest path from
// i to j, then update the value of
// dist[i][j]
if (dist[i][j] > (dist[i][k] +
dist[k][j])
&& (dist[k][j] != INF
&&dist[i][k] != INF))
dist[i][j] = dist[i][k] +
dist[k][j];
}
}
}

// Print the shortest distance matrix


printSolution(dist);

51
MD SARFUZZAMAN GAUHAR 4ED1( page
FACULTYOFTECHNOLO
GY
Information

/* A utility function to print solution */


void printSolution(int dist[][V])
{
cout<< "The following matrix shows the shortest
"
"distances"
" between every pair of vertices \n";
for (int i = 0; i< V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
cout<< "INF"
<< " ";
else
cout<<dist[i][j] << " ";
}
cout<<endl;
}
}

// Driver's
code int main()
{
int graph[V][V] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 } };

// Function call
floydWarshall(graph);
return 0;
}
Output
:

Time Complexity:

Best case:

Worst case:

52
MD SARFUZZAMAN GAUHAR 4ED1( page
FACULTYOFTECHNOLO
GY
Information

Average case:

Experiment 11

53
MD SARFUZZAMAN GAUHAR 4ED1( page
FACULTYOFTECHNOLO
GY
Information

11. Write a program for Traveling salesman problem.

Algorithm:

Step 1: Start with a set of cities and distances


between each pair of cities.
Step 2: Generate all possible permutations of the
cities.
Step 3: Calculate the total distance for each
permutation along with the distance to return the
starting city.
Step 4: Select the permutation with the minimum
total distance.
Step 5: End.

Code:

#include <bits/stdc++.h>
using namespace std;
//functionto find theminimum weight hamiltonian
cycle
int tsp(int graph[][4], int s, int V)
{
//store all vertex apart from source vertex
vector<int> vertex;
for(int i = 0; i< V; i++)
if (i != s)
vertex.push_back(i);
//store minimum weighted hamiltonian cycle
int min_path = INT_MAX;
do
{
//store current path weight(cost)
int current_pathweight = 0;
//compute current path weight
int k = s;
for (int i = 0; i<vertex.size(); i++)
{
current_pathweight += graph[k][vertex[i]];
k = vertex[i];
}
current_pathweight += graph[k][s];
//update minimum
min_path = min(min_path, current_pathweight);
}
while (next_permutation(vertex.begin(),

54
MD SARFUZZAMAN GAUHAR 4ED1( page
FACULTYOFTECHNOLO
GY
Information

vertex.end()));
return
min_path;
}
//Driver code
int main()
{
//matrix representation of graph
int graph[][4] = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
int s = 0; //starting city
int V = 4;//number of
cities
cout<< "Minimum weight Hamiltonian cycle: "
<<tsp(graph, s, V);
return 0;
}
Output:

Time Complexity:

Best case:

Worst case:

Average case:

55
MD SARFUZZAMAN GAUHAR 4ED1( page

You might also like