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