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