test/dijkstra_test.cc
author Balazs Dezso <deba@inf.elte.hu>
Thu, 10 Jul 2008 16:05:56 +0200
changeset 200 c0e2c043c060
parent 170 91fb4372688f
child 209 765619b7cbb2
permissions -rw-r--r--
Porting graph_copy_test.cc from SVN 3498
kpeter@170
     1
/* -*- C++ -*-
kpeter@170
     2
 *
kpeter@170
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@170
     4
 *
kpeter@170
     5
 * Copyright (C) 2003-2008
kpeter@170
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@170
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@170
     8
 *
kpeter@170
     9
 * Permission to use, modify and distribute this software is granted
kpeter@170
    10
 * provided that this copyright notice appears in all copies. For
kpeter@170
    11
 * precise terms see the accompanying LICENSE file.
kpeter@170
    12
 *
kpeter@170
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@170
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@170
    15
 * purpose.
kpeter@170
    16
 *
kpeter@170
    17
 */
kpeter@170
    18
kpeter@170
    19
#include <lemon/concepts/digraph.h>
kpeter@170
    20
#include <lemon/smart_graph.h>
kpeter@170
    21
#include <lemon/list_graph.h>
kpeter@170
    22
#include <lemon/graph_utils.h>
kpeter@170
    23
#include <lemon/dijkstra.h>
kpeter@170
    24
#include <lemon/path.h>
kpeter@170
    25
kpeter@171
    26
#include "graph_test.h"
kpeter@170
    27
#include "test_tools.h"
kpeter@170
    28
kpeter@170
    29
using namespace lemon;
kpeter@170
    30
kpeter@170
    31
void checkDijkstraCompile() 
kpeter@170
    32
{
kpeter@170
    33
  typedef int VType;
kpeter@170
    34
  typedef concepts::Digraph Digraph;
kpeter@170
    35
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
kpeter@170
    36
  typedef Dijkstra<Digraph, LengthMap> DType;
kpeter@170
    37
  
kpeter@170
    38
  Digraph G;
kpeter@170
    39
  Digraph::Node n;
kpeter@170
    40
  Digraph::Arc e;
kpeter@170
    41
  VType l;
kpeter@170
    42
  bool b;
kpeter@170
    43
  DType::DistMap d(G);
kpeter@170
    44
  DType::PredMap p(G);
kpeter@170
    45
  //  DType::PredNodeMap pn(G);
kpeter@170
    46
  LengthMap length;
kpeter@170
    47
kpeter@170
    48
  DType dijkstra_test(G,length);
kpeter@170
    49
kpeter@170
    50
  dijkstra_test.run(n);
kpeter@170
    51
kpeter@170
    52
  l  = dijkstra_test.dist(n);
kpeter@170
    53
  e  = dijkstra_test.predArc(n);
kpeter@170
    54
  n  = dijkstra_test.predNode(n);
kpeter@170
    55
  d  = dijkstra_test.distMap();
kpeter@170
    56
  p  = dijkstra_test.predMap();
kpeter@170
    57
  //  pn = dijkstra_test.predNodeMap();
kpeter@170
    58
  b  = dijkstra_test.reached(n);
kpeter@170
    59
kpeter@170
    60
  Path<Digraph> pp = dijkstra_test.path(n);
kpeter@170
    61
}
kpeter@170
    62
kpeter@170
    63
void checkDijkstraFunctionCompile() 
kpeter@170
    64
{
kpeter@170
    65
  typedef int VType;
kpeter@170
    66
  typedef concepts::Digraph Digraph;
kpeter@170
    67
  typedef Digraph::Arc Arc;
kpeter@170
    68
  typedef Digraph::Node Node;
kpeter@170
    69
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
kpeter@170
    70
   
kpeter@170
    71
  Digraph g;
kpeter@170
    72
  dijkstra(g,LengthMap(),Node()).run();
kpeter@170
    73
  dijkstra(g,LengthMap()).source(Node()).run();
kpeter@170
    74
  dijkstra(g,LengthMap())
kpeter@170
    75
    .predMap(concepts::WriteMap<Node,Arc>())
kpeter@170
    76
    .distMap(concepts::WriteMap<Node,VType>())
kpeter@170
    77
    .run(Node());
kpeter@170
    78
}
kpeter@170
    79
kpeter@170
    80
template <class Digraph>
kpeter@170
    81
void checkDijkstra() {    
kpeter@170
    82
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@170
    83
  typedef typename Digraph::template ArcMap<int> LengthMap;
kpeter@170
    84
kpeter@170
    85
  Digraph G;
kpeter@170
    86
  Node s, t;
kpeter@170
    87
  LengthMap length(G);
kpeter@170
    88
  PetStruct<Digraph> ps = addPetersen(G, 5);
kpeter@170
    89
   
kpeter@170
    90
  for(int i=0;i<5;i++) {
kpeter@170
    91
    length[ps.outcir[i]]=4;
kpeter@170
    92
    length[ps.incir[i]]=1;
kpeter@170
    93
    length[ps.chords[i]]=10;
kpeter@170
    94
  }
kpeter@170
    95
  s=ps.outer[0];
kpeter@170
    96
  t=ps.inner[1];
kpeter@170
    97
  
kpeter@170
    98
  Dijkstra<Digraph, LengthMap> 
kpeter@170
    99
	dijkstra_test(G, length);
kpeter@170
   100
  dijkstra_test.run(s);
kpeter@170
   101
  
kpeter@170
   102
  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
kpeter@170
   103
kpeter@170
   104
  Path<Digraph> p = dijkstra_test.path(t);
kpeter@170
   105
  check(p.length()==4,"getPath() found a wrong path.");
kpeter@170
   106
  check(checkPath(G, p),"path() found a wrong path.");
kpeter@170
   107
  check(pathSource(G, p) == s,"path() found a wrong path.");
kpeter@170
   108
  check(pathTarget(G, p) == t,"path() found a wrong path.");
kpeter@170
   109
  
kpeter@170
   110
  for(ArcIt e(G); e!=INVALID; ++e) {
kpeter@170
   111
    Node u=G.source(e);
kpeter@170
   112
    Node v=G.target(e);
kpeter@170
   113
    check( !dijkstra_test.reached(u) || (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
kpeter@170
   114
	   "dist(target)-dist(source)-arc_length= " << dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
kpeter@170
   115
  }
kpeter@170
   116
kpeter@170
   117
  for(NodeIt v(G); v!=INVALID; ++v){
kpeter@170
   118
    check(dijkstra_test.reached(v),"Each node should be reached.");
kpeter@170
   119
    if ( dijkstra_test.predArc(v)!=INVALID ) {
kpeter@170
   120
      Arc e=dijkstra_test.predArc(v);
kpeter@170
   121
      Node u=G.source(e);
kpeter@170
   122
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
kpeter@170
   123
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
kpeter@170
   124
	    "Wrong distance! Difference: " << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]));
kpeter@170
   125
    }
kpeter@170
   126
  }
kpeter@170
   127
  
kpeter@170
   128
  {
kpeter@170
   129
    NullMap<Node,Arc> myPredMap;
kpeter@170
   130
    dijkstra(G,length).predMap(myPredMap).run(s);
kpeter@170
   131
  }
kpeter@170
   132
}
kpeter@170
   133
kpeter@170
   134
int main() {
kpeter@170
   135
  checkDijkstra<ListDigraph>();
kpeter@170
   136
  checkDijkstra<SmartDigraph>();
kpeter@170
   137
  return 0;
kpeter@170
   138
}