算法导论 ch25 每对顶点间的最短路径
2010-05-23 21:37
429 查看
1. add method for class AdjMatrixGraph
2. test suite
3. run result
4. 推广:如果将E中每条边的权值赋为1,然后运行Floyd-Warshall算法,可以得到有向图的传递闭包。
4.1 add method transitiveClosure
4.2 test suite
4.3 test result
/* * All-Pairs Shortest Paths */ void AdjMatrixGraph::floydWarshall() { unsigned int *d = new unsigned int[v * v]; unsigned int *p = new unsigned int[v * v]; // d0 = A for (int i = 0; i < v; i++) { for (int j = 0; j < v; j++) { d[i * v + j] = A[i * v + j]; p[i * v + j] = v + 1; } } int n = v; for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { unsigned int distance = d[i * v + k] + d[k * v + j]; if (distance < d[i * v + j]) { d[i * v + j] = distance; p[i * v + j] = k; } } } } cout << "All-Pairs Shortest Paths distance matrix is "<< endl; for (int i = 0; i < v; i++) { for (int j = 0; j < v; j++) { cout << d[i * v + j]<< " "; } cout << endl; } cout << endl; cout << "All-Pairs Shortest Paths parent matrix is "<< endl; for (int i = 0; i < v; i++) { for (int j = 0; j < v; j++) { cout << p[i * v + j]<< " "; } cout << endl; } }
2. test suite
#include "../ch22/Edge.h" #include "../ch22/Graph.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { Edge e[] = { { 1, 2, 10 }, { 1, 4, 5 }, { 2, 3, 1 }, { 2, 4, 2 }, { 3, 5, 4 }, { 4, 2, 3 }, { 4, 3, 9 }, { 4, 5, 2 }, { 5, 1, 7 }, { 5, 3, 6 } }; Edges* edges = Edges::getInstance(); for (unsigned int i = 0; i < sizeof(e)/sizeof(Edge); i++) { edges->insert(&(e[i])); } int numberOfVertexes = 5; AdjMatrixGraph *g1 = new AdjMatrixGraph(numberOfVertexes, edges, true, true); MatrixGraphBuilder* mgb = MatrixGraphBuilder::getInstance(); g1->build(mgb); g1->display(); g1->floydWarshall(); }
3. run result
0 10 2147483647 5 2147483647 2147483647 0 1 2 2147483647 2147483647 2147483647 0 2147483647 4 2147483647 3 9 0 2 7 2147483647 6 2147483647 0 All-Pairs Shortest Paths distance matrix is 0 8 9 5 7 11 0 1 2 4 11 19 0 16 4 9 3 4 0 2 7 15 6 12 0 All-Pairs Shortest Paths parent matrix is 6 3 3 6 3 4 6 6 6 3 4 4 6 4 6 4 6 1 6 6 6 3 6 0 6
4. 推广:如果将E中每条边的权值赋为1,然后运行Floyd-Warshall算法,可以得到有向图的传递闭包。
4.1 add method transitiveClosure
/* * Transitive Closure */ void AdjMatrixGraph::transitiveClosure() { int *d = new int[v * v]; // init d0 for (int i = 0; i < v; i++) { for (int j = 0; j < v; j++) { if (i == j || ((A[i * v + j] > 0) && (A[i * v + j] < std::numeric_limits<int>::max()))) { d[i * v + j] = 1; } else { d[i * v + j] = 0; } } } int n = v; for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i * v + j] |= d[i * v + k] & d[k * v + j]; } } } cout << "Transitive Closure Matrix is "<< endl; for (int i = 0; i < v; i++) { for (int j = 0; j < v; j++) { cout << d[i * v + j]<< " "; } cout << endl; } }
4.2 test suite
#include "../ch22/Edge.h" #include "../ch22/Graph.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { Edge e[] = { { 1, 2, 10 }, { 1, 4, 5 }, { 2, 3, 1 }, { 2, 4, 2 }, { 3, 5, 4 }, { 4, 2, 3 }, { 4, 3, 9 }, { 4, 5, 2 }, { 5, 1, 7 }, { 5, 3, 6 } }; Edges* edges = Edges::getInstance(); for (unsigned int i = 0; i < sizeof(e)/sizeof(Edge); i++) { edges->insert(&(e[i])); } int numberOfVertexes = 5; AdjMatrixGraph *g1 = new AdjMatrixGraph(numberOfVertexes, edges, true, true); MatrixGraphBuilder* mgb = MatrixGraphBuilder::getInstance(); g1->build(mgb); g1->display(); g1->transitiveClosure(); }
4.3 test result
0 10 2147483647 5 2147483647 2147483647 0 1 2 2147483647 2147483647 2147483647 0 2147483647 4 2147483647 3 9 0 2 7 2147483647 6 2147483647 0 Transitive Closure Matrix is 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
相关文章推荐
- 算法导论-每对顶点间的最短路径习题解
- 算法导论 - 第25章 每对顶点间的最短路径
- 算法导论笔记:25所有节点对的最短路径问题
- 2011-02-28 CLRS Chapter25 All-Pairs Shortest Paths 每对顶点间的最短路径
- 算法导论 第二十五章:每对顶点间的最短路径
- 算法导论学习笔记(19)——每对顶点间的最短路径(基于Floyd_Warshall算法)
- 算法导论代码 第25章 每对顶点间的最短路径
- 算法导论25(所有结点对的最短路径问题)
- 图之 打印从源点到任何一个顶点最短路径所经过的顶点
- 【动态规划】每对顶点之间的最短路径之Floyd-Warshall算法
- 弗洛伊德算法(求各顶点间最短路径):可打印最短路径
- 每对顶点之间的最短路径
- 从一个顶点到其余各顶点的最短路径
- HDU 6166 2017多校 Team09 1006:Dijkstra顶点子集最短路径
- 图——每对顶点之间的最短路径 ——弗洛伊德算法
- 《算法导论》笔记(17) 所有结点对最短路径 部分习题
- 图之从一个顶点到其余各个顶点的最短路径(有向图)
- PAT 1003. Emergency (25)(俩点间最短路径)
- 所有顶点之间的最短路径算法:Floyd算法。
- hnu12519 Travelling Tom 求所有顶点的最短路径(floyd)