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

Floyd-Warshall Algorithm Implementation

Uploaded by

Mr X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views8 pages

Floyd-Warshall Algorithm Implementation

Uploaded by

Mr X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PART A

(PART A : TO BE REFFERED BY STUDENTS)

Experiment No. 06
A.1 Aim:
Write a program to implement all pair shortest path using Dynamic Programming
Approach.

A.2 Prerequisite: -

A.3 Outcome:

After successful completion of this experiment students will be able to solve a problem
by applying dynamic programming approach.

A.4 Theory:

The Floyd–Warshall algorithm is an algorithm for finding shortest paths in a weighted


graph with positive or negative edge weights (but with no negative cycles). A single
execution of the algorithm will find the lengths (summed weights) of the shortest paths
between all pairs of vertices, though it does not return details of the paths themselves.
Versions of the algorithm can also be used for finding the transitive closure of a relation
, or (in connection with the Schulze voting system) widest paths between all pairs of
vertices in a weighted graph.

The Floyd–Warshall algorithm is an example of dynamic programming, The algorithm is


also known as Floyd's algorithm, the Roy–Warshall algorithm, the Roy–Floyd algorithm,
or the WFI algorithm.

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

Example:

The algorithm is executed on the graph on the below:

Time Complexity:

First double for loop = O(n^2)

Nested 3 for loop = O(n^3)

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll No.: A05 Name: PRASHANTH NAIDU


Class: SE – A (COMPS) Batch: A1
Date of Experiment: Date of Submission : 25/03/24
18/03/24
Grade:

B.1 Software Code written by student:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define V 4
#define INF 99

int arr[4][4]={{0,3,INF,7},{8,0,2,INF},{5,INF,0,1},{2,INF,INF,0}};

int main()
{

int dist[V][V];
int i,j,k;
for (i = 0;i<V;i++)
{
for(j=0;j<V;j++)
{
dist[i][j] = arr[i][j];
}
}
for (k=0;k<V;k++)
{
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
{
if(dist[i][j] > (dist[i][k] + dist[k][j]))
{
dist[i][j] = dist [i][k] + dist[k][j];
}
}
}
}
printf("The Initial Array: \n");
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
{
printf("The %d -> %d = %d \n",i+1,j+1,arr[i][j]);
}
printf("\n --\n");

}
getch();
printf("The Final Sorted Array:\n");
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
{

printf("The %d -> %d = %d\n ",i+1,j+1,dist[i][j]);


}
printf("\n---\n");
}

getch();
return 0;

}
B.2 Input and Output:
B.4 Conclusion:

The provided C program implements the Floyd-Warshall algorithm for finding


all pairs shortest paths using dynamic programming. It initializes a graph with
weights, iteratively updates the shortest paths, and prints the resulting
shortest distances between every pair of vertices. This algorithm efficiently
computes shortest paths in graphs with both positive and negative edge
weights.

B.5 Question of Curiosity

Q1: What are different algorithms available to find


shortest path?
Q2: Derive time complexity of Floyd’s
algorithm?

Q3: Compare Floyd’s algorithm with other algorithms?


Q4: Explain application areas of all pair shortest path
algorithm.

************************

You might also like