test/dijkstra_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Sat, 27 Sep 2008 14:04:27 +0200
changeset 284 a16cc721259e
parent 228 b6732e0d38c5
child 286 da414906fe21
permissions -rw-r--r--
Make chg-len.py independent from the global Mercurial config files
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>
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
deba@228
    31
char test_lgf[] =
deba@228
    32
  "@nodes\n"
deba@228
    33
  "label\n"
deba@228
    34
  "0\n"
deba@228
    35
  "1\n"
deba@228
    36
  "2\n"
deba@228
    37
  "3\n"
deba@228
    38
  "4\n"
deba@228
    39
  "@arcs\n"
deba@228
    40
  "     label length\n"
deba@228
    41
  "0 1  0     1\n"
deba@228
    42
  "1 2  1     1\n"
deba@228
    43
  "2 3  2     1\n"
deba@228
    44
  "0 3  4     5\n"
deba@228
    45
  "0 3  5     10\n"
deba@228
    46
  "0 3  6     7\n"
deba@228
    47
  "4 2  7     1\n"
deba@228
    48
  "@attributes\n"
deba@228
    49
  "source 0\n"
deba@228
    50
  "target 3\n";
deba@228
    51
alpar@209
    52
void checkDijkstraCompile()
kpeter@170
    53
{
kpeter@170
    54
  typedef int VType;
kpeter@170
    55
  typedef concepts::Digraph Digraph;
kpeter@170
    56
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
kpeter@170
    57
  typedef Dijkstra<Digraph, LengthMap> DType;
alpar@209
    58
kpeter@170
    59
  Digraph G;
kpeter@170
    60
  Digraph::Node n;
kpeter@170
    61
  Digraph::Arc e;
kpeter@170
    62
  VType l;
kpeter@170
    63
  bool b;
kpeter@170
    64
  DType::DistMap d(G);
kpeter@170
    65
  DType::PredMap p(G);
kpeter@170
    66
  LengthMap length;
kpeter@170
    67
kpeter@170
    68
  DType dijkstra_test(G,length);
kpeter@170
    69
kpeter@170
    70
  dijkstra_test.run(n);
kpeter@170
    71
kpeter@170
    72
  l  = dijkstra_test.dist(n);
kpeter@170
    73
  e  = dijkstra_test.predArc(n);
kpeter@170
    74
  n  = dijkstra_test.predNode(n);
kpeter@170
    75
  d  = dijkstra_test.distMap();
kpeter@170
    76
  p  = dijkstra_test.predMap();
kpeter@170
    77
  b  = dijkstra_test.reached(n);
kpeter@170
    78
kpeter@170
    79
  Path<Digraph> pp = dijkstra_test.path(n);
kpeter@170
    80
}
kpeter@170
    81
alpar@209
    82
void checkDijkstraFunctionCompile()
kpeter@170
    83
{
kpeter@170
    84
  typedef int VType;
kpeter@170
    85
  typedef concepts::Digraph Digraph;
kpeter@170
    86
  typedef Digraph::Arc Arc;
kpeter@170
    87
  typedef Digraph::Node Node;
kpeter@170
    88
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
alpar@209
    89
kpeter@170
    90
  Digraph g;
kpeter@278
    91
  bool b;
kpeter@278
    92
  dijkstra(g,LengthMap()).run(Node());
kpeter@278
    93
  b=dijkstra(g,LengthMap()).run(Node(),Node());
kpeter@170
    94
  dijkstra(g,LengthMap())
kpeter@278
    95
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
    96
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
    97
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@170
    98
    .run(Node());
kpeter@278
    99
  b=dijkstra(g,LengthMap())
kpeter@278
   100
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   101
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   102
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   103
    .path(concepts::Path<Digraph>())
kpeter@278
   104
    .dist(VType())
kpeter@278
   105
    .run(Node(),Node());
kpeter@170
   106
}
kpeter@170
   107
kpeter@170
   108
template <class Digraph>
alpar@209
   109
void checkDijkstra() {
kpeter@170
   110
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@170
   111
  typedef typename Digraph::template ArcMap<int> LengthMap;
kpeter@170
   112
kpeter@170
   113
  Digraph G;
kpeter@170
   114
  Node s, t;
kpeter@170
   115
  LengthMap length(G);
alpar@209
   116
deba@228
   117
  std::istringstream input(test_lgf);
deba@228
   118
  digraphReader(input, G).
deba@228
   119
    arcMap("length", length).
deba@228
   120
    node("source", s).
deba@228
   121
    node("target", t).
deba@228
   122
    run();
alpar@209
   123
alpar@209
   124
  Dijkstra<Digraph, LengthMap>
alpar@209
   125
        dijkstra_test(G, length);
kpeter@170
   126
  dijkstra_test.run(s);
alpar@209
   127
deba@228
   128
  check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
kpeter@170
   129
kpeter@170
   130
  Path<Digraph> p = dijkstra_test.path(t);
kpeter@278
   131
  check(p.length()==3,"path() found a wrong path.");
kpeter@170
   132
  check(checkPath(G, p),"path() found a wrong path.");
kpeter@170
   133
  check(pathSource(G, p) == s,"path() found a wrong path.");
kpeter@170
   134
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209
   135
kpeter@170
   136
  for(ArcIt e(G); e!=INVALID; ++e) {
kpeter@170
   137
    Node u=G.source(e);
kpeter@170
   138
    Node v=G.target(e);
alpar@210
   139
    check( !dijkstra_test.reached(u) ||
alpar@210
   140
           (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
kpeter@278
   141
           "Wrong output. dist(target)-dist(source)-arc_length=" <<
alpar@210
   142
           dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
kpeter@170
   143
  }
kpeter@170
   144
deba@228
   145
  for(NodeIt v(G); v!=INVALID; ++v) {
deba@228
   146
    if (dijkstra_test.reached(v)) {
deba@228
   147
      check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree.");
deba@228
   148
      if (dijkstra_test.predArc(v)!=INVALID ) {
deba@228
   149
        Arc e=dijkstra_test.predArc(v);
deba@228
   150
        Node u=G.source(e);
deba@228
   151
        check(u==dijkstra_test.predNode(v),"Wrong tree.");
deba@228
   152
        check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
deba@228
   153
              "Wrong distance! Difference: " <<
deba@228
   154
              std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
deba@228
   155
      }
kpeter@170
   156
    }
kpeter@170
   157
  }
alpar@209
   158
kpeter@170
   159
  {
kpeter@170
   160
    NullMap<Node,Arc> myPredMap;
kpeter@170
   161
    dijkstra(G,length).predMap(myPredMap).run(s);
kpeter@170
   162
  }
kpeter@170
   163
}
kpeter@170
   164
kpeter@170
   165
int main() {
kpeter@170
   166
  checkDijkstra<ListDigraph>();
kpeter@170
   167
  checkDijkstra<SmartDigraph>();
kpeter@170
   168
  return 0;
kpeter@170
   169
}