alpar@209
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
kpeter@170
|
2 |
*
|
alpar@209
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
kpeter@170
|
4 |
*
|
alpar@440
|
5 |
* Copyright (C) 2003-2009
|
kpeter@170
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@170
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@170
|
8 |
*
|
kpeter@170
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@170
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@170
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@170
|
12 |
*
|
kpeter@170
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@170
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@170
|
15 |
* purpose.
|
kpeter@170
|
16 |
*
|
kpeter@170
|
17 |
*/
|
kpeter@170
|
18 |
|
kpeter@170
|
19 |
#include <lemon/concepts/digraph.h>
|
kpeter@170
|
20 |
#include <lemon/smart_graph.h>
|
kpeter@170
|
21 |
#include <lemon/list_graph.h>
|
deba@228
|
22 |
#include <lemon/lgf_reader.h>
|
kpeter@170
|
23 |
#include <lemon/dijkstra.h>
|
kpeter@170
|
24 |
#include <lemon/path.h>
|
kpeter@286
|
25 |
#include <lemon/bin_heap.h>
|
kpeter@170
|
26 |
|
kpeter@171
|
27 |
#include "graph_test.h"
|
kpeter@170
|
28 |
#include "test_tools.h"
|
kpeter@170
|
29 |
|
kpeter@170
|
30 |
using namespace lemon;
|
kpeter@170
|
31 |
|
deba@228
|
32 |
char test_lgf[] =
|
deba@228
|
33 |
"@nodes\n"
|
deba@228
|
34 |
"label\n"
|
deba@228
|
35 |
"0\n"
|
deba@228
|
36 |
"1\n"
|
deba@228
|
37 |
"2\n"
|
deba@228
|
38 |
"3\n"
|
deba@228
|
39 |
"4\n"
|
deba@228
|
40 |
"@arcs\n"
|
deba@228
|
41 |
" label length\n"
|
deba@228
|
42 |
"0 1 0 1\n"
|
deba@228
|
43 |
"1 2 1 1\n"
|
deba@228
|
44 |
"2 3 2 1\n"
|
deba@228
|
45 |
"0 3 4 5\n"
|
deba@228
|
46 |
"0 3 5 10\n"
|
deba@228
|
47 |
"0 3 6 7\n"
|
deba@228
|
48 |
"4 2 7 1\n"
|
deba@228
|
49 |
"@attributes\n"
|
deba@228
|
50 |
"source 0\n"
|
deba@228
|
51 |
"target 3\n";
|
deba@228
|
52 |
|
alpar@209
|
53 |
void checkDijkstraCompile()
|
kpeter@170
|
54 |
{
|
kpeter@170
|
55 |
typedef int VType;
|
kpeter@170
|
56 |
typedef concepts::Digraph Digraph;
|
kpeter@170
|
57 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
|
kpeter@170
|
58 |
typedef Dijkstra<Digraph, LengthMap> DType;
|
kpeter@286
|
59 |
typedef Digraph::Node Node;
|
kpeter@286
|
60 |
typedef Digraph::Arc Arc;
|
alpar@209
|
61 |
|
kpeter@170
|
62 |
Digraph G;
|
kpeter@585
|
63 |
Node s, t, n;
|
kpeter@286
|
64 |
Arc e;
|
kpeter@170
|
65 |
VType l;
|
kpeter@585
|
66 |
int i;
|
kpeter@170
|
67 |
bool b;
|
alpar@1007
|
68 |
ignore_unused_variable_warning(l,i,b);
|
alpar@1007
|
69 |
|
kpeter@170
|
70 |
DType::DistMap d(G);
|
kpeter@170
|
71 |
DType::PredMap p(G);
|
kpeter@170
|
72 |
LengthMap length;
|
kpeter@286
|
73 |
Path<Digraph> pp;
|
kpeter@585
|
74 |
concepts::ReadMap<Node,bool> nm;
|
kpeter@170
|
75 |
|
kpeter@286
|
76 |
{
|
kpeter@286
|
77 |
DType dijkstra_test(G,length);
|
kpeter@585
|
78 |
const DType& const_dijkstra_test = dijkstra_test;
|
kpeter@170
|
79 |
|
kpeter@286
|
80 |
dijkstra_test.run(s);
|
kpeter@286
|
81 |
dijkstra_test.run(s,t);
|
kpeter@170
|
82 |
|
kpeter@585
|
83 |
dijkstra_test.init();
|
kpeter@585
|
84 |
dijkstra_test.addSource(s);
|
kpeter@585
|
85 |
dijkstra_test.addSource(s, 1);
|
kpeter@585
|
86 |
n = dijkstra_test.processNextNode();
|
kpeter@585
|
87 |
n = const_dijkstra_test.nextNode();
|
kpeter@585
|
88 |
b = const_dijkstra_test.emptyQueue();
|
kpeter@585
|
89 |
i = const_dijkstra_test.queueSize();
|
kpeter@585
|
90 |
|
kpeter@585
|
91 |
dijkstra_test.start();
|
kpeter@585
|
92 |
dijkstra_test.start(t);
|
kpeter@585
|
93 |
dijkstra_test.start(nm);
|
kpeter@585
|
94 |
|
kpeter@585
|
95 |
l = const_dijkstra_test.dist(t);
|
kpeter@585
|
96 |
e = const_dijkstra_test.predArc(t);
|
kpeter@585
|
97 |
s = const_dijkstra_test.predNode(t);
|
kpeter@585
|
98 |
b = const_dijkstra_test.reached(t);
|
kpeter@585
|
99 |
b = const_dijkstra_test.processed(t);
|
kpeter@585
|
100 |
d = const_dijkstra_test.distMap();
|
kpeter@585
|
101 |
p = const_dijkstra_test.predMap();
|
kpeter@585
|
102 |
pp = const_dijkstra_test.path(t);
|
kpeter@585
|
103 |
l = const_dijkstra_test.currentDist(t);
|
kpeter@585
|
104 |
}
|
kpeter@585
|
105 |
{
|
kpeter@585
|
106 |
DType
|
kpeter@585
|
107 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
|
kpeter@585
|
108 |
::SetDistMap<concepts::ReadWriteMap<Node,VType> >
|
kpeter@585
|
109 |
::SetStandardProcessedMap
|
kpeter@585
|
110 |
::SetProcessedMap<concepts::WriteMap<Node,bool> >
|
kpeter@585
|
111 |
::SetOperationTraits<DijkstraDefaultOperationTraits<VType> >
|
kpeter@585
|
112 |
::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > >
|
kpeter@585
|
113 |
::SetStandardHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > >
|
kpeter@585
|
114 |
::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> >,
|
kpeter@585
|
115 |
concepts::ReadWriteMap<Node,int> >
|
kpeter@585
|
116 |
::Create dijkstra_test(G,length);
|
kpeter@585
|
117 |
|
kpeter@585
|
118 |
LengthMap length_map;
|
kpeter@585
|
119 |
concepts::ReadWriteMap<Node,Arc> pred_map;
|
kpeter@585
|
120 |
concepts::ReadWriteMap<Node,VType> dist_map;
|
kpeter@585
|
121 |
concepts::WriteMap<Node,bool> processed_map;
|
kpeter@585
|
122 |
concepts::ReadWriteMap<Node,int> heap_cross_ref;
|
kpeter@585
|
123 |
BinHeap<VType, concepts::ReadWriteMap<Node,int> > heap(heap_cross_ref);
|
kpeter@585
|
124 |
|
kpeter@585
|
125 |
dijkstra_test
|
kpeter@585
|
126 |
.lengthMap(length_map)
|
kpeter@585
|
127 |
.predMap(pred_map)
|
kpeter@585
|
128 |
.distMap(dist_map)
|
kpeter@585
|
129 |
.processedMap(processed_map)
|
kpeter@585
|
130 |
.heap(heap, heap_cross_ref);
|
kpeter@585
|
131 |
|
kpeter@585
|
132 |
dijkstra_test.run(s);
|
kpeter@585
|
133 |
dijkstra_test.run(s,t);
|
kpeter@585
|
134 |
|
kpeter@585
|
135 |
dijkstra_test.addSource(s);
|
kpeter@585
|
136 |
dijkstra_test.addSource(s, 1);
|
kpeter@585
|
137 |
n = dijkstra_test.processNextNode();
|
kpeter@585
|
138 |
n = dijkstra_test.nextNode();
|
kpeter@585
|
139 |
b = dijkstra_test.emptyQueue();
|
kpeter@585
|
140 |
i = dijkstra_test.queueSize();
|
kpeter@585
|
141 |
|
kpeter@585
|
142 |
dijkstra_test.start();
|
kpeter@585
|
143 |
dijkstra_test.start(t);
|
kpeter@585
|
144 |
dijkstra_test.start(nm);
|
kpeter@585
|
145 |
|
kpeter@286
|
146 |
l = dijkstra_test.dist(t);
|
kpeter@286
|
147 |
e = dijkstra_test.predArc(t);
|
kpeter@286
|
148 |
s = dijkstra_test.predNode(t);
|
kpeter@286
|
149 |
b = dijkstra_test.reached(t);
|
kpeter@585
|
150 |
b = dijkstra_test.processed(t);
|
kpeter@286
|
151 |
pp = dijkstra_test.path(t);
|
kpeter@585
|
152 |
l = dijkstra_test.currentDist(t);
|
kpeter@286
|
153 |
}
|
kpeter@286
|
154 |
|
kpeter@170
|
155 |
}
|
kpeter@170
|
156 |
|
alpar@209
|
157 |
void checkDijkstraFunctionCompile()
|
kpeter@170
|
158 |
{
|
kpeter@170
|
159 |
typedef int VType;
|
kpeter@170
|
160 |
typedef concepts::Digraph Digraph;
|
kpeter@170
|
161 |
typedef Digraph::Arc Arc;
|
kpeter@170
|
162 |
typedef Digraph::Node Node;
|
kpeter@170
|
163 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
|
alpar@209
|
164 |
|
kpeter@170
|
165 |
Digraph g;
|
kpeter@278
|
166 |
bool b;
|
alpar@1007
|
167 |
ignore_unused_variable_warning(b);
|
alpar@1007
|
168 |
|
kpeter@278
|
169 |
dijkstra(g,LengthMap()).run(Node());
|
kpeter@278
|
170 |
b=dijkstra(g,LengthMap()).run(Node(),Node());
|
kpeter@170
|
171 |
dijkstra(g,LengthMap())
|
kpeter@278
|
172 |
.predMap(concepts::ReadWriteMap<Node,Arc>())
|
kpeter@278
|
173 |
.distMap(concepts::ReadWriteMap<Node,VType>())
|
kpeter@278
|
174 |
.processedMap(concepts::WriteMap<Node,bool>())
|
kpeter@170
|
175 |
.run(Node());
|
kpeter@278
|
176 |
b=dijkstra(g,LengthMap())
|
kpeter@278
|
177 |
.predMap(concepts::ReadWriteMap<Node,Arc>())
|
kpeter@278
|
178 |
.distMap(concepts::ReadWriteMap<Node,VType>())
|
kpeter@278
|
179 |
.processedMap(concepts::WriteMap<Node,bool>())
|
kpeter@278
|
180 |
.path(concepts::Path<Digraph>())
|
kpeter@278
|
181 |
.dist(VType())
|
kpeter@278
|
182 |
.run(Node(),Node());
|
kpeter@170
|
183 |
}
|
kpeter@170
|
184 |
|
kpeter@170
|
185 |
template <class Digraph>
|
alpar@209
|
186 |
void checkDijkstra() {
|
kpeter@170
|
187 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
kpeter@170
|
188 |
typedef typename Digraph::template ArcMap<int> LengthMap;
|
kpeter@170
|
189 |
|
kpeter@170
|
190 |
Digraph G;
|
kpeter@170
|
191 |
Node s, t;
|
kpeter@170
|
192 |
LengthMap length(G);
|
alpar@209
|
193 |
|
deba@228
|
194 |
std::istringstream input(test_lgf);
|
kpeter@293
|
195 |
digraphReader(G, input).
|
deba@228
|
196 |
arcMap("length", length).
|
deba@228
|
197 |
node("source", s).
|
deba@228
|
198 |
node("target", t).
|
deba@228
|
199 |
run();
|
alpar@209
|
200 |
|
alpar@209
|
201 |
Dijkstra<Digraph, LengthMap>
|
alpar@209
|
202 |
dijkstra_test(G, length);
|
kpeter@170
|
203 |
dijkstra_test.run(s);
|
alpar@209
|
204 |
|
deba@228
|
205 |
check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
|
kpeter@170
|
206 |
|
kpeter@170
|
207 |
Path<Digraph> p = dijkstra_test.path(t);
|
kpeter@278
|
208 |
check(p.length()==3,"path() found a wrong path.");
|
kpeter@170
|
209 |
check(checkPath(G, p),"path() found a wrong path.");
|
kpeter@170
|
210 |
check(pathSource(G, p) == s,"path() found a wrong path.");
|
kpeter@170
|
211 |
check(pathTarget(G, p) == t,"path() found a wrong path.");
|
alpar@209
|
212 |
|
kpeter@170
|
213 |
for(ArcIt e(G); e!=INVALID; ++e) {
|
kpeter@170
|
214 |
Node u=G.source(e);
|
kpeter@170
|
215 |
Node v=G.target(e);
|
alpar@210
|
216 |
check( !dijkstra_test.reached(u) ||
|
alpar@210
|
217 |
(dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
|
kpeter@278
|
218 |
"Wrong output. dist(target)-dist(source)-arc_length=" <<
|
alpar@210
|
219 |
dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
|
kpeter@170
|
220 |
}
|
kpeter@170
|
221 |
|
deba@228
|
222 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
deba@228
|
223 |
if (dijkstra_test.reached(v)) {
|
deba@228
|
224 |
check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree.");
|
deba@228
|
225 |
if (dijkstra_test.predArc(v)!=INVALID ) {
|
deba@228
|
226 |
Arc e=dijkstra_test.predArc(v);
|
deba@228
|
227 |
Node u=G.source(e);
|
deba@228
|
228 |
check(u==dijkstra_test.predNode(v),"Wrong tree.");
|
deba@228
|
229 |
check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
|
deba@228
|
230 |
"Wrong distance! Difference: " <<
|
deba@228
|
231 |
std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
|
deba@228
|
232 |
}
|
kpeter@170
|
233 |
}
|
kpeter@170
|
234 |
}
|
alpar@209
|
235 |
|
kpeter@170
|
236 |
{
|
kpeter@170
|
237 |
NullMap<Node,Arc> myPredMap;
|
kpeter@170
|
238 |
dijkstra(G,length).predMap(myPredMap).run(s);
|
kpeter@170
|
239 |
}
|
kpeter@170
|
240 |
}
|
kpeter@170
|
241 |
|
kpeter@170
|
242 |
int main() {
|
kpeter@170
|
243 |
checkDijkstra<ListDigraph>();
|
kpeter@170
|
244 |
checkDijkstra<SmartDigraph>();
|
kpeter@170
|
245 |
return 0;
|
kpeter@170
|
246 |
}
|