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