-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlyodWarshall.cpp
66 lines (56 loc) · 1.62 KB
/
FlyodWarshall.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 4;
const int INF = 1e9; // Define infinity as a large value
// int d[MAXN][MAXN] = {INF}; // Distance matrix
int d[MAXN][MAXN] = {{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};;
int p[MAXN][MAXN]; // Ancestor matrix
void floydWarshall(int n) {
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (d[i][k] < INF && d[k][j] < INF) {
if (d[i][j] > d[i][k] + d[k][j]) {
d[i][j] = d[i][k] + d[k][j];
p[i][j] = p[k][j];
}
}
}
}
}
}
void initializeMatrices(int n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && d[i][j] < INF)
p[i][j] = i;
else
p[i][j] = -1;
}
}
}
void printPath(int i, int j) {
if (i != j)
printPath(i, p[i][j]);
cout << j << " ";
}
int main() {
int n = 4;
// Initialize the ancestor matrix
initializeMatrices(n);
// Run the Floyd-Warshall algorithm
floydWarshall(n);
// Now you can reconstruct the path from vertex `u` to vertex `v` like this:
int u = 0, v = 2; // Example vertices
if (d[u][v] < INF) {
cout << "Shortest path from " << u << " to " << v << ": ";
printPath(u, v);
cout << endl;
} else {
cout << "No path exists from " << u << " to " << v << endl;
}
return 0;
}