jacint@384
|
1 |
//Tests dijsktra.h with two heap implementations:
|
jacint@384
|
2 |
//the default binary heap of bin_heap.h, and the
|
jacint@384
|
3 |
//Fibonacci heap of fib_heap.h.
|
jacint@384
|
4 |
|
jacint@384
|
5 |
//The input is a graph in standard dimacs format from the standard input (like
|
jacint@384
|
6 |
//in /hugo_loc/testfiles/dimacs). It runs dijkstra.h on this graph with
|
jacint@384
|
7 |
//both heaps, checking two postconditions:
|
jacint@384
|
8 |
|
jacint@384
|
9 |
//- if the edges e=uv of the shortest path tree reported by dijkstra.h have
|
jacint@384
|
10 |
//dist(v)-dist(u)=length(e)
|
jacint@384
|
11 |
|
jacint@384
|
12 |
// - if all edges e=uv with u reachable from the root have
|
jacint@384
|
13 |
//dist(v)-dist(u)>=length(e)
|
jacint@384
|
14 |
#include <iostream>
|
jacint@384
|
15 |
#include <math.h>
|
jacint@384
|
16 |
|
jacint@384
|
17 |
#include <smart_graph.h>
|
jacint@384
|
18 |
#include <dimacs.h>
|
jacint@384
|
19 |
#include <dijkstra.h>
|
jacint@384
|
20 |
#include <time_measure.h>
|
jacint@384
|
21 |
#include <bin_heap.h>
|
jacint@384
|
22 |
#include <for_each_macros.h>
|
jacint@384
|
23 |
|
jacint@384
|
24 |
using namespace hugo;
|
jacint@384
|
25 |
|
jacint@384
|
26 |
int main(int, char **) {
|
jacint@384
|
27 |
|
jacint@384
|
28 |
typedef SmartGraph Graph;
|
jacint@384
|
29 |
|
jacint@384
|
30 |
typedef Graph::Edge Edge;
|
jacint@384
|
31 |
typedef Graph::Node Node;
|
jacint@384
|
32 |
typedef Graph::EdgeIt EdgeIt;
|
jacint@384
|
33 |
typedef Graph::NodeIt NodeIt;
|
jacint@384
|
34 |
typedef Graph::EdgeMap<int> LengthMap;
|
jacint@384
|
35 |
|
jacint@384
|
36 |
Graph G;
|
jacint@384
|
37 |
Node s, t;
|
jacint@384
|
38 |
LengthMap cap(G);
|
jacint@384
|
39 |
readDimacsMaxFlow(std::cin, G, s, t, cap);
|
jacint@384
|
40 |
Timer ts;
|
jacint@384
|
41 |
|
jacint@384
|
42 |
std::cout <<
|
jacint@384
|
43 |
"\n Testing dijkstra.h with binary heap implementation bin_heap.h,"
|
jacint@384
|
44 |
<<std::endl;
|
jacint@384
|
45 |
std::cout<<" on a graph with " <<
|
jacint@384
|
46 |
G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
|
jacint@384
|
47 |
<< std::endl<<std::endl;
|
jacint@384
|
48 |
|
jacint@384
|
49 |
Dijkstra<Graph, LengthMap>
|
jacint@384
|
50 |
dijkstra_test(G, cap);
|
jacint@384
|
51 |
ts.reset();
|
jacint@384
|
52 |
dijkstra_test.run(s);
|
jacint@384
|
53 |
std::cout << "elapsed time: " << ts << std::endl;
|
jacint@384
|
54 |
|
jacint@384
|
55 |
int error1=0;
|
jacint@384
|
56 |
int error2=0;
|
jacint@384
|
57 |
|
jacint@384
|
58 |
FOR_EACH_LOC ( EdgeIt, e, G) {
|
jacint@384
|
59 |
Node u=G.tail(e);
|
jacint@384
|
60 |
Node v=G.head(e);
|
jacint@384
|
61 |
if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) > cap[e] )
|
jacint@384
|
62 |
if ( dijkstra_test.reached(u) ) {
|
jacint@384
|
63 |
std::cout<<"Error! dist(head)-dist(tail)- edge_length= "
|
jacint@384
|
64 |
<<dijkstra_test.dist(v) - dijkstra_test.dist(u)
|
jacint@384
|
65 |
- cap[e]<<std::endl;
|
jacint@384
|
66 |
++error1;
|
jacint@384
|
67 |
}
|
jacint@384
|
68 |
}
|
jacint@384
|
69 |
|
jacint@384
|
70 |
FOR_EACH_LOC ( NodeIt, v, G) {
|
jacint@384
|
71 |
if ( dijkstra_test.reached(v) ) {
|
jacint@384
|
72 |
Edge e=dijkstra_test.pred(v);
|
jacint@384
|
73 |
Node u=G.tail(e);
|
jacint@384
|
74 |
if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) != cap[e] ) {
|
jacint@384
|
75 |
std::cout<<"Error in a shortest path tree edge! Difference: "
|
jacint@384
|
76 |
<<std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u)
|
jacint@384
|
77 |
- cap[e])<<std::endl;
|
jacint@384
|
78 |
++error2;
|
jacint@384
|
79 |
}
|
jacint@384
|
80 |
}
|
jacint@384
|
81 |
}
|
jacint@384
|
82 |
|
jacint@384
|
83 |
|
jacint@384
|
84 |
|
jacint@384
|
85 |
std::cout << error1 << " non-tree and " << error2
|
jacint@384
|
86 |
<< " shortest path tree edge is erroneous."<<std::endl;
|
jacint@384
|
87 |
|
jacint@384
|
88 |
|
jacint@384
|
89 |
|
jacint@384
|
90 |
std::cout <<
|
jacint@384
|
91 |
"\n Testing dijkstra.h with Fibonacci heap implementation fib_heap.h,"
|
jacint@384
|
92 |
<<std::endl;
|
jacint@384
|
93 |
std::cout<<" on a graph with " <<
|
jacint@384
|
94 |
G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
|
jacint@384
|
95 |
<< std::endl<<std::endl;
|
jacint@384
|
96 |
|
jacint@384
|
97 |
Dijkstra<Graph, LengthMap, FibHeap>
|
jacint@384
|
98 |
dijkstra_test2(G, cap);
|
jacint@384
|
99 |
ts.reset();
|
jacint@384
|
100 |
dijkstra_test2.run(s);
|
jacint@384
|
101 |
std::cout << "elapsed time: " << ts << std::endl;
|
jacint@384
|
102 |
|
jacint@384
|
103 |
error1=0;
|
jacint@384
|
104 |
error2=0;
|
jacint@384
|
105 |
|
jacint@384
|
106 |
FOR_EACH_LOC ( EdgeIt, e, G) {
|
jacint@384
|
107 |
Node u=G.tail(e);
|
jacint@384
|
108 |
Node v=G.head(e);
|
jacint@384
|
109 |
if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) > cap[e] )
|
jacint@384
|
110 |
if ( dijkstra_test2.reached(u) ) {
|
jacint@384
|
111 |
std::cout<<"Error! dist(head)-dist(tail)- edge_length= "
|
jacint@384
|
112 |
<<dijkstra_test2.dist(v) - dijkstra_test2.dist(u)
|
jacint@384
|
113 |
- cap[e]<<std::endl;
|
jacint@384
|
114 |
++error1;
|
jacint@384
|
115 |
}
|
jacint@384
|
116 |
}
|
jacint@384
|
117 |
|
jacint@384
|
118 |
FOR_EACH_LOC ( NodeIt, v, G) {
|
jacint@384
|
119 |
if ( dijkstra_test2.reached(v) ) {
|
jacint@384
|
120 |
Edge e=dijkstra_test2.pred(v);
|
jacint@384
|
121 |
Node u=G.tail(e);
|
jacint@384
|
122 |
if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) != cap[e] ) {
|
jacint@384
|
123 |
std::cout<<"Error in a shortest path tree edge! Difference: "
|
jacint@384
|
124 |
<<std::abs(dijkstra_test2.dist(v) - dijkstra_test2.dist(u)
|
jacint@384
|
125 |
- cap[e])<<std::endl;
|
jacint@384
|
126 |
++error2;
|
jacint@384
|
127 |
}
|
jacint@384
|
128 |
}
|
jacint@384
|
129 |
}
|
jacint@384
|
130 |
|
jacint@384
|
131 |
|
jacint@384
|
132 |
std::cout << error1 << " non-tree and " << error2
|
jacint@384
|
133 |
<< " shortest path tree edge is erroneous."<<std::endl;
|
jacint@384
|
134 |
|
jacint@384
|
135 |
|
jacint@384
|
136 |
return 0;
|
jacint@384
|
137 |
}
|