5 #include <list_graph.hh>
6 #include <bfs_iterator.hh>
7 #include <graph_wrapper.h>
14 template <typename Graph, typename NodeNameMap>
17 NodeNameMap& node_name_map;
19 EdgeNameMap(Graph& _graph, NodeNameMap& _node_name_map) :
20 graph(_graph), node_name_map(_node_name_map) { }
21 string get(typename Graph::EdgeIt e) const {
23 (node_name_map.get(graph.tail(e))+"->"+node_name_map.get(graph.head(e)));
27 int main (int, char*[])
29 typedef ListGraph::NodeIt NodeIt;
30 typedef ListGraph::EdgeIt EdgeIt;
31 //typedef ListGraph::EachNodeIt EachNodeIt;
32 //typedef ListGraph::EachEdgeIt EachEdgeIt;
33 //typedef ListGraph::OutEdgeIt OutEdgeIt;
34 //typedef ListGraph::InEdgeIt InEdgeIt;
35 //typedef ListGraph::SymEdgeIt SymEdgeIt;
40 NodeIt v1=G.addNode();
41 NodeIt v2=G.addNode();
42 NodeIt v3=G.addNode();
43 NodeIt v4=G.addNode();
46 ListGraph::NodeMap<string> node_name(G);
47 node_name.set(s, "s");
48 node_name.set(v1, "v1");
49 node_name.set(v2, "v2");
50 node_name.set(v3, "v3");
51 node_name.set(v4, "v4");
52 node_name.set(t, "t");
65 cout << " /--> -------------> "<< endl;
66 cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
67 cout << " / | | / /-> \\ "<< endl;
68 cout << " / | | / | ^ \\ "<< endl;
69 cout << "s | | / | | \\-> t "<< endl;
70 cout << " \\ | | / | | /-> "<< endl;
71 cout << " \\ | --/ / | | / "<< endl;
72 cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
73 cout << " \\--> -------------> "<< endl;
77 cout << "iterator bfs demo 4 ..." << endl;
78 BfsIterator4< ListGraph, ListGraph::OutEdgeIt, ListGraph::NodeMap<bool> > bfs(G);
79 bfs.pushAndSetReached(s);
80 while (!bfs.finished()) {
81 if (OutEdgeIt(bfs).valid()) {
82 cout << "OutEdgeIt: " << bfs;
83 cout << " aNode: " << G.aNode(bfs);
84 cout << " bNode: " << G.bNode(bfs) << " ";
86 cout << "OutEdgeIt: " << "invalid";
87 cout << " aNode: " << bfs.aNode();
88 cout << " bNode: " << "invalid" << " ";
90 if (bfs.isBNodeNewlyReached()) {
91 cout << "bNodeIsNewlyReached ";
93 cout << "bNodeIsNotNewlyReached ";
95 if (bfs.isANodeExamined()) {
96 cout << "aNodeIsExamined ";
98 cout << "aNodeIsNotExamined ";
106 cout << "iterator dfs demo 4 ..." << endl;
107 DfsIterator4< ListGraph, ListGraph::OutEdgeIt, ListGraph::NodeMap<bool> > dfs(G);
108 dfs.pushAndSetReached(s);
109 while (!dfs.finished()) {
111 if (OutEdgeIt(dfs).valid()) {
112 cout << "OutEdgeIt: " << dfs;
113 cout << " aNode: " << G.aNode(dfs);
114 cout << " bNode: " << G.bNode(dfs) << " ";
116 cout << "OutEdgeIt: " << "invalid";
117 cout << " aNode: " << dfs.aNode();
118 cout << " bNode: " << "invalid" << " ";
120 if (dfs.isBNodeNewlyReached()) {
121 cout << "bNodeIsNewlyReached ";
123 cout << "bNodeIsNotNewlyReached ";
125 if (dfs.isANodeExamined()) {
126 cout << "aNodeIsExamined ";
128 cout << "aNodeIsNotExamined ";
136 // typedef TrivGraphWrapper<const ListGraph> CGW;
139 // cout << "bfs and dfs demo on the directed graph" << endl;
140 // for(CGW::EachNodeIt n=wG.first<CGW::EachNodeIt>(); n.valid(); ++n) {
141 // cout << n << ": ";
142 // cout << "out edges: ";
143 // for(CGW::OutEdgeIt e=wG.first<CGW::OutEdgeIt>(n); e.valid(); ++e)
145 // cout << "in edges: ";
146 // for(CGW::InEdgeIt e=wG.first<CGW::InEdgeIt>(n); e.valid(); ++e)
152 typedef TrivGraphWrapper<const ListGraph> GW;
155 EdgeNameMap< GW, ListGraph::NodeMap<string> > edge_name(wG, node_name);
157 cout << "bfs and dfs iterator demo on the directed graph" << endl;
158 for(GW::EachNodeIt n=wG.first<GW::EachNodeIt>(); wG.valid(n); wG.next(n)) {
159 cout << node_name.get(n) << ": ";
160 cout << "out edges: ";
161 for(GW::OutEdgeIt e=wG.first<GW::OutEdgeIt>(n); wG.valid(e); wG.next(e))
162 cout << edge_name.get(e) << " ";
163 cout << "in edges: ";
164 for(GW::InEdgeIt e=wG.first<GW::InEdgeIt>(n); wG.valid(e); wG.next(e))
165 cout << edge_name.get(e) << " ";
169 cout << "bfs from s ..." << endl;
170 BfsIterator5< GW, GW::NodeMap<bool> > bfs(wG);
171 bfs.pushAndSetReached(s);
172 while (!bfs.finished()) {
175 cout << edge_name.get(bfs) << /*endl*/", " <<
176 /*" aNode: " <<*/ node_name.get(wG.aNode(bfs)) <<
177 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
178 /*" bNode: " <<*/ node_name.get(wG.bNode(bfs)) <<
179 (bfs.isBNodeNewlyReached() ? ": is newly reached." :
180 ": is not newly reached.");
182 cout << "invalid" << /*endl*/", " <<
183 /*" aNode: " <<*/ node_name.get(bfs.aNode()) <<
184 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
192 cout << " /--> -------------> "<< endl;
193 cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
194 cout << " / | | / /-> \\ "<< endl;
195 cout << " / | | / | ^ \\ "<< endl;
196 cout << "s | | / | | \\-> t "<< endl;
197 cout << " \\ | | / | | /-> "<< endl;
198 cout << " \\ | --/ / | | / "<< endl;
199 cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
200 cout << " \\--> -------------> "<< endl;
202 cout << "dfs from s ..." << endl;
203 DfsIterator5< GW, GW::NodeMap<bool> > dfs(wG);
204 dfs.pushAndSetReached(s);
205 while (!dfs.finished()) {
209 cout << edge_name.get(dfs) << /*endl*/", " <<
210 /*" aNode: " <<*/ node_name.get(wG.aNode(dfs)) <<
211 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
212 /*" bNode: " <<*/ node_name.get(wG.bNode(dfs)) <<
213 (dfs.isBNodeNewlyReached() ? ": is newly reached." :
214 ": is not newly reached.");
216 cout << "invalid" << /*endl*/", " <<
217 /*" aNode: " <<*/ node_name.get(dfs.aNode()) <<
218 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
228 typedef RevGraphWrapper<const ListGraph> GW;
231 EdgeNameMap< GW, ListGraph::NodeMap<string> > edge_name(wG, node_name);
233 cout << "bfs and dfs iterator demo on the reversed directed graph" << endl;
234 for(GW::EachNodeIt n=wG.first<GW::EachNodeIt>(); wG.valid(n); wG.next(n)) {
235 cout << node_name.get(n) << ": ";
236 cout << "out edges: ";
237 for(GW::OutEdgeIt e=wG.first<GW::OutEdgeIt>(n); wG.valid(e); wG.next(e))
238 cout << edge_name.get(e) << " ";
239 cout << "in edges: ";
240 for(GW::InEdgeIt e=wG.first<GW::InEdgeIt>(n); wG.valid(e); wG.next(e))
241 cout << edge_name.get(e) << " ";
245 cout << "bfs from t ..." << endl;
246 BfsIterator5< GW, GW::NodeMap<bool> > bfs(wG);
247 bfs.pushAndSetReached(t);
248 while (!bfs.finished()) {
251 cout << edge_name.get(bfs) << /*endl*/", " <<
252 /*" aNode: " <<*/ node_name.get(wG.aNode(bfs)) <<
253 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
254 /*" bNode: " <<*/ node_name.get(wG.bNode(bfs)) <<
255 (bfs.isBNodeNewlyReached() ? ": is newly reached." :
256 ": is not newly reached.");
258 cout << "invalid" << /*endl*/", " <<
259 /*" aNode: " <<*/ node_name.get(bfs.aNode()) <<
260 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
268 cout << " /--> -------------> "<< endl;
269 cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
270 cout << " / | | / /-> \\ "<< endl;
271 cout << " / | | / | ^ \\ "<< endl;
272 cout << "s | | / | | \\-> t "<< endl;
273 cout << " \\ | | / | | /-> "<< endl;
274 cout << " \\ | --/ / | | / "<< endl;
275 cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
276 cout << " \\--> -------------> "<< endl;
278 cout << "dfs from t ..." << endl;
279 DfsIterator5< GW, GW::NodeMap<bool> > dfs(wG);
280 dfs.pushAndSetReached(t);
281 while (!dfs.finished()) {
285 cout << edge_name.get(dfs) << /*endl*/", " <<
286 /*" aNode: " <<*/ node_name.get(wG.aNode(dfs)) <<
287 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
288 /*" bNode: " <<*/ node_name.get(wG.bNode(dfs)) <<
289 (dfs.isBNodeNewlyReached() ? ": is newly reached." :
290 ": is not newly reached.");
292 cout << "invalid" << /*endl*/", " <<
293 /*" aNode: " <<*/ node_name.get(dfs.aNode()) <<
294 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
303 typedef UndirGraphWrapper<const ListGraph> GW;
306 EdgeNameMap< GW, ListGraph::NodeMap<string> > edge_name(wG, node_name);
308 cout << "bfs and dfs iterator demo on the undirected graph" << endl;
309 for(GW::EachNodeIt n=wG.first<GW::EachNodeIt>(); wG.valid(n); wG.next(n)) {
310 cout << node_name.get(n) << ": ";
311 cout << "out edges: ";
312 for(GW::OutEdgeIt e=wG.first<GW::OutEdgeIt>(n); wG.valid(e); wG.next(e))
313 cout << edge_name.get(e) << " ";
314 cout << "in edges: ";
315 for(GW::InEdgeIt e=wG.first<GW::InEdgeIt>(n); wG.valid(e); wG.next(e))
316 cout << edge_name.get(e) << " ";
320 cout << "bfs from t ..." << endl;
321 BfsIterator5< GW, GW::NodeMap<bool> > bfs(wG);
322 bfs.pushAndSetReached(t);
323 while (!bfs.finished()) {
325 if (wG.valid(GW::OutEdgeIt(bfs))) {
326 cout << edge_name.get(GW::OutEdgeIt(bfs)) << /*endl*/", " <<
327 /*" aNode: " <<*/ node_name.get(wG.aNode(bfs)) <<
328 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
329 /*" bNode: " <<*/ node_name.get(wG.bNode(bfs)) <<
330 (bfs.isBNodeNewlyReached() ? ": is newly reached." :
331 ": is not newly reached.");
333 cout << "invalid" << /*endl*/", " <<
334 /*" aNode: " <<*/ node_name.get(bfs.aNode()) <<
335 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
343 cout << " /--> -------------> "<< endl;
344 cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
345 cout << " / | | / /-> \\ "<< endl;
346 cout << " / | | / | ^ \\ "<< endl;
347 cout << "s | | / | | \\-> t "<< endl;
348 cout << " \\ | | / | | /-> "<< endl;
349 cout << " \\ | --/ / | | / "<< endl;
350 cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
351 cout << " \\--> -------------> "<< endl;
353 cout << "dfs from t ..." << endl;
354 DfsIterator5< GW, GW::NodeMap<bool> > dfs(wG);
355 dfs.pushAndSetReached(t);
356 while (!dfs.finished()) {
359 if (wG.valid(GW::OutEdgeIt(dfs))) {
360 cout << edge_name.get(GW::OutEdgeIt(dfs)) << /*endl*/", " <<
361 /*" aNode: " <<*/ node_name.get(wG.aNode(dfs)) <<
362 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
363 /*" bNode: " <<*/ node_name.get(wG.bNode(dfs)) <<
364 (dfs.isBNodeNewlyReached() ? ": is newly reached." :
365 ": is not newly reached.");
367 cout << "invalid" << /*endl*/", " <<
368 /*" aNode: " <<*/ node_name.get(dfs.aNode()) <<
369 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<