[1711] | 1 | #include <iostream> |
---|
| 2 | #include <vector> |
---|
| 3 | |
---|
| 4 | #include <cmath> |
---|
| 5 | #include <cstdlib> |
---|
| 6 | |
---|
| 7 | #include <lemon/smart_graph.h> |
---|
| 8 | #include <lemon/dijkstra.h> |
---|
| 9 | #include <lemon/floyd_warshall.h> |
---|
| 10 | #include <lemon/johnson.h> |
---|
| 11 | |
---|
[1745] | 12 | #include <lemon/fib_heap.h> |
---|
| 13 | |
---|
[1711] | 14 | #include <lemon/time_measure.h> |
---|
[1732] | 15 | #include "test_tools.h" |
---|
[1711] | 16 | |
---|
| 17 | using namespace lemon; |
---|
| 18 | using namespace std; |
---|
| 19 | |
---|
| 20 | int main(int argc, const char *argv[]) { |
---|
| 21 | srand(time(0)); |
---|
| 22 | typedef SmartGraph Graph; |
---|
| 23 | typedef Graph::Node Node; |
---|
| 24 | typedef Graph::Edge Edge; |
---|
| 25 | typedef Graph::NodeIt NodeIt; |
---|
| 26 | typedef Graph::EdgeIt EdgeIt; |
---|
| 27 | |
---|
| 28 | typedef Graph::EdgeMap<double> LengthMap; |
---|
| 29 | typedef Graph::NodeMap<double> DistMap; |
---|
| 30 | |
---|
| 31 | const int n = argc > 1 ? atoi(argv[1]) : 20; |
---|
[1732] | 32 | const int e = argc > 2 ? atoi(argv[2]) : (int)(n * log((double)n)); |
---|
[1711] | 33 | const double m = argc > 3 ? (double)atoi(argv[3]) : 100.0; |
---|
| 34 | |
---|
| 35 | Graph graph; |
---|
| 36 | LengthMap length(graph); |
---|
| 37 | vector<Node> nodes; |
---|
| 38 | |
---|
| 39 | DistMap shift(graph); |
---|
| 40 | for (int i = 0; i < n; ++i) { |
---|
| 41 | Node node = graph.addNode(); |
---|
| 42 | nodes.push_back(node); |
---|
| 43 | shift[node] = m * (double)rand() / (RAND_MAX + 1.0); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | for (int i = 0; i < e; ++i) { |
---|
| 47 | int s = (int)(n * (double)rand() / (RAND_MAX + 1.0)); |
---|
| 48 | int t = (int)(n * (double)rand() / (RAND_MAX + 1.0)); |
---|
| 49 | double c = m * (double)rand() / (RAND_MAX + 1.0); |
---|
| 50 | Edge edge = graph.addEdge(nodes[s], nodes[t]); |
---|
| 51 | length[edge] = c - shift[nodes[s]] + shift[nodes[t]]; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | Johnson<Graph, LengthMap> johnson(graph, length); |
---|
| 55 | { |
---|
| 56 | Timer timer; |
---|
| 57 | johnson.run(); |
---|
| 58 | cout << "Johnson: " << timer << endl; |
---|
| 59 | } |
---|
| 60 | |
---|
[1745] | 61 | typedef FibHeap<Node, double, Graph::NodeMap<int> > DoubleFibHeap; |
---|
| 62 | Johnson<Graph, LengthMap>::DefStandardHeap<DoubleFibHeap> |
---|
| 63 | ::Create fibJohnson(graph, length); |
---|
| 64 | { |
---|
| 65 | Timer timer; |
---|
| 66 | fibJohnson.run(); |
---|
| 67 | cout << "Johnson with fibonacci heap: " << timer << endl; |
---|
| 68 | } |
---|
| 69 | |
---|
[1711] | 70 | FloydWarshall<Graph, LengthMap> floyd(graph, length); |
---|
| 71 | { |
---|
| 72 | Timer timer; |
---|
| 73 | floyd.run(); |
---|
| 74 | cout << "FloydWarshall: " << timer << endl; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | for (NodeIt it(graph); it != INVALID; ++it) { |
---|
| 78 | for (NodeIt jt(graph); jt != INVALID; ++jt) { |
---|
[1732] | 79 | check(johnson.connected(it, jt) == floyd.connected(it, jt), |
---|
| 80 | "Wrong connection in all pairs shortest path"); |
---|
[1745] | 81 | check(johnson.connected(it, jt) == fibJohnson.connected(it, jt), |
---|
| 82 | "Wrong connection in all pairs shortest path"); |
---|
[1711] | 83 | if (johnson.connected(it, jt)) { |
---|
[1732] | 84 | check(johnson.dist(it, jt) == floyd.dist(it, jt), |
---|
| 85 | "Wrong distance in all pairs shortest path"); |
---|
[1745] | 86 | check(johnson.dist(it, jt) == fibJohnson.dist(it, jt), |
---|
| 87 | "Wrong distance in all pairs shortest path"); |
---|
[1711] | 88 | if (it != jt) { |
---|
[1732] | 89 | check(johnson.dist(it, jt) == |
---|
| 90 | johnson.dist(it, johnson.predNode(it, jt)) + |
---|
[1763] | 91 | length[johnson.predEdge(it, jt)], |
---|
[1732] | 92 | "Wrong edge in all pairs shortest path"); |
---|
[1745] | 93 | check(fibJohnson.dist(it, jt) == |
---|
| 94 | fibJohnson.dist(it, fibJohnson.predNode(it, jt)) + |
---|
[1763] | 95 | length[fibJohnson.predEdge(it, jt)], |
---|
[1745] | 96 | "Wrong edge in all pairs shortest path"); |
---|
[1732] | 97 | check(floyd.dist(it, jt) == |
---|
| 98 | floyd.dist(it, floyd.predNode(it, jt)) + |
---|
[1763] | 99 | length[floyd.predEdge(it, jt)], |
---|
[1732] | 100 | "Wrong edge in all pairs shortest path"); |
---|
[1711] | 101 | } |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | return 0; |
---|
| 107 | } |
---|