src/test/dijkstra_heap_test.cc
author deba
Fri, 04 Mar 2005 17:20:11 +0000
changeset 1194 7bce0ef61d6b
parent 1164 80bb73097736
child 1195 4d07dd56fa9a
permissions -rw-r--r--
Change test to be up to date.
Deprecated test, it should be used rather the heap_test.cc and heap_test.h.
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@1164
     4
 * Copyright (C) 2005 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>
deba@1194
    34
deba@1194
    35
#include <lemon/graph_writer.h>
deba@1194
    36
#include <lemon/map_utils.h>
deba@1194
    37
deba@1194
    38
alpar@921
    39
#include <lemon/dimacs.h>
alpar@921
    40
#include <lemon/dijkstra.h>
alpar@921
    41
#include <lemon/time_measure.h>
deba@1194
    42
#include <lemon/graph_utils.h>
deba@1194
    43
alpar@921
    44
#include <lemon/bin_heap.h>
alpar@921
    45
#include <lemon/fib_heap.h>
deba@1194
    46
#include <lemon/radix_heap.h>
jacint@384
    47
alpar@921
    48
using namespace lemon;
jacint@384
    49
deba@1194
    50
typedef ListGraph Graph;
deba@1194
    51
deba@1194
    52
typedef Graph::Edge Edge;
deba@1194
    53
typedef Graph::Node Node;
deba@1194
    54
typedef Graph::EdgeIt EdgeIt;
deba@1194
    55
typedef Graph::NodeIt NodeIt;
deba@1194
    56
typedef Graph::EdgeMap<int> LengthMap;
deba@1194
    57
deba@1194
    58
deba@1194
    59
struct FibTraits : public DijkstraDefaultTraits<Graph, LengthMap> {
deba@1194
    60
  typedef FibHeap<Graph::Node, LengthMap::Value, Graph::NodeMap<int> > Heap;
deba@1194
    61
};
deba@1194
    62
deba@1194
    63
struct RadixTraits : public DijkstraDefaultTraits<Graph, LengthMap> {
deba@1194
    64
  typedef RadixHeap<Graph::Node, Graph::NodeMap<int> > Heap;
deba@1194
    65
};
deba@1194
    66
deba@1194
    67
deba@1194
    68
int main(int argc, const char* argv[]) {
jacint@384
    69
  
jacint@384
    70
deba@1194
    71
  Graph graph;
deba@1194
    72
  Node s, t;
deba@1194
    73
  LengthMap cap(graph);
deba@1194
    74
  readDimacs(std::cin, graph, cap, s, t);
deba@1194
    75
  Timer ts;
jacint@384
    76
deba@1194
    77
  GraphWriter<Graph> writer(std::cout, graph);
deba@1194
    78
deba@1194
    79
  IdMap<Graph, Node> nodeIdMap(graph);
deba@1194
    80
  writer.addNodeMap("id", nodeIdMap);
deba@1194
    81
deba@1194
    82
  IdMap<Graph, Edge> edgeIdMap(graph);
deba@1194
    83
  writer.addEdgeMap("id", edgeIdMap);
deba@1194
    84
deba@1194
    85
  writer.addEdgeMap("cap", cap);
deba@1194
    86
deba@1194
    87
  writer.run();
jacint@384
    88
    
jacint@384
    89
  std::cout <<
jacint@384
    90
    "\n  Testing dijkstra.h with binary heap implementation bin_heap.h,"
jacint@384
    91
	    <<std::endl;
jacint@384
    92
  std::cout<<"  on a graph with " << 
deba@1194
    93
    countNodes(graph) << " nodes and " << countEdges(graph) << " edges..."
jacint@384
    94
	   << std::endl<<std::endl;
deba@1194
    95
deba@1194
    96
jacint@384
    97
  Dijkstra<Graph, LengthMap> 
deba@1194
    98
    dijkstra_test(graph, cap);
jacint@384
    99
  ts.reset();
deba@1194
   100
   dijkstra_test.run(s);
jacint@384
   101
  std::cout << "elapsed time: " << ts << std::endl;
deba@1194
   102
jacint@384
   103
  int error1=0;
jacint@384
   104
  int error2=0;
jacint@384
   105
deba@1194
   106
  for(EdgeIt e(graph); e!=INVALID; ++e) {
deba@1194
   107
    Node u=graph.source(e); 
deba@1194
   108
    Node v=graph.target(e);
jacint@384
   109
    if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) > cap[e] )
jacint@384
   110
      if ( dijkstra_test.reached(u) ) {
alpar@986
   111
	std::cout<<"Error! dist(target)-dist(source)- edge_length= " 
jacint@384
   112
		 <<dijkstra_test.dist(v) - dijkstra_test.dist(u) 
jacint@384
   113
	  - cap[e]<<std::endl;
jacint@384
   114
	++error1;
jacint@384
   115
      }
jacint@384
   116
  }
jacint@384
   117
jacint@449
   118
  NodeIt v;
deba@1194
   119
  for(NodeIt v(graph); v!=INVALID; ++v) {
jacint@384
   120
    if ( dijkstra_test.reached(v) ) {
jacint@384
   121
      Edge e=dijkstra_test.pred(v);
deba@1194
   122
      Node u=graph.source(e);
jacint@384
   123
      if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) != cap[e] ) {
jacint@384
   124
	std::cout<<"Error in a shortest path tree edge! Difference: " 
jacint@384
   125
		 <<std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
jacint@384
   126
			    - cap[e])<<std::endl;
jacint@384
   127
	++error2;
jacint@384
   128
      }
jacint@384
   129
    }
jacint@384
   130
  }
jacint@384
   131
jacint@384
   132
jacint@384
   133
  
jacint@384
   134
  std::cout << error1 << " non-tree and " << error2 
jacint@384
   135
	    << " shortest path tree edge is erroneous."<<std::endl;
jacint@384
   136
jacint@384
   137
jacint@384
   138
jacint@384
   139
  std::cout <<
jacint@449
   140
    "\n\n  Testing dijkstra.h with Fibonacci heap implementation fib_heap.h,"
jacint@384
   141
	    <<std::endl;
jacint@384
   142
  std::cout<<"  on a graph with " << 
deba@1194
   143
    countNodes(graph) << " nodes and " << countEdges(graph) << " edges..."
jacint@384
   144
	   << std::endl<<std::endl;
jacint@384
   145
  
deba@1194
   146
deba@1194
   147
  Dijkstra<Graph, LengthMap, FibTraits> 
deba@1194
   148
    dijkstra_test2(graph, cap);
jacint@384
   149
  ts.reset();
jacint@384
   150
  dijkstra_test2.run(s);
jacint@384
   151
  std::cout << "elapsed time: " << ts << std::endl;
jacint@384
   152
  
jacint@384
   153
  error1=0;
jacint@384
   154
  error2=0;
jacint@384
   155
deba@1194
   156
  for(EdgeIt e(graph); e!=INVALID; ++e) {
deba@1194
   157
    Node u=graph.source(e);
deba@1194
   158
    Node v=graph.target(e);
jacint@384
   159
    if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) > cap[e] )
jacint@384
   160
      if ( dijkstra_test2.reached(u) ) {
alpar@986
   161
	std::cout<<"Error! dist(target)-dist(source)- edge_length= " 
jacint@384
   162
		 <<dijkstra_test2.dist(v) - dijkstra_test2.dist(u) 
jacint@384
   163
	  - cap[e]<<std::endl;
jacint@384
   164
	++error1;
jacint@384
   165
      }
jacint@384
   166
  }
jacint@384
   167
deba@1194
   168
  for(NodeIt n(graph); v!=INVALID; ++v) {
jacint@384
   169
    if ( dijkstra_test2.reached(v) ) {
jacint@384
   170
      Edge e=dijkstra_test2.pred(v);
deba@1194
   171
      Node u=graph.source(e);
jacint@384
   172
      if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) != cap[e] ) {
jacint@384
   173
	std::cout<<"Error in a shortest path tree edge! Difference: " 
jacint@384
   174
		 <<std::abs(dijkstra_test2.dist(v) - dijkstra_test2.dist(u) 
jacint@384
   175
			    - cap[e])<<std::endl;
jacint@384
   176
	++error2;
jacint@384
   177
      }
jacint@384
   178
    }
jacint@384
   179
  }
jacint@384
   180
jacint@384
   181
jacint@384
   182
  std::cout << error1 << " non-tree and " << error2 
jacint@384
   183
	    << " shortest path tree edge is erroneous."<<std::endl;
jacint@384
   184
deba@1194
   185
  std::cout <<
deba@1194
   186
    "\n\n  Testing dijkstra.h with Radix heap implementation radix_heap.h,"
deba@1194
   187
	    <<std::endl;
deba@1194
   188
  std::cout<<"  on a graph with " << 
deba@1194
   189
    countNodes(graph) << " nodes and " << countEdges(graph) << " edges..."
deba@1194
   190
	   << std::endl<<std::endl;
deba@1194
   191
deba@1194
   192
deba@1194
   193
  Dijkstra<Graph, LengthMap, RadixTraits> 
deba@1194
   194
    dijkstra_test3(graph, cap);
deba@1194
   195
  ts.reset();
deba@1194
   196
  dijkstra_test3.run(s);
deba@1194
   197
  std::cout << "elapsed time: " << ts << std::endl;
deba@1194
   198
  
deba@1194
   199
deba@1194
   200
  error1=0;
deba@1194
   201
  error2=0;
deba@1194
   202
deba@1194
   203
  for(EdgeIt e(graph); e!=INVALID; ++e) {
deba@1194
   204
    Node u=graph.source(e);
deba@1194
   205
    Node v=graph.target(e);
deba@1194
   206
    if ( dijkstra_test3.dist(v) - dijkstra_test3.dist(u) > cap[e] )
deba@1194
   207
      if ( dijkstra_test3.reached(u) ) {
deba@1194
   208
	std::cout<<"Error! dist(target)-dist(source)- edge_length= " 
deba@1194
   209
		 <<dijkstra_test3.dist(v) - dijkstra_test3.dist(u) 
deba@1194
   210
	  - cap[e]<<std::endl;
deba@1194
   211
	++error1;
deba@1194
   212
      }
deba@1194
   213
  }
deba@1194
   214
deba@1194
   215
  for(NodeIt n(graph); v!=INVALID; ++v) {
deba@1194
   216
    if ( dijkstra_test3.reached(v) ) {
deba@1194
   217
      Edge e=dijkstra_test3.pred(v);
deba@1194
   218
      Node u=graph.source(e);
deba@1194
   219
      if ( dijkstra_test3.dist(v) - dijkstra_test3.dist(u) != cap[e] ) {
deba@1194
   220
	std::cout<<"Error in a shortest path tree edge! Difference: " 
deba@1194
   221
		 <<std::abs(dijkstra_test3.dist(v) - dijkstra_test3.dist(u) 
deba@1194
   222
			    - cap[e])<<std::endl;
deba@1194
   223
	++error2;
deba@1194
   224
      }
deba@1194
   225
    }
deba@1194
   226
  }
deba@1194
   227
deba@1194
   228
deba@1194
   229
  std::cout << error1 << " non-tree and " << error2 
deba@1194
   230
	    << " shortest path tree edge is erroneous."<<std::endl;
jacint@384
   231
jacint@384
   232
  return 0;
jacint@384
   233
}