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

Floyd Warshall Algorithm

Floyd warshall algorithm

Uploaded by

remus94144
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)
36 views4 pages

Floyd Warshall Algorithm

Floyd warshall algorithm

Uploaded by

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

11/4/2015

Code:

http://code.geeksforgeeks.org/index.php

GeeksforgeeksIDE

1/4

11/4/2015

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

GeeksforgeeksIDE

//CProgramforFloydWarshallAlgorithm
#include<stdio.h>
//Numberofverticesinthegraph
#defineV4
/*DefineInfiniteasalargeenoughvalue.Thisvaluewillbeused
forverticesnotconnectedtoeachother*/
#defineINF99999
//Afunctiontoprintthesolutionmatrix
voidprintSolution(intdist[][V]);

//SolvestheallpairsshortestpathproblemusingFloydWarshallalgorithm
voidfloydWarshell(intgraph[][V])
{
/*dist[][]willbetheoutputmatrixthatwillfinallyhavetheshortes
distancesbetweeneverypairofvertices*/
intdist[V][V],i,j,k;
/*Initializethesolutionmatrixsameasinputgraphmatrix.Or
wecansaytheinitialvaluesofshortestdistancesarebased
onshortestpathsconsideringnointermediatevertex.*/
for(i=0;i<V;i++)
for(j=0;j<V;j++)
dist[i][j]=graph[i][j];

/*Addallverticesonebyonetothesetofintermediatevertices.
>Beforestartofaiteration,wehaveshortestdistancesbetweenall
pairsofverticessuchthattheshortestdistancesconsideronlythe
verticesinset{0,1,2,..k1}asintermediatevertices.
>Aftertheendofaiteration,vertexno.kisaddedtothesetof
intermediateverticesandthesetbecomes{0,1,2,..k}*/
for(k=0;k<V;k++)
{
//Pickallverticesassourceonebyone
for(i=0;i<V;i++)
{
//Pickallverticesasdestinationforthe
//abovepickedsource
for(j=0;j<V;j++)
{
//Ifvertexkisontheshortestpathfrom
//itoj,thenupdatethevalueofdist[i][j]
if(dist[i][k]+dist[k][j]<dist[i][j])
dist[i][j]=dist[i][k]+dist[k][j];
}
}

http://code.geeksforgeeks.org/index.php

2/4

11/4/2015

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

GeeksforgeeksIDE

}
//Printtheshortestdistancematrix
printSolution(dist);
}
/*Autilityfunctiontoprintsolution*/
voidprintSolution(intdist[][V])
{
printf("Followingmatrixshowstheshortestdistances"
"betweeneverypairofvertices\n");
for(inti=0;i<V;i++)
{
for(intj=0;j<V;j++)
{
if(dist[i][j]==INF)
printf("%7s","INF");
else
printf("%7d",dist[i][j]);
}
printf("\n");
}
}
//driverprogramtotestabovefunction
intmain()
{
/*Letuscreatethefollowingweightedgraph
10
(0)>(3)
|/|\
5||
||1
\|/|
(1)>(2)
3*/
intgraph[V][V]={{0,5,INF,10},
{INF,0,3,INF},
{INF,INF,0,1},
{INF,INF,INF,0}
};
//Printthesolution
floydWarshell(graph);
return0;
}

Input:

http://code.geeksforgeeks.org/index.php

3/4

11/4/2015

GeeksforgeeksIDE

Input:

C++

Java


Python2.7

Reset

Run

GenerateURL

GeeksforGeeks
117,016likes

LikePage

Share

23friendslikethis

1. CrackInterviewQuestions

Advertisement

2. BuildYourOwnWebsite
3. HadoopTutorial
4. WebDesignCourses
5. JavaProgrammingCourses

http://code.geeksforgeeks.org/index.php

4/4

You might also like