COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/test/dijkstra_heap_test.cc @ 1194:7bce0ef61d6b

Last change on this file since 1194:7bce0ef61d6b was 1194:7bce0ef61d6b, checked in by Balazs Dezso, 15 years ago

Change test to be up to date.
Deprecated test, it should be used rather the heap_test.cc and heap_test.h.

File size: 6.2 KB
Line 
1/* -*- C++ -*-
2 * src/test/dijkstra_heap_test.cc - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
17//Tests dijsktra.h with two heap implementations:
18//the default binary heap of bin_heap.h, and the
19//Fibonacci heap of fib_heap.h.
20
21//The input is a graph in standard dimacs format from the standard input (like
22//in /lemon_loc/testfiles/dimacs). It runs dijkstra.h on this graph with both
23//heaps, checking two postconditions:
24
25//- if the edges e=uv of the shortest path tree reported by dijkstra.h have
26//dist(v)-dist(u)=length(e)
27
28// - if all edges e=uv with u reachable from the root have
29//dist(v)-dist(u)>=length(e)
30#include <iostream>
31#include <math.h>
32
33#include <lemon/smart_graph.h>
34
35#include <lemon/graph_writer.h>
36#include <lemon/map_utils.h>
37
38
39#include <lemon/dimacs.h>
40#include <lemon/dijkstra.h>
41#include <lemon/time_measure.h>
42#include <lemon/graph_utils.h>
43
44#include <lemon/bin_heap.h>
45#include <lemon/fib_heap.h>
46#include <lemon/radix_heap.h>
47
48using namespace lemon;
49
50typedef ListGraph Graph;
51
52typedef Graph::Edge Edge;
53typedef Graph::Node Node;
54typedef Graph::EdgeIt EdgeIt;
55typedef Graph::NodeIt NodeIt;
56typedef Graph::EdgeMap<int> LengthMap;
57
58
59struct FibTraits : public DijkstraDefaultTraits<Graph, LengthMap> {
60  typedef FibHeap<Graph::Node, LengthMap::Value, Graph::NodeMap<int> > Heap;
61};
62
63struct RadixTraits : public DijkstraDefaultTraits<Graph, LengthMap> {
64  typedef RadixHeap<Graph::Node, Graph::NodeMap<int> > Heap;
65};
66
67
68int main(int argc, const char* argv[]) {
69 
70
71  Graph graph;
72  Node s, t;
73  LengthMap cap(graph);
74  readDimacs(std::cin, graph, cap, s, t);
75  Timer ts;
76
77  GraphWriter<Graph> writer(std::cout, graph);
78
79  IdMap<Graph, Node> nodeIdMap(graph);
80  writer.addNodeMap("id", nodeIdMap);
81
82  IdMap<Graph, Edge> edgeIdMap(graph);
83  writer.addEdgeMap("id", edgeIdMap);
84
85  writer.addEdgeMap("cap", cap);
86
87  writer.run();
88   
89  std::cout <<
90    "\n  Testing dijkstra.h with binary heap implementation bin_heap.h,"
91            <<std::endl;
92  std::cout<<"  on a graph with " <<
93    countNodes(graph) << " nodes and " << countEdges(graph) << " edges..."
94           << std::endl<<std::endl;
95
96
97  Dijkstra<Graph, LengthMap>
98    dijkstra_test(graph, cap);
99  ts.reset();
100   dijkstra_test.run(s);
101  std::cout << "elapsed time: " << ts << std::endl;
102
103  int error1=0;
104  int error2=0;
105
106  for(EdgeIt e(graph); e!=INVALID; ++e) {
107    Node u=graph.source(e);
108    Node v=graph.target(e);
109    if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) > cap[e] )
110      if ( dijkstra_test.reached(u) ) {
111        std::cout<<"Error! dist(target)-dist(source)- edge_length= "
112                 <<dijkstra_test.dist(v) - dijkstra_test.dist(u)
113          - cap[e]<<std::endl;
114        ++error1;
115      }
116  }
117
118  NodeIt v;
119  for(NodeIt v(graph); v!=INVALID; ++v) {
120    if ( dijkstra_test.reached(v) ) {
121      Edge e=dijkstra_test.pred(v);
122      Node u=graph.source(e);
123      if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) != cap[e] ) {
124        std::cout<<"Error in a shortest path tree edge! Difference: "
125                 <<std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u)
126                            - cap[e])<<std::endl;
127        ++error2;
128      }
129    }
130  }
131
132
133 
134  std::cout << error1 << " non-tree and " << error2
135            << " shortest path tree edge is erroneous."<<std::endl;
136
137
138
139  std::cout <<
140    "\n\n  Testing dijkstra.h with Fibonacci heap implementation fib_heap.h,"
141            <<std::endl;
142  std::cout<<"  on a graph with " <<
143    countNodes(graph) << " nodes and " << countEdges(graph) << " edges..."
144           << std::endl<<std::endl;
145 
146
147  Dijkstra<Graph, LengthMap, FibTraits>
148    dijkstra_test2(graph, cap);
149  ts.reset();
150  dijkstra_test2.run(s);
151  std::cout << "elapsed time: " << ts << std::endl;
152 
153  error1=0;
154  error2=0;
155
156  for(EdgeIt e(graph); e!=INVALID; ++e) {
157    Node u=graph.source(e);
158    Node v=graph.target(e);
159    if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) > cap[e] )
160      if ( dijkstra_test2.reached(u) ) {
161        std::cout<<"Error! dist(target)-dist(source)- edge_length= "
162                 <<dijkstra_test2.dist(v) - dijkstra_test2.dist(u)
163          - cap[e]<<std::endl;
164        ++error1;
165      }
166  }
167
168  for(NodeIt n(graph); v!=INVALID; ++v) {
169    if ( dijkstra_test2.reached(v) ) {
170      Edge e=dijkstra_test2.pred(v);
171      Node u=graph.source(e);
172      if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) != cap[e] ) {
173        std::cout<<"Error in a shortest path tree edge! Difference: "
174                 <<std::abs(dijkstra_test2.dist(v) - dijkstra_test2.dist(u)
175                            - cap[e])<<std::endl;
176        ++error2;
177      }
178    }
179  }
180
181
182  std::cout << error1 << " non-tree and " << error2
183            << " shortest path tree edge is erroneous."<<std::endl;
184
185  std::cout <<
186    "\n\n  Testing dijkstra.h with Radix heap implementation radix_heap.h,"
187            <<std::endl;
188  std::cout<<"  on a graph with " <<
189    countNodes(graph) << " nodes and " << countEdges(graph) << " edges..."
190           << std::endl<<std::endl;
191
192
193  Dijkstra<Graph, LengthMap, RadixTraits>
194    dijkstra_test3(graph, cap);
195  ts.reset();
196  dijkstra_test3.run(s);
197  std::cout << "elapsed time: " << ts << std::endl;
198 
199
200  error1=0;
201  error2=0;
202
203  for(EdgeIt e(graph); e!=INVALID; ++e) {
204    Node u=graph.source(e);
205    Node v=graph.target(e);
206    if ( dijkstra_test3.dist(v) - dijkstra_test3.dist(u) > cap[e] )
207      if ( dijkstra_test3.reached(u) ) {
208        std::cout<<"Error! dist(target)-dist(source)- edge_length= "
209                 <<dijkstra_test3.dist(v) - dijkstra_test3.dist(u)
210          - cap[e]<<std::endl;
211        ++error1;
212      }
213  }
214
215  for(NodeIt n(graph); v!=INVALID; ++v) {
216    if ( dijkstra_test3.reached(v) ) {
217      Edge e=dijkstra_test3.pred(v);
218      Node u=graph.source(e);
219      if ( dijkstra_test3.dist(v) - dijkstra_test3.dist(u) != cap[e] ) {
220        std::cout<<"Error in a shortest path tree edge! Difference: "
221                 <<std::abs(dijkstra_test3.dist(v) - dijkstra_test3.dist(u)
222                            - cap[e])<<std::endl;
223        ++error2;
224      }
225    }
226  }
227
228
229  std::cout << error1 << " non-tree and " << error2
230            << " shortest path tree edge is erroneous."<<std::endl;
231
232  return 0;
233}
Note: See TracBrowser for help on using the repository browser.