src/test/dijkstra_heap_test.cc
author marci
Sun, 05 Nov 2006 00:39:31 +0000
changeset 746 6ee2046cc210
parent 449 c30569f54936
child 776 f2994a2b10b2
permissions -rw-r--r--
(none)
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@422
     6
//in /hugo_loc/testfiles/dimacs). It runs dijkstra.h on this graph with both
jacint@422
     7
//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
ladanyi@542
    17
#include <hugo/smart_graph.h>
ladanyi@542
    18
#include <hugo/dimacs.h>
ladanyi@542
    19
#include <hugo/dijkstra.h>
ladanyi@542
    20
#include <hugo/time_measure.h>
ladanyi@542
    21
#include <hugo/bin_heap.h>
ladanyi@542
    22
#include <hugo/fib_heap.h>
jacint@384
    23
jacint@384
    24
using namespace hugo;
jacint@384
    25
jacint@384
    26
int main(int, char **) {
jacint@384
    27
  
jacint@384
    28
  typedef SmartGraph Graph;
jacint@384
    29
jacint@384
    30
  typedef Graph::Edge Edge;
jacint@384
    31
  typedef Graph::Node Node;
jacint@384
    32
  typedef Graph::EdgeIt EdgeIt;
jacint@384
    33
  typedef Graph::NodeIt NodeIt;
jacint@384
    34
  typedef Graph::EdgeMap<int> LengthMap;
jacint@384
    35
jacint@384
    36
  Graph G;
jacint@384
    37
  Node s, t;
jacint@384
    38
  LengthMap cap(G);
jacint@384
    39
  readDimacsMaxFlow(std::cin, G, s, t, cap);
jacint@384
    40
  Timer ts;
jacint@384
    41
    
jacint@384
    42
  std::cout <<
jacint@384
    43
    "\n  Testing dijkstra.h with binary heap implementation bin_heap.h,"
jacint@384
    44
	    <<std::endl;
jacint@384
    45
  std::cout<<"  on a graph with " << 
jacint@384
    46
    G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
jacint@384
    47
	   << std::endl<<std::endl;
jacint@384
    48
  
jacint@384
    49
  Dijkstra<Graph, LengthMap> 
jacint@384
    50
    dijkstra_test(G, cap);
jacint@384
    51
  ts.reset();
jacint@384
    52
  dijkstra_test.run(s);
jacint@384
    53
  std::cout << "elapsed time: " << ts << std::endl;
jacint@384
    54
  
jacint@384
    55
  int error1=0;
jacint@384
    56
  int error2=0;
jacint@384
    57
jacint@449
    58
  EdgeIt e;
jacint@449
    59
  for(G.first(e); G.valid(e); G.next(e)) {
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@449
    71
  NodeIt v;
jacint@449
    72
  for(G.first(v); G.valid(v); G.next(v)) {
jacint@384
    73
    if ( dijkstra_test.reached(v) ) {
jacint@384
    74
      Edge e=dijkstra_test.pred(v);
jacint@384
    75
      Node u=G.tail(e);
jacint@384
    76
      if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) != cap[e] ) {
jacint@384
    77
	std::cout<<"Error in a shortest path tree edge! Difference: " 
jacint@384
    78
		 <<std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
jacint@384
    79
			    - cap[e])<<std::endl;
jacint@384
    80
	++error2;
jacint@384
    81
      }
jacint@384
    82
    }
jacint@384
    83
  }
jacint@384
    84
jacint@384
    85
jacint@384
    86
  
jacint@384
    87
  std::cout << error1 << " non-tree and " << error2 
jacint@384
    88
	    << " shortest path tree edge is erroneous."<<std::endl;
jacint@384
    89
jacint@384
    90
jacint@384
    91
jacint@384
    92
  std::cout <<
jacint@449
    93
    "\n\n  Testing dijkstra.h with Fibonacci heap implementation fib_heap.h,"
jacint@384
    94
	    <<std::endl;
jacint@384
    95
  std::cout<<"  on a graph with " << 
jacint@384
    96
    G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
jacint@384
    97
	   << std::endl<<std::endl;
jacint@384
    98
  
jacint@384
    99
  Dijkstra<Graph, LengthMap, FibHeap> 
jacint@384
   100
    dijkstra_test2(G, cap);
jacint@384
   101
  ts.reset();
jacint@384
   102
  dijkstra_test2.run(s);
jacint@384
   103
  std::cout << "elapsed time: " << ts << std::endl;
jacint@384
   104
  
jacint@384
   105
  error1=0;
jacint@384
   106
  error2=0;
jacint@384
   107
jacint@449
   108
  for(G.first(e); G.valid(e); G.next(e)) {
jacint@384
   109
    Node u=G.tail(e);
jacint@384
   110
    Node v=G.head(e);
jacint@384
   111
    if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) > cap[e] )
jacint@384
   112
      if ( dijkstra_test2.reached(u) ) {
jacint@384
   113
	std::cout<<"Error! dist(head)-dist(tail)- edge_length= " 
jacint@384
   114
		 <<dijkstra_test2.dist(v) - dijkstra_test2.dist(u) 
jacint@384
   115
	  - cap[e]<<std::endl;
jacint@384
   116
	++error1;
jacint@384
   117
      }
jacint@384
   118
  }
jacint@384
   119
jacint@449
   120
  for(G.first(v); G.valid(v); G.next(v)) {
jacint@384
   121
    if ( dijkstra_test2.reached(v) ) {
jacint@384
   122
      Edge e=dijkstra_test2.pred(v);
jacint@384
   123
      Node u=G.tail(e);
jacint@384
   124
      if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) != cap[e] ) {
jacint@384
   125
	std::cout<<"Error in a shortest path tree edge! Difference: " 
jacint@384
   126
		 <<std::abs(dijkstra_test2.dist(v) - dijkstra_test2.dist(u) 
jacint@384
   127
			    - cap[e])<<std::endl;
jacint@384
   128
	++error2;
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
  return 0;
jacint@384
   139
}