src/test/dijkstra_test.cc
author alpar
Fri, 15 Apr 2005 19:56:25 +0000
changeset 1359 1581f961cfaa
parent 1283 fc20371677b9
permissions -rw-r--r--
Correct the english name of EGRES.
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@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, 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@1283
    20
#include <lemon/path.h>
alpar@1148
    21
#include <lemon/maps.h>
klao@959
    22
#include <lemon/concept/graph.h>
klao@959
    23
#include <lemon/concept/maps.h>
alpar@921
    24
using namespace lemon;
alpar@568
    25
alpar@568
    26
const int PET_SIZE =5;
alpar@568
    27
alpar@570
    28
alpar@793
    29
void check_Dijkstra_BinHeap_Compile() 
alpar@570
    30
{
alpar@570
    31
  typedef int VType;
klao@959
    32
  typedef concept::StaticGraph Graph;
alpar@570
    33
alpar@570
    34
  typedef Graph::Edge Edge;
alpar@570
    35
  typedef Graph::Node Node;
alpar@570
    36
  typedef Graph::EdgeIt EdgeIt;
alpar@570
    37
  typedef Graph::NodeIt NodeIt;
klao@959
    38
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@570
    39
 
alpar@570
    40
  typedef Dijkstra<Graph, LengthMap> DType;
alpar@570
    41
  
alpar@570
    42
  Graph G;
alpar@570
    43
  Node n;
alpar@570
    44
  Edge e;
alpar@570
    45
  VType l;
alpar@570
    46
  bool b;
alpar@570
    47
  DType::DistMap d(G);
alpar@570
    48
  DType::PredMap p(G);
alpar@1148
    49
  //  DType::PredNodeMap pn(G);
alpar@793
    50
  LengthMap cap;
alpar@570
    51
alpar@570
    52
  DType dijkstra_test(G,cap);
alpar@570
    53
alpar@570
    54
  dijkstra_test.run(n);
alpar@570
    55
alpar@570
    56
  l  = dijkstra_test.dist(n);
alpar@570
    57
  e  = dijkstra_test.pred(n);
alpar@570
    58
  n  = dijkstra_test.predNode(n);
alpar@570
    59
  d  = dijkstra_test.distMap();
alpar@570
    60
  p  = dijkstra_test.predMap();
alpar@1148
    61
  //  pn = dijkstra_test.predNodeMap();
alpar@570
    62
  b  = dijkstra_test.reached(n);
alpar@1283
    63
alpar@1283
    64
  DirPath<Graph> pp(G);
alpar@1283
    65
  dijkstra_test.getPath(pp,n);
alpar@570
    66
}
alpar@570
    67
alpar@1220
    68
void check_Dijkstra_Function_Compile() 
alpar@1220
    69
{
alpar@1220
    70
  typedef int VType;
alpar@1220
    71
  typedef concept::StaticGraph Graph;
alpar@1220
    72
alpar@1220
    73
  typedef Graph::Edge Edge;
alpar@1220
    74
  typedef Graph::Node Node;
alpar@1220
    75
  typedef Graph::EdgeIt EdgeIt;
alpar@1220
    76
  typedef Graph::NodeIt NodeIt;
alpar@1220
    77
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@1220
    78
   
alpar@1220
    79
  dijkstra(Graph(),LengthMap(),Node()).run();
alpar@1220
    80
  dijkstra(Graph(),LengthMap()).source(Node()).run();
alpar@1220
    81
  dijkstra(Graph(),LengthMap())
alpar@1220
    82
    .predMap(concept::WriteMap<Node,Edge>())
alpar@1220
    83
    .distMap(concept::WriteMap<Node,VType>())
alpar@1220
    84
    .run(Node());
alpar@1220
    85
  
alpar@1220
    86
}
alpar@1220
    87
alpar@1220
    88
alpar@568
    89
int main()
alpar@568
    90
{
alpar@568
    91
    
alpar@568
    92
  typedef SmartGraph Graph;
alpar@568
    93
alpar@568
    94
  typedef Graph::Edge Edge;
alpar@568
    95
  typedef Graph::Node Node;
alpar@568
    96
  typedef Graph::EdgeIt EdgeIt;
alpar@568
    97
  typedef Graph::NodeIt NodeIt;
alpar@568
    98
  typedef Graph::EdgeMap<int> LengthMap;
alpar@568
    99
alpar@568
   100
  Graph G;
alpar@568
   101
  Node s, t;
alpar@568
   102
  LengthMap cap(G);
alpar@568
   103
  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
alpar@578
   104
   
alpar@568
   105
  for(int i=0;i<PET_SIZE;i++) {
alpar@568
   106
    cap[ps.outcir[i]]=4;
alpar@568
   107
    cap[ps.incir[i]]=1;
alpar@568
   108
    cap[ps.chords[i]]=10;
alpar@568
   109
  }
alpar@568
   110
  s=ps.outer[0];
alpar@568
   111
  t=ps.inner[1];
alpar@568
   112
  
alpar@568
   113
  Dijkstra<Graph, LengthMap> 
alpar@568
   114
	dijkstra_test(G, cap);
alpar@568
   115
  dijkstra_test.run(s);
alpar@568
   116
  
alpar@568
   117
  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
alpar@585
   118
alpar@585
   119
alpar@1283
   120
  DirPath<Graph> p(G);
alpar@1283
   121
  check(dijkstra_test.getPath(p,t),"getPath() failed to set the path.");
alpar@1283
   122
  check(p.length()==4,"getPath() found a wrong path.");
alpar@1283
   123
  
alpar@1283
   124
hegyi@776
   125
  for(EdgeIt e(G); e!=INVALID; ++e) {
alpar@986
   126
    Node u=G.source(e);
alpar@986
   127
    Node v=G.target(e);
alpar@585
   128
    check( !dijkstra_test.reached(u) ||
alpar@585
   129
	   (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]),
alpar@986
   130
	   "dist(target)-dist(source)- edge_length= " 
alpar@585
   131
	   << dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   132
	   - cap[e]);
alpar@585
   133
  }
alpar@585
   134
alpar@585
   135
  ///\bug This works only for integer lengths
alpar@780
   136
  for(NodeIt v(G); v!=INVALID; ++v){
alpar@780
   137
    check(dijkstra_test.reached(v),"Each node should be reached.");
alpar@780
   138
    if ( dijkstra_test.pred(v)!=INVALID ) {
alpar@585
   139
      Edge e=dijkstra_test.pred(v);
alpar@986
   140
      Node u=G.source(e);
alpar@780
   141
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
alpar@585
   142
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e],
alpar@780
   143
	    "Wrong distance! Difference: " 
alpar@585
   144
	    << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   145
			    - cap[e]));
alpar@585
   146
    }
alpar@780
   147
  }
alpar@1148
   148
alpar@1148
   149
  
alpar@1148
   150
  {
alpar@1218
   151
    NullMap<Node,Edge> myPredMap;
alpar@1218
   152
    dijkstra(G,cap).predMap(myPredMap).run(s);
alpar@1148
   153
  }
alpar@1148
   154
  return 0;
alpar@568
   155
}