test/dijkstra_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 14 Jul 2008 09:41:18 +0200
changeset 211 542dd614cbb4
parent 209 765619b7cbb2
child 220 a5d8c039f218
permissions -rw-r--r--
Small fixes in graph_to_eps_demo.cc
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@170
     2
 *
alpar@209
     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
alpar@209
    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;
alpar@209
    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
alpar@209
    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;
alpar@209
    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>
alpar@209
    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);
alpar@209
    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];
alpar@209
    97
alpar@209
    98
  Dijkstra<Digraph, LengthMap>
alpar@209
    99
        dijkstra_test(G, length);
kpeter@170
   100
  dijkstra_test.run(s);
alpar@209
   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.");
alpar@209
   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);
alpar@210
   113
    check( !dijkstra_test.reached(u) ||
alpar@210
   114
           (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
alpar@210
   115
           "dist(target)-dist(source)-arc_length= " <<
alpar@210
   116
           dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
kpeter@170
   117
  }
kpeter@170
   118
kpeter@170
   119
  for(NodeIt v(G); v!=INVALID; ++v){
kpeter@170
   120
    check(dijkstra_test.reached(v),"Each node should be reached.");
kpeter@170
   121
    if ( dijkstra_test.predArc(v)!=INVALID ) {
kpeter@170
   122
      Arc e=dijkstra_test.predArc(v);
kpeter@170
   123
      Node u=G.source(e);
kpeter@170
   124
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
kpeter@170
   125
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
alpar@210
   126
            "Wrong distance! Difference: " <<
alpar@210
   127
            std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
kpeter@170
   128
    }
kpeter@170
   129
  }
alpar@209
   130
kpeter@170
   131
  {
kpeter@170
   132
    NullMap<Node,Arc> myPredMap;
kpeter@170
   133
    dijkstra(G,length).predMap(myPredMap).run(s);
kpeter@170
   134
  }
kpeter@170
   135
}
kpeter@170
   136
kpeter@170
   137
int main() {
kpeter@170
   138
  checkDijkstra<ListDigraph>();
kpeter@170
   139
  checkDijkstra<SmartDigraph>();
kpeter@170
   140
  return 0;
kpeter@170
   141
}