You are on page 1of 4

DEPARTMENT OF COMPUTER ENGINEERING

Semester

S.E. Semester IV Computer Engineering

Subject

Analysis of Algorithms

Lectures
charge

Professor

In-

Practicals
Charge

Professor

In-

Laboratory number
Student Name

Prof. Sanjeev Dwivedi


Prof. Ashwin Ganesan
L07D

Sakharam S. Gawade

Roll Number

14102C2015

Grade

Subject Teachers
Signature

Experiment
Number

07

Experiment
Title

All Pairs Shortest Path

Resources
Apparatus
Used

Computer, Code::Blocks(IDE)

Objectives
Dynamic approach, Spanning Tree, Minimum spanning
(Skill
Set/ tree
Knowledge
tested/imparte
d)

Description of the experiment:


The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path
problem. The problem is to find shortest distances between every pair of
vertices in a given edge weighted directed Graph.

We initialize the solution matrix same as the input graph matrix as a first
step. Then we update the solution matrix by considering all vertices as an
intermediate vertex. The idea is to one by one pick all vertices and update all
shortest paths which include the picked vertex as an intermediate vertex in
the shortest path. When we pick vertex number k as an intermediate vertex,
we already have considered vertices {0, 1, 2, .. k-1} as intermediate
vertices. For every pair (i, j) of source and destination vertices respectively,
there are two possible cases.
1) k is not an intermediate vertex in shortest path from i to j. We keep the
value of dist[i][j] as it is.
2) k is an intermediate vertex in shortest path from i to j. We update the
value of dist[i][j] as dist[i][k] + dist[k][j].
Algorithm:
let dist be a |V| |V| array of minimum distances initialized to (infinity)
for each vertex v
dist[v][v] 0
for each edge (u,v)
dist[u][v] w(u,v) // the weight of the edge (u,v)
for k from 1 to |V|
for i from 1 to |V|
for j from 1 to |V|
if dist[i][j] > dist[i][k] + dist[k][j]
dist[i][j] dist[i][k] + dist[k][j]
end if

Program:
//All Pair Shortest Path
#include<stdio.h>
#define inf 100000
int main()
{
int a[100][100];
int v,e,i,j,k;
printf("Enter the number of edges and vertices: ");
scanf("%d%d",&e,&v);
for(i=1;i<=v;i++)
{
for(j=i;j<=v;j++)
{
if(i==j)
{
a[i][i]=0;
}
else
{

a[i][j]=a[j][i]=inf;
}
}
}
for(k=1;k<=e;k++)
{
printf("Enter the vertices between edge is formed: ");
scanf("%d%d",&i,&j);
printf("Enter the weight of vertex %d and %d: ",i,j);
scanf("%d",&a[i][j]);
if(i==j)
{
a[i][i]=0;
}
}
for(k=1;k<=v;k++)
{
for(i=1;i<=v;i++)
{
for(j=1;j<=v;j++)
{
if((a[k][j]+a[i][k])<a[i][j])//Checks
whether
new
path
is //shorter
{
a[i][j]=a[k][j]+a[i][k];
}
}
}
}
printf(" In the matrix below, c[i][j]-> shortest path from ith vertex to
jth vertex\n");
for(i=1;i<=v;i++)
{
for(j=1;j<=v;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}

Output run:

Conclusions:
The all-pairs shortest path problem is the determination of the shortest
graph distances between every pair of vertices in a given graph. It has a
time complexity of O(V3) where, V no of vertices.

You might also like