0% found this document useful (0 votes)
5 views4 pages

Floyd's algorithm

The document describes the implementation of Floyd's algorithm for solving the All-Pairs Shortest Paths problem using C/C++. It includes the algorithm's explanation, complexity analysis, and a sample program that reads a cost adjacency matrix and computes the shortest paths. The program outputs the resulting distance matrix after applying the algorithm.

Uploaded by

anushat1910
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)
5 views4 pages

Floyd's algorithm

The document describes the implementation of Floyd's algorithm for solving the All-Pairs Shortest Paths problem using C/C++. It includes the algorithm's explanation, complexity analysis, and a sample program that reads a cost adjacency matrix and computes the shortest paths. The program outputs the resulting distance matrix after applying the algorithm.

Uploaded by

anushat1910
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/ 4

3.

a Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's
algorithm.

Floyd’s Algorithm:

Floyd’s algorithm is applicable to both directed and undirected graphs provided that they do not
contain a cycle. It is convenient to record the lengths of shortest path in an n- by- n matrix D called
the distance matrix. The element dij in the ith row and jth column of matrix indicates the shortest path
from the ith vertex to jth vertex (1<=i, j<=n). The element in the ith row and jth column of the current
matrix D(k-1) is replaced by the sum of elements in the same row i and kth column and in the same
column j and the kth column if and only if the latter sum is smaller than its current value.

Algorithm Floyd(W[1..n,1..n])
//Implements Floyd’s algorithm for the all-pairs shortest paths problem
//Input: The weight matrix W of a graph
//Output: The distance matrix of shortest paths length
{
D←W
for k←1 to n do
{
for i ← 1 to n do
{
for j ← 1 to n do
{
D[i,j] ← min (D[i, j], D[i, k]+D[k, j] )
}
}
}
return D
}

Complexity: The time efficiency of Floyd’s algorithm is cubic i.e. Θ (n3)


Program:

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

void floyd(int[10][10],int);
int min(int,int);

void main()
{
int n,a[10][10],i,j;
printf("Enter the no.of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
floyd(a,n);
getch();
}

void floyd(int a[10][10],int n)


{
int d[10][10],i,j,k;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
d[i][j]=a[i][j];
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
printf("\nThe distance matrix is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
}

int min (int a,int b)


{
if(a<b)
return a;
else
return b;
}
OUTPUT:

You might also like