test/dijkstra_test.cc
author kpeter
Thu, 28 Feb 2008 02:58:26 +0000
changeset 2584 84ef3c5b3698
parent 2391 14a343be7a5a
permissions -rw-r--r--
Test file for the min cost flow algorithms.
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@2553
     5
 * Copyright (C) 2003-2008
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@2335
    66
  Path<Graph> pp = dijkstra_test.path(n);
alpar@570
    67
}
alpar@570
    68
alpar@1220
    69
void check_Dijkstra_Function_Compile() 
alpar@1220
    70
{
alpar@1220
    71
  typedef int VType;
alpar@2260
    72
  typedef concepts::Graph Graph;
alpar@1220
    73
alpar@1220
    74
  typedef Graph::Edge Edge;
alpar@1220
    75
  typedef Graph::Node Node;
alpar@1220
    76
  typedef Graph::EdgeIt EdgeIt;
alpar@1220
    77
  typedef Graph::NodeIt NodeIt;
alpar@2260
    78
  typedef concepts::ReadMap<Edge,VType> LengthMap;
alpar@1220
    79
   
alpar@2135
    80
  Graph g;
alpar@2135
    81
  dijkstra(g,LengthMap(),Node()).run();
alpar@2135
    82
  dijkstra(g,LengthMap()).source(Node()).run();
alpar@2135
    83
  dijkstra(g,LengthMap())
alpar@2260
    84
    .predMap(concepts::WriteMap<Node,Edge>())
alpar@2260
    85
    .distMap(concepts::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
deba@2335
   122
  Path<Graph> p = dijkstra_test.path(t);
alpar@1283
   123
  check(p.length()==4,"getPath() found a wrong path.");
deba@2335
   124
  check(checkPath(G, p),"path() found a wrong path.");
deba@2335
   125
  check(pathSource(G, p) == s,"path() found a wrong path.");
deba@2335
   126
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@1283
   127
  
alpar@1283
   128
hegyi@776
   129
  for(EdgeIt e(G); e!=INVALID; ++e) {
alpar@986
   130
    Node u=G.source(e);
alpar@986
   131
    Node v=G.target(e);
alpar@585
   132
    check( !dijkstra_test.reached(u) ||
alpar@585
   133
	   (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]),
alpar@986
   134
	   "dist(target)-dist(source)- edge_length= " 
alpar@585
   135
	   << dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   136
	   - cap[e]);
alpar@585
   137
  }
alpar@585
   138
alpar@585
   139
  ///\bug This works only for integer lengths
alpar@780
   140
  for(NodeIt v(G); v!=INVALID; ++v){
alpar@780
   141
    check(dijkstra_test.reached(v),"Each node should be reached.");
deba@1763
   142
    if ( dijkstra_test.predEdge(v)!=INVALID ) {
deba@1763
   143
      Edge e=dijkstra_test.predEdge(v);
alpar@986
   144
      Node u=G.source(e);
alpar@780
   145
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
alpar@585
   146
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e],
alpar@780
   147
	    "Wrong distance! Difference: " 
alpar@585
   148
	    << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   149
			    - cap[e]));
alpar@585
   150
    }
alpar@780
   151
  }
alpar@1148
   152
alpar@1148
   153
  
alpar@1148
   154
  {
alpar@1218
   155
    NullMap<Node,Edge> myPredMap;
alpar@1218
   156
    dijkstra(G,cap).predMap(myPredMap).run(s);
alpar@1148
   157
  }
alpar@1148
   158
  return 0;
alpar@568
   159
}