COIN-OR::LEMON - Graph Library

source: lemon-main/test/bellman_ford_test.cc @ 844:a6eb9698c321

Last change on this file since 844:a6eb9698c321 was 844:a6eb9698c321, checked in by Peter Kovacs <kpeter@…>, 14 years ago

Support tolerance technique for BellmanFord? (#51)

A new operation traits class BellmanFordToleranceOperationTraits?
is introduced, which uses the tolerance technique in its less()
function. This class can be used with the SetOperationTraits?
named template parameter.

File size: 7.2 KB
Line 
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-2009
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
29using namespace lemon;
30
31char 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
55void 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  int k=3;
69  bool b;
70  BF::DistMap d(gr);
71  BF::PredMap p(gr);
72  LengthMap length;
73  concepts::Path<Digraph> pp;
74
75  {
76    BF bf_test(gr,length);
77    const BF& const_bf_test = bf_test;
78
79    bf_test.run(s);
80    bf_test.run(s,k);
81
82    bf_test.init();
83    bf_test.addSource(s);
84    bf_test.addSource(s, 1);
85    b = bf_test.processNextRound();
86    b = bf_test.processNextWeakRound();
87
88    bf_test.start();
89    bf_test.checkedStart();
90    bf_test.limitedStart(k);
91
92    l  = const_bf_test.dist(t);
93    e  = const_bf_test.predArc(t);
94    s  = const_bf_test.predNode(t);
95    b  = const_bf_test.reached(t);
96    d  = const_bf_test.distMap();
97    p  = const_bf_test.predMap();
98    pp = const_bf_test.path(t);
99    pp = const_bf_test.negativeCycle();
100   
101    for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
102  }
103  {
104    BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
105      ::SetDistMap<concepts::ReadWriteMap<Node,Value> >
106      ::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> >
107      ::SetOperationTraits<BellmanFordToleranceOperationTraits<Value, 0> >
108      ::Create bf_test(gr,length);
109
110    LengthMap length_map;
111    concepts::ReadWriteMap<Node,Arc> pred_map;
112    concepts::ReadWriteMap<Node,Value> dist_map;
113   
114    bf_test
115      .lengthMap(length_map)
116      .predMap(pred_map)
117      .distMap(dist_map);
118
119    bf_test.run(s);
120    bf_test.run(s,k);
121
122    bf_test.init();
123    bf_test.addSource(s);
124    bf_test.addSource(s, 1);
125    b = bf_test.processNextRound();
126    b = bf_test.processNextWeakRound();
127
128    bf_test.start();
129    bf_test.checkedStart();
130    bf_test.limitedStart(k);
131
132    l  = bf_test.dist(t);
133    e  = bf_test.predArc(t);
134    s  = bf_test.predNode(t);
135    b  = bf_test.reached(t);
136    pp = bf_test.path(t);
137    pp = bf_test.negativeCycle();
138  }
139}
140
141void checkBellmanFordFunctionCompile()
142{
143  typedef int Value;
144  typedef concepts::Digraph Digraph;
145  typedef Digraph::Arc Arc;
146  typedef Digraph::Node Node;
147  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
148
149  Digraph g;
150  bool b;
151  bellmanFord(g,LengthMap()).run(Node());
152  b = bellmanFord(g,LengthMap()).run(Node(),Node());
153  bellmanFord(g,LengthMap())
154    .predMap(concepts::ReadWriteMap<Node,Arc>())
155    .distMap(concepts::ReadWriteMap<Node,Value>())
156    .run(Node());
157  b=bellmanFord(g,LengthMap())
158    .predMap(concepts::ReadWriteMap<Node,Arc>())
159    .distMap(concepts::ReadWriteMap<Node,Value>())
160    .path(concepts::Path<Digraph>())
161    .dist(Value())
162    .run(Node(),Node());
163}
164
165
166template <typename Digraph, typename Value>
167void checkBellmanFord() {
168  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
169  typedef typename Digraph::template ArcMap<Value> LengthMap;
170
171  Digraph gr;
172  Node s, t;
173  LengthMap length(gr);
174
175  std::istringstream input(test_lgf);
176  digraphReader(gr, input).
177    arcMap("length", length).
178    node("source", s).
179    node("target", t).
180    run();
181
182  BellmanFord<Digraph, LengthMap>
183    bf(gr, length);
184  bf.run(s);
185  Path<Digraph> p = bf.path(t);
186
187  check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path.");
188  check(p.length() == 3, "path() found a wrong path.");
189  check(checkPath(gr, p), "path() found a wrong path.");
190  check(pathSource(gr, p) == s, "path() found a wrong path.");
191  check(pathTarget(gr, p) == t, "path() found a wrong path.");
192 
193  ListPath<Digraph> path;
194  Value dist;
195  bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t);
196
197  check(reached && dist == -1, "Bellman-Ford found a wrong path.");
198  check(path.length() == 3, "path() found a wrong path.");
199  check(checkPath(gr, path), "path() found a wrong path.");
200  check(pathSource(gr, path) == s, "path() found a wrong path.");
201  check(pathTarget(gr, path) == t, "path() found a wrong path.");
202
203  for(ArcIt e(gr); e!=INVALID; ++e) {
204    Node u=gr.source(e);
205    Node v=gr.target(e);
206    check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]),
207          "Wrong output. dist(target)-dist(source)-arc_length=" <<
208          bf.dist(v) - bf.dist(u) - length[e]);
209  }
210
211  for(NodeIt v(gr); v!=INVALID; ++v) {
212    if (bf.reached(v)) {
213      check(v==s || bf.predArc(v)!=INVALID, "Wrong tree.");
214      if (bf.predArc(v)!=INVALID ) {
215        Arc e=bf.predArc(v);
216        Node u=gr.source(e);
217        check(u==bf.predNode(v),"Wrong tree.");
218        check(bf.dist(v) - bf.dist(u) == length[e],
219              "Wrong distance! Difference: " <<
220              bf.dist(v) - bf.dist(u) - length[e]);
221      }
222    }
223  }
224}
225
226void checkBellmanFordNegativeCycle() {
227  DIGRAPH_TYPEDEFS(SmartDigraph);
228
229  SmartDigraph gr;
230  IntArcMap length(gr);
231 
232  Node n1 = gr.addNode();
233  Node n2 = gr.addNode();
234  Node n3 = gr.addNode();
235  Node n4 = gr.addNode();
236 
237  Arc a1 = gr.addArc(n1, n2);
238  Arc a2 = gr.addArc(n2, n2);
239 
240  length[a1] = 2;
241  length[a2] = -1;
242 
243  {
244    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
245    bf.run(n1);
246    StaticPath<SmartDigraph> p = bf.negativeCycle();
247    check(p.length() == 1 && p.front() == p.back() && p.front() == a2,
248          "Wrong negative cycle.");
249  }
250 
251  length[a2] = 0;
252 
253  {
254    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
255    bf.run(n1);
256    check(bf.negativeCycle().empty(),
257          "Negative cycle should not be found.");
258  }
259 
260  length[gr.addArc(n1, n3)] = 5;
261  length[gr.addArc(n4, n3)] = 1;
262  length[gr.addArc(n2, n4)] = 2;
263  length[gr.addArc(n3, n2)] = -4;
264 
265  {
266    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
267    bf.init();
268    bf.addSource(n1);
269    for (int i = 0; i < 4; ++i) {
270      check(bf.negativeCycle().empty(),
271            "Negative cycle should not be found.");
272      bf.processNextRound();
273    }
274    StaticPath<SmartDigraph> p = bf.negativeCycle();
275    check(p.length() == 3, "Wrong negative cycle.");
276    check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1,
277          "Wrong negative cycle.");
278  }
279}
280
281int main() {
282  checkBellmanFord<ListDigraph, int>();
283  checkBellmanFord<SmartDigraph, double>();
284  checkBellmanFordNegativeCycle();
285  return 0;
286}
Note: See TracBrowser for help on using the repository browser.