Skip to content

Commit 7d4e7ff

Browse files
Create Floyd Warshall Algorithm
Solves the all-pairs shortest path problem using Floyd Warshall algorithm
1 parent a62729a commit 7d4e7ff

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// C++ Program for Floyd Warshall Algorithm
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
// Number of vertices in the graph
6+
#define V 4
7+
8+
/* Define Infinite as a large enough
9+
value.This value will be used for
10+
vertices not connected to each other */
11+
#define INF 99999
12+
13+
// A function to print the solution matrix
14+
void printSolution(int dist[][V]);
15+
16+
// Solves the all-pairs shortest path
17+
// problem using Floyd Warshall algorithm
18+
void floydWarshall(int graph[][V])
19+
{
20+
/* dist[][] will be the output matrix
21+
that will finally have the shortest
22+
distances between every pair of vertices */
23+
int dist[V][V], i, j, k;
24+
25+
/* Initialize the solution matrix same
26+
as input graph matrix. Or we can say
27+
the initial values of shortest distances
28+
are based on shortest paths considering
29+
no intermediate vertex. */
30+
for (i = 0; i < V; i++)
31+
for (j = 0; j < V; j++)
32+
dist[i][j] = graph[i][j];
33+
34+
/* Add all vertices one by one to
35+
the set of intermediate vertices.
36+
---> Before start of an iteration,
37+
we have shortest distances between all
38+
pairs of vertices such that the
39+
shortest distances consider only the
40+
vertices in set {0, 1, 2, .. k-1} as
41+
intermediate vertices.
42+
----> After the end of an iteration,
43+
vertex no. k is added to the set of
44+
intermediate vertices and the set becomes {0, 1, 2, ..
45+
k} */
46+
for (k = 0; k < V; k++) {
47+
// Pick all vertices as source one by one
48+
for (i = 0; i < V; i++) {
49+
// Pick all vertices as destination for the
50+
// above picked source
51+
for (j = 0; j < V; j++) {
52+
// If vertex k is on the shortest path from
53+
// i to j, then update the value of
54+
// dist[i][j]
55+
if (dist[i][j] > (dist[i][k] + dist[k][j])
56+
&& (dist[k][j] != INF
57+
&& dist[i][k] != INF))
58+
dist[i][j] = dist[i][k] + dist[k][j];
59+
}
60+
}
61+
}
62+
63+
// Print the shortest distance matrix
64+
printSolution(dist);
65+
}
66+
67+
/* A utility function to print solution */
68+
void printSolution(int dist[][V])
69+
{
70+
cout << "The following matrix shows the shortest "
71+
"distances"
72+
" between every pair of vertices \n";
73+
for (int i = 0; i < V; i++) {
74+
for (int j = 0; j < V; j++) {
75+
if (dist[i][j] == INF)
76+
cout << "INF"
77+
<< " ";
78+
else
79+
cout << dist[i][j] << " ";
80+
}
81+
cout << endl;
82+
}
83+
}
84+
85+
// Driver's code
86+
int main()
87+
{
88+
/* Let us create the following weighted graph
89+
10
90+
(0)------->(3)
91+
| /|\
92+
5 | |
93+
| | 1
94+
\|/ |
95+
(1)------->(2)
96+
3 */
97+
int graph[V][V] = { { 0, 5, INF, 10 },
98+
{ INF, 0, 3, INF },
99+
{ INF, INF, 0, 1 },
100+
{ INF, INF, INF, 0 } };
101+
102+
// Function call
103+
floydWarshall(graph);
104+
return 0;
105+
}
106+
107+

0 commit comments

Comments
 (0)