marci@45
|
1 |
#include <iostream>
|
marci@45
|
2 |
#include <vector>
|
marci@45
|
3 |
#include <string>
|
marci@45
|
4 |
|
marci@45
|
5 |
#include <list_graph.hh>
|
marci@45
|
6 |
#include <bfs_iterator.hh>
|
marci@45
|
7 |
|
marci@45
|
8 |
using namespace marci;
|
marci@45
|
9 |
|
marci@45
|
10 |
int main (int, char*[])
|
marci@45
|
11 |
{
|
marci@45
|
12 |
typedef ListGraph::NodeIt NodeIt;
|
marci@45
|
13 |
typedef ListGraph::EdgeIt EdgeIt;
|
marci@45
|
14 |
typedef ListGraph::EachNodeIt EachNodeIt;
|
marci@45
|
15 |
typedef ListGraph::EachEdgeIt EachEdgeIt;
|
marci@45
|
16 |
typedef ListGraph::OutEdgeIt OutEdgeIt;
|
marci@45
|
17 |
typedef ListGraph::InEdgeIt InEdgeIt;
|
marci@45
|
18 |
typedef ListGraph::SymEdgeIt SymEdgeIt;
|
marci@45
|
19 |
|
marci@45
|
20 |
ListGraph G;
|
marci@45
|
21 |
|
marci@45
|
22 |
NodeIt s=G.addNode();
|
marci@45
|
23 |
NodeIt v1=G.addNode();
|
marci@45
|
24 |
NodeIt v2=G.addNode();
|
marci@45
|
25 |
NodeIt v3=G.addNode();
|
marci@45
|
26 |
NodeIt v4=G.addNode();
|
marci@45
|
27 |
NodeIt t=G.addNode();
|
marci@45
|
28 |
|
marci@45
|
29 |
G.addEdge(s, v1);
|
marci@45
|
30 |
G.addEdge(s, v2);
|
marci@45
|
31 |
G.addEdge(v1, v2);
|
marci@45
|
32 |
G.addEdge(v2, v1);
|
marci@45
|
33 |
G.addEdge(v1, v3);
|
marci@45
|
34 |
G.addEdge(v3, v2);
|
marci@45
|
35 |
G.addEdge(v2, v4);
|
marci@45
|
36 |
G.addEdge(v4, v3);
|
marci@45
|
37 |
G.addEdge(v3, t);
|
marci@45
|
38 |
G.addEdge(v4, t);
|
marci@45
|
39 |
|
marci@45
|
40 |
std::cout << "bfs and dfs demo on the directed graph" << std::endl;
|
marci@45
|
41 |
for(EachNodeIt i=G.first<EachNodeIt>(); i.valid(); ++i) {
|
marci@45
|
42 |
std::cout << i << ": ";
|
marci@45
|
43 |
std::cout << "out edges: ";
|
marci@45
|
44 |
for(OutEdgeIt j=G.first<OutEdgeIt>(i); j.valid(); ++j)
|
marci@45
|
45 |
std::cout << j << " ";
|
marci@45
|
46 |
std::cout << "in edges: ";
|
marci@45
|
47 |
for(InEdgeIt j=G.first<InEdgeIt>(i); j.valid(); ++j)
|
marci@45
|
48 |
std::cout << j << " ";
|
marci@45
|
49 |
std::cout << std::endl;
|
marci@45
|
50 |
}
|
marci@45
|
51 |
|
marci@45
|
52 |
//std::cout << std::endl;
|
marci@45
|
53 |
//EachNodeIt u1=G.first<EachNodeIt>();
|
marci@45
|
54 |
//EachEdgeIt u=G.first<EachEdgeIt>();
|
marci@45
|
55 |
//OutEdgeIt u=G.first<OutEdgeIt>(s);
|
marci@45
|
56 |
//InEdgeIt u=G.first<InEdgeIt>(s);
|
marci@45
|
57 |
//SymEdgeIt u=G.first<SymEdgeIt>(s);
|
marci@45
|
58 |
//OutEdgeIt u=G.first<OutEdgeIt>(s);
|
marci@45
|
59 |
//EachNodeIt u=G.first<EachNodeIt>();
|
marci@45
|
60 |
//EachEdgeIt u=G.first<EachEdgeIt>();
|
marci@45
|
61 |
//OutEdgeIt u=G.first<OutEdgeIt>(s);
|
marci@45
|
62 |
//InEdgeIt u=G.first<InEdgeIt>(s);
|
marci@45
|
63 |
//SymEdgeIt u=G.first<SymEdgeIt>(s);
|
marci@45
|
64 |
//u=G.first(s);
|
marci@45
|
65 |
//u=G.first_ize(s, OutEdgeIt());
|
marci@45
|
66 |
//std::cout << "ize " << u << std::endl;
|
marci@45
|
67 |
|
marci@45
|
68 |
/*
|
marci@45
|
69 |
{
|
marci@45
|
70 |
std::cout << "iterator bfs demo..." << std::endl;
|
marci@45
|
71 |
NodePropertyVector<ListGraph, bool> reached(G, false);
|
marci@45
|
72 |
reached.set(s, true);
|
marci@45
|
73 |
std::queue<ListGraph::OutEdgeIt> bfs_queue;
|
marci@45
|
74 |
bfs_queue.push(G.firstOutEdge(G.firstNode()));
|
marci@45
|
75 |
BfsIterator< ListGraph, ListGraph::OutEdgeIt, NodePropertyVector<ListGraph, bool> > bfs(G, bfs_queue, reached);
|
marci@45
|
76 |
for ( ; !bfs.finished(); ++bfs) {
|
marci@45
|
77 |
if (OutEdgeIt(bfs).valid()) {
|
marci@45
|
78 |
std::cout << "OutEdgeIt: " << bfs;
|
marci@45
|
79 |
std::cout << " aNode: " << G.aNode(bfs);
|
marci@45
|
80 |
std::cout << " bNode: " << G.bNode(bfs) << " ";
|
marci@45
|
81 |
} else {
|
marci@45
|
82 |
std::cout << "OutEdgeIt: " << "invalid";
|
marci@45
|
83 |
std::cout << " aNode: " << G.aNode(bfs);
|
marci@45
|
84 |
std::cout << " bNode: " << "invalid" << " ";
|
marci@45
|
85 |
}
|
marci@45
|
86 |
if (bfs.bNodeIsNewlyReached()) {
|
marci@45
|
87 |
std::cout << "bNodeIsNewlyReached ";
|
marci@45
|
88 |
} else {
|
marci@45
|
89 |
std::cout << "bNodeIsNotNewlyReached ";
|
marci@45
|
90 |
}
|
marci@45
|
91 |
if (bfs.aNodeIsExamined()) {
|
marci@45
|
92 |
std::cout << "aNodeIsExamined ";
|
marci@45
|
93 |
} else {
|
marci@45
|
94 |
std::cout << "aNodeIsNotExamined ";
|
marci@45
|
95 |
}
|
marci@45
|
96 |
std::cout<<std::endl;
|
marci@45
|
97 |
}
|
marci@45
|
98 |
}
|
marci@45
|
99 |
|
marci@45
|
100 |
{
|
marci@45
|
101 |
std::cout << "iterator dfs demo..." << std::endl;
|
marci@45
|
102 |
NodePropertyVector<ListGraph, bool> reached(G, false);
|
marci@45
|
103 |
reached.set(s, true);
|
marci@45
|
104 |
std::stack<ListGraph::OutEdgeIt> dfs_stack;
|
marci@45
|
105 |
dfs_stack.push(G.firstOutEdge(G.firstNode()));
|
marci@45
|
106 |
DfsIterator< ListGraph, ListGraph::OutEdgeIt, NodePropertyVector<ListGraph, bool> > dfs(G, dfs_stack, reached);
|
marci@45
|
107 |
for(; !dfs.finished(); ++dfs) {
|
marci@45
|
108 |
if (OutEdgeIt(dfs).valid()) {
|
marci@45
|
109 |
std::cout << "OutEdgeIt: " << dfs;
|
marci@45
|
110 |
std::cout << " aNode: " << G.aNode(dfs);
|
marci@45
|
111 |
std::cout << " bNode: " << G.bNode(dfs) << " ";
|
marci@45
|
112 |
} else {
|
marci@45
|
113 |
std::cout << "OutEdgeIt: " << "invalid";
|
marci@45
|
114 |
std::cout << " aNode: " << G.aNode(dfs);
|
marci@45
|
115 |
std::cout << " bNode: " << "invalid" << " ";
|
marci@45
|
116 |
}
|
marci@45
|
117 |
if (dfs.bNodeIsNewlyReached()) {
|
marci@45
|
118 |
std::cout << "bNodeIsNewlyReached ";
|
marci@45
|
119 |
} else {
|
marci@45
|
120 |
std::cout << "bNodeIsNotNewlyReached ";
|
marci@45
|
121 |
}
|
marci@45
|
122 |
if (dfs.aNodeIsLeaved()) {
|
marci@45
|
123 |
std::cout << "aNodeIsLeaved ";
|
marci@45
|
124 |
} else {
|
marci@45
|
125 |
std::cout << "aNodeIsNotLeaved ";
|
marci@45
|
126 |
}
|
marci@45
|
127 |
std::cout<<std::endl;
|
marci@45
|
128 |
}
|
marci@45
|
129 |
if (OutEdgeIt(dfs).valid()) {
|
marci@45
|
130 |
std::cout << "OutEdgeIt: " << dfs;
|
marci@45
|
131 |
std::cout << " aNode: " << G.aNode(dfs);
|
marci@45
|
132 |
std::cout << " bNode: " << G.bNode(dfs) << " ";
|
marci@45
|
133 |
} else {
|
marci@45
|
134 |
std::cout << "OutEdgeIt: " << "invalid";
|
marci@45
|
135 |
std::cout << " aNode: " << G.aNode(dfs);
|
marci@45
|
136 |
std::cout << " bNode: " << "invalid" << " ";
|
marci@45
|
137 |
}
|
marci@45
|
138 |
if (dfs.bNodeIsNewlyReached()) {
|
marci@45
|
139 |
std::cout << "bNodeIsNewlyReached ";
|
marci@45
|
140 |
} else {
|
marci@45
|
141 |
std::cout << "bNodeIsNotNewlyReached ";
|
marci@45
|
142 |
}
|
marci@45
|
143 |
if (dfs.aNodeIsLeaved()) {
|
marci@45
|
144 |
std::cout << "aNodeIsLeaved ";
|
marci@45
|
145 |
} else {
|
marci@45
|
146 |
std::cout << "aNodeIsNotLeaved ";
|
marci@45
|
147 |
}
|
marci@45
|
148 |
std::cout<<std::endl;
|
marci@45
|
149 |
}
|
marci@45
|
150 |
*/
|
marci@45
|
151 |
|
marci@45
|
152 |
{
|
marci@45
|
153 |
std::cout << "iterator bfs demo 1 ..." << std::endl;
|
marci@45
|
154 |
ListGraph::NodeMap<bool> reached(G, false);
|
marci@45
|
155 |
reached.set(s, true);
|
marci@45
|
156 |
std::queue<ListGraph::OutEdgeIt> bfs_queue;
|
marci@45
|
157 |
bfs_queue.push(G.first<OutEdgeIt>(s));
|
marci@45
|
158 |
BfsIterator1< ListGraph, ListGraph::OutEdgeIt, ListGraph::NodeMap<bool> > bfs(G, bfs_queue, reached);
|
marci@45
|
159 |
while (!bfs.finished()) {
|
marci@45
|
160 |
if (OutEdgeIt(bfs).valid()) {
|
marci@45
|
161 |
std::cout << "OutEdgeIt: " << bfs;
|
marci@45
|
162 |
std::cout << " aNode: " << G.aNode(bfs);
|
marci@45
|
163 |
std::cout << " bNode: " << G.bNode(bfs) << " ";
|
marci@45
|
164 |
} else {
|
marci@45
|
165 |
std::cout << "OutEdgeIt: " << "invalid";
|
marci@45
|
166 |
std::cout << " aNode: " << G.aNode(bfs);
|
marci@45
|
167 |
std::cout << " bNode: " << "invalid" << " ";
|
marci@45
|
168 |
}
|
marci@45
|
169 |
if (bfs.bNodeIsNewlyReached()) {
|
marci@45
|
170 |
std::cout << "bNodeIsNewlyReached ";
|
marci@45
|
171 |
} else {
|
marci@45
|
172 |
std::cout << "bNodeIsNotNewlyReached ";
|
marci@45
|
173 |
}
|
marci@45
|
174 |
if (bfs.aNodeIsExamined()) {
|
marci@45
|
175 |
std::cout << "aNodeIsExamined ";
|
marci@45
|
176 |
} else {
|
marci@45
|
177 |
std::cout << "aNodeIsNotExamined ";
|
marci@45
|
178 |
}
|
marci@45
|
179 |
std::cout<<std::endl;
|
marci@45
|
180 |
bfs.next();
|
marci@45
|
181 |
}
|
marci@45
|
182 |
}
|
marci@45
|
183 |
|
marci@45
|
184 |
|
marci@45
|
185 |
{
|
marci@45
|
186 |
std::cout << "iterator dfs demo 1..." << std::endl;
|
marci@45
|
187 |
ListGraph::NodeMap<bool> reached(G, false);
|
marci@45
|
188 |
reached.set(s, true);
|
marci@45
|
189 |
std::stack<ListGraph::OutEdgeIt> dfs_stack;
|
marci@45
|
190 |
dfs_stack.push(G.first<OutEdgeIt>(s));
|
marci@45
|
191 |
DfsIterator1< ListGraph, ListGraph::OutEdgeIt, ListGraph::NodeMap<bool> > dfs(G, dfs_stack, reached);
|
marci@45
|
192 |
do {
|
marci@45
|
193 |
dfs.next();
|
marci@45
|
194 |
if (OutEdgeIt(dfs).valid()) {
|
marci@45
|
195 |
std::cout << "OutEdgeIt: " << dfs;
|
marci@45
|
196 |
std::cout << " aNode: " << G.aNode(dfs);
|
marci@45
|
197 |
std::cout << " bNode: " << G.bNode(dfs) << " ";
|
marci@45
|
198 |
} else {
|
marci@45
|
199 |
std::cout << "OutEdgeIt: " << "invalid";
|
marci@45
|
200 |
std::cout << " aNode: " << G.aNode(dfs);
|
marci@45
|
201 |
std::cout << " bNode: " << "invalid" << " ";
|
marci@45
|
202 |
}
|
marci@45
|
203 |
if (dfs.bNodeIsNewlyReached()) {
|
marci@45
|
204 |
std::cout << "bNodeIsNewlyReached ";
|
marci@45
|
205 |
} else {
|
marci@45
|
206 |
std::cout << "bNodeIsNotNewlyReached ";
|
marci@45
|
207 |
}
|
marci@45
|
208 |
if (dfs.aNodeIsLeaved()) {
|
marci@45
|
209 |
std::cout << "aNodeIsLeaved ";
|
marci@45
|
210 |
} else {
|
marci@45
|
211 |
std::cout << "aNodeIsNotLeaved ";
|
marci@45
|
212 |
}
|
marci@45
|
213 |
std::cout<<std::endl;
|
marci@45
|
214 |
} while (!dfs.finished());
|
marci@45
|
215 |
}
|
marci@45
|
216 |
|
marci@45
|
217 |
|
marci@45
|
218 |
{
|
marci@45
|
219 |
std::cout << "iterator bfs demo from node 5 with replacing \n OutEdgeIt by InEdgeIt ..." << std::endl;
|
marci@45
|
220 |
ListGraph::NodeMap<bool> reached(G, false);
|
marci@45
|
221 |
reached.set(t, true);
|
marci@45
|
222 |
std::queue<ListGraph::InEdgeIt> bfs_queue;
|
marci@45
|
223 |
bfs_queue.push(G.first<InEdgeIt>(t));
|
marci@45
|
224 |
BfsIterator1< ListGraph, ListGraph::InEdgeIt, ListGraph::NodeMap<bool> > bfs(G, bfs_queue, reached);
|
marci@45
|
225 |
while (!bfs.finished()) {
|
marci@45
|
226 |
if (InEdgeIt(bfs).valid()) {
|
marci@45
|
227 |
std::cout << "InEdgeIt: " << bfs;
|
marci@45
|
228 |
std::cout << " aNode: " << G.aNode(bfs);
|
marci@45
|
229 |
std::cout << " bNode: " << G.bNode(bfs) << " ";
|
marci@45
|
230 |
} else {
|
marci@45
|
231 |
std::cout << "InEdgeIt: " << "invalid";
|
marci@45
|
232 |
std::cout << " aNode: " << G.aNode(bfs);
|
marci@45
|
233 |
std::cout << " bNode: " << "invalid" << " ";
|
marci@45
|
234 |
}
|
marci@45
|
235 |
if (bfs.bNodeIsNewlyReached()) {
|
marci@45
|
236 |
std::cout << "bNodeIsNewlyReached ";
|
marci@45
|
237 |
} else {
|
marci@45
|
238 |
std::cout << "bNodeIsNotNewlyReached ";
|
marci@45
|
239 |
}
|
marci@45
|
240 |
if (bfs.aNodeIsExamined()) {
|
marci@45
|
241 |
std::cout << "aNodeIsExamined ";
|
marci@45
|
242 |
} else {
|
marci@45
|
243 |
std::cout << "aNodeIsNotExamined ";
|
marci@45
|
244 |
}
|
marci@45
|
245 |
std::cout<<std::endl;
|
marci@45
|
246 |
bfs.next();
|
marci@45
|
247 |
}
|
marci@45
|
248 |
}
|
marci@45
|
249 |
|
marci@45
|
250 |
|
marci@45
|
251 |
{
|
marci@45
|
252 |
std::cout << "the graph is considered as an undirected graph \n by replacing InEdgeIt by SymEdgeIt ..." << std::endl;
|
marci@45
|
253 |
ListGraph::NodeMap<bool> reached(G, false);
|
marci@45
|
254 |
reached.set(t, true);
|
marci@45
|
255 |
std::queue<ListGraph::SymEdgeIt> bfs_queue;
|
marci@45
|
256 |
bfs_queue.push(G.first<SymEdgeIt>(t));
|
marci@45
|
257 |
BfsIterator1< ListGraph, ListGraph::SymEdgeIt, ListGraph::NodeMap<bool> > bfs(G, bfs_queue, reached);
|
marci@45
|
258 |
while (!bfs.finished()) {
|
marci@45
|
259 |
if (SymEdgeIt(bfs).valid()) {
|
marci@45
|
260 |
std::cout << "SymEdgeIt: " << bfs;
|
marci@45
|
261 |
std::cout << " aNode: " << G.aNode(bfs);
|
marci@45
|
262 |
std::cout << " bNode: " << G.bNode(bfs) << " ";
|
marci@45
|
263 |
} else {
|
marci@45
|
264 |
std::cout << "SymEdgeIt: " << "invalid";
|
marci@45
|
265 |
std::cout << " aNode: " << G.aNode(bfs);
|
marci@45
|
266 |
std::cout << " bNode: " << "invalid" << " ";
|
marci@45
|
267 |
}
|
marci@45
|
268 |
if (bfs.bNodeIsNewlyReached()) {
|
marci@45
|
269 |
std::cout << "bNodeIsNewlyReached ";
|
marci@45
|
270 |
} else {
|
marci@45
|
271 |
std::cout << "bNodeIsNotNewlyReached ";
|
marci@45
|
272 |
}
|
marci@45
|
273 |
if (bfs.aNodeIsExamined()) {
|
marci@45
|
274 |
std::cout << "aNodeIsExamined ";
|
marci@45
|
275 |
} else {
|
marci@45
|
276 |
std::cout << "aNodeIsNotExamined ";
|
marci@45
|
277 |
}
|
marci@45
|
278 |
std::cout<<std::endl;
|
marci@45
|
279 |
bfs.next();
|
marci@45
|
280 |
}
|
marci@45
|
281 |
}
|
marci@45
|
282 |
|
marci@45
|
283 |
return 0;
|
marci@45
|
284 |
}
|