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