src/test/dijkstra_test.cc
author klao
Wed, 09 Mar 2005 14:23:36 +0000
changeset 1209 dc9fdf77007f
parent 1148 1eea022c7a16
child 1218 5331168bbb18
permissions -rw-r--r--
Fix a bug noticed by deba.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/test/dijkstra_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
alpar@578
    17
#include "test_tools.h"
alpar@921
    18
#include <lemon/smart_graph.h>
alpar@921
    19
#include <lemon/dijkstra.h>
alpar@1148
    20
#include <lemon/maps.h>
klao@959
    21
#include <lemon/concept/graph.h>
klao@959
    22
#include <lemon/concept/maps.h>
alpar@921
    23
using namespace lemon;
alpar@568
    24
alpar@568
    25
const int PET_SIZE =5;
alpar@568
    26
alpar@570
    27
alpar@793
    28
void check_Dijkstra_BinHeap_Compile() 
alpar@570
    29
{
alpar@570
    30
  typedef int VType;
klao@959
    31
  typedef concept::StaticGraph Graph;
alpar@570
    32
alpar@570
    33
  typedef Graph::Edge Edge;
alpar@570
    34
  typedef Graph::Node Node;
alpar@570
    35
  typedef Graph::EdgeIt EdgeIt;
alpar@570
    36
  typedef Graph::NodeIt NodeIt;
klao@959
    37
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@570
    38
 
alpar@570
    39
  typedef Dijkstra<Graph, LengthMap> DType;
alpar@570
    40
  
alpar@570
    41
  Graph G;
alpar@570
    42
  Node n;
alpar@570
    43
  Edge e;
alpar@570
    44
  VType l;
alpar@570
    45
  bool b;
alpar@570
    46
  DType::DistMap d(G);
alpar@570
    47
  DType::PredMap p(G);
alpar@1148
    48
  //  DType::PredNodeMap pn(G);
alpar@793
    49
  LengthMap cap;
alpar@570
    50
alpar@570
    51
  DType dijkstra_test(G,cap);
alpar@570
    52
alpar@570
    53
  dijkstra_test.run(n);
alpar@570
    54
alpar@570
    55
  l  = dijkstra_test.dist(n);
alpar@570
    56
  e  = dijkstra_test.pred(n);
alpar@570
    57
  n  = dijkstra_test.predNode(n);
alpar@570
    58
  d  = dijkstra_test.distMap();
alpar@570
    59
  p  = dijkstra_test.predMap();
alpar@1148
    60
  //  pn = dijkstra_test.predNodeMap();
alpar@570
    61
  b  = dijkstra_test.reached(n);
alpar@1148
    62
  
alpar@570
    63
}
alpar@570
    64
alpar@568
    65
int main()
alpar@568
    66
{
alpar@568
    67
    
alpar@568
    68
  typedef SmartGraph Graph;
alpar@568
    69
alpar@568
    70
  typedef Graph::Edge Edge;
alpar@568
    71
  typedef Graph::Node Node;
alpar@568
    72
  typedef Graph::EdgeIt EdgeIt;
alpar@568
    73
  typedef Graph::NodeIt NodeIt;
alpar@568
    74
  typedef Graph::EdgeMap<int> LengthMap;
alpar@568
    75
alpar@568
    76
  Graph G;
alpar@568
    77
  Node s, t;
alpar@568
    78
  LengthMap cap(G);
alpar@568
    79
  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
alpar@578
    80
   
alpar@568
    81
  for(int i=0;i<PET_SIZE;i++) {
alpar@568
    82
    cap[ps.outcir[i]]=4;
alpar@568
    83
    cap[ps.incir[i]]=1;
alpar@568
    84
    cap[ps.chords[i]]=10;
alpar@568
    85
  }
alpar@568
    86
  s=ps.outer[0];
alpar@568
    87
  t=ps.inner[1];
alpar@568
    88
  
alpar@568
    89
  Dijkstra<Graph, LengthMap> 
alpar@568
    90
	dijkstra_test(G, cap);
alpar@568
    91
  dijkstra_test.run(s);
alpar@568
    92
  
alpar@568
    93
  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
alpar@585
    94
alpar@585
    95
hegyi@776
    96
  for(EdgeIt e(G); e!=INVALID; ++e) {
alpar@986
    97
    Node u=G.source(e);
alpar@986
    98
    Node v=G.target(e);
alpar@585
    99
    check( !dijkstra_test.reached(u) ||
alpar@585
   100
	   (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]),
alpar@986
   101
	   "dist(target)-dist(source)- edge_length= " 
alpar@585
   102
	   << dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   103
	   - cap[e]);
alpar@585
   104
  }
alpar@585
   105
alpar@585
   106
  ///\bug This works only for integer lengths
alpar@780
   107
  for(NodeIt v(G); v!=INVALID; ++v){
alpar@780
   108
    check(dijkstra_test.reached(v),"Each node should be reached.");
alpar@780
   109
    if ( dijkstra_test.pred(v)!=INVALID ) {
alpar@585
   110
      Edge e=dijkstra_test.pred(v);
alpar@986
   111
      Node u=G.source(e);
alpar@780
   112
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
alpar@585
   113
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e],
alpar@780
   114
	    "Wrong distance! Difference: " 
alpar@585
   115
	    << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   116
			    - cap[e]));
alpar@585
   117
    }
alpar@780
   118
  }
alpar@1148
   119
alpar@1148
   120
  
alpar@1148
   121
  {
alpar@1148
   122
    NullMap<Node,Node> myPredNodeMap;
alpar@1148
   123
    dijkstra(G,cap).predNodeMap(myPredNodeMap).run(s);
alpar@1148
   124
  }
alpar@1148
   125
  return 0;
alpar@568
   126
}