diff -r ce9438c5a82d -r 4297098d9677 src/work/marci/iterator_bfs_demo.cc --- a/src/work/marci/iterator_bfs_demo.cc Wed Aug 25 18:55:57 2004 +0000 +++ b/src/work/marci/iterator_bfs_demo.cc Mon Aug 30 12:01:47 2004 +0000 @@ -4,7 +4,7 @@ #include #include -//#include +#include #include #include @@ -28,8 +28,8 @@ int main (int, char*[]) { - //typedef SmartGraph Graph; - typedef SageGraph Graph; + typedef SmartGraph Graph; + //typedef SageGraph Graph; typedef Graph::Node Node; typedef Graph::Edge Edge; @@ -91,13 +91,13 @@ EdgeNameMap< Graph, Graph::NodeMap > edge_name(G, node_name); cout << "bfs and dfs iterator demo on the directed graph" << endl; - for(Graph::NodeIt n(G); G.valid(n); G.next(n)) { + for(Graph::NodeIt n(G); n!=INVALID; ++n) { cout << node_name[n] << ": "; cout << "out edges: "; - for(Graph::OutEdgeIt e(G, n); G.valid(e); G.next(e)) + for(Graph::OutEdgeIt e(G, n); e!=INVALID; ++e) cout << edge_name[e] << " "; cout << "in edges: "; - for(Graph::InEdgeIt e(G, n); G.valid(e); G.next(e)) + for(Graph::InEdgeIt e(G, n); e!=INVALID; ++e) cout << edge_name[e] << " "; cout << endl; } @@ -107,11 +107,11 @@ bfs.pushAndSetReached(s); while (!bfs.finished()) { //cout << "edge: "; - if (G.valid(bfs)) { + if (Graph::Edge(Graph::OutEdgeIt(bfs))!=INVALID) { cout << edge_name[bfs] << /*endl*/", " << - node_name[G.aNode(bfs)] << + node_name[G.tail(bfs)] << (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - node_name[G.bNode(bfs)] << + node_name[G.head(bfs)] << (bfs.isBNodeNewlyReached() ? ": is newly reached." : ": is not newly reached."); } else { @@ -141,11 +141,11 @@ while (!dfs.finished()) { ++dfs; //cout << "edge: "; - if (G.valid(dfs)) { + if (Graph::Edge(Graph::OutEdgeIt(dfs))!=INVALID) { cout << edge_name[dfs] << /*endl*/", " << - node_name[G.aNode(dfs)] << + node_name[G.tail(dfs)] << (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - node_name[G.bNode(dfs)] << + node_name[G.head(dfs)] << (dfs.isBNodeNewlyReached() ? ": is newly reached." : ": is not newly reached."); } else { @@ -167,13 +167,13 @@ EdgeNameMap< GW, Graph::NodeMap > edge_name(gw, node_name); cout << "bfs and dfs iterator demo on the reversed directed graph" << endl; - for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { + for(GW::NodeIt n(gw); n!=INVALID; ++n) { cout << node_name[GW::Node(n)] << ": "; cout << "out edges: "; - for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e)) + for(GW::OutEdgeIt e(gw, n); e!=INVALID; ++e) cout << edge_name[e] << " "; cout << "in edges: "; - for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e)) + for(GW::InEdgeIt e(gw, n); e!=INVALID; ++e) cout << edge_name[e] << " "; cout << endl; } @@ -183,11 +183,11 @@ bfs.pushAndSetReached(t); while (!bfs.finished()) { //cout << "edge: "; - if (gw.valid(GW::OutEdgeIt(bfs))) { + if (GW::Edge(GW::OutEdgeIt(bfs))!=INVALID) { cout << edge_name[GW::OutEdgeIt(bfs)] << /*endl*/", " << - node_name[gw.aNode(bfs)] << + node_name[gw.tail(bfs)] << (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - node_name[gw.bNode(bfs)] << + node_name[gw.head(bfs)] << (bfs.isBNodeNewlyReached() ? ": is newly reached." : ": is not newly reached."); } else { @@ -217,11 +217,11 @@ while (!dfs.finished()) { ++dfs; //cout << "edge: "; - if (gw.valid(GW::OutEdgeIt(dfs))) { + if (GW::OutEdgeIt(dfs)!=INVALID) { cout << edge_name[GW::OutEdgeIt(dfs)] << /*endl*/", " << - node_name[gw.aNode(dfs)] << + node_name[gw.tail(dfs)] << (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - node_name[gw.bNode(dfs)] << + node_name[gw.head(dfs)] << (dfs.isBNodeNewlyReached() ? ": is newly reached." : ": is not newly reached."); } else { @@ -235,20 +235,104 @@ } } +// { +// typedef UndirGraphWrapper GW; +// GW gw(G); + +// EdgeNameMap< GW, Graph::NodeMap > edge_name(gw, node_name); + +// cout << "bfs and dfs iterator demo on the undirected graph" << endl; +// for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { +// cout << node_name[GW::Node(n)] << ": "; +// cout << "out edges: "; +// for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e)) +// cout << edge_name[e] << " "; +// cout << "in edges: "; +// for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e)) +// cout << edge_name[e] << " "; +// cout << endl; +// } +// // for(GW::EdgeIt e=gw.first(); gw.valid(e); gw.next(e)) { +// // cout << edge_name.get(e) << " "; +// // } +// // cout << endl; + +// cout << "bfs from t ..." << endl; +// BfsIterator< GW, GW::NodeMap > bfs(gw); +// bfs.pushAndSetReached(t); +// while (!bfs.finished()) { +// //cout << "edge: "; +// if (gw.valid(GW::OutEdgeIt(bfs))) { +// cout << edge_name[GW::OutEdgeIt(bfs)] << /*endl*/", " << +// node_name[gw.aNode(bfs)] << +// (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << +// node_name[gw.bNode(bfs)] << +// (bfs.isBNodeNewlyReached() ? ": is newly reached." : +// ": is not newly reached."); +// } else { +// cout << "invalid" << /*endl*/", " << +// node_name[bfs.aNode()] << +// (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << + +// "invalid."; +// } +// cout << endl; +// ++bfs; +// } + +// cout << " /--> -------------> "<< endl; +// cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl; +// cout << " / | | / /-> \\ "<< endl; +// cout << " / | | / | ^ \\ "<< endl; +// cout << "s | | / | | \\-> t "<< endl; +// cout << " \\ | | / | | /-> "<< endl; +// cout << " \\ | --/ / | | / "<< endl; +// cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl; +// cout << " \\--> -------------> "<< endl; + +// cout << "dfs from t ..." << endl; +// DfsIterator< GW, GW::NodeMap > dfs(gw); +// dfs.pushAndSetReached(t); +// while (!dfs.finished()) { +// ++dfs; +// //cout << "edge: "; +// if (gw.valid(GW::OutEdgeIt(dfs))) { +// cout << edge_name[GW::OutEdgeIt(dfs)] << /*endl*/", " << +// node_name[gw.aNode(dfs)] << +// (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << +// node_name[gw.bNode(dfs)] << +// (dfs.isBNodeNewlyReached() ? ": is newly reached." : +// ": is not newly reached."); +// } else { +// cout << "invalid" << /*endl*/", " << +// node_name[dfs.aNode()] << +// (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << + +// "invalid."; +// } +// cout << endl; +// } +// } + + + { - typedef UndirGraphWrapper GW; + typedef BidirGraphWrapper GW; GW gw(G); EdgeNameMap< GW, Graph::NodeMap > edge_name(gw, node_name); - cout << "bfs and dfs iterator demo on the undirected graph" << endl; - for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { + cout << "bfs and dfs iterator demo on the bidirected graph" << endl; +// for(GW::EdgeIt e(gw); e!=INVALID; ++e) { +// cout << node_name[gw.tail(e)] << "->" << node_name[gw.head(e)] << " "; +// } + for(GW::NodeIt n(gw); n!=INVALID; ++n) { cout << node_name[GW::Node(n)] << ": "; cout << "out edges: "; - for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e)) + for(GW::OutEdgeIt e(gw, n); e!=INVALID; ++e) cout << edge_name[e] << " "; cout << "in edges: "; - for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e)) + for(GW::InEdgeIt e(gw, n); e!=INVALID; ++e) cout << edge_name[e] << " "; cout << endl; } @@ -262,11 +346,11 @@ bfs.pushAndSetReached(t); while (!bfs.finished()) { //cout << "edge: "; - if (gw.valid(GW::OutEdgeIt(bfs))) { + if (GW::OutEdgeIt(bfs)!=INVALID) { cout << edge_name[GW::OutEdgeIt(bfs)] << /*endl*/", " << - node_name[gw.aNode(bfs)] << + node_name[gw.tail(bfs)] << (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - node_name[gw.bNode(bfs)] << + node_name[gw.head(bfs)] << (bfs.isBNodeNewlyReached() ? ": is newly reached." : ": is not newly reached."); } else { @@ -296,11 +380,11 @@ while (!dfs.finished()) { ++dfs; //cout << "edge: "; - if (gw.valid(GW::OutEdgeIt(dfs))) { + if (GW::OutEdgeIt(dfs)!=INVALID) { cout << edge_name[GW::OutEdgeIt(dfs)] << /*endl*/", " << - node_name[gw.aNode(dfs)] << + node_name[gw.tail(dfs)] << (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - node_name[gw.bNode(dfs)] << + node_name[gw.head(dfs)] << (dfs.isBNodeNewlyReached() ? ": is newly reached." : ": is not newly reached."); } else { @@ -314,88 +398,5 @@ } } - - - { - typedef BidirGraphWrapper GW; - GW gw(G); - - EdgeNameMap< GW, Graph::NodeMap > edge_name(gw, node_name); - - cout << "bfs and dfs iterator demo on the undirected graph" << endl; - for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { - cout << node_name[GW::Node(n)] << ": "; - cout << "out edges: "; - for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e)) - cout << edge_name[e] << " "; - cout << "in edges: "; - for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e)) - cout << edge_name[e] << " "; - cout << endl; - } -// for(GW::EdgeIt e=gw.first(); gw.valid(e); gw.next(e)) { -// cout << edge_name.get(e) << " "; -// } -// cout << endl; - - cout << "bfs from t ..." << endl; - BfsIterator< GW, GW::NodeMap > bfs(gw); - bfs.pushAndSetReached(t); - while (!bfs.finished()) { - //cout << "edge: "; - if (gw.valid(GW::OutEdgeIt(bfs))) { - cout << edge_name[GW::OutEdgeIt(bfs)] << /*endl*/", " << - node_name[gw.aNode(bfs)] << - (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - node_name[gw.bNode(bfs)] << - (bfs.isBNodeNewlyReached() ? ": is newly reached." : - ": is not newly reached."); - } else { - cout << "invalid" << /*endl*/", " << - node_name[bfs.aNode()] << - (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - - "invalid."; - } - cout << endl; - ++bfs; - } - - cout << " /--> -------------> "<< endl; - cout << " / /-- v1 <-\\ /---- v3-\\ "<< endl; - cout << " / | | / /-> \\ "<< endl; - cout << " / | | / | ^ \\ "<< endl; - cout << "s | | / | | \\-> t "<< endl; - cout << " \\ | | / | | /-> "<< endl; - cout << " \\ | --/ / | | / "<< endl; - cout << " \\ \\-> v2 <--/ \\-- v4 -/ "<< endl; - cout << " \\--> -------------> "<< endl; - - cout << "dfs from t ..." << endl; - DfsIterator< GW, GW::NodeMap > dfs(gw); - dfs.pushAndSetReached(t); - while (!dfs.finished()) { - ++dfs; - //cout << "edge: "; - if (gw.valid(GW::OutEdgeIt(dfs))) { - cout << edge_name[GW::OutEdgeIt(dfs)] << /*endl*/", " << - node_name[gw.aNode(dfs)] << - (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - node_name[gw.bNode(dfs)] << - (dfs.isBNodeNewlyReached() ? ": is newly reached." : - ": is not newly reached."); - } else { - cout << "invalid" << /*endl*/", " << - node_name[dfs.aNode()] << - (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << - - "invalid."; - } - cout << endl; - } - } - - - return 0; }