|
| 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 | + |
0 commit comments