COIN-OR::LEMON - Graph Library

source: lemon-main/test/bellman_ford_test.cc @ 1208:c6aa2cc1af04

Last change on this file since 1208:c6aa2cc1af04 was 1162:881e4168c65f, checked in by Peter Kovacs <kpeter@…>, 7 years ago

Add missing #ifdef to bellman_ford_test.cc (#325)

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