src/test/dijkstra_heap_test.cc
author marci
Mon, 26 Apr 2004 15:14:23 +0000
changeset 417 4ce3d5f675ea
parent 384 f27d21767d38
child 422 ede61a3d229b
permissions -rw-r--r--
Better control of gcc version
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@386
    22
#include <fib_heap.h>
jacint@384
    23
#include <for_each_macros.h>
jacint@384
    24
jacint@384
    25
using namespace hugo;
jacint@384
    26
jacint@384
    27
int main(int, char **) {
jacint@384
    28
  
jacint@384
    29
  typedef SmartGraph Graph;
jacint@384
    30
jacint@384
    31
  typedef Graph::Edge Edge;
jacint@384
    32
  typedef Graph::Node Node;
jacint@384
    33
  typedef Graph::EdgeIt EdgeIt;
jacint@384
    34
  typedef Graph::NodeIt NodeIt;
jacint@384
    35
  typedef Graph::EdgeMap<int> LengthMap;
jacint@384
    36
jacint@384
    37
  Graph G;
jacint@384
    38
  Node s, t;
jacint@384
    39
  LengthMap cap(G);
jacint@384
    40
  readDimacsMaxFlow(std::cin, G, s, t, cap);
jacint@384
    41
  Timer ts;
jacint@384
    42
    
jacint@384
    43
  std::cout <<
jacint@384
    44
    "\n  Testing dijkstra.h with binary heap implementation bin_heap.h,"
jacint@384
    45
	    <<std::endl;
jacint@384
    46
  std::cout<<"  on a graph with " << 
jacint@384
    47
    G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
jacint@384
    48
	   << std::endl<<std::endl;
jacint@384
    49
  
jacint@384
    50
  Dijkstra<Graph, LengthMap> 
jacint@384
    51
    dijkstra_test(G, cap);
jacint@384
    52
  ts.reset();
jacint@384
    53
  dijkstra_test.run(s);
jacint@384
    54
  std::cout << "elapsed time: " << ts << std::endl;
jacint@384
    55
  
jacint@384
    56
  int error1=0;
jacint@384
    57
  int error2=0;
jacint@384
    58
jacint@384
    59
  FOR_EACH_LOC ( EdgeIt, e, G) {
jacint@384
    60
    Node u=G.tail(e);
jacint@384
    61
    Node v=G.head(e);
jacint@384
    62
    if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) > cap[e] )
jacint@384
    63
      if ( dijkstra_test.reached(u) ) {
jacint@384
    64
	std::cout<<"Error! dist(head)-dist(tail)- edge_length= " 
jacint@384
    65
		 <<dijkstra_test.dist(v) - dijkstra_test.dist(u) 
jacint@384
    66
	  - cap[e]<<std::endl;
jacint@384
    67
	++error1;
jacint@384
    68
      }
jacint@384
    69
  }
jacint@384
    70
jacint@384
    71
  FOR_EACH_LOC ( NodeIt, v, G) {
jacint@384
    72
    if ( dijkstra_test.reached(v) ) {
jacint@384
    73
      Edge e=dijkstra_test.pred(v);
jacint@384
    74
      Node u=G.tail(e);
jacint@384
    75
      if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) != cap[e] ) {
jacint@384
    76
	std::cout<<"Error in a shortest path tree edge! Difference: " 
jacint@384
    77
		 <<std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
jacint@384
    78
			    - cap[e])<<std::endl;
jacint@384
    79
	++error2;
jacint@384
    80
      }
jacint@384
    81
    }
jacint@384
    82
  }
jacint@384
    83
jacint@384
    84
jacint@384
    85
  
jacint@384
    86
  std::cout << error1 << " non-tree and " << error2 
jacint@384
    87
	    << " shortest path tree edge is erroneous."<<std::endl;
jacint@384
    88
jacint@384
    89
jacint@384
    90
jacint@384
    91
  std::cout <<
jacint@384
    92
    "\n  Testing dijkstra.h with Fibonacci heap implementation fib_heap.h,"
jacint@384
    93
	    <<std::endl;
jacint@384
    94
  std::cout<<"  on a graph with " << 
jacint@384
    95
    G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
jacint@384
    96
	   << std::endl<<std::endl;
jacint@384
    97
  
jacint@384
    98
  Dijkstra<Graph, LengthMap, FibHeap> 
jacint@384
    99
    dijkstra_test2(G, cap);
jacint@384
   100
  ts.reset();
jacint@384
   101
  dijkstra_test2.run(s);
jacint@384
   102
  std::cout << "elapsed time: " << ts << std::endl;
jacint@384
   103
  
jacint@384
   104
  error1=0;
jacint@384
   105
  error2=0;
jacint@384
   106
jacint@384
   107
  FOR_EACH_LOC ( EdgeIt, e, G) {
jacint@384
   108
    Node u=G.tail(e);
jacint@384
   109
    Node v=G.head(e);
jacint@384
   110
    if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) > cap[e] )
jacint@384
   111
      if ( dijkstra_test2.reached(u) ) {
jacint@384
   112
	std::cout<<"Error! dist(head)-dist(tail)- edge_length= " 
jacint@384
   113
		 <<dijkstra_test2.dist(v) - dijkstra_test2.dist(u) 
jacint@384
   114
	  - cap[e]<<std::endl;
jacint@384
   115
	++error1;
jacint@384
   116
      }
jacint@384
   117
  }
jacint@384
   118
jacint@384
   119
  FOR_EACH_LOC ( NodeIt, v, G) {
jacint@384
   120
    if ( dijkstra_test2.reached(v) ) {
jacint@384
   121
      Edge e=dijkstra_test2.pred(v);
jacint@384
   122
      Node u=G.tail(e);
jacint@384
   123
      if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) != cap[e] ) {
jacint@384
   124
	std::cout<<"Error in a shortest path tree edge! Difference: " 
jacint@384
   125
		 <<std::abs(dijkstra_test2.dist(v) - dijkstra_test2.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
  std::cout << error1 << " non-tree and " << error2 
jacint@384
   134
	    << " shortest path tree edge is erroneous."<<std::endl;
jacint@384
   135
jacint@384
   136
jacint@384
   137
  return 0;
jacint@384
   138
}