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