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/dfs.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 | "5\n" |
---|
40 | "6\n" |
---|
41 | "@arcs\n" |
---|
42 | " label\n" |
---|
43 | "0 1 0\n" |
---|
44 | "1 2 1\n" |
---|
45 | "2 3 2\n" |
---|
46 | "1 4 3\n" |
---|
47 | "4 2 4\n" |
---|
48 | "4 5 5\n" |
---|
49 | "5 0 6\n" |
---|
50 | "6 3 7\n" |
---|
51 | "@attributes\n" |
---|
52 | "source 0\n" |
---|
53 | "target 5\n" |
---|
54 | "source1 6\n" |
---|
55 | "target1 3\n"; |
---|
56 | |
---|
57 | |
---|
58 | void checkDfsCompile() |
---|
59 | { |
---|
60 | typedef concepts::Digraph Digraph; |
---|
61 | typedef Dfs<Digraph> DType; |
---|
62 | typedef Digraph::Node Node; |
---|
63 | typedef Digraph::Arc Arc; |
---|
64 | |
---|
65 | Digraph G; |
---|
66 | Node s, t; |
---|
67 | Arc e; |
---|
68 | int l, i; |
---|
69 | bool b; |
---|
70 | ::lemon::ignore_unused_variable_warning(l,i,b); |
---|
71 | |
---|
72 | DType::DistMap d(G); |
---|
73 | DType::PredMap p(G); |
---|
74 | Path<Digraph> pp; |
---|
75 | concepts::ReadMap<Arc,bool> am; |
---|
76 | |
---|
77 | { |
---|
78 | DType dfs_test(G); |
---|
79 | const DType& const_dfs_test = dfs_test; |
---|
80 | |
---|
81 | dfs_test.run(s); |
---|
82 | dfs_test.run(s,t); |
---|
83 | dfs_test.run(); |
---|
84 | |
---|
85 | dfs_test.init(); |
---|
86 | dfs_test.addSource(s); |
---|
87 | e = dfs_test.processNextArc(); |
---|
88 | e = const_dfs_test.nextArc(); |
---|
89 | b = const_dfs_test.emptyQueue(); |
---|
90 | i = const_dfs_test.queueSize(); |
---|
91 | |
---|
92 | dfs_test.start(); |
---|
93 | dfs_test.start(t); |
---|
94 | dfs_test.start(am); |
---|
95 | |
---|
96 | l = const_dfs_test.dist(t); |
---|
97 | e = const_dfs_test.predArc(t); |
---|
98 | s = const_dfs_test.predNode(t); |
---|
99 | b = const_dfs_test.reached(t); |
---|
100 | d = const_dfs_test.distMap(); |
---|
101 | p = const_dfs_test.predMap(); |
---|
102 | pp = const_dfs_test.path(t); |
---|
103 | } |
---|
104 | { |
---|
105 | DType |
---|
106 | ::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
---|
107 | ::SetDistMap<concepts::ReadWriteMap<Node,int> > |
---|
108 | ::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
---|
109 | ::SetStandardProcessedMap |
---|
110 | ::SetProcessedMap<concepts::WriteMap<Node,bool> > |
---|
111 | ::Create dfs_test(G); |
---|
112 | |
---|
113 | concepts::ReadWriteMap<Node,Arc> pred_map; |
---|
114 | concepts::ReadWriteMap<Node,int> dist_map; |
---|
115 | concepts::ReadWriteMap<Node,bool> reached_map; |
---|
116 | concepts::WriteMap<Node,bool> processed_map; |
---|
117 | |
---|
118 | dfs_test |
---|
119 | .predMap(pred_map) |
---|
120 | .distMap(dist_map) |
---|
121 | .reachedMap(reached_map) |
---|
122 | .processedMap(processed_map); |
---|
123 | |
---|
124 | dfs_test.run(s); |
---|
125 | dfs_test.run(s,t); |
---|
126 | dfs_test.run(); |
---|
127 | dfs_test.init(); |
---|
128 | |
---|
129 | dfs_test.addSource(s); |
---|
130 | e = dfs_test.processNextArc(); |
---|
131 | e = dfs_test.nextArc(); |
---|
132 | b = dfs_test.emptyQueue(); |
---|
133 | i = dfs_test.queueSize(); |
---|
134 | |
---|
135 | dfs_test.start(); |
---|
136 | dfs_test.start(t); |
---|
137 | dfs_test.start(am); |
---|
138 | |
---|
139 | l = dfs_test.dist(t); |
---|
140 | e = dfs_test.predArc(t); |
---|
141 | s = dfs_test.predNode(t); |
---|
142 | b = dfs_test.reached(t); |
---|
143 | pp = dfs_test.path(t); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | void checkDfsFunctionCompile() |
---|
148 | { |
---|
149 | typedef int VType; |
---|
150 | typedef concepts::Digraph Digraph; |
---|
151 | typedef Digraph::Arc Arc; |
---|
152 | typedef Digraph::Node Node; |
---|
153 | |
---|
154 | Digraph g; |
---|
155 | bool b; |
---|
156 | ::lemon::ignore_unused_variable_warning(b); |
---|
157 | |
---|
158 | dfs(g).run(Node()); |
---|
159 | b=dfs(g).run(Node(),Node()); |
---|
160 | dfs(g).run(); |
---|
161 | dfs(g) |
---|
162 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
163 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
164 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
165 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
166 | .run(Node()); |
---|
167 | b=dfs(g) |
---|
168 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
169 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
170 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
171 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
172 | .path(concepts::Path<Digraph>()) |
---|
173 | .dist(VType()) |
---|
174 | .run(Node(),Node()); |
---|
175 | dfs(g) |
---|
176 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
177 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
178 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
179 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
180 | .run(); |
---|
181 | } |
---|
182 | |
---|
183 | template <class Digraph> |
---|
184 | void checkDfs() { |
---|
185 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
186 | |
---|
187 | Digraph G; |
---|
188 | Node s, t; |
---|
189 | Node s1, t1; |
---|
190 | |
---|
191 | std::istringstream input(test_lgf); |
---|
192 | digraphReader(G, input). |
---|
193 | node("source", s). |
---|
194 | node("target", t). |
---|
195 | node("source1", s1). |
---|
196 | node("target1", t1). |
---|
197 | run(); |
---|
198 | |
---|
199 | Dfs<Digraph> dfs_test(G); |
---|
200 | dfs_test.run(s); |
---|
201 | |
---|
202 | Path<Digraph> p = dfs_test.path(t); |
---|
203 | check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
---|
204 | check(checkPath(G, p),"path() found a wrong path."); |
---|
205 | check(pathSource(G, p) == s,"path() found a wrong path."); |
---|
206 | check(pathTarget(G, p) == t,"path() found a wrong path."); |
---|
207 | |
---|
208 | for(NodeIt v(G); v!=INVALID; ++v) { |
---|
209 | if (dfs_test.reached(v)) { |
---|
210 | check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
---|
211 | if (dfs_test.predArc(v)!=INVALID ) { |
---|
212 | Arc e=dfs_test.predArc(v); |
---|
213 | Node u=G.source(e); |
---|
214 | check(u==dfs_test.predNode(v),"Wrong tree."); |
---|
215 | check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
---|
216 | "Wrong distance. (" << dfs_test.dist(u) << "->" |
---|
217 | << dfs_test.dist(v) << ")"); |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | { |
---|
223 | Dfs<Digraph> dfs(G); |
---|
224 | check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6."); |
---|
225 | } |
---|
226 | |
---|
227 | { |
---|
228 | NullMap<Node,Arc> myPredMap; |
---|
229 | dfs(G).predMap(myPredMap).run(s); |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | int main() |
---|
234 | { |
---|
235 | checkDfs<ListDigraph>(); |
---|
236 | checkDfs<SmartDigraph>(); |
---|
237 | return 0; |
---|
238 | } |
---|