marci@301
|
1 |
// -*- c++ -*-
|
marci@301
|
2 |
#include <iostream>
|
marci@301
|
3 |
#include <vector>
|
marci@301
|
4 |
#include <string>
|
marci@301
|
5 |
|
marci@301
|
6 |
#include <list_graph.h>
|
marci@301
|
7 |
#include <smart_graph.h>
|
marci@301
|
8 |
#include <bfs_iterator.h>
|
marci@301
|
9 |
#include <graph_wrapper.h>
|
marci@301
|
10 |
|
marci@301
|
11 |
using namespace hugo;
|
marci@301
|
12 |
using std::cout;
|
marci@301
|
13 |
using std::endl;
|
marci@301
|
14 |
using std::string;
|
marci@301
|
15 |
|
marci@301
|
16 |
template <typename Graph, typename NodeNameMap>
|
marci@301
|
17 |
class EdgeNameMap {
|
marci@301
|
18 |
Graph& graph;
|
marci@301
|
19 |
NodeNameMap& node_name_map;
|
marci@301
|
20 |
public:
|
marci@301
|
21 |
EdgeNameMap(Graph& _graph, NodeNameMap& _node_name_map) :
|
marci@301
|
22 |
graph(_graph), node_name_map(_node_name_map) { }
|
marci@301
|
23 |
string get(typename Graph::Edge e) const {
|
marci@301
|
24 |
return
|
marci@301
|
25 |
(node_name_map.get(graph.tail(e))+"->"+node_name_map.get(graph.head(e)));
|
marci@301
|
26 |
}
|
marci@301
|
27 |
};
|
marci@301
|
28 |
|
marci@301
|
29 |
int main (int, char*[])
|
marci@301
|
30 |
{
|
marci@301
|
31 |
//typedef SmartGraph Graph;
|
marci@301
|
32 |
typedef ListGraph Graph;
|
marci@301
|
33 |
|
marci@301
|
34 |
typedef Graph::Node Node;
|
marci@301
|
35 |
typedef Graph::Edge Edge;
|
marci@301
|
36 |
|
marci@301
|
37 |
Graph G;
|
marci@301
|
38 |
|
marci@301
|
39 |
Node s=G.addNode();
|
marci@301
|
40 |
Node v1=G.addNode();
|
marci@301
|
41 |
Node v2=G.addNode();
|
marci@301
|
42 |
Node v3=G.addNode();
|
marci@301
|
43 |
Node v4=G.addNode();
|
marci@301
|
44 |
Node t=G.addNode();
|
marci@301
|
45 |
|
marci@301
|
46 |
Graph::NodeMap<string> node_name(G);
|
marci@301
|
47 |
node_name.set(s, "s");
|
marci@301
|
48 |
node_name.set(v1, "v1");
|
marci@301
|
49 |
node_name.set(v2, "v2");
|
marci@301
|
50 |
node_name.set(v3, "v3");
|
marci@301
|
51 |
node_name.set(v4, "v4");
|
marci@301
|
52 |
node_name.set(t, "t");
|
marci@301
|
53 |
|
marci@301
|
54 |
G.addEdge(s, v1);
|
marci@301
|
55 |
G.addEdge(s, v2);
|
marci@301
|
56 |
G.addEdge(v1, v2);
|
marci@301
|
57 |
G.addEdge(v2, v1);
|
marci@301
|
58 |
G.addEdge(v1, v3);
|
marci@301
|
59 |
G.addEdge(v3, v2);
|
marci@301
|
60 |
G.addEdge(v2, v4);
|
marci@301
|
61 |
G.addEdge(v4, v3);
|
marci@301
|
62 |
G.addEdge(v3, t);
|
marci@301
|
63 |
G.addEdge(v4, t);
|
marci@301
|
64 |
|
marci@301
|
65 |
cout << " /--> -------------> "<< endl;
|
marci@301
|
66 |
cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
|
marci@301
|
67 |
cout << " / | | / /-> \\ "<< endl;
|
marci@301
|
68 |
cout << " / | | / | ^ \\ "<< endl;
|
marci@301
|
69 |
cout << "s | | / | | \\-> t "<< endl;
|
marci@301
|
70 |
cout << " \\ | | / | | /-> "<< endl;
|
marci@301
|
71 |
cout << " \\ | --/ / | | / "<< endl;
|
marci@301
|
72 |
cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
|
marci@301
|
73 |
cout << " \\--> -------------> "<< endl;
|
marci@301
|
74 |
|
marci@301
|
75 |
// typedef TrivGraphWrapper<const Graph> CGW;
|
marci@301
|
76 |
// CGW gw(G);
|
marci@301
|
77 |
|
marci@301
|
78 |
// cout << "bfs and dfs demo on the directed graph" << endl;
|
marci@301
|
79 |
// for(CGW::NodeIt n=gw.first<CGW::NodeIt>(); n.valid(); ++n) {
|
marci@301
|
80 |
// cout << n << ": ";
|
marci@301
|
81 |
// cout << "out edges: ";
|
marci@301
|
82 |
// for(CGW::OutEdgeIt e=gw.first<CGW::OutEdgeIt>(n); e.valid(); ++e)
|
marci@301
|
83 |
// cout << e << " ";
|
marci@301
|
84 |
// cout << "in edges: ";
|
marci@301
|
85 |
// for(CGW::InEdgeIt e=gw.first<CGW::InEdgeIt>(n); e.valid(); ++e)
|
marci@301
|
86 |
// cout << e << " ";
|
marci@301
|
87 |
// cout << endl;
|
marci@301
|
88 |
// }
|
marci@301
|
89 |
|
marci@301
|
90 |
{
|
marci@301
|
91 |
typedef TrivGraphWrapper<const Graph> GW;
|
marci@301
|
92 |
GW gw(G);
|
marci@301
|
93 |
|
marci@301
|
94 |
EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
|
marci@301
|
95 |
|
marci@301
|
96 |
cout << "bfs and dfs iterator demo on the directed graph" << endl;
|
marci@301
|
97 |
for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) {
|
marci@301
|
98 |
cout << node_name.get(n) << ": ";
|
marci@301
|
99 |
cout << "out edges: ";
|
marci@301
|
100 |
for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e))
|
marci@301
|
101 |
cout << edge_name.get(e) << " ";
|
marci@301
|
102 |
cout << "in edges: ";
|
marci@301
|
103 |
for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e))
|
marci@301
|
104 |
cout << edge_name.get(e) << " ";
|
marci@301
|
105 |
cout << endl;
|
marci@301
|
106 |
}
|
marci@301
|
107 |
|
marci@301
|
108 |
cout << "bfs from s ..." << endl;
|
marci@301
|
109 |
BfsIterator5< GW, GW::NodeMap<bool> > bfs(gw);
|
marci@301
|
110 |
bfs.pushAndSetReached(s);
|
marci@301
|
111 |
while (!bfs.finished()) {
|
marci@301
|
112 |
//cout << "edge: ";
|
marci@301
|
113 |
if (gw.valid(bfs)) {
|
marci@301
|
114 |
cout << edge_name.get(bfs) << /*endl*/", " <<
|
marci@301
|
115 |
node_name.get(gw.aNode(bfs)) <<
|
marci@301
|
116 |
(bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
117 |
node_name.get(gw.bNode(bfs)) <<
|
marci@301
|
118 |
(bfs.isBNodeNewlyReached() ? ": is newly reached." :
|
marci@301
|
119 |
": is not newly reached.");
|
marci@301
|
120 |
} else {
|
marci@301
|
121 |
cout << "invalid" << /*endl*/", " <<
|
marci@301
|
122 |
node_name.get(bfs.aNode()) <<
|
marci@301
|
123 |
(bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
124 |
|
marci@301
|
125 |
"invalid.";
|
marci@301
|
126 |
}
|
marci@301
|
127 |
cout << endl;
|
marci@301
|
128 |
++bfs;
|
marci@301
|
129 |
}
|
marci@301
|
130 |
|
marci@301
|
131 |
cout << " /--> -------------> "<< endl;
|
marci@301
|
132 |
cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
|
marci@301
|
133 |
cout << " / | | / /-> \\ "<< endl;
|
marci@301
|
134 |
cout << " / | | / | ^ \\ "<< endl;
|
marci@301
|
135 |
cout << "s | | / | | \\-> t "<< endl;
|
marci@301
|
136 |
cout << " \\ | | / | | /-> "<< endl;
|
marci@301
|
137 |
cout << " \\ | --/ / | | / "<< endl;
|
marci@301
|
138 |
cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
|
marci@301
|
139 |
cout << " \\--> -------------> "<< endl;
|
marci@301
|
140 |
|
marci@301
|
141 |
cout << "dfs from s ..." << endl;
|
marci@301
|
142 |
DfsIterator5< GW, GW::NodeMap<bool> > dfs(gw);
|
marci@301
|
143 |
dfs.pushAndSetReached(s);
|
marci@301
|
144 |
while (!dfs.finished()) {
|
marci@301
|
145 |
++dfs;
|
marci@301
|
146 |
//cout << "edge: ";
|
marci@301
|
147 |
if (gw.valid(dfs)) {
|
marci@301
|
148 |
cout << edge_name.get(dfs) << /*endl*/", " <<
|
marci@301
|
149 |
node_name.get(gw.aNode(dfs)) <<
|
marci@301
|
150 |
(dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
151 |
node_name.get(gw.bNode(dfs)) <<
|
marci@301
|
152 |
(dfs.isBNodeNewlyReached() ? ": is newly reached." :
|
marci@301
|
153 |
": is not newly reached.");
|
marci@301
|
154 |
} else {
|
marci@301
|
155 |
cout << "invalid" << /*endl*/", " <<
|
marci@301
|
156 |
node_name.get(dfs.aNode()) <<
|
marci@301
|
157 |
(dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
158 |
|
marci@301
|
159 |
"invalid.";
|
marci@301
|
160 |
}
|
marci@301
|
161 |
cout << endl;
|
marci@301
|
162 |
}
|
marci@301
|
163 |
}
|
marci@301
|
164 |
|
marci@301
|
165 |
|
marci@301
|
166 |
{
|
marci@301
|
167 |
typedef RevGraphWrapper<const TrivGraphWrapper<const Graph> > GW;
|
marci@301
|
168 |
GW gw(G);
|
marci@301
|
169 |
|
marci@301
|
170 |
EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
|
marci@301
|
171 |
|
marci@301
|
172 |
cout << "bfs and dfs iterator demo on the reversed directed graph" << endl;
|
marci@301
|
173 |
for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) {
|
marci@301
|
174 |
cout << node_name.get(n) << ": ";
|
marci@301
|
175 |
cout << "out edges: ";
|
marci@301
|
176 |
for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e))
|
marci@301
|
177 |
cout << edge_name.get(e) << " ";
|
marci@301
|
178 |
cout << "in edges: ";
|
marci@301
|
179 |
for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e))
|
marci@301
|
180 |
cout << edge_name.get(e) << " ";
|
marci@301
|
181 |
cout << endl;
|
marci@301
|
182 |
}
|
marci@301
|
183 |
|
marci@301
|
184 |
cout << "bfs from t ..." << endl;
|
marci@301
|
185 |
BfsIterator5< GW, GW::NodeMap<bool> > bfs(gw);
|
marci@301
|
186 |
bfs.pushAndSetReached(t);
|
marci@301
|
187 |
while (!bfs.finished()) {
|
marci@301
|
188 |
//cout << "edge: ";
|
marci@301
|
189 |
if (gw.valid(bfs)) {
|
marci@301
|
190 |
cout << edge_name.get(bfs) << /*endl*/", " <<
|
marci@301
|
191 |
node_name.get(gw.aNode(bfs)) <<
|
marci@301
|
192 |
(bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
193 |
node_name.get(gw.bNode(bfs)) <<
|
marci@301
|
194 |
(bfs.isBNodeNewlyReached() ? ": is newly reached." :
|
marci@301
|
195 |
": is not newly reached.");
|
marci@301
|
196 |
} else {
|
marci@301
|
197 |
cout << "invalid" << /*endl*/", " <<
|
marci@301
|
198 |
node_name.get(bfs.aNode()) <<
|
marci@301
|
199 |
(bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
200 |
|
marci@301
|
201 |
"invalid.";
|
marci@301
|
202 |
}
|
marci@301
|
203 |
cout << endl;
|
marci@301
|
204 |
++bfs;
|
marci@301
|
205 |
}
|
marci@301
|
206 |
|
marci@301
|
207 |
cout << " /--> -------------> "<< endl;
|
marci@301
|
208 |
cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
|
marci@301
|
209 |
cout << " / | | / /-> \\ "<< endl;
|
marci@301
|
210 |
cout << " / | | / | ^ \\ "<< endl;
|
marci@301
|
211 |
cout << "s | | / | | \\-> t "<< endl;
|
marci@301
|
212 |
cout << " \\ | | / | | /-> "<< endl;
|
marci@301
|
213 |
cout << " \\ | --/ / | | / "<< endl;
|
marci@301
|
214 |
cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
|
marci@301
|
215 |
cout << " \\--> -------------> "<< endl;
|
marci@301
|
216 |
|
marci@301
|
217 |
cout << "dfs from t ..." << endl;
|
marci@301
|
218 |
DfsIterator5< GW, GW::NodeMap<bool> > dfs(gw);
|
marci@301
|
219 |
dfs.pushAndSetReached(t);
|
marci@301
|
220 |
while (!dfs.finished()) {
|
marci@301
|
221 |
++dfs;
|
marci@301
|
222 |
//cout << "edge: ";
|
marci@301
|
223 |
if (gw.valid(dfs)) {
|
marci@301
|
224 |
cout << edge_name.get(dfs) << /*endl*/", " <<
|
marci@301
|
225 |
node_name.get(gw.aNode(dfs)) <<
|
marci@301
|
226 |
(dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
227 |
node_name.get(gw.bNode(dfs)) <<
|
marci@301
|
228 |
(dfs.isBNodeNewlyReached() ? ": is newly reached." :
|
marci@301
|
229 |
": is not newly reached.");
|
marci@301
|
230 |
} else {
|
marci@301
|
231 |
cout << "invalid" << /*endl*/", " <<
|
marci@301
|
232 |
node_name.get(dfs.aNode()) <<
|
marci@301
|
233 |
(dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
234 |
|
marci@301
|
235 |
"invalid.";
|
marci@301
|
236 |
}
|
marci@301
|
237 |
cout << endl;
|
marci@301
|
238 |
}
|
marci@301
|
239 |
}
|
marci@301
|
240 |
|
marci@301
|
241 |
{
|
marci@301
|
242 |
//typedef UndirGraphWrapper<const Graph> GW;
|
marci@301
|
243 |
typedef UndirGraphWrapper<const TrivGraphWrapper<const Graph> > GW;
|
marci@301
|
244 |
GW gw(G);
|
marci@301
|
245 |
|
marci@301
|
246 |
EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
|
marci@301
|
247 |
|
marci@301
|
248 |
cout << "bfs and dfs iterator demo on the undirected graph" << endl;
|
marci@301
|
249 |
for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) {
|
marci@301
|
250 |
cout << node_name.get(n) << ": ";
|
marci@301
|
251 |
cout << "out edges: ";
|
marci@301
|
252 |
for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e))
|
marci@301
|
253 |
cout << edge_name.get(e) << " ";
|
marci@301
|
254 |
cout << "in edges: ";
|
marci@301
|
255 |
for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e))
|
marci@301
|
256 |
cout << edge_name.get(e) << " ";
|
marci@301
|
257 |
cout << endl;
|
marci@301
|
258 |
}
|
marci@301
|
259 |
// for(GW::EdgeIt e=gw.first<GW::EdgeIt>(); gw.valid(e); gw.next(e)) {
|
marci@301
|
260 |
// cout << edge_name.get(e) << " ";
|
marci@301
|
261 |
// }
|
marci@301
|
262 |
// cout << endl;
|
marci@301
|
263 |
|
marci@301
|
264 |
cout << "bfs from t ..." << endl;
|
marci@301
|
265 |
BfsIterator5< GW, GW::NodeMap<bool> > bfs(gw);
|
marci@301
|
266 |
bfs.pushAndSetReached(t);
|
marci@301
|
267 |
while (!bfs.finished()) {
|
marci@301
|
268 |
//cout << "edge: ";
|
marci@301
|
269 |
if (gw.valid(GW::OutEdgeIt(bfs))) {
|
marci@301
|
270 |
cout << edge_name.get(GW::OutEdgeIt(bfs)) << /*endl*/", " <<
|
marci@301
|
271 |
node_name.get(gw.aNode(bfs)) <<
|
marci@301
|
272 |
(bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
273 |
node_name.get(gw.bNode(bfs)) <<
|
marci@301
|
274 |
(bfs.isBNodeNewlyReached() ? ": is newly reached." :
|
marci@301
|
275 |
": is not newly reached.");
|
marci@301
|
276 |
} else {
|
marci@301
|
277 |
cout << "invalid" << /*endl*/", " <<
|
marci@301
|
278 |
node_name.get(bfs.aNode()) <<
|
marci@301
|
279 |
(bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
280 |
|
marci@301
|
281 |
"invalid.";
|
marci@301
|
282 |
}
|
marci@301
|
283 |
cout << endl;
|
marci@301
|
284 |
++bfs;
|
marci@301
|
285 |
}
|
marci@301
|
286 |
|
marci@301
|
287 |
cout << " /--> -------------> "<< endl;
|
marci@301
|
288 |
cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
|
marci@301
|
289 |
cout << " / | | / /-> \\ "<< endl;
|
marci@301
|
290 |
cout << " / | | / | ^ \\ "<< endl;
|
marci@301
|
291 |
cout << "s | | / | | \\-> t "<< endl;
|
marci@301
|
292 |
cout << " \\ | | / | | /-> "<< endl;
|
marci@301
|
293 |
cout << " \\ | --/ / | | / "<< endl;
|
marci@301
|
294 |
cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
|
marci@301
|
295 |
cout << " \\--> -------------> "<< endl;
|
marci@301
|
296 |
|
marci@301
|
297 |
cout << "dfs from t ..." << endl;
|
marci@301
|
298 |
DfsIterator5< GW, GW::NodeMap<bool> > dfs(gw);
|
marci@301
|
299 |
dfs.pushAndSetReached(t);
|
marci@301
|
300 |
while (!dfs.finished()) {
|
marci@301
|
301 |
++dfs;
|
marci@301
|
302 |
//cout << "edge: ";
|
marci@301
|
303 |
if (gw.valid(GW::OutEdgeIt(dfs))) {
|
marci@301
|
304 |
cout << edge_name.get(GW::OutEdgeIt(dfs)) << /*endl*/", " <<
|
marci@301
|
305 |
node_name.get(gw.aNode(dfs)) <<
|
marci@301
|
306 |
(dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
307 |
node_name.get(gw.bNode(dfs)) <<
|
marci@301
|
308 |
(dfs.isBNodeNewlyReached() ? ": is newly reached." :
|
marci@301
|
309 |
": is not newly reached.");
|
marci@301
|
310 |
} else {
|
marci@301
|
311 |
cout << "invalid" << /*endl*/", " <<
|
marci@301
|
312 |
node_name.get(dfs.aNode()) <<
|
marci@301
|
313 |
(dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
|
marci@301
|
314 |
|
marci@301
|
315 |
"invalid.";
|
marci@301
|
316 |
}
|
marci@301
|
317 |
cout << endl;
|
marci@301
|
318 |
}
|
marci@301
|
319 |
}
|
marci@301
|
320 |
|
marci@301
|
321 |
return 0;
|
marci@301
|
322 |
}
|