6 #include <list_graph.h>
7 #include <smart_graph.h>
8 #include <bfs_iterator.h>
9 #include <graph_wrapper.h>
16 template <typename Graph, typename NodeNameMap>
19 NodeNameMap& node_name_map;
21 EdgeNameMap(Graph& _graph, NodeNameMap& _node_name_map) :
22 graph(_graph), node_name_map(_node_name_map) { }
23 string get(typename Graph::Edge e) const {
25 (node_name_map.get(graph.tail(e))+"->"+node_name_map.get(graph.head(e)));
29 int main (int, char*[])
31 //typedef SmartGraph Graph;
32 typedef ListGraph Graph;
34 typedef Graph::Node Node;
35 typedef Graph::Edge Edge;
36 //typedef Graph::NodeIt NodeIt;
37 //typedef Graph::EdgeIt EdgeIt;
38 //typedef Graph::OutEdgeIt OutEdgeIt;
39 //typedef Graph::InEdgeIt InEdgeIt;
40 //typedef Graph::SymEdgeIt SymEdgeIt;
51 Graph::NodeMap<string> node_name(G);
52 node_name.set(s, "s");
53 node_name.set(v1, "v1");
54 node_name.set(v2, "v2");
55 node_name.set(v3, "v3");
56 node_name.set(v4, "v4");
57 node_name.set(t, "t");
70 cout << " /--> -------------> "<< endl;
71 cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
72 cout << " / | | / /-> \\ "<< endl;
73 cout << " / | | / | ^ \\ "<< endl;
74 cout << "s | | / | | \\-> t "<< endl;
75 cout << " \\ | | / | | /-> "<< endl;
76 cout << " \\ | --/ / | | / "<< endl;
77 cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
78 cout << " \\--> -------------> "<< endl;
80 // typedef TrivGraphWrapper<const Graph> CGW;
83 // cout << "bfs and dfs demo on the directed graph" << endl;
84 // for(CGW::NodeIt n=gw.first<CGW::NodeIt>(); n.valid(); ++n) {
86 // cout << "out edges: ";
87 // for(CGW::OutEdgeIt e=gw.first<CGW::OutEdgeIt>(n); e.valid(); ++e)
89 // cout << "in edges: ";
90 // for(CGW::InEdgeIt e=gw.first<CGW::InEdgeIt>(n); e.valid(); ++e)
96 typedef TrivGraphWrapper<const Graph> GW;
99 EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
101 cout << "bfs and dfs iterator demo on the directed graph" << endl;
102 for(GW::NodeIt n=gw.first<GW::NodeIt>();
105 cout << node_name.get(n) << ": ";
106 cout << "out edges: ";
107 for(GW::OutEdgeIt e=gw.first<GW::OutEdgeIt>(n); gw.valid(e); gw.next(e))
108 cout << edge_name.get(e) << " ";
109 cout << "in edges: ";
110 for(GW::InEdgeIt e=gw.first<GW::InEdgeIt>(n); gw.valid(e); gw.next(e))
111 cout << edge_name.get(e) << " ";
115 cout << "bfs from s ..." << endl;
116 BfsIterator5< GW, GW::NodeMap<bool> > bfs(gw);
117 bfs.pushAndSetReached(s);
118 while (!bfs.finished()) {
121 cout << edge_name.get(bfs) << /*endl*/", " <<
122 node_name.get(gw.aNode(bfs)) <<
123 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
124 node_name.get(gw.bNode(bfs)) <<
125 (bfs.isBNodeNewlyReached() ? ": is newly reached." :
126 ": is not newly reached.");
128 cout << "invalid" << /*endl*/", " <<
129 node_name.get(bfs.aNode()) <<
130 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
138 cout << " /--> -------------> "<< endl;
139 cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
140 cout << " / | | / /-> \\ "<< endl;
141 cout << " / | | / | ^ \\ "<< endl;
142 cout << "s | | / | | \\-> t "<< endl;
143 cout << " \\ | | / | | /-> "<< endl;
144 cout << " \\ | --/ / | | / "<< endl;
145 cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
146 cout << " \\--> -------------> "<< endl;
148 cout << "dfs from s ..." << endl;
149 DfsIterator5< GW, GW::NodeMap<bool> > dfs(gw);
150 dfs.pushAndSetReached(s);
151 while (!dfs.finished()) {
155 cout << edge_name.get(dfs) << /*endl*/", " <<
156 node_name.get(gw.aNode(dfs)) <<
157 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
158 node_name.get(gw.bNode(dfs)) <<
159 (dfs.isBNodeNewlyReached() ? ": is newly reached." :
160 ": is not newly reached.");
162 cout << "invalid" << /*endl*/", " <<
163 node_name.get(dfs.aNode()) <<
164 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
174 typedef RevGraphWrapper<const TrivGraphWrapper<const Graph> > GW;
177 EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
179 cout << "bfs and dfs iterator demo on the reversed directed graph" << endl;
180 for(GW::NodeIt n=gw.first<GW::NodeIt>(); gw.valid(n); gw.next(n)) {
181 cout << node_name.get(n) << ": ";
182 cout << "out edges: ";
183 for(GW::OutEdgeIt e=gw.first<GW::OutEdgeIt>(n); gw.valid(e); gw.next(e))
184 cout << edge_name.get(e) << " ";
185 cout << "in edges: ";
186 for(GW::InEdgeIt e=gw.first<GW::InEdgeIt>(n); gw.valid(e); gw.next(e))
187 cout << edge_name.get(e) << " ";
191 cout << "bfs from t ..." << endl;
192 BfsIterator5< GW, GW::NodeMap<bool> > bfs(gw);
193 bfs.pushAndSetReached(t);
194 while (!bfs.finished()) {
197 cout << edge_name.get(bfs) << /*endl*/", " <<
198 node_name.get(gw.aNode(bfs)) <<
199 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
200 node_name.get(gw.bNode(bfs)) <<
201 (bfs.isBNodeNewlyReached() ? ": is newly reached." :
202 ": is not newly reached.");
204 cout << "invalid" << /*endl*/", " <<
205 node_name.get(bfs.aNode()) <<
206 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
214 cout << " /--> -------------> "<< endl;
215 cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
216 cout << " / | | / /-> \\ "<< endl;
217 cout << " / | | / | ^ \\ "<< endl;
218 cout << "s | | / | | \\-> t "<< endl;
219 cout << " \\ | | / | | /-> "<< endl;
220 cout << " \\ | --/ / | | / "<< endl;
221 cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
222 cout << " \\--> -------------> "<< endl;
224 cout << "dfs from t ..." << endl;
225 DfsIterator5< GW, GW::NodeMap<bool> > dfs(gw);
226 dfs.pushAndSetReached(t);
227 while (!dfs.finished()) {
231 cout << edge_name.get(dfs) << /*endl*/", " <<
232 node_name.get(gw.aNode(dfs)) <<
233 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
234 node_name.get(gw.bNode(dfs)) <<
235 (dfs.isBNodeNewlyReached() ? ": is newly reached." :
236 ": is not newly reached.");
238 cout << "invalid" << /*endl*/", " <<
239 node_name.get(dfs.aNode()) <<
240 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
249 //typedef UndirGraphWrapper<const Graph> GW;
250 typedef UndirGraphWrapper<const TrivGraphWrapper<const Graph> > GW;
253 EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
255 cout << "bfs and dfs iterator demo on the undirected graph" << endl;
256 for(GW::NodeIt n=gw.first<GW::NodeIt>(); gw.valid(n); gw.next(n)) {
257 cout << node_name.get(n) << ": ";
258 cout << "out edges: ";
259 for(GW::OutEdgeIt e=gw.first<GW::OutEdgeIt>(n); gw.valid(e); gw.next(e))
260 cout << edge_name.get(e) << " ";
261 cout << "in edges: ";
262 for(GW::InEdgeIt e=gw.first<GW::InEdgeIt>(n); gw.valid(e); gw.next(e))
263 cout << edge_name.get(e) << " ";
266 // for(GW::EdgeIt e=gw.first<GW::EdgeIt>(); gw.valid(e); gw.next(e)) {
267 // cout << edge_name.get(e) << " ";
271 cout << "bfs from t ..." << endl;
272 BfsIterator5< GW, GW::NodeMap<bool> > bfs(gw);
273 bfs.pushAndSetReached(t);
274 while (!bfs.finished()) {
276 if (gw.valid(GW::OutEdgeIt(bfs))) {
277 cout << edge_name.get(GW::OutEdgeIt(bfs)) << /*endl*/", " <<
278 node_name.get(gw.aNode(bfs)) <<
279 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
280 node_name.get(gw.bNode(bfs)) <<
281 (bfs.isBNodeNewlyReached() ? ": is newly reached." :
282 ": is not newly reached.");
284 cout << "invalid" << /*endl*/", " <<
285 node_name.get(bfs.aNode()) <<
286 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
294 cout << " /--> -------------> "<< endl;
295 cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;
296 cout << " / | | / /-> \\ "<< endl;
297 cout << " / | | / | ^ \\ "<< endl;
298 cout << "s | | / | | \\-> t "<< endl;
299 cout << " \\ | | / | | /-> "<< endl;
300 cout << " \\ | --/ / | | / "<< endl;
301 cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;
302 cout << " \\--> -------------> "<< endl;
304 cout << "dfs from t ..." << endl;
305 DfsIterator5< GW, GW::NodeMap<bool> > dfs(gw);
306 dfs.pushAndSetReached(t);
307 while (!dfs.finished()) {
310 if (gw.valid(GW::OutEdgeIt(dfs))) {
311 cout << edge_name.get(GW::OutEdgeIt(dfs)) << /*endl*/", " <<
312 node_name.get(gw.aNode(dfs)) <<
313 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<
314 node_name.get(gw.bNode(dfs)) <<
315 (dfs.isBNodeNewlyReached() ? ": is newly reached." :
316 ": is not newly reached.");
318 cout << "invalid" << /*endl*/", " <<
319 node_name.get(dfs.aNode()) <<
320 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<