Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit 761dfd0

Browse files
committed
FloydWarshall.cpp
1 parent cb70276 commit 761dfd0

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

FloydWarshall.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//
2+
// algorithm - some algorithms in "Introduction to Algorithms", third edition
3+
// Copyright (C) 2018 lxylxy123456
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as
7+
// published by the Free Software Foundation, either version 3 of the
8+
// License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
19+
#ifndef MAIN
20+
#define MAIN
21+
#define MAIN_FloydWarshall
22+
#endif
23+
24+
#ifndef FUNC_FloydWarshall
25+
#define FUNC_FloydWarshall
26+
27+
#include "utils.h"
28+
29+
#include "Graph.cpp"
30+
31+
void PrintAllPairsShortestPath(Matrix<T_ptr<size_t>>& PI, size_t i, size_t j,
32+
std::vector<size_t>& ans) {
33+
if (i == j)
34+
ans.push_back(i);
35+
else if (!PI[i][j].nil) {
36+
PrintAllPairsShortestPath(PI, i, PI[i][j].val, ans);
37+
ans.push_back(j);
38+
}
39+
}
40+
41+
template <typename T>
42+
std::pair<Matrix<T>, Matrix<T_ptr<size_t>>> FloydWarshall(Matrix<T>& W) {
43+
const size_t n = W.rows;
44+
Matrix<T> D = W;
45+
Matrix<T_ptr<size_t>> PI(n, n, T_ptr<size_t>());
46+
for (size_t i = 0; i < n; i++)
47+
for (size_t j = 0; j < n; j++)
48+
if (i != j && !W[i][j].inf)
49+
PI[i][j] = i;
50+
for (size_t k = 0; k < n; k++) {
51+
Matrix<T> D_new = D;
52+
Matrix<T_ptr<size_t>> PI_new = PI;
53+
for (size_t i = 0; i < n; i++)
54+
for (size_t j = 0; j < n; j++) {
55+
T n = D[i][k] + D[k][j];
56+
if (n < D_new[i][j]) {
57+
D_new[i][j] = n;
58+
PI_new[i][j] = PI[k][j];
59+
}
60+
}
61+
D = D_new;
62+
PI = PI_new;
63+
}
64+
return std::pair<Matrix<T>, Matrix<T_ptr<size_t>>>(D, PI);
65+
}
66+
#endif
67+
68+
#ifdef MAIN_FloydWarshall
69+
int main(int argc, char *argv[]) {
70+
const size_t v = get_argv(argc, argv, 1, 5);
71+
const size_t e = get_argv(argc, argv, 2, 10);
72+
const bool dir = 1;
73+
const int weight_lower = get_argv<int>(argc, argv, 3, 0);
74+
const int weight_upper = get_argv<int>(argc, argv, 4, e);
75+
WeightedAdjMatrix<size_t, int> G(dir);
76+
G.random_graph(v, e, weight_lower, weight_upper);
77+
G.graphviz();
78+
std::cout << std::endl;
79+
Matrix<Weight<int>> W(v, v);
80+
G.to_matrix(W);
81+
auto ans = FloydWarshall(W);
82+
Matrix<Weight<int>> D = ans.first;
83+
Matrix<T_ptr<size_t>> PI = ans.second;
84+
std::cout << D << std::endl;
85+
std::cout << PI << std::endl;
86+
for (size_t i = 0; i < v; i++)
87+
for (size_t j = 0; j < v; j++)
88+
if (i != j) {
89+
std::cout << i << " ~> " << j << ": ";
90+
std::vector<size_t> ans;
91+
PrintAllPairsShortestPath(PI, i, j, ans);
92+
if (ans.size())
93+
output_integers(ans);
94+
else
95+
std::cout << "no path from " << i << " to " << j
96+
<< " exists" << std::endl;
97+
}
98+
return 0;
99+
}
100+
#endif
101+

Graph.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ class Weight {
120120
int inf;
121121
};
122122

123+
template <typename T>
124+
class T_ptr {
125+
public:
126+
T_ptr(): nil(true) {}
127+
T_ptr(T v): val(v), nil(false) {}
128+
friend std::ostream& operator<<(std::ostream& os, const T_ptr<T>& rhs) {
129+
if (rhs.nil)
130+
return os << "NIL";
131+
else
132+
return os << rhs.val;
133+
}
134+
T val;
135+
bool nil;
136+
};
137+
123138
template <typename T>
124139
class EdgeIteratorAL1 {
125140
public:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,11 @@
157157
| 24 | BellmanFord.cpp | Bellman Ford | 651 |
158158
| 24 | DagShortestPaths.cpp | Dag Shortest Paths | 655 |
159159
| 24 | Dijkstra.cpp | Dijkstra | 658 |
160+
| 25 | FloydWarshall.cpp | Print All Pairs Shortest Path | 685 |
160161
| 25 | AllPairsShortestPaths.cpp | Extend Shortest Paths | 688 |
161162
| 25 | AllPairsShortestPaths.cpp | Slow All Pairs Shortest Paths | 689 |
162163
| 25 | AllPairsShortestPaths.cpp | Faster All Pairs Shortest Paths | 691 |
164+
| 25 | FloydWarshall.cpp | Floyd Warshall | 695 |
163165

164166
# Supplementary Files
165167
* `utils.h`: Utils

0 commit comments

Comments
 (0)