alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@921
|
2 |
* src/test/dijkstra_heap_test.cc - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@906
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@906
|
16 |
|
jacint@384
|
17 |
//Tests dijsktra.h with two heap implementations:
|
jacint@384
|
18 |
//the default binary heap of bin_heap.h, and the
|
jacint@384
|
19 |
//Fibonacci heap of fib_heap.h.
|
jacint@384
|
20 |
|
jacint@384
|
21 |
//The input is a graph in standard dimacs format from the standard input (like
|
alpar@921
|
22 |
//in /lemon_loc/testfiles/dimacs). It runs dijkstra.h on this graph with both
|
jacint@422
|
23 |
//heaps, checking two postconditions:
|
jacint@384
|
24 |
|
jacint@384
|
25 |
//- if the edges e=uv of the shortest path tree reported by dijkstra.h have
|
jacint@384
|
26 |
//dist(v)-dist(u)=length(e)
|
jacint@384
|
27 |
|
jacint@384
|
28 |
// - if all edges e=uv with u reachable from the root have
|
jacint@384
|
29 |
//dist(v)-dist(u)>=length(e)
|
jacint@384
|
30 |
#include <iostream>
|
jacint@384
|
31 |
#include <math.h>
|
jacint@384
|
32 |
|
alpar@921
|
33 |
#include <lemon/smart_graph.h>
|
alpar@921
|
34 |
#include <lemon/dimacs.h>
|
alpar@921
|
35 |
#include <lemon/dijkstra.h>
|
alpar@921
|
36 |
#include <lemon/time_measure.h>
|
alpar@921
|
37 |
#include <lemon/bin_heap.h>
|
alpar@921
|
38 |
#include <lemon/fib_heap.h>
|
jacint@384
|
39 |
|
alpar@921
|
40 |
using namespace lemon;
|
jacint@384
|
41 |
|
jacint@384
|
42 |
int main(int, char **) {
|
jacint@384
|
43 |
|
jacint@384
|
44 |
typedef SmartGraph Graph;
|
jacint@384
|
45 |
|
jacint@384
|
46 |
typedef Graph::Edge Edge;
|
jacint@384
|
47 |
typedef Graph::Node Node;
|
jacint@384
|
48 |
typedef Graph::EdgeIt EdgeIt;
|
jacint@384
|
49 |
typedef Graph::NodeIt NodeIt;
|
jacint@833
|
50 |
typedef Graph::template EdgeMap<int> LengthMap;
|
jacint@384
|
51 |
|
jacint@384
|
52 |
Graph G;
|
jacint@384
|
53 |
Node s, t;
|
jacint@384
|
54 |
LengthMap cap(G);
|
jacint@384
|
55 |
readDimacsMaxFlow(std::cin, G, s, t, cap);
|
jacint@384
|
56 |
Timer ts;
|
jacint@384
|
57 |
|
jacint@384
|
58 |
std::cout <<
|
jacint@384
|
59 |
"\n Testing dijkstra.h with binary heap implementation bin_heap.h,"
|
jacint@384
|
60 |
<<std::endl;
|
jacint@384
|
61 |
std::cout<<" on a graph with " <<
|
jacint@384
|
62 |
G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
|
jacint@384
|
63 |
<< std::endl<<std::endl;
|
jacint@384
|
64 |
|
jacint@384
|
65 |
Dijkstra<Graph, LengthMap>
|
jacint@384
|
66 |
dijkstra_test(G, cap);
|
jacint@384
|
67 |
ts.reset();
|
jacint@384
|
68 |
dijkstra_test.run(s);
|
jacint@384
|
69 |
std::cout << "elapsed time: " << ts << std::endl;
|
jacint@384
|
70 |
|
jacint@384
|
71 |
int error1=0;
|
jacint@384
|
72 |
int error2=0;
|
jacint@384
|
73 |
|
jacint@449
|
74 |
EdgeIt e;
|
hegyi@776
|
75 |
for(G.first(e); e!=INVALID; ++e) {
|
jacint@384
|
76 |
Node u=G.tail(e);
|
jacint@384
|
77 |
Node v=G.head(e);
|
jacint@384
|
78 |
if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) > cap[e] )
|
jacint@384
|
79 |
if ( dijkstra_test.reached(u) ) {
|
jacint@384
|
80 |
std::cout<<"Error! dist(head)-dist(tail)- edge_length= "
|
jacint@384
|
81 |
<<dijkstra_test.dist(v) - dijkstra_test.dist(u)
|
jacint@384
|
82 |
- cap[e]<<std::endl;
|
jacint@384
|
83 |
++error1;
|
jacint@384
|
84 |
}
|
jacint@384
|
85 |
}
|
jacint@384
|
86 |
|
jacint@449
|
87 |
NodeIt v;
|
hegyi@776
|
88 |
for(G.first(v); v!=INVALID; ++v) {
|
jacint@384
|
89 |
if ( dijkstra_test.reached(v) ) {
|
jacint@384
|
90 |
Edge e=dijkstra_test.pred(v);
|
jacint@384
|
91 |
Node u=G.tail(e);
|
jacint@384
|
92 |
if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) != cap[e] ) {
|
jacint@384
|
93 |
std::cout<<"Error in a shortest path tree edge! Difference: "
|
jacint@384
|
94 |
<<std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u)
|
jacint@384
|
95 |
- cap[e])<<std::endl;
|
jacint@384
|
96 |
++error2;
|
jacint@384
|
97 |
}
|
jacint@384
|
98 |
}
|
jacint@384
|
99 |
}
|
jacint@384
|
100 |
|
jacint@384
|
101 |
|
jacint@384
|
102 |
|
jacint@384
|
103 |
std::cout << error1 << " non-tree and " << error2
|
jacint@384
|
104 |
<< " shortest path tree edge is erroneous."<<std::endl;
|
jacint@384
|
105 |
|
jacint@384
|
106 |
|
jacint@384
|
107 |
|
jacint@384
|
108 |
std::cout <<
|
jacint@449
|
109 |
"\n\n Testing dijkstra.h with Fibonacci heap implementation fib_heap.h,"
|
jacint@384
|
110 |
<<std::endl;
|
jacint@384
|
111 |
std::cout<<" on a graph with " <<
|
jacint@384
|
112 |
G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
|
jacint@384
|
113 |
<< std::endl<<std::endl;
|
jacint@384
|
114 |
|
jacint@384
|
115 |
Dijkstra<Graph, LengthMap, FibHeap>
|
jacint@384
|
116 |
dijkstra_test2(G, cap);
|
jacint@384
|
117 |
ts.reset();
|
jacint@384
|
118 |
dijkstra_test2.run(s);
|
jacint@384
|
119 |
std::cout << "elapsed time: " << ts << std::endl;
|
jacint@384
|
120 |
|
jacint@384
|
121 |
error1=0;
|
jacint@384
|
122 |
error2=0;
|
jacint@384
|
123 |
|
hegyi@776
|
124 |
for(G.first(e); e!=INVALID; ++e) {
|
jacint@384
|
125 |
Node u=G.tail(e);
|
jacint@384
|
126 |
Node v=G.head(e);
|
jacint@384
|
127 |
if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) > cap[e] )
|
jacint@384
|
128 |
if ( dijkstra_test2.reached(u) ) {
|
jacint@384
|
129 |
std::cout<<"Error! dist(head)-dist(tail)- edge_length= "
|
jacint@384
|
130 |
<<dijkstra_test2.dist(v) - dijkstra_test2.dist(u)
|
jacint@384
|
131 |
- cap[e]<<std::endl;
|
jacint@384
|
132 |
++error1;
|
jacint@384
|
133 |
}
|
jacint@384
|
134 |
}
|
jacint@384
|
135 |
|
hegyi@776
|
136 |
for(G.first(v); v!=INVALID; ++v) {
|
jacint@384
|
137 |
if ( dijkstra_test2.reached(v) ) {
|
jacint@384
|
138 |
Edge e=dijkstra_test2.pred(v);
|
jacint@384
|
139 |
Node u=G.tail(e);
|
jacint@384
|
140 |
if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) != cap[e] ) {
|
jacint@384
|
141 |
std::cout<<"Error in a shortest path tree edge! Difference: "
|
jacint@384
|
142 |
<<std::abs(dijkstra_test2.dist(v) - dijkstra_test2.dist(u)
|
jacint@384
|
143 |
- cap[e])<<std::endl;
|
jacint@384
|
144 |
++error2;
|
jacint@384
|
145 |
}
|
jacint@384
|
146 |
}
|
jacint@384
|
147 |
}
|
jacint@384
|
148 |
|
jacint@384
|
149 |
|
jacint@384
|
150 |
std::cout << error1 << " non-tree and " << error2
|
jacint@384
|
151 |
<< " shortest path tree edge is erroneous."<<std::endl;
|
jacint@384
|
152 |
|
jacint@384
|
153 |
|
jacint@384
|
154 |
return 0;
|
jacint@384
|
155 |
}
|