Changeset 774:4297098d9677 in lemon-0.x for src/work/marci
- Timestamp:
- 08/30/04 14:01:47 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1066
- Location:
- src/work/marci
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/marci/bfs_dfs.h
r671 r774 61 61 bfs_queue.push(s); 62 62 graph->first(actual_edge, s); 63 if ( graph->valid(actual_edge)) {64 Node w=graph-> bNode(actual_edge);63 if (actual_edge!=INVALID) { 64 Node w=graph->head(actual_edge); 65 65 if (!reached[w]) { 66 66 bfs_queue.push(w); … … 79 79 BfsIterator<Graph, /*OutEdgeIt,*/ ReachedMap>& 80 80 operator++() { 81 if ( graph->valid(actual_edge)) {82 graph->next(actual_edge);83 if ( graph->valid(actual_edge)) {84 Node w=graph-> bNode(actual_edge);81 if (actual_edge!=INVALID) { 82 ++actual_edge; 83 if (actual_edge!=INVALID) { 84 Node w=graph->head(actual_edge); 85 85 if (!reached[w]) { 86 86 bfs_queue.push(w); … … 95 95 if (!bfs_queue.empty()) { 96 96 graph->first(actual_edge, bfs_queue.front()); 97 if ( graph->valid(actual_edge)) {98 Node w=graph-> bNode(actual_edge);97 if (actual_edge!=INVALID) { 98 Node w=graph->head(actual_edge); 99 99 if (!reached[w]) { 100 100 bfs_queue.push(w); … … 118 118 bool isBNodeNewlyReached() const { return b_node_newly_reached; } 119 119 /// Returns if a-node is examined. 120 bool isANodeExamined() const { return !(graph->valid(actual_edge)); }120 bool isANodeExamined() const { return actual_edge==INVALID; } 121 121 /// Returns a-node of the actual edge, so does if the edge is invalid. 122 122 Node aNode() const { return bfs_queue.front(); } … … 238 238 actual_edge=dfs_stack.top(); 239 239 //actual_node=G.aNode(actual_edge); 240 if ( graph->valid(actual_edge)/*.valid()*/) {241 Node w=graph-> bNode(actual_edge);240 if (actual_edge!=INVALID/*.valid()*/) { 241 Node w=graph->head(actual_edge); 242 242 actual_node=w; 243 243 if (!reached[w]) { … … 248 248 b_node_newly_reached=true; 249 249 } else { 250 actual_node=graph-> aNode(actual_edge);251 graph->next(dfs_stack.top());250 actual_node=graph->tail(actual_edge); 251 ++dfs_stack.top(); 252 252 b_node_newly_reached=false; 253 253 } … … 267 267 bool isBNodeNewlyReached() const { return b_node_newly_reached; } 268 268 /// Returns if a-node is examined. 269 bool isANodeExamined() const { return !(graph->valid(actual_edge)); }269 bool isANodeExamined() const { return actual_edge==INVALID; } 270 270 /// Returns a-node of the actual edge, so does if the edge is invalid. 271 271 Node aNode() const { return actual_node; /*FIXME*/} -
src/work/marci/iterator_bfs_demo.cc
r642 r774 5 5 6 6 #include <sage_graph.h> 7 //#include <smart_graph.h>7 #include <hugo/smart_graph.h> 8 8 #include <bfs_dfs.h> 9 9 #include <hugo/graph_wrapper.h> … … 29 29 int main (int, char*[]) 30 30 { 31 //typedef SmartGraph Graph;32 typedef SageGraph Graph;31 typedef SmartGraph Graph; 32 //typedef SageGraph Graph; 33 33 34 34 typedef Graph::Node Node; … … 92 92 93 93 cout << "bfs and dfs iterator demo on the directed graph" << endl; 94 for(Graph::NodeIt n(G); G.valid(n); G.next(n)) {94 for(Graph::NodeIt n(G); n!=INVALID; ++n) { 95 95 cout << node_name[n] << ": "; 96 96 cout << "out edges: "; 97 for(Graph::OutEdgeIt e(G, n); G.valid(e); G.next(e))97 for(Graph::OutEdgeIt e(G, n); e!=INVALID; ++e) 98 98 cout << edge_name[e] << " "; 99 99 cout << "in edges: "; 100 for(Graph::InEdgeIt e(G, n); G.valid(e); G.next(e))100 for(Graph::InEdgeIt e(G, n); e!=INVALID; ++e) 101 101 cout << edge_name[e] << " "; 102 102 cout << endl; … … 108 108 while (!bfs.finished()) { 109 109 //cout << "edge: "; 110 if (G .valid(bfs)) {110 if (Graph::Edge(Graph::OutEdgeIt(bfs))!=INVALID) { 111 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)] <<112 node_name[G.tail(bfs)] << 113 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 114 node_name[G.head(bfs)] << 115 115 (bfs.isBNodeNewlyReached() ? ": is newly reached." : 116 116 ": is not newly reached."); … … 142 142 ++dfs; 143 143 //cout << "edge: "; 144 if (G .valid(dfs)) {144 if (Graph::Edge(Graph::OutEdgeIt(dfs))!=INVALID) { 145 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)] <<146 node_name[G.tail(dfs)] << 147 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 148 node_name[G.head(dfs)] << 149 149 (dfs.isBNodeNewlyReached() ? ": is newly reached." : 150 150 ": is not newly reached."); … … 168 168 169 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)) {170 for(GW::NodeIt n(gw); n!=INVALID; ++n) { 171 171 cout << node_name[GW::Node(n)] << ": "; 172 172 cout << "out edges: "; 173 for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e))173 for(GW::OutEdgeIt e(gw, n); e!=INVALID; ++e) 174 174 cout << edge_name[e] << " "; 175 175 cout << "in edges: "; 176 for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e))176 for(GW::InEdgeIt e(gw, n); e!=INVALID; ++e) 177 177 cout << edge_name[e] << " "; 178 178 cout << endl; … … 184 184 while (!bfs.finished()) { 185 185 //cout << "edge: "; 186 if ( gw.valid(GW::OutEdgeIt(bfs))) {186 if (GW::Edge(GW::OutEdgeIt(bfs))!=INVALID) { 187 187 cout << edge_name[GW::OutEdgeIt(bfs)] << /*endl*/", " << 188 node_name[gw. aNode(bfs)] <<189 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 190 node_name[gw. bNode(bfs)] <<188 node_name[gw.tail(bfs)] << 189 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 190 node_name[gw.head(bfs)] << 191 191 (bfs.isBNodeNewlyReached() ? ": is newly reached." : 192 192 ": is not newly reached."); … … 218 218 ++dfs; 219 219 //cout << "edge: "; 220 if ( gw.valid(GW::OutEdgeIt(dfs))) {220 if (GW::OutEdgeIt(dfs)!=INVALID) { 221 221 cout << edge_name[GW::OutEdgeIt(dfs)] << /*endl*/", " << 222 node_name[gw. aNode(dfs)] <<223 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 224 node_name[gw. bNode(dfs)] <<222 node_name[gw.tail(dfs)] << 223 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 224 node_name[gw.head(dfs)] << 225 225 (dfs.isBNodeNewlyReached() ? ": is newly reached." : 226 226 ": is not newly reached."); … … 236 236 } 237 237 238 // { 239 // typedef UndirGraphWrapper<const Graph> GW; 240 // GW gw(G); 241 242 // EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name); 243 244 // cout << "bfs and dfs iterator demo on the undirected graph" << endl; 245 // for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { 246 // cout << node_name[GW::Node(n)] << ": "; 247 // cout << "out edges: "; 248 // for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 249 // cout << edge_name[e] << " "; 250 // cout << "in edges: "; 251 // for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 252 // cout << edge_name[e] << " "; 253 // cout << endl; 254 // } 255 // // for(GW::EdgeIt e=gw.first<GW::EdgeIt>(); gw.valid(e); gw.next(e)) { 256 // // cout << edge_name.get(e) << " "; 257 // // } 258 // // cout << endl; 259 260 // cout << "bfs from t ..." << endl; 261 // BfsIterator< GW, GW::NodeMap<bool> > bfs(gw); 262 // bfs.pushAndSetReached(t); 263 // while (!bfs.finished()) { 264 // //cout << "edge: "; 265 // if (gw.valid(GW::OutEdgeIt(bfs))) { 266 // cout << edge_name[GW::OutEdgeIt(bfs)] << /*endl*/", " << 267 // node_name[gw.aNode(bfs)] << 268 // (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 269 // node_name[gw.bNode(bfs)] << 270 // (bfs.isBNodeNewlyReached() ? ": is newly reached." : 271 // ": is not newly reached."); 272 // } else { 273 // cout << "invalid" << /*endl*/", " << 274 // node_name[bfs.aNode()] << 275 // (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 276 277 // "invalid."; 278 // } 279 // cout << endl; 280 // ++bfs; 281 // } 282 283 // cout << " /--> -------------> "<< endl; 284 // cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl; 285 // cout << " / | | / /-> \\ "<< endl; 286 // cout << " / | | / | ^ \\ "<< endl; 287 // cout << "s | | / | | \\-> t "<< endl; 288 // cout << " \\ | | / | | /-> "<< endl; 289 // cout << " \\ | --/ / | | / "<< endl; 290 // cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl; 291 // cout << " \\--> -------------> "<< endl; 292 293 // cout << "dfs from t ..." << endl; 294 // DfsIterator< GW, GW::NodeMap<bool> > dfs(gw); 295 // dfs.pushAndSetReached(t); 296 // while (!dfs.finished()) { 297 // ++dfs; 298 // //cout << "edge: "; 299 // if (gw.valid(GW::OutEdgeIt(dfs))) { 300 // cout << edge_name[GW::OutEdgeIt(dfs)] << /*endl*/", " << 301 // node_name[gw.aNode(dfs)] << 302 // (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 303 // node_name[gw.bNode(dfs)] << 304 // (dfs.isBNodeNewlyReached() ? ": is newly reached." : 305 // ": is not newly reached."); 306 // } else { 307 // cout << "invalid" << /*endl*/", " << 308 // node_name[dfs.aNode()] << 309 // (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 310 311 // "invalid."; 312 // } 313 // cout << endl; 314 // } 315 // } 316 317 318 238 319 { 239 typedef UndirGraphWrapper<const Graph> GW;320 typedef BidirGraphWrapper<const Graph> GW; 240 321 GW gw(G); 241 322 242 323 EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name); 243 324 244 cout << "bfs and dfs iterator demo on the undirected graph" << endl; 245 for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { 325 cout << "bfs and dfs iterator demo on the bidirected graph" << endl; 326 // for(GW::EdgeIt e(gw); e!=INVALID; ++e) { 327 // cout << node_name[gw.tail(e)] << "->" << node_name[gw.head(e)] << " "; 328 // } 329 for(GW::NodeIt n(gw); n!=INVALID; ++n) { 246 330 cout << node_name[GW::Node(n)] << ": "; 247 331 cout << "out edges: "; 248 for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e))332 for(GW::OutEdgeIt e(gw, n); e!=INVALID; ++e) 249 333 cout << edge_name[e] << " "; 250 334 cout << "in edges: "; 251 for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e))335 for(GW::InEdgeIt e(gw, n); e!=INVALID; ++e) 252 336 cout << edge_name[e] << " "; 253 337 cout << endl; … … 263 347 while (!bfs.finished()) { 264 348 //cout << "edge: "; 265 if ( gw.valid(GW::OutEdgeIt(bfs))) {349 if (GW::OutEdgeIt(bfs)!=INVALID) { 266 350 cout << edge_name[GW::OutEdgeIt(bfs)] << /*endl*/", " << 267 node_name[gw. aNode(bfs)] <<268 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 269 node_name[gw. bNode(bfs)] <<351 node_name[gw.tail(bfs)] << 352 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 353 node_name[gw.head(bfs)] << 270 354 (bfs.isBNodeNewlyReached() ? ": is newly reached." : 271 355 ": is not newly reached."); … … 297 381 ++dfs; 298 382 //cout << "edge: "; 299 if ( gw.valid(GW::OutEdgeIt(dfs))) {383 if (GW::OutEdgeIt(dfs)!=INVALID) { 300 384 cout << edge_name[GW::OutEdgeIt(dfs)] << /*endl*/", " << 301 node_name[gw. aNode(dfs)] <<302 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 303 node_name[gw. bNode(dfs)] <<385 node_name[gw.tail(dfs)] << 386 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 387 node_name[gw.head(dfs)] << 304 388 (dfs.isBNodeNewlyReached() ? ": is newly reached." : 305 389 ": is not newly reached."); … … 314 398 } 315 399 } 316 317 318 319 {320 typedef BidirGraphWrapper<const Graph> GW;321 GW gw(G);322 323 EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);324 325 cout << "bfs and dfs iterator demo on the undirected graph" << endl;326 for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) {327 cout << node_name[GW::Node(n)] << ": ";328 cout << "out edges: ";329 for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e))330 cout << edge_name[e] << " ";331 cout << "in edges: ";332 for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e))333 cout << edge_name[e] << " ";334 cout << endl;335 }336 // for(GW::EdgeIt e=gw.first<GW::EdgeIt>(); gw.valid(e); gw.next(e)) {337 // cout << edge_name.get(e) << " ";338 // }339 // cout << endl;340 341 cout << "bfs from t ..." << endl;342 BfsIterator< GW, GW::NodeMap<bool> > bfs(gw);343 bfs.pushAndSetReached(t);344 while (!bfs.finished()) {345 //cout << "edge: ";346 if (gw.valid(GW::OutEdgeIt(bfs))) {347 cout << edge_name[GW::OutEdgeIt(bfs)] << /*endl*/", " <<348 node_name[gw.aNode(bfs)] <<349 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<350 node_name[gw.bNode(bfs)] <<351 (bfs.isBNodeNewlyReached() ? ": is newly reached." :352 ": is not newly reached.");353 } else {354 cout << "invalid" << /*endl*/", " <<355 node_name[bfs.aNode()] <<356 (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<357 358 "invalid.";359 }360 cout << endl;361 ++bfs;362 }363 364 cout << " /--> -------------> "<< endl;365 cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl;366 cout << " / | | / /-> \\ "<< endl;367 cout << " / | | / | ^ \\ "<< endl;368 cout << "s | | / | | \\-> t "<< endl;369 cout << " \\ | | / | | /-> "<< endl;370 cout << " \\ | --/ / | | / "<< endl;371 cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl;372 cout << " \\--> -------------> "<< endl;373 374 cout << "dfs from t ..." << endl;375 DfsIterator< GW, GW::NodeMap<bool> > dfs(gw);376 dfs.pushAndSetReached(t);377 while (!dfs.finished()) {378 ++dfs;379 //cout << "edge: ";380 if (gw.valid(GW::OutEdgeIt(dfs))) {381 cout << edge_name[GW::OutEdgeIt(dfs)] << /*endl*/", " <<382 node_name[gw.aNode(dfs)] <<383 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<384 node_name[gw.bNode(dfs)] <<385 (dfs.isBNodeNewlyReached() ? ": is newly reached." :386 ": is not newly reached.");387 } else {388 cout << "invalid" << /*endl*/", " <<389 node_name[dfs.aNode()] <<390 (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") <<391 392 "invalid.";393 }394 cout << endl;395 }396 }397 398 399 400 400 401 return 0;
Note: See TracChangeset
for help on using the changeset viewer.