[1956] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[1711] | 19 | #include <iostream> |
---|
| 20 | #include <vector> |
---|
| 21 | |
---|
| 22 | #include <cmath> |
---|
| 23 | #include <cstdlib> |
---|
| 24 | |
---|
| 25 | #include <lemon/smart_graph.h> |
---|
| 26 | #include <lemon/dijkstra.h> |
---|
| 27 | #include <lemon/floyd_warshall.h> |
---|
| 28 | #include <lemon/johnson.h> |
---|
| 29 | |
---|
[1745] | 30 | #include <lemon/fib_heap.h> |
---|
| 31 | |
---|
[1711] | 32 | #include <lemon/time_measure.h> |
---|
[1732] | 33 | #include "test_tools.h" |
---|
[1711] | 34 | |
---|
| 35 | using namespace lemon; |
---|
| 36 | using namespace std; |
---|
| 37 | |
---|
| 38 | int main(int argc, const char *argv[]) { |
---|
| 39 | typedef SmartGraph Graph; |
---|
| 40 | typedef Graph::Node Node; |
---|
| 41 | typedef Graph::Edge Edge; |
---|
| 42 | typedef Graph::NodeIt NodeIt; |
---|
| 43 | typedef Graph::EdgeIt EdgeIt; |
---|
| 44 | |
---|
[2242] | 45 | typedef Graph::EdgeMap<int> LengthMap; |
---|
| 46 | typedef Graph::NodeMap<int> DistMap; |
---|
[1711] | 47 | |
---|
| 48 | const int n = argc > 1 ? atoi(argv[1]) : 20; |
---|
[1732] | 49 | const int e = argc > 2 ? atoi(argv[2]) : (int)(n * log((double)n)); |
---|
[2242] | 50 | const int m = argc > 3 ? atoi(argv[3]) : 100; |
---|
[1711] | 51 | |
---|
| 52 | Graph graph; |
---|
| 53 | LengthMap length(graph); |
---|
| 54 | vector<Node> nodes; |
---|
| 55 | |
---|
| 56 | DistMap shift(graph); |
---|
| 57 | for (int i = 0; i < n; ++i) { |
---|
| 58 | Node node = graph.addNode(); |
---|
| 59 | nodes.push_back(node); |
---|
[2242] | 60 | shift[node] = rnd[m]; |
---|
[1711] | 61 | } |
---|
| 62 | |
---|
| 63 | for (int i = 0; i < e; ++i) { |
---|
[2242] | 64 | int s = rnd[n]; |
---|
| 65 | int t = rnd[n]; |
---|
[1711] | 66 | Edge edge = graph.addEdge(nodes[s], nodes[t]); |
---|
[2242] | 67 | length[edge] = rnd[m] - shift[nodes[s]] + shift[nodes[t]]; |
---|
[1711] | 68 | } |
---|
| 69 | |
---|
| 70 | Johnson<Graph, LengthMap> johnson(graph, length); |
---|
| 71 | { |
---|
| 72 | Timer timer; |
---|
| 73 | johnson.run(); |
---|
| 74 | cout << "Johnson: " << timer << endl; |
---|
| 75 | } |
---|
| 76 | |
---|
[2242] | 77 | typedef FibHeap<Node, int, Graph::NodeMap<int> > IntFibHeap; |
---|
| 78 | Johnson<Graph, LengthMap>::DefStandardHeap<IntFibHeap> |
---|
[1745] | 79 | ::Create fibJohnson(graph, length); |
---|
| 80 | { |
---|
| 81 | Timer timer; |
---|
| 82 | fibJohnson.run(); |
---|
| 83 | cout << "Johnson with fibonacci heap: " << timer << endl; |
---|
| 84 | } |
---|
| 85 | |
---|
[1711] | 86 | FloydWarshall<Graph, LengthMap> floyd(graph, length); |
---|
| 87 | { |
---|
| 88 | Timer timer; |
---|
| 89 | floyd.run(); |
---|
| 90 | cout << "FloydWarshall: " << timer << endl; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | for (NodeIt it(graph); it != INVALID; ++it) { |
---|
| 94 | for (NodeIt jt(graph); jt != INVALID; ++jt) { |
---|
[1732] | 95 | check(johnson.connected(it, jt) == floyd.connected(it, jt), |
---|
| 96 | "Wrong connection in all pairs shortest path"); |
---|
[1745] | 97 | check(johnson.connected(it, jt) == fibJohnson.connected(it, jt), |
---|
| 98 | "Wrong connection in all pairs shortest path"); |
---|
[1711] | 99 | if (johnson.connected(it, jt)) { |
---|
[1732] | 100 | check(johnson.dist(it, jt) == floyd.dist(it, jt), |
---|
| 101 | "Wrong distance in all pairs shortest path"); |
---|
[1745] | 102 | check(johnson.dist(it, jt) == fibJohnson.dist(it, jt), |
---|
| 103 | "Wrong distance in all pairs shortest path"); |
---|
[1711] | 104 | if (it != jt) { |
---|
[1732] | 105 | check(johnson.dist(it, jt) == |
---|
| 106 | johnson.dist(it, johnson.predNode(it, jt)) + |
---|
[1763] | 107 | length[johnson.predEdge(it, jt)], |
---|
[1732] | 108 | "Wrong edge in all pairs shortest path"); |
---|
[1745] | 109 | check(fibJohnson.dist(it, jt) == |
---|
| 110 | fibJohnson.dist(it, fibJohnson.predNode(it, jt)) + |
---|
[1763] | 111 | length[fibJohnson.predEdge(it, jt)], |
---|
[1745] | 112 | "Wrong edge in all pairs shortest path"); |
---|
[1732] | 113 | check(floyd.dist(it, jt) == |
---|
| 114 | floyd.dist(it, floyd.predNode(it, jt)) + |
---|
[1763] | 115 | length[floyd.predEdge(it, jt)], |
---|
[1732] | 116 | "Wrong edge in all pairs shortest path"); |
---|
[1711] | 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | return 0; |
---|
| 123 | } |
---|