test/bellman_ford_test.cc
changeset 964 2b6bffe0e7e8
parent 844 a6eb9698c321
child 880 b89e46862dc2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/bellman_ford_test.cc	Tue Dec 20 18:15:14 2011 +0100
     1.3 @@ -0,0 +1,286 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2010
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include <lemon/concepts/digraph.h>
    1.23 +#include <lemon/smart_graph.h>
    1.24 +#include <lemon/list_graph.h>
    1.25 +#include <lemon/lgf_reader.h>
    1.26 +#include <lemon/bellman_ford.h>
    1.27 +#include <lemon/path.h>
    1.28 +
    1.29 +#include "graph_test.h"
    1.30 +#include "test_tools.h"
    1.31 +
    1.32 +using namespace lemon;
    1.33 +
    1.34 +char test_lgf[] =
    1.35 +  "@nodes\n"
    1.36 +  "label\n"
    1.37 +  "0\n"
    1.38 +  "1\n"
    1.39 +  "2\n"
    1.40 +  "3\n"
    1.41 +  "4\n"
    1.42 +  "@arcs\n"
    1.43 +  "    length\n"
    1.44 +  "0 1 3\n"
    1.45 +  "1 2 -3\n"
    1.46 +  "1 2 -5\n"
    1.47 +  "1 3 -2\n"
    1.48 +  "0 2 -1\n"
    1.49 +  "1 2 -4\n"
    1.50 +  "0 3 2\n"
    1.51 +  "4 2 -5\n"
    1.52 +  "2 3 1\n"
    1.53 +  "@attributes\n"
    1.54 +  "source 0\n"
    1.55 +  "target 3\n";
    1.56 +
    1.57 +
    1.58 +void checkBellmanFordCompile()
    1.59 +{
    1.60 +  typedef int Value;
    1.61 +  typedef concepts::Digraph Digraph;
    1.62 +  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
    1.63 +  typedef BellmanFord<Digraph, LengthMap> BF;
    1.64 +  typedef Digraph::Node Node;
    1.65 +  typedef Digraph::Arc Arc;
    1.66 +
    1.67 +  Digraph gr;
    1.68 +  Node s, t, n;
    1.69 +  Arc e;
    1.70 +  Value l;
    1.71 +  int k=3;
    1.72 +  bool b;
    1.73 +  BF::DistMap d(gr);
    1.74 +  BF::PredMap p(gr);
    1.75 +  LengthMap length;
    1.76 +  concepts::Path<Digraph> pp;
    1.77 +
    1.78 +  {
    1.79 +    BF bf_test(gr,length);
    1.80 +    const BF& const_bf_test = bf_test;
    1.81 +
    1.82 +    bf_test.run(s);
    1.83 +    bf_test.run(s,k);
    1.84 +
    1.85 +    bf_test.init();
    1.86 +    bf_test.addSource(s);
    1.87 +    bf_test.addSource(s, 1);
    1.88 +    b = bf_test.processNextRound();
    1.89 +    b = bf_test.processNextWeakRound();
    1.90 +
    1.91 +    bf_test.start();
    1.92 +    bf_test.checkedStart();
    1.93 +    bf_test.limitedStart(k);
    1.94 +
    1.95 +    l  = const_bf_test.dist(t);
    1.96 +    e  = const_bf_test.predArc(t);
    1.97 +    s  = const_bf_test.predNode(t);
    1.98 +    b  = const_bf_test.reached(t);
    1.99 +    d  = const_bf_test.distMap();
   1.100 +    p  = const_bf_test.predMap();
   1.101 +    pp = const_bf_test.path(t);
   1.102 +    pp = const_bf_test.negativeCycle();
   1.103 +
   1.104 +    for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
   1.105 +  }
   1.106 +  {
   1.107 +    BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
   1.108 +      ::SetDistMap<concepts::ReadWriteMap<Node,Value> >
   1.109 +      ::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> >
   1.110 +      ::SetOperationTraits<BellmanFordToleranceOperationTraits<Value, 0> >
   1.111 +      ::Create bf_test(gr,length);
   1.112 +
   1.113 +    LengthMap length_map;
   1.114 +    concepts::ReadWriteMap<Node,Arc> pred_map;
   1.115 +    concepts::ReadWriteMap<Node,Value> dist_map;
   1.116 +
   1.117 +    bf_test
   1.118 +      .lengthMap(length_map)
   1.119 +      .predMap(pred_map)
   1.120 +      .distMap(dist_map);
   1.121 +
   1.122 +    bf_test.run(s);
   1.123 +    bf_test.run(s,k);
   1.124 +
   1.125 +    bf_test.init();
   1.126 +    bf_test.addSource(s);
   1.127 +    bf_test.addSource(s, 1);
   1.128 +    b = bf_test.processNextRound();
   1.129 +    b = bf_test.processNextWeakRound();
   1.130 +
   1.131 +    bf_test.start();
   1.132 +    bf_test.checkedStart();
   1.133 +    bf_test.limitedStart(k);
   1.134 +
   1.135 +    l  = bf_test.dist(t);
   1.136 +    e  = bf_test.predArc(t);
   1.137 +    s  = bf_test.predNode(t);
   1.138 +    b  = bf_test.reached(t);
   1.139 +    pp = bf_test.path(t);
   1.140 +    pp = bf_test.negativeCycle();
   1.141 +  }
   1.142 +}
   1.143 +
   1.144 +void checkBellmanFordFunctionCompile()
   1.145 +{
   1.146 +  typedef int Value;
   1.147 +  typedef concepts::Digraph Digraph;
   1.148 +  typedef Digraph::Arc Arc;
   1.149 +  typedef Digraph::Node Node;
   1.150 +  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
   1.151 +
   1.152 +  Digraph g;
   1.153 +  bool b;
   1.154 +  bellmanFord(g,LengthMap()).run(Node());
   1.155 +  b = bellmanFord(g,LengthMap()).run(Node(),Node());
   1.156 +  bellmanFord(g,LengthMap())
   1.157 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
   1.158 +    .distMap(concepts::ReadWriteMap<Node,Value>())
   1.159 +    .run(Node());
   1.160 +  b=bellmanFord(g,LengthMap())
   1.161 +    .predMap(concepts::ReadWriteMap<Node,Arc>())
   1.162 +    .distMap(concepts::ReadWriteMap<Node,Value>())
   1.163 +    .path(concepts::Path<Digraph>())
   1.164 +    .dist(Value())
   1.165 +    .run(Node(),Node());
   1.166 +}
   1.167 +
   1.168 +
   1.169 +template <typename Digraph, typename Value>
   1.170 +void checkBellmanFord() {
   1.171 +  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
   1.172 +  typedef typename Digraph::template ArcMap<Value> LengthMap;
   1.173 +
   1.174 +  Digraph gr;
   1.175 +  Node s, t;
   1.176 +  LengthMap length(gr);
   1.177 +
   1.178 +  std::istringstream input(test_lgf);
   1.179 +  digraphReader(gr, input).
   1.180 +    arcMap("length", length).
   1.181 +    node("source", s).
   1.182 +    node("target", t).
   1.183 +    run();
   1.184 +
   1.185 +  BellmanFord<Digraph, LengthMap>
   1.186 +    bf(gr, length);
   1.187 +  bf.run(s);
   1.188 +  Path<Digraph> p = bf.path(t);
   1.189 +
   1.190 +  check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path.");
   1.191 +  check(p.length() == 3, "path() found a wrong path.");
   1.192 +  check(checkPath(gr, p), "path() found a wrong path.");
   1.193 +  check(pathSource(gr, p) == s, "path() found a wrong path.");
   1.194 +  check(pathTarget(gr, p) == t, "path() found a wrong path.");
   1.195 +
   1.196 +  ListPath<Digraph> path;
   1.197 +  Value dist;
   1.198 +  bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t);
   1.199 +
   1.200 +  check(reached && dist == -1, "Bellman-Ford found a wrong path.");
   1.201 +  check(path.length() == 3, "path() found a wrong path.");
   1.202 +  check(checkPath(gr, path), "path() found a wrong path.");
   1.203 +  check(pathSource(gr, path) == s, "path() found a wrong path.");
   1.204 +  check(pathTarget(gr, path) == t, "path() found a wrong path.");
   1.205 +
   1.206 +  for(ArcIt e(gr); e!=INVALID; ++e) {
   1.207 +    Node u=gr.source(e);
   1.208 +    Node v=gr.target(e);
   1.209 +    check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]),
   1.210 +          "Wrong output. dist(target)-dist(source)-arc_length=" <<
   1.211 +          bf.dist(v) - bf.dist(u) - length[e]);
   1.212 +  }
   1.213 +
   1.214 +  for(NodeIt v(gr); v!=INVALID; ++v) {
   1.215 +    if (bf.reached(v)) {
   1.216 +      check(v==s || bf.predArc(v)!=INVALID, "Wrong tree.");
   1.217 +      if (bf.predArc(v)!=INVALID ) {
   1.218 +        Arc e=bf.predArc(v);
   1.219 +        Node u=gr.source(e);
   1.220 +        check(u==bf.predNode(v),"Wrong tree.");
   1.221 +        check(bf.dist(v) - bf.dist(u) == length[e],
   1.222 +              "Wrong distance! Difference: " <<
   1.223 +              bf.dist(v) - bf.dist(u) - length[e]);
   1.224 +      }
   1.225 +    }
   1.226 +  }
   1.227 +}
   1.228 +
   1.229 +void checkBellmanFordNegativeCycle() {
   1.230 +  DIGRAPH_TYPEDEFS(SmartDigraph);
   1.231 +
   1.232 +  SmartDigraph gr;
   1.233 +  IntArcMap length(gr);
   1.234 +
   1.235 +  Node n1 = gr.addNode();
   1.236 +  Node n2 = gr.addNode();
   1.237 +  Node n3 = gr.addNode();
   1.238 +  Node n4 = gr.addNode();
   1.239 +
   1.240 +  Arc a1 = gr.addArc(n1, n2);
   1.241 +  Arc a2 = gr.addArc(n2, n2);
   1.242 +
   1.243 +  length[a1] = 2;
   1.244 +  length[a2] = -1;
   1.245 +
   1.246 +  {
   1.247 +    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
   1.248 +    bf.run(n1);
   1.249 +    StaticPath<SmartDigraph> p = bf.negativeCycle();
   1.250 +    check(p.length() == 1 && p.front() == p.back() && p.front() == a2,
   1.251 +          "Wrong negative cycle.");
   1.252 +  }
   1.253 +
   1.254 +  length[a2] = 0;
   1.255 +
   1.256 +  {
   1.257 +    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
   1.258 +    bf.run(n1);
   1.259 +    check(bf.negativeCycle().empty(),
   1.260 +          "Negative cycle should not be found.");
   1.261 +  }
   1.262 +
   1.263 +  length[gr.addArc(n1, n3)] = 5;
   1.264 +  length[gr.addArc(n4, n3)] = 1;
   1.265 +  length[gr.addArc(n2, n4)] = 2;
   1.266 +  length[gr.addArc(n3, n2)] = -4;
   1.267 +
   1.268 +  {
   1.269 +    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
   1.270 +    bf.init();
   1.271 +    bf.addSource(n1);
   1.272 +    for (int i = 0; i < 4; ++i) {
   1.273 +      check(bf.negativeCycle().empty(),
   1.274 +            "Negative cycle should not be found.");
   1.275 +      bf.processNextRound();
   1.276 +    }
   1.277 +    StaticPath<SmartDigraph> p = bf.negativeCycle();
   1.278 +    check(p.length() == 3, "Wrong negative cycle.");
   1.279 +    check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1,
   1.280 +          "Wrong negative cycle.");
   1.281 +  }
   1.282 +}
   1.283 +
   1.284 +int main() {
   1.285 +  checkBellmanFord<ListDigraph, int>();
   1.286 +  checkBellmanFord<SmartDigraph, double>();
   1.287 +  checkBellmanFordNegativeCycle();
   1.288 +  return 0;
   1.289 +}