Ada 10-11
Ada 10-11
GY
Information
Experiment 10
49
MD SARFUZZAMAN GAUHAR 4ED1( page
FACULTYOFTECHNOLO
GY
Information
Algorithm:
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:
50
MD SARFUZZAMAN GAUHAR 4ED1( page
FACULTYOFTECHNOLO
GY
Information
int i, j, k;
51
MD SARFUZZAMAN GAUHAR 4ED1( page
FACULTYOFTECHNOLO
GY
Information
// 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
Algorithm:
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