test/bellman_ford_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 23 Mar 2018 15:37:23 +0100
changeset 1387 50b7e16a135a
parent 1337 4add05447ca0
permissions -rw-r--r--
Merge bugfix #609
kpeter@745
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@745
     2
 *
kpeter@745
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@745
     4
 *
alpar@1270
     5
 * Copyright (C) 2003-2013
kpeter@745
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@745
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@745
     8
 *
kpeter@745
     9
 * Permission to use, modify and distribute this software is granted
kpeter@745
    10
 * provided that this copyright notice appears in all copies. For
kpeter@745
    11
 * precise terms see the accompanying LICENSE file.
kpeter@745
    12
 *
kpeter@745
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@745
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@745
    15
 * purpose.
kpeter@745
    16
 *
kpeter@745
    17
 */
kpeter@745
    18
kpeter@745
    19
#include <lemon/concepts/digraph.h>
kpeter@745
    20
#include <lemon/smart_graph.h>
kpeter@745
    21
#include <lemon/list_graph.h>
kpeter@745
    22
#include <lemon/lgf_reader.h>
kpeter@745
    23
#include <lemon/bellman_ford.h>
kpeter@745
    24
#include <lemon/path.h>
kpeter@745
    25
kpeter@745
    26
#include "graph_test.h"
kpeter@745
    27
#include "test_tools.h"
kpeter@745
    28
kpeter@745
    29
using namespace lemon;
kpeter@745
    30
kpeter@745
    31
char test_lgf[] =
kpeter@745
    32
  "@nodes\n"
kpeter@745
    33
  "label\n"
kpeter@745
    34
  "0\n"
kpeter@745
    35
  "1\n"
kpeter@745
    36
  "2\n"
kpeter@745
    37
  "3\n"
kpeter@745
    38
  "4\n"
kpeter@745
    39
  "@arcs\n"
kpeter@745
    40
  "    length\n"
kpeter@745
    41
  "0 1 3\n"
kpeter@745
    42
  "1 2 -3\n"
kpeter@745
    43
  "1 2 -5\n"
kpeter@745
    44
  "1 3 -2\n"
kpeter@745
    45
  "0 2 -1\n"
kpeter@745
    46
  "1 2 -4\n"
kpeter@745
    47
  "0 3 2\n"
kpeter@745
    48
  "4 2 -5\n"
kpeter@745
    49
  "2 3 1\n"
kpeter@745
    50
  "@attributes\n"
kpeter@745
    51
  "source 0\n"
kpeter@745
    52
  "target 3\n";
kpeter@745
    53
kpeter@745
    54
kpeter@745
    55
void checkBellmanFordCompile()
kpeter@745
    56
{
kpeter@745
    57
  typedef int Value;
kpeter@745
    58
  typedef concepts::Digraph Digraph;
kpeter@745
    59
  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
kpeter@745
    60
  typedef BellmanFord<Digraph, LengthMap> BF;
kpeter@745
    61
  typedef Digraph::Node Node;
kpeter@745
    62
  typedef Digraph::Arc Arc;
kpeter@745
    63
kpeter@745
    64
  Digraph gr;
kpeter@745
    65
  Node s, t, n;
kpeter@745
    66
  Arc e;
kpeter@745
    67
  Value l;
alpar@1260
    68
  ::lemon::ignore_unused_variable_warning(l);
alpar@837
    69
  int k=3;
kpeter@745
    70
  bool b;
alpar@1260
    71
  ::lemon::ignore_unused_variable_warning(b);
kpeter@745
    72
  BF::DistMap d(gr);
kpeter@745
    73
  BF::PredMap p(gr);
kpeter@745
    74
  LengthMap length;
kpeter@745
    75
  concepts::Path<Digraph> pp;
kpeter@745
    76
kpeter@745
    77
  {
kpeter@745
    78
    BF bf_test(gr,length);
kpeter@745
    79
    const BF& const_bf_test = bf_test;
kpeter@745
    80
kpeter@745
    81
    bf_test.run(s);
kpeter@745
    82
    bf_test.run(s,k);
kpeter@745
    83
kpeter@745
    84
    bf_test.init();
kpeter@745
    85
    bf_test.addSource(s);
kpeter@745
    86
    bf_test.addSource(s, 1);
kpeter@745
    87
    b = bf_test.processNextRound();
kpeter@745
    88
    b = bf_test.processNextWeakRound();
kpeter@745
    89
kpeter@745
    90
    bf_test.start();
kpeter@745
    91
    bf_test.checkedStart();
kpeter@745
    92
    bf_test.limitedStart(k);
kpeter@745
    93
kpeter@745
    94
    l  = const_bf_test.dist(t);
kpeter@745
    95
    e  = const_bf_test.predArc(t);
kpeter@745
    96
    s  = const_bf_test.predNode(t);
kpeter@745
    97
    b  = const_bf_test.reached(t);
kpeter@745
    98
    d  = const_bf_test.distMap();
kpeter@745
    99
    p  = const_bf_test.predMap();
kpeter@745
   100
    pp = const_bf_test.path(t);
kpeter@828
   101
    pp = const_bf_test.negativeCycle();
alpar@956
   102
kpeter@1378
   103
#ifdef LEMON_CXX11
kpeter@745
   104
    for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
alpar@1337
   105
    for (auto n: const_bf_test.activeNodes()) { ::lemon::ignore_unused_variable_warning(n); }
alpar@1337
   106
    for (Digraph::Node n: const_bf_test.activeNodes()) {
alpar@1337
   107
      ::lemon::ignore_unused_variable_warning(n);
alpar@1337
   108
    }
kpeter@1378
   109
#endif
kpeter@745
   110
  }
kpeter@745
   111
  {
kpeter@745
   112
    BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
kpeter@745
   113
      ::SetDistMap<concepts::ReadWriteMap<Node,Value> >
kpeter@745
   114
      ::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> >
kpeter@745
   115
      ::Create bf_test(gr,length);
kpeter@745
   116
kpeter@745
   117
    LengthMap length_map;
kpeter@745
   118
    concepts::ReadWriteMap<Node,Arc> pred_map;
kpeter@745
   119
    concepts::ReadWriteMap<Node,Value> dist_map;
alpar@956
   120
kpeter@745
   121
    bf_test
kpeter@745
   122
      .lengthMap(length_map)
kpeter@745
   123
      .predMap(pred_map)
kpeter@745
   124
      .distMap(dist_map);
kpeter@745
   125
kpeter@745
   126
    bf_test.run(s);
kpeter@745
   127
    bf_test.run(s,k);
kpeter@745
   128
kpeter@745
   129
    bf_test.init();
kpeter@745
   130
    bf_test.addSource(s);
kpeter@745
   131
    bf_test.addSource(s, 1);
kpeter@745
   132
    b = bf_test.processNextRound();
kpeter@745
   133
    b = bf_test.processNextWeakRound();
kpeter@745
   134
kpeter@745
   135
    bf_test.start();
kpeter@745
   136
    bf_test.checkedStart();
kpeter@745
   137
    bf_test.limitedStart(k);
kpeter@745
   138
kpeter@745
   139
    l  = bf_test.dist(t);
kpeter@745
   140
    e  = bf_test.predArc(t);
kpeter@745
   141
    s  = bf_test.predNode(t);
kpeter@745
   142
    b  = bf_test.reached(t);
kpeter@745
   143
    pp = bf_test.path(t);
kpeter@828
   144
    pp = bf_test.negativeCycle();
kpeter@745
   145
  }
kpeter@745
   146
}
kpeter@745
   147
kpeter@745
   148
void checkBellmanFordFunctionCompile()
kpeter@745
   149
{
kpeter@745
   150
  typedef int Value;
kpeter@745
   151
  typedef concepts::Digraph Digraph;
kpeter@745
   152
  typedef Digraph::Arc Arc;
kpeter@745
   153
  typedef Digraph::Node Node;
kpeter@745
   154
  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
kpeter@745
   155
kpeter@745
   156
  Digraph g;
kpeter@745
   157
  bool b;
alpar@1260
   158
  ::lemon::ignore_unused_variable_warning(b);
alpar@1174
   159
kpeter@745
   160
  bellmanFord(g,LengthMap()).run(Node());
kpeter@745
   161
  b = bellmanFord(g,LengthMap()).run(Node(),Node());
kpeter@745
   162
  bellmanFord(g,LengthMap())
kpeter@745
   163
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@745
   164
    .distMap(concepts::ReadWriteMap<Node,Value>())
kpeter@745
   165
    .run(Node());
kpeter@745
   166
  b=bellmanFord(g,LengthMap())
kpeter@745
   167
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@745
   168
    .distMap(concepts::ReadWriteMap<Node,Value>())
kpeter@745
   169
    .path(concepts::Path<Digraph>())
kpeter@745
   170
    .dist(Value())
kpeter@745
   171
    .run(Node(),Node());
kpeter@745
   172
}
kpeter@745
   173
kpeter@745
   174
kpeter@745
   175
template <typename Digraph, typename Value>
kpeter@745
   176
void checkBellmanFord() {
kpeter@745
   177
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@745
   178
  typedef typename Digraph::template ArcMap<Value> LengthMap;
kpeter@745
   179
kpeter@745
   180
  Digraph gr;
kpeter@745
   181
  Node s, t;
kpeter@745
   182
  LengthMap length(gr);
kpeter@745
   183
kpeter@745
   184
  std::istringstream input(test_lgf);
kpeter@745
   185
  digraphReader(gr, input).
kpeter@745
   186
    arcMap("length", length).
kpeter@745
   187
    node("source", s).
kpeter@745
   188
    node("target", t).
kpeter@745
   189
    run();
kpeter@745
   190
kpeter@745
   191
  BellmanFord<Digraph, LengthMap>
kpeter@745
   192
    bf(gr, length);
kpeter@745
   193
  bf.run(s);
kpeter@745
   194
  Path<Digraph> p = bf.path(t);
kpeter@745
   195
kpeter@745
   196
  check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path.");
kpeter@745
   197
  check(p.length() == 3, "path() found a wrong path.");
kpeter@745
   198
  check(checkPath(gr, p), "path() found a wrong path.");
kpeter@745
   199
  check(pathSource(gr, p) == s, "path() found a wrong path.");
kpeter@745
   200
  check(pathTarget(gr, p) == t, "path() found a wrong path.");
alpar@956
   201
kpeter@745
   202
  ListPath<Digraph> path;
alpar@1160
   203
  Value dist = 0;
kpeter@745
   204
  bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t);
kpeter@745
   205
kpeter@745
   206
  check(reached && dist == -1, "Bellman-Ford found a wrong path.");
kpeter@745
   207
  check(path.length() == 3, "path() found a wrong path.");
kpeter@745
   208
  check(checkPath(gr, path), "path() found a wrong path.");
kpeter@745
   209
  check(pathSource(gr, path) == s, "path() found a wrong path.");
kpeter@745
   210
  check(pathTarget(gr, path) == t, "path() found a wrong path.");
kpeter@745
   211
kpeter@745
   212
  for(ArcIt e(gr); e!=INVALID; ++e) {
kpeter@745
   213
    Node u=gr.source(e);
kpeter@745
   214
    Node v=gr.target(e);
kpeter@745
   215
    check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]),
kpeter@745
   216
          "Wrong output. dist(target)-dist(source)-arc_length=" <<
kpeter@745
   217
          bf.dist(v) - bf.dist(u) - length[e]);
kpeter@745
   218
  }
kpeter@745
   219
kpeter@745
   220
  for(NodeIt v(gr); v!=INVALID; ++v) {
kpeter@745
   221
    if (bf.reached(v)) {
kpeter@745
   222
      check(v==s || bf.predArc(v)!=INVALID, "Wrong tree.");
kpeter@745
   223
      if (bf.predArc(v)!=INVALID ) {
kpeter@745
   224
        Arc e=bf.predArc(v);
kpeter@745
   225
        Node u=gr.source(e);
kpeter@745
   226
        check(u==bf.predNode(v),"Wrong tree.");
kpeter@745
   227
        check(bf.dist(v) - bf.dist(u) == length[e],
kpeter@745
   228
              "Wrong distance! Difference: " <<
kpeter@745
   229
              bf.dist(v) - bf.dist(u) - length[e]);
kpeter@745
   230
      }
kpeter@745
   231
    }
kpeter@745
   232
  }
kpeter@745
   233
}
kpeter@745
   234
kpeter@746
   235
void checkBellmanFordNegativeCycle() {
kpeter@746
   236
  DIGRAPH_TYPEDEFS(SmartDigraph);
kpeter@746
   237
kpeter@746
   238
  SmartDigraph gr;
kpeter@746
   239
  IntArcMap length(gr);
alpar@956
   240
kpeter@746
   241
  Node n1 = gr.addNode();
kpeter@746
   242
  Node n2 = gr.addNode();
kpeter@746
   243
  Node n3 = gr.addNode();
kpeter@746
   244
  Node n4 = gr.addNode();
alpar@956
   245
kpeter@746
   246
  Arc a1 = gr.addArc(n1, n2);
kpeter@746
   247
  Arc a2 = gr.addArc(n2, n2);
alpar@956
   248
kpeter@746
   249
  length[a1] = 2;
kpeter@746
   250
  length[a2] = -1;
alpar@956
   251
kpeter@746
   252
  {
kpeter@746
   253
    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
kpeter@746
   254
    bf.run(n1);
kpeter@746
   255
    StaticPath<SmartDigraph> p = bf.negativeCycle();
kpeter@746
   256
    check(p.length() == 1 && p.front() == p.back() && p.front() == a2,
kpeter@746
   257
          "Wrong negative cycle.");
kpeter@746
   258
  }
alpar@956
   259
kpeter@746
   260
  length[a2] = 0;
alpar@956
   261
kpeter@746
   262
  {
kpeter@746
   263
    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
kpeter@746
   264
    bf.run(n1);
kpeter@746
   265
    check(bf.negativeCycle().empty(),
kpeter@746
   266
          "Negative cycle should not be found.");
kpeter@746
   267
  }
alpar@956
   268
kpeter@746
   269
  length[gr.addArc(n1, n3)] = 5;
kpeter@746
   270
  length[gr.addArc(n4, n3)] = 1;
kpeter@746
   271
  length[gr.addArc(n2, n4)] = 2;
kpeter@746
   272
  length[gr.addArc(n3, n2)] = -4;
alpar@956
   273
kpeter@746
   274
  {
kpeter@746
   275
    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
kpeter@746
   276
    bf.init();
kpeter@746
   277
    bf.addSource(n1);
kpeter@746
   278
    for (int i = 0; i < 4; ++i) {
kpeter@746
   279
      check(bf.negativeCycle().empty(),
kpeter@746
   280
            "Negative cycle should not be found.");
kpeter@746
   281
      bf.processNextRound();
kpeter@746
   282
    }
kpeter@746
   283
    StaticPath<SmartDigraph> p = bf.negativeCycle();
kpeter@746
   284
    check(p.length() == 3, "Wrong negative cycle.");
kpeter@746
   285
    check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1,
kpeter@746
   286
          "Wrong negative cycle.");
kpeter@746
   287
  }
kpeter@746
   288
}
kpeter@746
   289
kpeter@745
   290
int main() {
kpeter@745
   291
  checkBellmanFord<ListDigraph, int>();
kpeter@745
   292
  checkBellmanFord<SmartDigraph, double>();
kpeter@746
   293
  checkBellmanFordNegativeCycle();
kpeter@745
   294
  return 0;
kpeter@745
   295
}