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

EXPERIMENT NO.1

Uploaded by

Karthik Nadar
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 views5 pages

EXPERIMENT NO.1

Uploaded by

Karthik Nadar
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/ 5

EXPERIMENT No.

AIM: Program to implement All Pairs shortest path algorithm in C/Java/Python.

Theory:

The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to find all
pair shortest path problem from a given weighted graph. As a result of this algorithm, it will
generate a matrix, which will represent the minimum distance from any node to all other nodes in
the graph.

At first the output matrix is same as given cost matrix of the graph. After that the output matrix
will be updated with all vertices k as the intermediate vertex.

ALGO:

1. For i = 1 To n

For j = 1 To n

A[i ,j]=cost[i,j]

2. For k = 1 To n

For i = 1 To n

For j = 1 To n

A[i, j] ← min(A[i, j],A[i, k] + A[k, j])

3. return A

ANALYSIS:

1) The time complexity of above algorithm is O (n^3).


Code:

#include <stdio.h>

#include <conio.h>

int min (int,int);

void main ()

int v,i,j,k,adj[10][10];

clrscr ();

printf("\nEnter number of vertices");

scanf("%d",&v);

for(i=1;i<=v;i++)

for(j=1;j<=v;j++)

if (i==j)

adj[i][j]=0;

else{

printf("\n If edge exists between %d and %d, enter weight",i,j);

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

printf("\n The adjacency matrix is:");

for(i=1;i<=v;i++)

{
printf("\n");

for(j=1;j<=v;j++)

printf("\t%d",adj[i][j]);

for(k=1;k<=v;k++)

for(i=1;i<=v;i++)

for(j=1;j<=v;j++)

adj[i][j]=min(adj[i][j],adj[i][k]+adj[k][j]);

printf("\n The new adjacency matrix is:");

for(i=1;i<=v;i++)

printf("\n");

for(j=1;j<=v;j++)

printf("\t%d",adj[i][j]);

}
printf("\n The new adjacency matrix is:");

for(i=1;i<=v;i++)

printf("\n");

for(j=1;j<=v;j++)

printf("\t%d",adj[i][j]);

getch ();

int min(int a,int b)

if(a<b)

return a;

else

return b;

/*Use below Input and execute your code and attach output screenshot below*/

/*Enter number of vertices 3

The adjacency matrix is:

0 1 8

9 0 5

1 7 0

*/
Output:-

Enter number of vertices: 3


If edge exists between 1 and 2, enter weight: 1
If edge exists between 1 and 3, enter weight: 8
If edge exists between 2 and 1, enter weight: 9
If edge exists between 2 and 3, enter weight: 5
If edge exists between 3 and 1, enter weight: 1
If edge exists between 3 and 2, enter weight: 7
The adjacency matrix is:
0 1 8
9 0 5
1 7 0
The new adjacency matrix is:
0 1 8
9 0 5
1 2 0
The new adjacency matrix is:
0 1 6
6 0 5
1 2 0
The final adjacency matrix is:
0 1 6
6 0 5
1 2 0

You might also like