pgm3 & 4
pgm3 & 4
Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's
algorithm.
#include <iostream>
#iclude <conio.h>
int main(void)
{
int iN, i, j, k;
int iaFloyd[10][10], iaCost[10][10];
cout << "\n*********************************************************";
cout << "\n*\tPROGRAM TO IMPLEMENT FLOYD'S ALGORITHM\t*\n";
cout << "*********************************************************";
cout << "\nEnter the number of vertices\n";
cin >> iN;
cout << "\nEnter the Cost adjacency Matrix\n";
for(i=0;i<iN;i++)
for(j=0;j<iN;j++)
cin >> iaCost[i][j];
cout << "\nInput Graph\n";
for(i=0;i<iN;i++)
{
for(j=0;j<iN;j++)
{
cout << iaCost[i][j] << "\t";
}
cout << endl;
}
cout << endl;
for(i=0;i<iN;i++)
{
for(j=0;j<iN;j++)
{
iaFloyd[i][j] = iaCost[i][j];
}
}
for(k=0;k<iN;k++)
{
for(i=0;i<iN;i++)
{
for(j=0;j<iN;j++)
{
if(iaFloyd[i][j] > (iaFloyd[i][k] + iaFloyd[k][j]))
iaFloyd[i][j] = (iaFloyd[i][k] +
iaFloyd[k][j]);
}
}
}
cout << "\nAll Pair Shortest Path Matrix\n";
for(i=0;i<iN;i++)
{
for(j=0;j<iN;j++)
{
cout << iaFloyd[i][j] << "\t";
}
cout << endl;
}
cout << "\n";
return 0;
}
Input Graph
Output
*********************************************************
* PROGRAM TO IMPLEMENT FLOYD'S ALGORITHM *
*********************************************************
Enter the number of vertices
5
Input Graph
0 3 9999 7 9
3 0 4 2 9999
9999 4 0 5 6
7 2 5 0 4
9 9999 6 4 0
#include<iostream>
#include<conio.h>
int main()
{
int num_nodes, k, n;
char i, j, res, c;
int adj[10][10], path[10][10];
c = 65 + i;
cout << "\t\t\t" << c << " ";
for (int j = 0; j < num_nodes; j++)
cout << adj[i][j] << " ";
cout << "\n";
}
return 0;
}
Output:
Maximum number of nodes in the graph :5
Adjacency Matrix:
ABCDE
A 10010
B 00000
C 01110
D 01010
E 01111
ABCDE
A 11010
B 00000
C 01110
D 01010
E 01111
4. Design and implement C/C++ Program to find shortest paths from a given vertex in a weighted
connected graph to other vertices using Dijkstra's algorithm.
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXNODES = 10,INF = 9999;
void fnDijkstra(int [][MAXNODES], int [], int [], int[], int, int, int);
/*****************************************************************************
*
*Function : main
*Input parameters: no parameters
*RETURNS : 0 on success
******************************************************************************
/
int main(void)
{
int
n,cost[MAXNODES][MAXNODES],dist[MAXNODES],visited[MAXNODES],path[MAXNODES],i,j
,source,dest;
cout << "\nEnter the number of nodes\n";
cin >> n;
cout << "Enter the Cost Matrix\n" << endl;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
cin >> cost[i][j];
cout << "Enter the Source vertex" << endl;
cin >> source;
cout << "\n//For Source Vertex : " << source << " shortest path to
other vertices//"<< endl;
for (dest=0; dest < n; dest++)
{
fnDijkstra(cost,dist,path,visited,source,dest,n);
if (dist[dest] == INF)
cout << dest << " not reachable" << endl;
else
{
cout << endl;
i = dest;
do
{
cout << i << "<--";
i = path[i];
}while (i!= source);
cout << i << " = " << dist[dest] << endl;
}
}
return 0;
}
/*****************************************************************************
*
*Function : fnDijkstra
*Description : Function to find shortest paths to other vertices
* using Dijkstra's algorithm.
*Input parameters:
* int c[][] - cost adjacency matrix of the graph
* int d[] - distance vector
* int p[] - path vector
* int s[] - vector to store visited information
* int so - source vertex
* int de - destination vertex
* int n - no of vertices in the graph
*RETURNS : no value
******************************************************************************
/
void fnDijkstra(int c[MAXNODES][MAXNODES], int d[MAXNODES], int p[MAXNODES],
int s[MAXNODES], int so, int de, int n)
{
int i,j,a,b,min;
for (i=0;i<n;i++)
{
s[i] = 0;
d[i] = c[so][i];
p[i] = so;
}
s[so] = 1;
for (i=1;i<n;i++)
{
min = INF;
a = -1;
for (j=0;j<n;j++)
{
if (s[j] == 0)
{
if (d[j] < min)
{
min = d[j];
a = j;
}
}
}
if (a == -1) return;
s[a] = 1;
if (a == de) return;
for (b=0;b<n;b++)
{
if (s[b] == 0)
{
if (d[a] + c[a][b] < d[b])
{
d[b] = d[a] + c[a][b];
p[b] = a;
}
}
}
}
}
Input graph
Output
Enter the number of nodes
6
Enter the Cost Matrix
0<--0 = 0
1<--0 = 4
2<--0 = 4
3<--2<--0 = 7
4<--2<--0 = 5
5<--4<--2<--0 = 8