1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2011
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
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/dijkstra.h>
24 #include <lemon/path.h>
25 #include <lemon/bin_heap.h>
27 #include "graph_test.h"
28 #include "test_tools.h"
30 using namespace lemon;
53 void checkDijkstraCompile()
56 typedef concepts::Digraph Digraph;
57 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
58 typedef Dijkstra<Digraph, LengthMap> DType;
59 typedef Digraph::Node Node;
60 typedef Digraph::Arc Arc;
68 ::lemon::ignore_unused_variable_warning(l,i,b);
74 concepts::ReadMap<Node,bool> nm;
77 DType dijkstra_test(G,length);
78 const DType& const_dijkstra_test = dijkstra_test;
81 dijkstra_test.run(s,t);
84 dijkstra_test.addSource(s);
85 dijkstra_test.addSource(s, 1);
86 n = dijkstra_test.processNextNode();
87 n = const_dijkstra_test.nextNode();
88 b = const_dijkstra_test.emptyQueue();
89 i = const_dijkstra_test.queueSize();
91 dijkstra_test.start();
92 dijkstra_test.start(t);
93 dijkstra_test.start(nm);
95 l = const_dijkstra_test.dist(t);
96 e = const_dijkstra_test.predArc(t);
97 s = const_dijkstra_test.predNode(t);
98 b = const_dijkstra_test.reached(t);
99 b = const_dijkstra_test.processed(t);
100 d = const_dijkstra_test.distMap();
101 p = const_dijkstra_test.predMap();
102 pp = const_dijkstra_test.path(t);
103 l = const_dijkstra_test.currentDist(t);
107 ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
108 ::SetDistMap<concepts::ReadWriteMap<Node,VType> >
109 ::SetStandardProcessedMap
110 ::SetProcessedMap<concepts::WriteMap<Node,bool> >
111 ::SetOperationTraits<DijkstraDefaultOperationTraits<VType> >
112 ::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > >
113 ::SetStandardHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > >
114 ::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> >,
115 concepts::ReadWriteMap<Node,int> >
116 ::Create dijkstra_test(G,length);
118 LengthMap length_map;
119 concepts::ReadWriteMap<Node,Arc> pred_map;
120 concepts::ReadWriteMap<Node,VType> dist_map;
121 concepts::WriteMap<Node,bool> processed_map;
122 concepts::ReadWriteMap<Node,int> heap_cross_ref;
123 BinHeap<VType, concepts::ReadWriteMap<Node,int> > heap(heap_cross_ref);
126 .lengthMap(length_map)
129 .processedMap(processed_map)
130 .heap(heap, heap_cross_ref);
132 dijkstra_test.run(s);
133 dijkstra_test.run(s,t);
135 dijkstra_test.addSource(s);
136 dijkstra_test.addSource(s, 1);
137 n = dijkstra_test.processNextNode();
138 n = dijkstra_test.nextNode();
139 b = dijkstra_test.emptyQueue();
140 i = dijkstra_test.queueSize();
142 dijkstra_test.start();
143 dijkstra_test.start(t);
144 dijkstra_test.start(nm);
146 l = dijkstra_test.dist(t);
147 e = dijkstra_test.predArc(t);
148 s = dijkstra_test.predNode(t);
149 b = dijkstra_test.reached(t);
150 b = dijkstra_test.processed(t);
151 pp = dijkstra_test.path(t);
152 l = dijkstra_test.currentDist(t);
157 void checkDijkstraFunctionCompile()
160 typedef concepts::Digraph Digraph;
161 typedef Digraph::Arc Arc;
162 typedef Digraph::Node Node;
163 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
167 ::lemon::ignore_unused_variable_warning(b);
169 dijkstra(g,LengthMap()).run(Node());
170 b=dijkstra(g,LengthMap()).run(Node(),Node());
171 dijkstra(g,LengthMap())
172 .predMap(concepts::ReadWriteMap<Node,Arc>())
173 .distMap(concepts::ReadWriteMap<Node,VType>())
174 .processedMap(concepts::WriteMap<Node,bool>())
176 b=dijkstra(g,LengthMap())
177 .predMap(concepts::ReadWriteMap<Node,Arc>())
178 .distMap(concepts::ReadWriteMap<Node,VType>())
179 .processedMap(concepts::WriteMap<Node,bool>())
180 .path(concepts::Path<Digraph>())
185 template <class Digraph>
186 void checkDijkstra() {
187 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
188 typedef typename Digraph::template ArcMap<int> LengthMap;
194 std::istringstream input(test_lgf);
195 digraphReader(G, input).
196 arcMap("length", length).
201 Dijkstra<Digraph, LengthMap>
202 dijkstra_test(G, length);
203 dijkstra_test.run(s);
205 check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
207 Path<Digraph> p = dijkstra_test.path(t);
208 check(p.length()==3,"path() found a wrong path.");
209 check(checkPath(G, p),"path() found a wrong path.");
210 check(pathSource(G, p) == s,"path() found a wrong path.");
211 check(pathTarget(G, p) == t,"path() found a wrong path.");
213 for(ArcIt e(G); e!=INVALID; ++e) {
216 check( !dijkstra_test.reached(u) ||
217 (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
218 "Wrong output. dist(target)-dist(source)-arc_length=" <<
219 dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
222 for(NodeIt v(G); v!=INVALID; ++v) {
223 if (dijkstra_test.reached(v)) {
224 check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree.");
225 if (dijkstra_test.predArc(v)!=INVALID ) {
226 Arc e=dijkstra_test.predArc(v);
228 check(u==dijkstra_test.predNode(v),"Wrong tree.");
229 check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
230 "Wrong distance! Difference: " <<
231 std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
237 NullMap<Node,Arc> myPredMap;
238 dijkstra(G,length).predMap(myPredMap).run(s);
243 checkDijkstra<ListDigraph>();
244 checkDijkstra<SmartDigraph>();