0% found this document useful (0 votes)
3 views8 pages

pgm3 & 4

The document contains C/C++ programs that implement algorithms for solving various graph problems, including Floyd's algorithm for All-Pairs Shortest Paths, Warshall's algorithm for transitive closure, and Dijkstra's algorithm for finding shortest paths from a given vertex. Each program prompts the user for input, constructs the necessary matrices, and outputs the results. The provided code examples illustrate the implementation details and expected outputs for each algorithm.

Uploaded by

deadshotlev
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)
3 views8 pages

pgm3 & 4

The document contains C/C++ programs that implement algorithms for solving various graph problems, including Floyd's algorithm for All-Pairs Shortest Paths, Warshall's algorithm for transitive closure, and Dijkstra's algorithm for finding shortest paths from a given vertex. Each program prompts the user for input, constructs the necessary matrices, and outputs the results. The provided code examples illustrate the implementation details and expected outputs for each algorithm.

Uploaded by

deadshotlev
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/ 8

3. a.

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

For this program lets consider the following graph

Output

*********************************************************
* PROGRAM TO IMPLEMENT FLOYD'S ALGORITHM *
*********************************************************
Enter the number of vertices
5

Enter the Cost adjacency Matrix


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

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

All Pair Shortest Path Matrix


0 3 7 5 9
3 0 4 2 6
7 4 0 5 6
5 2 5 0 4
9 6 6 4 0
b. Design and implement C/C++ Program to find the transitive closure using Warshal's algorithm.

#include<iostream>
#include<conio.h>

using namespace std;


const int num_nodes = 10;

int main()
{
int num_nodes, k, n;
char i, j, res, c;
int adj[10][10], path[10][10];

cout << "\n\tMaximum number of nodes in the graph :";


cin >> n;
num_nodes = n;
cout << "\n\n\tNODES ARE LABELED AS A,B,C,......\n\n";
cout << "\nEnter 'y'for 'YES' and 'n' for 'NO' !!\n";

for (i = 65; i < 65 + num_nodes; i++)


for (j = 65; j < 65 + num_nodes; j++)
{
cout << "\n\tIs there an EDGE from " << i << " to " << j << " ? ";
cin >> res;
if (res == 'y')
adj[i - 65][j - 65] = 1;
else
adj[i - 65][j - 65] = 0;
}
cout << "\nAdjacency Matrix:\n";

cout << "\n\t\t\t ";


for (i = 0; i < num_nodes; i++)
{
c = 65 + i;
cout << c << " ";
}
cout << "\n\n";
for (int i = 0; i < num_nodes; i++)
{
c = 65 + i;
cout << "\t\t\t" << c << " ";
for (int j = 0; j < num_nodes; j++)
cout << adj[i][j] << " ";
cout << "\n";
}
for (int i = 0; i < num_nodes; i++)
for (int j = 0; j < num_nodes; j++)
path[i][j] = adj[i][j];
for (int k = 0; k < num_nodes; k++)
for (int i = 0; i < num_nodes; i++)
if (path[i][k] == 1)
for (int j = 0; j < num_nodes; j++)
path[i][j] = path[i][j] || path[k][j];

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


for (int j = 0; j < num_nodes; j++)
adj[i][j] = path[i][j];

cout << "\nTransitive Closure of the Graph:\n";

cout << "\n\t\t\t ";


for (i = 0; i < num_nodes; i++)
{
c = 65 + i;
cout << c << " ";
}
cout << "\n\n";
for (int i = 0; i < num_nodes; i++)
{

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

NODES ARE LABELED AS A,B,C,......

Enter 'y'for 'YES' and 'n' for 'NO' !!

Is there an EDGE from A to A ? y


Is there an EDGE from A to B ?
Is there an EDGE from A to C ?
Is there an EDGE from A to D ? y
Is there an EDGE from A to E ?
Is there an EDGE from B to A ?
Is there an EDGE from B to B ? n
Is there an EDGE from B to C ? n
Is there an EDGE from B to D ? n
Is there an EDGE from B to E ? n
Is there an EDGE from C to A ? n
Is there an EDGE from C to B ? y
Is there an EDGE from C to C ? y
Is there an EDGE from C to D ? y
Is there an EDGE from C to E ? n
Is there an EDGE from D to A ? n
Is there an EDGE from D to B ? y
Is there an EDGE from D to C ? n
Is there an EDGE from D to D ? y
Is there an EDGE from D to E ? n
Is there an EDGE from E to A ? n
Is there an EDGE from E to B ? y
Is there an EDGE from E to C ? y
Is there an EDGE from E to D ? y
Is there an EDGE from E to E ? y

Adjacency Matrix:

ABCDE

A 10010
B 00000
C 01110
D 01010
E 01111

Transitive Closure of the Graph:

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 4 4 9999 9999 9999


4 0 2 9999 9999 9999
4 2 0 3 1 6
9999 9999 3 0 9999 2
9999 9999 1 9999 0 3
9999 9999 6 2 3 0
Enter the Source vertex
0

//For Source Vertex : 0 shortest path to other vertices//

0<--0 = 0

1<--0 = 4

2<--0 = 4

3<--2<--0 = 7

4<--2<--0 = 5

5<--4<--2<--0 = 8

You might also like