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