test/dijkstra_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 15 Jul 2008 18:49:30 +0100
changeset 221 64613d8fae28
parent 210 81cfc04531e8
child 228 b6732e0d38c5
permissions -rw-r--r--
Merge
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/dijkstra.h>
kpeter@170
    23
#include <lemon/path.h>
kpeter@170
    24
kpeter@171
    25
#include "graph_test.h"
kpeter@170
    26
#include "test_tools.h"
kpeter@170
    27
kpeter@170
    28
using namespace lemon;
kpeter@170
    29
alpar@209
    30
void checkDijkstraCompile()
kpeter@170
    31
{
kpeter@170
    32
  typedef int VType;
kpeter@170
    33
  typedef concepts::Digraph Digraph;
kpeter@170
    34
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
kpeter@170
    35
  typedef Dijkstra<Digraph, LengthMap> DType;
alpar@209
    36
kpeter@170
    37
  Digraph G;
kpeter@170
    38
  Digraph::Node n;
kpeter@170
    39
  Digraph::Arc e;
kpeter@170
    40
  VType l;
kpeter@170
    41
  bool b;
kpeter@170
    42
  DType::DistMap d(G);
kpeter@170
    43
  DType::PredMap p(G);
kpeter@170
    44
  //  DType::PredNodeMap pn(G);
kpeter@170
    45
  LengthMap length;
kpeter@170
    46
kpeter@170
    47
  DType dijkstra_test(G,length);
kpeter@170
    48
kpeter@170
    49
  dijkstra_test.run(n);
kpeter@170
    50
kpeter@170
    51
  l  = dijkstra_test.dist(n);
kpeter@170
    52
  e  = dijkstra_test.predArc(n);
kpeter@170
    53
  n  = dijkstra_test.predNode(n);
kpeter@170
    54
  d  = dijkstra_test.distMap();
kpeter@170
    55
  p  = dijkstra_test.predMap();
kpeter@170
    56
  //  pn = dijkstra_test.predNodeMap();
kpeter@170
    57
  b  = dijkstra_test.reached(n);
kpeter@170
    58
kpeter@170
    59
  Path<Digraph> pp = dijkstra_test.path(n);
kpeter@170
    60
}
kpeter@170
    61
alpar@209
    62
void checkDijkstraFunctionCompile()
kpeter@170
    63
{
kpeter@170
    64
  typedef int VType;
kpeter@170
    65
  typedef concepts::Digraph Digraph;
kpeter@170
    66
  typedef Digraph::Arc Arc;
kpeter@170
    67
  typedef Digraph::Node Node;
kpeter@170
    68
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
alpar@209
    69
kpeter@170
    70
  Digraph g;
kpeter@170
    71
  dijkstra(g,LengthMap(),Node()).run();
kpeter@170
    72
  dijkstra(g,LengthMap()).source(Node()).run();
kpeter@170
    73
  dijkstra(g,LengthMap())
kpeter@170
    74
    .predMap(concepts::WriteMap<Node,Arc>())
kpeter@170
    75
    .distMap(concepts::WriteMap<Node,VType>())
kpeter@170
    76
    .run(Node());
kpeter@170
    77
}
kpeter@170
    78
kpeter@170
    79
template <class Digraph>
alpar@209
    80
void checkDijkstra() {
kpeter@170
    81
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@170
    82
  typedef typename Digraph::template ArcMap<int> LengthMap;
kpeter@170
    83
kpeter@170
    84
  Digraph G;
kpeter@170
    85
  Node s, t;
kpeter@170
    86
  LengthMap length(G);
kpeter@170
    87
  PetStruct<Digraph> ps = addPetersen(G, 5);
alpar@209
    88
kpeter@170
    89
  for(int i=0;i<5;i++) {
kpeter@170
    90
    length[ps.outcir[i]]=4;
kpeter@170
    91
    length[ps.incir[i]]=1;
kpeter@170
    92
    length[ps.chords[i]]=10;
kpeter@170
    93
  }
kpeter@170
    94
  s=ps.outer[0];
kpeter@170
    95
  t=ps.inner[1];
alpar@209
    96
alpar@209
    97
  Dijkstra<Digraph, LengthMap>
alpar@209
    98
        dijkstra_test(G, length);
kpeter@170
    99
  dijkstra_test.run(s);
alpar@209
   100
kpeter@170
   101
  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
kpeter@170
   102
kpeter@170
   103
  Path<Digraph> p = dijkstra_test.path(t);
kpeter@170
   104
  check(p.length()==4,"getPath() found a wrong path.");
kpeter@170
   105
  check(checkPath(G, p),"path() found a wrong path.");
kpeter@170
   106
  check(pathSource(G, p) == s,"path() found a wrong path.");
kpeter@170
   107
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209
   108
kpeter@170
   109
  for(ArcIt e(G); e!=INVALID; ++e) {
kpeter@170
   110
    Node u=G.source(e);
kpeter@170
   111
    Node v=G.target(e);
alpar@210
   112
    check( !dijkstra_test.reached(u) ||
alpar@210
   113
           (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
alpar@210
   114
           "dist(target)-dist(source)-arc_length= " <<
alpar@210
   115
           dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
kpeter@170
   116
  }
kpeter@170
   117
kpeter@170
   118
  for(NodeIt v(G); v!=INVALID; ++v){
kpeter@170
   119
    check(dijkstra_test.reached(v),"Each node should be reached.");
kpeter@170
   120
    if ( dijkstra_test.predArc(v)!=INVALID ) {
kpeter@170
   121
      Arc e=dijkstra_test.predArc(v);
kpeter@170
   122
      Node u=G.source(e);
kpeter@170
   123
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
kpeter@170
   124
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
alpar@210
   125
            "Wrong distance! Difference: " <<
alpar@210
   126
            std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
kpeter@170
   127
    }
kpeter@170
   128
  }
alpar@209
   129
kpeter@170
   130
  {
kpeter@170
   131
    NullMap<Node,Arc> myPredMap;
kpeter@170
   132
    dijkstra(G,length).predMap(myPredMap).run(s);
kpeter@170
   133
  }
kpeter@170
   134
}
kpeter@170
   135
kpeter@170
   136
int main() {
kpeter@170
   137
  checkDijkstra<ListDigraph>();
kpeter@170
   138
  checkDijkstra<SmartDigraph>();
kpeter@170
   139
  return 0;
kpeter@170
   140
}