test/dijkstra_test.cc
author alpar
Mon, 30 Oct 2006 16:19:18 +0000
changeset 2274 432d0469a87e
parent 2247 269a0dcee70b
child 2296 02088c3c0d14
permissions -rw-r--r--
Turn off 32 bit only tests, cont'd.
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>
alpar@2260
    24
#include <lemon/concepts/graph.h>
alpar@2260
    25
#include <lemon/concepts/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;
alpar@2260
    34
  typedef concepts::Graph 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;
alpar@2260
    40
  typedef concepts::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
deba@2247
    66
  Path<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@2260
    73
  typedef concepts::Graph 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@2260
    79
  typedef concepts::ReadMap<Edge,VType> LengthMap;
alpar@1220
    80
   
alpar@2135
    81
  Graph g;
alpar@2135
    82
  dijkstra(g,LengthMap(),Node()).run();
alpar@2135
    83
  dijkstra(g,LengthMap()).source(Node()).run();
alpar@2135
    84
  dijkstra(g,LengthMap())
alpar@2260
    85
    .predMap(concepts::WriteMap<Node,Edge>())
alpar@2260
    86
    .distMap(concepts::WriteMap<Node,VType>())
alpar@1220
    87
    .run(Node());
alpar@1220
    88
  
alpar@1220
    89
}
alpar@1220
    90
alpar@1220
    91
alpar@568
    92
int main()
alpar@568
    93
{
alpar@568
    94
    
alpar@568
    95
  typedef SmartGraph Graph;
alpar@568
    96
alpar@568
    97
  typedef Graph::Edge Edge;
alpar@568
    98
  typedef Graph::Node Node;
alpar@568
    99
  typedef Graph::EdgeIt EdgeIt;
alpar@568
   100
  typedef Graph::NodeIt NodeIt;
alpar@568
   101
  typedef Graph::EdgeMap<int> LengthMap;
alpar@568
   102
alpar@568
   103
  Graph G;
alpar@568
   104
  Node s, t;
alpar@568
   105
  LengthMap cap(G);
alpar@568
   106
  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
alpar@578
   107
   
alpar@568
   108
  for(int i=0;i<PET_SIZE;i++) {
alpar@568
   109
    cap[ps.outcir[i]]=4;
alpar@568
   110
    cap[ps.incir[i]]=1;
alpar@568
   111
    cap[ps.chords[i]]=10;
alpar@568
   112
  }
alpar@568
   113
  s=ps.outer[0];
alpar@568
   114
  t=ps.inner[1];
alpar@568
   115
  
alpar@568
   116
  Dijkstra<Graph, LengthMap> 
alpar@568
   117
	dijkstra_test(G, cap);
alpar@568
   118
  dijkstra_test.run(s);
alpar@568
   119
  
alpar@568
   120
  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
alpar@585
   121
alpar@585
   122
deba@2247
   123
  Path<Graph> p(G);
alpar@1283
   124
  check(dijkstra_test.getPath(p,t),"getPath() failed to set the path.");
alpar@1283
   125
  check(p.length()==4,"getPath() found a wrong path.");
alpar@1283
   126
  
alpar@1283
   127
hegyi@776
   128
  for(EdgeIt e(G); e!=INVALID; ++e) {
alpar@986
   129
    Node u=G.source(e);
alpar@986
   130
    Node v=G.target(e);
alpar@585
   131
    check( !dijkstra_test.reached(u) ||
alpar@585
   132
	   (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]),
alpar@986
   133
	   "dist(target)-dist(source)- edge_length= " 
alpar@585
   134
	   << dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   135
	   - cap[e]);
alpar@585
   136
  }
alpar@585
   137
alpar@585
   138
  ///\bug This works only for integer lengths
alpar@780
   139
  for(NodeIt v(G); v!=INVALID; ++v){
alpar@780
   140
    check(dijkstra_test.reached(v),"Each node should be reached.");
deba@1763
   141
    if ( dijkstra_test.predEdge(v)!=INVALID ) {
deba@1763
   142
      Edge e=dijkstra_test.predEdge(v);
alpar@986
   143
      Node u=G.source(e);
alpar@780
   144
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
alpar@585
   145
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e],
alpar@780
   146
	    "Wrong distance! Difference: " 
alpar@585
   147
	    << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   148
			    - cap[e]));
alpar@585
   149
    }
alpar@780
   150
  }
alpar@1148
   151
alpar@1148
   152
  
alpar@1148
   153
  {
alpar@1218
   154
    NullMap<Node,Edge> myPredMap;
alpar@1218
   155
    dijkstra(G,cap).predMap(myPredMap).run(s);
alpar@1148
   156
  }
alpar@1148
   157
  return 0;
alpar@568
   158
}