This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floydWarshall.cpp
71 lines (66 loc) · 1.6 KB
/
floydWarshall.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
66
67
68
69
70
71
#include <limits>
#include "floydWarshall.hpp"
#include "view.hpp"
/*
* Floyd Warshall's algorithm implementation of the shortest path problem
*/
FloydWarshall::FloydWarshall(Graph &g) : g(g){
matrix = g.getMatrix();
next.resize(g.V());
for(int i = 0; i < g.V(); ++i) {
next[i].resize(g.V());
for(int j = 0; j < g.V(); ++j) {
next[i][j] = -1;
if(matrix[i][j] == 0 && i != j) {
matrix[i][j] = numeric_limits<int>::max()/2;
}
}
}
compute();
}
void FloydWarshall::compute() {
for(int k = 0; k < g.V(); ++k) {
for(int i = 0; i < g.V(); ++i) {
for(int j = 0; j < g.V(); ++j) {
if(matrix[i][k] + matrix[k][j] < matrix[i][j]) {
matrix[i][j] = matrix[i][k] + matrix[k][j];
next[i][j] = k;
}
}
}
}
}
vector<int>* FloydWarshall::recursivePath(int i, int j) {
vector<int>* path = nullptr;
if(matrix[i][j] != -1) {
int intermediate = next[i][j];
if(intermediate == -1) {
path = new vector<int>();
}
else {
path = new vector<int>();
vector<int>* prev = recursivePath(i, intermediate);
vector<int>* forward = recursivePath(intermediate, j);
path->insert(path->begin(), prev->begin(), prev->end());
path->push_back(intermediate);
path->insert(path->end(), forward->begin(), forward->end());
delete prev;
delete forward;
}
}
return path;
}
vector<int> FloydWarshall::getPath(int i, int j) {
vector<int> finalPath;
vector<int>* p = recursivePath(i,j);
if(p == nullptr) {
cout << "IMPOSSIBLE";
}
else {
finalPath.push_back(i);
finalPath.insert(finalPath.end(), p->begin(), p->end());
finalPath.push_back(j);
delete p;
}
return finalPath;
}