test/dijkstra_test.cc
author deba
Mon, 27 Feb 2006 10:36:01 +0000
changeset 1986 9b56cca61e2e
parent 1875 98698b69a902
child 2111 ea1fa1bc3f6d
permissions -rw-r--r--
An additional simplier interface for static size graphs.
Node operator()(int) for getting node by index
int index(Node node) for getting index by node
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@578
    19
#include "test_tools.h"
alpar@921
    20
#include <lemon/smart_graph.h>
alpar@921
    21
#include <lemon/dijkstra.h>
alpar@1283
    22
#include <lemon/path.h>
alpar@1148
    23
#include <lemon/maps.h>
klao@959
    24
#include <lemon/concept/graph.h>
klao@959
    25
#include <lemon/concept/maps.h>
alpar@921
    26
using namespace lemon;
alpar@568
    27
alpar@568
    28
const int PET_SIZE =5;
alpar@568
    29
alpar@570
    30
alpar@793
    31
void check_Dijkstra_BinHeap_Compile() 
alpar@570
    32
{
alpar@570
    33
  typedef int VType;
klao@959
    34
  typedef concept::StaticGraph Graph;
alpar@570
    35
alpar@570
    36
  typedef Graph::Edge Edge;
alpar@570
    37
  typedef Graph::Node Node;
alpar@570
    38
  typedef Graph::EdgeIt EdgeIt;
alpar@570
    39
  typedef Graph::NodeIt NodeIt;
klao@959
    40
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@570
    41
 
alpar@570
    42
  typedef Dijkstra<Graph, LengthMap> DType;
alpar@570
    43
  
alpar@570
    44
  Graph G;
alpar@570
    45
  Node n;
alpar@570
    46
  Edge e;
alpar@570
    47
  VType l;
alpar@570
    48
  bool b;
alpar@570
    49
  DType::DistMap d(G);
alpar@570
    50
  DType::PredMap p(G);
alpar@1148
    51
  //  DType::PredNodeMap pn(G);
alpar@793
    52
  LengthMap cap;
alpar@570
    53
alpar@570
    54
  DType dijkstra_test(G,cap);
alpar@570
    55
alpar@570
    56
  dijkstra_test.run(n);
alpar@570
    57
alpar@570
    58
  l  = dijkstra_test.dist(n);
deba@1763
    59
  e  = dijkstra_test.predEdge(n);
alpar@570
    60
  n  = dijkstra_test.predNode(n);
alpar@570
    61
  d  = dijkstra_test.distMap();
alpar@570
    62
  p  = dijkstra_test.predMap();
alpar@1148
    63
  //  pn = dijkstra_test.predNodeMap();
alpar@570
    64
  b  = dijkstra_test.reached(n);
alpar@1283
    65
alpar@1283
    66
  DirPath<Graph> pp(G);
alpar@1283
    67
  dijkstra_test.getPath(pp,n);
alpar@570
    68
}
alpar@570
    69
alpar@1220
    70
void check_Dijkstra_Function_Compile() 
alpar@1220
    71
{
alpar@1220
    72
  typedef int VType;
alpar@1220
    73
  typedef concept::StaticGraph Graph;
alpar@1220
    74
alpar@1220
    75
  typedef Graph::Edge Edge;
alpar@1220
    76
  typedef Graph::Node Node;
alpar@1220
    77
  typedef Graph::EdgeIt EdgeIt;
alpar@1220
    78
  typedef Graph::NodeIt NodeIt;
alpar@1220
    79
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@1220
    80
   
alpar@1220
    81
  dijkstra(Graph(),LengthMap(),Node()).run();
alpar@1220
    82
  dijkstra(Graph(),LengthMap()).source(Node()).run();
alpar@1220
    83
  dijkstra(Graph(),LengthMap())
alpar@1220
    84
    .predMap(concept::WriteMap<Node,Edge>())
alpar@1220
    85
    .distMap(concept::WriteMap<Node,VType>())
alpar@1220
    86
    .run(Node());
alpar@1220
    87
  
alpar@1220
    88
}
alpar@1220
    89
alpar@1220
    90
alpar@568
    91
int main()
alpar@568
    92
{
alpar@568
    93
    
alpar@568
    94
  typedef SmartGraph Graph;
alpar@568
    95
alpar@568
    96
  typedef Graph::Edge Edge;
alpar@568
    97
  typedef Graph::Node Node;
alpar@568
    98
  typedef Graph::EdgeIt EdgeIt;
alpar@568
    99
  typedef Graph::NodeIt NodeIt;
alpar@568
   100
  typedef Graph::EdgeMap<int> LengthMap;
alpar@568
   101
alpar@568
   102
  Graph G;
alpar@568
   103
  Node s, t;
alpar@568
   104
  LengthMap cap(G);
alpar@568
   105
  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
alpar@578
   106
   
alpar@568
   107
  for(int i=0;i<PET_SIZE;i++) {
alpar@568
   108
    cap[ps.outcir[i]]=4;
alpar@568
   109
    cap[ps.incir[i]]=1;
alpar@568
   110
    cap[ps.chords[i]]=10;
alpar@568
   111
  }
alpar@568
   112
  s=ps.outer[0];
alpar@568
   113
  t=ps.inner[1];
alpar@568
   114
  
alpar@568
   115
  Dijkstra<Graph, LengthMap> 
alpar@568
   116
	dijkstra_test(G, cap);
alpar@568
   117
  dijkstra_test.run(s);
alpar@568
   118
  
alpar@568
   119
  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
alpar@585
   120
alpar@585
   121
alpar@1283
   122
  DirPath<Graph> p(G);
alpar@1283
   123
  check(dijkstra_test.getPath(p,t),"getPath() failed to set the path.");
alpar@1283
   124
  check(p.length()==4,"getPath() found a wrong path.");
alpar@1283
   125
  
alpar@1283
   126
hegyi@776
   127
  for(EdgeIt e(G); e!=INVALID; ++e) {
alpar@986
   128
    Node u=G.source(e);
alpar@986
   129
    Node v=G.target(e);
alpar@585
   130
    check( !dijkstra_test.reached(u) ||
alpar@585
   131
	   (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]),
alpar@986
   132
	   "dist(target)-dist(source)- edge_length= " 
alpar@585
   133
	   << dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   134
	   - cap[e]);
alpar@585
   135
  }
alpar@585
   136
alpar@585
   137
  ///\bug This works only for integer lengths
alpar@780
   138
  for(NodeIt v(G); v!=INVALID; ++v){
alpar@780
   139
    check(dijkstra_test.reached(v),"Each node should be reached.");
deba@1763
   140
    if ( dijkstra_test.predEdge(v)!=INVALID ) {
deba@1763
   141
      Edge e=dijkstra_test.predEdge(v);
alpar@986
   142
      Node u=G.source(e);
alpar@780
   143
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
alpar@585
   144
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e],
alpar@780
   145
	    "Wrong distance! Difference: " 
alpar@585
   146
	    << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   147
			    - cap[e]));
alpar@585
   148
    }
alpar@780
   149
  }
alpar@1148
   150
alpar@1148
   151
  
alpar@1148
   152
  {
alpar@1218
   153
    NullMap<Node,Edge> myPredMap;
alpar@1218
   154
    dijkstra(G,cap).predMap(myPredMap).run(s);
alpar@1148
   155
  }
alpar@1148
   156
  return 0;
alpar@568
   157
}