Copyright header added.
2 * src/test/dijkstra_heap_test.cc - Part of HUGOlib, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
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.
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
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.
21 //The input is a graph in standard dimacs format from the standard input (like
22 //in /hugo_loc/testfiles/dimacs). It runs dijkstra.h on this graph with both
23 //heaps, checking two postconditions:
25 //- if the edges e=uv of the shortest path tree reported by dijkstra.h have
26 //dist(v)-dist(u)=length(e)
28 // - if all edges e=uv with u reachable from the root have
29 //dist(v)-dist(u)>=length(e)
33 #include <hugo/smart_graph.h>
34 #include <hugo/dimacs.h>
35 #include <hugo/dijkstra.h>
36 #include <hugo/time_measure.h>
37 #include <hugo/bin_heap.h>
38 #include <hugo/fib_heap.h>
42 int main(int, char **) {
44 typedef SmartGraph Graph;
46 typedef Graph::Edge Edge;
47 typedef Graph::Node Node;
48 typedef Graph::EdgeIt EdgeIt;
49 typedef Graph::NodeIt NodeIt;
50 typedef Graph::template EdgeMap<int> LengthMap;
55 readDimacsMaxFlow(std::cin, G, s, t, cap);
59 "\n Testing dijkstra.h with binary heap implementation bin_heap.h,"
61 std::cout<<" on a graph with " <<
62 G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
63 << std::endl<<std::endl;
65 Dijkstra<Graph, LengthMap>
66 dijkstra_test(G, cap);
69 std::cout << "elapsed time: " << ts << std::endl;
75 for(G.first(e); e!=INVALID; ++e) {
78 if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) > cap[e] )
79 if ( dijkstra_test.reached(u) ) {
80 std::cout<<"Error! dist(head)-dist(tail)- edge_length= "
81 <<dijkstra_test.dist(v) - dijkstra_test.dist(u)
88 for(G.first(v); v!=INVALID; ++v) {
89 if ( dijkstra_test.reached(v) ) {
90 Edge e=dijkstra_test.pred(v);
92 if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) != cap[e] ) {
93 std::cout<<"Error in a shortest path tree edge! Difference: "
94 <<std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u)
103 std::cout << error1 << " non-tree and " << error2
104 << " shortest path tree edge is erroneous."<<std::endl;
109 "\n\n Testing dijkstra.h with Fibonacci heap implementation fib_heap.h,"
111 std::cout<<" on a graph with " <<
112 G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
113 << std::endl<<std::endl;
115 Dijkstra<Graph, LengthMap, FibHeap>
116 dijkstra_test2(G, cap);
118 dijkstra_test2.run(s);
119 std::cout << "elapsed time: " << ts << std::endl;
124 for(G.first(e); e!=INVALID; ++e) {
127 if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) > cap[e] )
128 if ( dijkstra_test2.reached(u) ) {
129 std::cout<<"Error! dist(head)-dist(tail)- edge_length= "
130 <<dijkstra_test2.dist(v) - dijkstra_test2.dist(u)
136 for(G.first(v); v!=INVALID; ++v) {
137 if ( dijkstra_test2.reached(v) ) {
138 Edge e=dijkstra_test2.pred(v);
140 if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) != cap[e] ) {
141 std::cout<<"Error in a shortest path tree edge! Difference: "
142 <<std::abs(dijkstra_test2.dist(v) - dijkstra_test2.dist(u)
143 - cap[e])<<std::endl;
150 std::cout << error1 << " non-tree and " << error2
151 << " shortest path tree edge is erroneous."<<std::endl;