test/dijkstra_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Wed, 27 Aug 2008 10:50:04 +0200
changeset 250 d0aae16df1bb
parent 220 a5d8c039f218
child 278 931190050520
permissions -rw-r--r--
Stream operators for Point and BoundingBox classes (ticket #126)
- Add operator<< and operator>> for BoundingBox.
- operator<< of Point gives space-less output.
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>
deba@228
    22
#include <lemon/lgf_reader.h>
deba@228
    23
kpeter@170
    24
#include <lemon/dijkstra.h>
kpeter@170
    25
#include <lemon/path.h>
kpeter@170
    26
kpeter@171
    27
#include "graph_test.h"
kpeter@170
    28
#include "test_tools.h"
kpeter@170
    29
kpeter@170
    30
using namespace lemon;
kpeter@170
    31
deba@228
    32
char test_lgf[] =
deba@228
    33
  "@nodes\n"
deba@228
    34
  "label\n"
deba@228
    35
  "0\n"
deba@228
    36
  "1\n"
deba@228
    37
  "2\n"
deba@228
    38
  "3\n"
deba@228
    39
  "4\n"
deba@228
    40
  "@arcs\n"
deba@228
    41
  "     label length\n"
deba@228
    42
  "0 1  0     1\n"
deba@228
    43
  "1 2  1     1\n"
deba@228
    44
  "2 3  2     1\n"
deba@228
    45
  "0 3  4     5\n"
deba@228
    46
  "0 3  5     10\n"
deba@228
    47
  "0 3  6     7\n"
deba@228
    48
  "4 2  7     1\n"
deba@228
    49
  "@attributes\n"
deba@228
    50
  "source 0\n"
deba@228
    51
  "target 3\n";
deba@228
    52
alpar@209
    53
void checkDijkstraCompile()
kpeter@170
    54
{
kpeter@170
    55
  typedef int VType;
kpeter@170
    56
  typedef concepts::Digraph Digraph;
kpeter@170
    57
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
kpeter@170
    58
  typedef Dijkstra<Digraph, LengthMap> DType;
alpar@209
    59
kpeter@170
    60
  Digraph G;
kpeter@170
    61
  Digraph::Node n;
kpeter@170
    62
  Digraph::Arc e;
kpeter@170
    63
  VType l;
kpeter@170
    64
  bool b;
kpeter@170
    65
  DType::DistMap d(G);
kpeter@170
    66
  DType::PredMap p(G);
kpeter@170
    67
  //  DType::PredNodeMap pn(G);
kpeter@170
    68
  LengthMap length;
kpeter@170
    69
kpeter@170
    70
  DType dijkstra_test(G,length);
kpeter@170
    71
kpeter@170
    72
  dijkstra_test.run(n);
kpeter@170
    73
kpeter@170
    74
  l  = dijkstra_test.dist(n);
kpeter@170
    75
  e  = dijkstra_test.predArc(n);
kpeter@170
    76
  n  = dijkstra_test.predNode(n);
kpeter@170
    77
  d  = dijkstra_test.distMap();
kpeter@170
    78
  p  = dijkstra_test.predMap();
kpeter@170
    79
  //  pn = dijkstra_test.predNodeMap();
kpeter@170
    80
  b  = dijkstra_test.reached(n);
kpeter@170
    81
kpeter@170
    82
  Path<Digraph> pp = dijkstra_test.path(n);
kpeter@170
    83
}
kpeter@170
    84
alpar@209
    85
void checkDijkstraFunctionCompile()
kpeter@170
    86
{
kpeter@170
    87
  typedef int VType;
kpeter@170
    88
  typedef concepts::Digraph Digraph;
kpeter@170
    89
  typedef Digraph::Arc Arc;
kpeter@170
    90
  typedef Digraph::Node Node;
kpeter@170
    91
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
alpar@209
    92
kpeter@170
    93
  Digraph g;
kpeter@170
    94
  dijkstra(g,LengthMap(),Node()).run();
kpeter@170
    95
  dijkstra(g,LengthMap()).source(Node()).run();
kpeter@170
    96
  dijkstra(g,LengthMap())
kpeter@170
    97
    .predMap(concepts::WriteMap<Node,Arc>())
kpeter@170
    98
    .distMap(concepts::WriteMap<Node,VType>())
kpeter@170
    99
    .run(Node());
kpeter@170
   100
}
kpeter@170
   101
kpeter@170
   102
template <class Digraph>
alpar@209
   103
void checkDijkstra() {
kpeter@170
   104
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@170
   105
  typedef typename Digraph::template ArcMap<int> LengthMap;
kpeter@170
   106
kpeter@170
   107
  Digraph G;
kpeter@170
   108
  Node s, t;
kpeter@170
   109
  LengthMap length(G);
alpar@209
   110
deba@228
   111
  std::istringstream input(test_lgf);
deba@228
   112
  digraphReader(input, G).
deba@228
   113
    arcMap("length", length).
deba@228
   114
    node("source", s).
deba@228
   115
    node("target", t).
deba@228
   116
    run();
alpar@209
   117
alpar@209
   118
  Dijkstra<Digraph, LengthMap>
alpar@209
   119
        dijkstra_test(G, length);
kpeter@170
   120
  dijkstra_test.run(s);
alpar@209
   121
deba@228
   122
  check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
kpeter@170
   123
kpeter@170
   124
  Path<Digraph> p = dijkstra_test.path(t);
deba@228
   125
  check(p.length()==3,"getPath() found a wrong path.");
kpeter@170
   126
  check(checkPath(G, p),"path() found a wrong path.");
kpeter@170
   127
  check(pathSource(G, p) == s,"path() found a wrong path.");
kpeter@170
   128
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209
   129
kpeter@170
   130
  for(ArcIt e(G); e!=INVALID; ++e) {
kpeter@170
   131
    Node u=G.source(e);
kpeter@170
   132
    Node v=G.target(e);
alpar@210
   133
    check( !dijkstra_test.reached(u) ||
alpar@210
   134
           (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
alpar@210
   135
           "dist(target)-dist(source)-arc_length= " <<
alpar@210
   136
           dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
kpeter@170
   137
  }
kpeter@170
   138
deba@228
   139
  for(NodeIt v(G); v!=INVALID; ++v) {
deba@228
   140
    if (dijkstra_test.reached(v)) {
deba@228
   141
      check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree.");
deba@228
   142
      if (dijkstra_test.predArc(v)!=INVALID ) {
deba@228
   143
        Arc e=dijkstra_test.predArc(v);
deba@228
   144
        Node u=G.source(e);
deba@228
   145
        check(u==dijkstra_test.predNode(v),"Wrong tree.");
deba@228
   146
        check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
deba@228
   147
              "Wrong distance! Difference: " <<
deba@228
   148
              std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
deba@228
   149
      }
kpeter@170
   150
    }
kpeter@170
   151
  }
alpar@209
   152
kpeter@170
   153
  {
kpeter@170
   154
    NullMap<Node,Arc> myPredMap;
kpeter@170
   155
    dijkstra(G,length).predMap(myPredMap).run(s);
kpeter@170
   156
  }
kpeter@170
   157
}
kpeter@170
   158
kpeter@170
   159
int main() {
kpeter@170
   160
  checkDijkstra<ListDigraph>();
kpeter@170
   161
  checkDijkstra<SmartDigraph>();
kpeter@170
   162
  return 0;
kpeter@170
   163
}