src/work/marci/iterator_bfs_demo.cc
author alpar
Wed, 29 Sep 2004 15:30:04 +0000
changeset 921 818510fa3d99
parent 777 a82713ed19f3
child 986 e997802b855c
permissions -rw-r--r--
hugo -> lemon
marci@301
     1
// -*- c++ -*-
marci@301
     2
#include <iostream>
marci@301
     3
#include <vector>
marci@301
     4
#include <string>
marci@301
     5
marci@642
     6
#include <sage_graph.h>
alpar@921
     7
#include <lemon/smart_graph.h>
marci@602
     8
#include <bfs_dfs.h>
alpar@921
     9
#include <lemon/graph_wrapper.h>
marci@301
    10
alpar@921
    11
using namespace lemon;
marci@777
    12
marci@301
    13
using std::cout; 
marci@301
    14
using std::endl;
marci@301
    15
using std::string;
marci@301
    16
marci@301
    17
template <typename Graph, typename NodeNameMap>
marci@301
    18
class EdgeNameMap {
marci@301
    19
  Graph& graph;
marci@301
    20
  NodeNameMap& node_name_map;
marci@301
    21
public:
marci@301
    22
  EdgeNameMap(Graph& _graph, NodeNameMap& _node_name_map) : 
marci@301
    23
    graph(_graph), node_name_map(_node_name_map) { }
marci@304
    24
  string operator[](typename Graph::Edge e) const { 
marci@301
    25
    return 
marci@304
    26
      (node_name_map[graph.tail(e)]+"->"+node_name_map[graph.head(e)]);
marci@301
    27
  }
marci@301
    28
};
marci@301
    29
marci@301
    30
int main (int, char*[])
marci@301
    31
{
alpar@774
    32
  typedef SmartGraph Graph;
alpar@774
    33
  //typedef SageGraph Graph;
marci@301
    34
marci@301
    35
  typedef Graph::Node Node;
marci@301
    36
  typedef Graph::Edge Edge;
marci@301
    37
 
marci@301
    38
  Graph G;
marci@301
    39
marci@301
    40
  Node s=G.addNode();
marci@301
    41
  Node v1=G.addNode();
marci@301
    42
  Node v2=G.addNode();
marci@301
    43
  Node v3=G.addNode();
marci@301
    44
  Node v4=G.addNode();
marci@301
    45
  Node t=G.addNode();
marci@301
    46
  
marci@301
    47
  Graph::NodeMap<string> node_name(G);
marci@301
    48
  node_name.set(s, "s");
marci@301
    49
  node_name.set(v1, "v1");
marci@301
    50
  node_name.set(v2, "v2");
marci@301
    51
  node_name.set(v3, "v3");
marci@301
    52
  node_name.set(v4, "v4");
marci@301
    53
  node_name.set(t, "t");
marci@301
    54
marci@301
    55
  G.addEdge(s, v1);
marci@301
    56
  G.addEdge(s, v2);
marci@301
    57
  G.addEdge(v1, v2);
marci@301
    58
  G.addEdge(v2, v1);
marci@301
    59
  G.addEdge(v1, v3);
marci@301
    60
  G.addEdge(v3, v2);
marci@301
    61
  G.addEdge(v2, v4);
marci@301
    62
  G.addEdge(v4, v3);
marci@301
    63
  G.addEdge(v3, t);
marci@301
    64
  G.addEdge(v4, t);
marci@301
    65
marci@301
    66
  cout << "    /-->    ------------->            "<< endl;
marci@301
    67
  cout << "   / /-- v1 <-\\      /---- v3-\\      "<< endl;
marci@301
    68
  cout << "  / |          |    /  /->     \\     "<< endl;
marci@301
    69
  cout << " /  |          |   /  |    ^    \\  "<< endl;
marci@301
    70
  cout << "s   |          |  /   |    |     \\->  t "<< endl;
marci@301
    71
  cout << " \\  |          | /    |    |     /->  "<< endl;
marci@301
    72
  cout << "  \\ |       --/ /     |    |    /     "<< endl;
marci@301
    73
  cout << "   \\ \\-> v2 <--/       \\-- v4 -/      "<< endl;
marci@301
    74
  cout << "    \\-->    ------------->         "<< endl;
marci@301
    75
  
marci@301
    76
  {
marci@312
    77
    EdgeNameMap< Graph, Graph::NodeMap<string> > edge_name(G, node_name);
marci@301
    78
    
marci@301
    79
    cout << "bfs and dfs iterator demo on the directed graph" << endl;
alpar@774
    80
    for(Graph::NodeIt n(G); n!=INVALID; ++n) { 
marci@304
    81
      cout << node_name[n] << ": ";
marci@301
    82
      cout << "out edges: ";
alpar@774
    83
      for(Graph::OutEdgeIt e(G, n); e!=INVALID; ++e) 
marci@304
    84
	cout << edge_name[e] << " ";
marci@301
    85
      cout << "in edges: ";
alpar@774
    86
      for(Graph::InEdgeIt e(G, n); e!=INVALID; ++e) 
marci@304
    87
	cout << edge_name[e] << " ";
marci@301
    88
      cout << endl;
marci@301
    89
    }
marci@301
    90
marci@301
    91
    cout << "bfs from s ..." << endl;
marci@360
    92
    BfsIterator< Graph, Graph::NodeMap<bool> > bfs(G);
marci@301
    93
    bfs.pushAndSetReached(s);
marci@301
    94
    while (!bfs.finished()) {
marci@301
    95
      //cout << "edge: ";
marci@777
    96
      if (Graph::Edge(bfs)!=INVALID) {
marci@304
    97
	cout << edge_name[bfs] << /*endl*/", " << 
alpar@774
    98
	  node_name[G.tail(bfs)] << 
marci@301
    99
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   100
	  node_name[G.head(bfs)] << 
marci@301
   101
	  (bfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@301
   102
	   ": is not newly reached.");
marci@301
   103
      } else { 
marci@301
   104
	cout << "invalid" << /*endl*/", " << 
marci@777
   105
	  node_name[bfs.tail()] << 
marci@301
   106
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@301
   107
	  
marci@301
   108
	  "invalid.";
marci@301
   109
      }
marci@301
   110
      cout << endl;
marci@301
   111
      ++bfs;
marci@301
   112
    }
marci@301
   113
marci@301
   114
    cout << "    /-->    ------------->            "<< endl;
marci@301
   115
    cout << "   / /-- v1 <-\\      /---- v3-\\      "<< endl;
marci@301
   116
    cout << "  / |          |    /  /->     \\     "<< endl;
marci@301
   117
    cout << " /  |          |   /  |    ^    \\  "<< endl;
marci@301
   118
    cout << "s   |          |  /   |    |     \\->  t "<< endl;
marci@301
   119
    cout << " \\  |          | /    |    |     /->  "<< endl;
marci@301
   120
    cout << "  \\ |       --/ /     |    |    /     "<< endl;
marci@301
   121
    cout << "   \\ \\-> v2 <--/       \\-- v4 -/      "<< endl;
marci@301
   122
    cout << "    \\-->    ------------->         "<< endl;
marci@301
   123
marci@301
   124
    cout << "dfs from s ..." << endl;
marci@360
   125
    DfsIterator< Graph, Graph::NodeMap<bool> > dfs(G);
marci@301
   126
    dfs.pushAndSetReached(s);
marci@301
   127
    while (!dfs.finished()) {
marci@301
   128
      ++dfs;
marci@301
   129
      //cout << "edge: ";
marci@777
   130
      if (Graph::Edge(dfs)!=INVALID) {
marci@304
   131
	cout << edge_name[dfs] << /*endl*/", " << 
alpar@774
   132
	  node_name[G.tail(dfs)] << 
marci@301
   133
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   134
	  node_name[G.head(dfs)] << 
marci@301
   135
	  (dfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@301
   136
	   ": is not newly reached.");
marci@301
   137
      } else { 
marci@301
   138
	cout << "invalid" << /*endl*/", " << 
marci@777
   139
	  node_name[dfs.tail()] << 
marci@301
   140
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@301
   141
	  
marci@301
   142
	  "invalid.";
marci@301
   143
      }
marci@301
   144
      cout << endl;
marci@301
   145
    }
marci@301
   146
  }
marci@301
   147
marci@301
   148
marci@301
   149
  {
marci@312
   150
    typedef RevGraphWrapper<const Graph> GW;
marci@301
   151
    GW gw(G);
marci@301
   152
    
marci@301
   153
    EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
marci@301
   154
    
marci@301
   155
    cout << "bfs and dfs iterator demo on the reversed directed graph" << endl;
alpar@774
   156
    for(GW::NodeIt n(gw); n!=INVALID; ++n) { 
marci@317
   157
      cout << node_name[GW::Node(n)] << ": ";
marci@301
   158
      cout << "out edges: ";
alpar@774
   159
      for(GW::OutEdgeIt e(gw, n); e!=INVALID; ++e) 
marci@304
   160
	cout << edge_name[e] << " ";
marci@301
   161
      cout << "in edges: ";
alpar@774
   162
      for(GW::InEdgeIt e(gw, n); e!=INVALID; ++e) 
marci@304
   163
	cout << edge_name[e] << " ";
marci@301
   164
      cout << endl;
marci@301
   165
    }
marci@301
   166
marci@301
   167
    cout << "bfs from t ..." << endl;
marci@360
   168
    BfsIterator< GW, GW::NodeMap<bool> > bfs(gw);
marci@301
   169
    bfs.pushAndSetReached(t);
marci@301
   170
    while (!bfs.finished()) {
marci@301
   171
      //cout << "edge: ";
marci@777
   172
      if (GW::Edge(bfs)!=INVALID) {
marci@777
   173
	cout << edge_name[GW::Edge(bfs)] << /*endl*/", " << 
alpar@774
   174
	  node_name[gw.tail(bfs)] << 
marci@301
   175
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   176
	  node_name[gw.head(bfs)] << 
marci@301
   177
	  (bfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@301
   178
	   ": is not newly reached.");
marci@301
   179
      } else { 
marci@301
   180
	cout << "invalid" << /*endl*/", " << 
marci@777
   181
	  node_name[bfs.tail()] << 
marci@301
   182
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@301
   183
	  
marci@301
   184
	  "invalid.";
marci@301
   185
      }
marci@301
   186
      cout << endl;
marci@301
   187
      ++bfs;
marci@301
   188
    }
marci@301
   189
marci@301
   190
    cout << "    /-->    ------------->            "<< endl;
marci@301
   191
    cout << "   / /-- v1 <-\\      /---- v3-\\      "<< endl;
marci@301
   192
    cout << "  / |          |    /  /->     \\     "<< endl;
marci@301
   193
    cout << " /  |          |   /  |    ^    \\  "<< endl;
marci@301
   194
    cout << "s   |          |  /   |    |     \\->  t "<< endl;
marci@301
   195
    cout << " \\  |          | /    |    |     /->  "<< endl;
marci@301
   196
    cout << "  \\ |       --/ /     |    |    /     "<< endl;
marci@301
   197
    cout << "   \\ \\-> v2 <--/       \\-- v4 -/      "<< endl;
marci@301
   198
    cout << "    \\-->    ------------->         "<< endl;
marci@301
   199
    
marci@301
   200
    cout << "dfs from t ..." << endl;
marci@360
   201
    DfsIterator< GW, GW::NodeMap<bool> > dfs(gw);
marci@301
   202
    dfs.pushAndSetReached(t);
marci@301
   203
    while (!dfs.finished()) {
marci@301
   204
      ++dfs;
marci@301
   205
      //cout << "edge: ";
marci@777
   206
      if (GW::Edge(dfs)!=INVALID) {
marci@777
   207
	cout << edge_name[GW::Edge(dfs)] << /*endl*/", " << 
alpar@774
   208
	  node_name[gw.tail(dfs)] << 
marci@301
   209
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   210
	  node_name[gw.head(dfs)] << 
marci@301
   211
	  (dfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@301
   212
	   ": is not newly reached.");
marci@301
   213
      } else { 
marci@301
   214
	cout << "invalid" << /*endl*/", " << 
marci@777
   215
	  node_name[dfs.tail()] << 
marci@301
   216
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@301
   217
	  
marci@301
   218
	  "invalid.";
marci@301
   219
      }
marci@301
   220
      cout << endl;
marci@301
   221
    }
marci@301
   222
  }
marci@301
   223
alpar@774
   224
//   {
alpar@774
   225
//     typedef UndirGraphWrapper<const Graph> GW;
alpar@774
   226
//     GW gw(G);
alpar@774
   227
    
alpar@774
   228
//     EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
alpar@774
   229
    
alpar@774
   230
//     cout << "bfs and dfs iterator demo on the undirected graph" << endl;
alpar@774
   231
//     for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { 
alpar@774
   232
//       cout << node_name[GW::Node(n)] << ": ";
alpar@774
   233
//       cout << "out edges: ";
alpar@774
   234
//       for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 
alpar@774
   235
// 	cout << edge_name[e] << " ";
alpar@774
   236
//       cout << "in edges: ";
alpar@774
   237
//       for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 
alpar@774
   238
// 	cout << edge_name[e] << " ";
alpar@774
   239
//       cout << endl;
alpar@774
   240
//     }
alpar@774
   241
// //     for(GW::EdgeIt e=gw.first<GW::EdgeIt>(); gw.valid(e); gw.next(e)) { 
alpar@774
   242
// //       cout << edge_name.get(e) << " ";
alpar@774
   243
// //     }
alpar@774
   244
// //     cout << endl;
alpar@774
   245
alpar@774
   246
//     cout << "bfs from t ..." << endl;
alpar@774
   247
//     BfsIterator< GW, GW::NodeMap<bool> > bfs(gw);
alpar@774
   248
//     bfs.pushAndSetReached(t);
alpar@774
   249
//     while (!bfs.finished()) {
alpar@774
   250
//       //cout << "edge: ";
alpar@774
   251
//       if (gw.valid(GW::OutEdgeIt(bfs))) {
alpar@774
   252
// 	cout << edge_name[GW::OutEdgeIt(bfs)] << /*endl*/", " << 
alpar@774
   253
// 	  node_name[gw.aNode(bfs)] << 
alpar@774
   254
// 	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   255
// 	  node_name[gw.bNode(bfs)] << 
alpar@774
   256
// 	  (bfs.isBNodeNewlyReached() ? ": is newly reached." : 
alpar@774
   257
// 	   ": is not newly reached.");
alpar@774
   258
//       } else { 
alpar@774
   259
// 	cout << "invalid" << /*endl*/", " << 
alpar@774
   260
// 	  node_name[bfs.aNode()] << 
alpar@774
   261
// 	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   262
	  
alpar@774
   263
// 	  "invalid.";
alpar@774
   264
//       }
alpar@774
   265
//       cout << endl;
alpar@774
   266
//       ++bfs;
alpar@774
   267
//     }
alpar@774
   268
alpar@774
   269
//     cout << "    /-->    ------------->            "<< endl;
alpar@774
   270
//     cout << "   / /-- v1 <-\\      /---- v3-\\      "<< endl;
alpar@774
   271
//     cout << "  / |          |    /  /->     \\     "<< endl;
alpar@774
   272
//     cout << " /  |          |   /  |    ^    \\  "<< endl;
alpar@774
   273
//     cout << "s   |          |  /   |    |     \\->  t "<< endl;
alpar@774
   274
//     cout << " \\  |          | /    |    |     /->  "<< endl;
alpar@774
   275
//     cout << "  \\ |       --/ /     |    |    /     "<< endl;
alpar@774
   276
//     cout << "   \\ \\-> v2 <--/       \\-- v4 -/      "<< endl;
alpar@774
   277
//     cout << "    \\-->    ------------->         "<< endl;
alpar@774
   278
    
alpar@774
   279
//     cout << "dfs from t ..." << endl;
alpar@774
   280
//     DfsIterator< GW, GW::NodeMap<bool> > dfs(gw);
alpar@774
   281
//     dfs.pushAndSetReached(t);
alpar@774
   282
//     while (!dfs.finished()) {
alpar@774
   283
//       ++dfs;
alpar@774
   284
//       //cout << "edge: ";
alpar@774
   285
//       if (gw.valid(GW::OutEdgeIt(dfs))) {
alpar@774
   286
// 	cout << edge_name[GW::OutEdgeIt(dfs)] << /*endl*/", " << 
alpar@774
   287
// 	  node_name[gw.aNode(dfs)] << 
alpar@774
   288
// 	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   289
// 	  node_name[gw.bNode(dfs)] << 
alpar@774
   290
// 	  (dfs.isBNodeNewlyReached() ? ": is newly reached." : 
alpar@774
   291
// 	   ": is not newly reached.");
alpar@774
   292
//       } else { 
alpar@774
   293
// 	cout << "invalid" << /*endl*/", " << 
alpar@774
   294
// 	  node_name[dfs.aNode()] << 
alpar@774
   295
// 	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   296
	  
alpar@774
   297
// 	  "invalid.";
alpar@774
   298
//       }
alpar@774
   299
//       cout << endl;
alpar@774
   300
//     }
alpar@774
   301
//   }
alpar@774
   302
alpar@774
   303
alpar@774
   304
marci@301
   305
  {
alpar@774
   306
    typedef BidirGraphWrapper<const Graph> GW;
marci@301
   307
    GW gw(G);
marci@301
   308
    
marci@301
   309
    EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
marci@301
   310
    
alpar@774
   311
    cout << "bfs and dfs iterator demo on the bidirected graph" << endl;
alpar@774
   312
//     for(GW::EdgeIt e(gw); e!=INVALID; ++e) {
alpar@774
   313
//       cout << node_name[gw.tail(e)] << "->" << node_name[gw.head(e)] << " ";
alpar@774
   314
//     } 
alpar@774
   315
    for(GW::NodeIt n(gw); n!=INVALID; ++n) { 
marci@317
   316
      cout << node_name[GW::Node(n)] << ": ";
marci@301
   317
      cout << "out edges: ";
alpar@774
   318
      for(GW::OutEdgeIt e(gw, n); e!=INVALID; ++e) 
marci@304
   319
	cout << edge_name[e] << " ";
marci@301
   320
      cout << "in edges: ";
alpar@774
   321
      for(GW::InEdgeIt e(gw, n); e!=INVALID; ++e) 
marci@304
   322
	cout << edge_name[e] << " ";
marci@301
   323
      cout << endl;
marci@301
   324
    }
marci@301
   325
//     for(GW::EdgeIt e=gw.first<GW::EdgeIt>(); gw.valid(e); gw.next(e)) { 
marci@301
   326
//       cout << edge_name.get(e) << " ";
marci@301
   327
//     }
marci@301
   328
//     cout << endl;
marci@301
   329
marci@301
   330
    cout << "bfs from t ..." << endl;
marci@360
   331
    BfsIterator< GW, GW::NodeMap<bool> > bfs(gw);
marci@301
   332
    bfs.pushAndSetReached(t);
marci@301
   333
    while (!bfs.finished()) {
marci@301
   334
      //cout << "edge: ";
marci@777
   335
      if (GW::Edge(bfs)!=INVALID) {
marci@777
   336
	cout << edge_name[GW::Edge(bfs)] << /*endl*/", " << 
alpar@774
   337
	  node_name[gw.tail(bfs)] << 
marci@301
   338
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   339
	  node_name[gw.head(bfs)] << 
marci@301
   340
	  (bfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@301
   341
	   ": is not newly reached.");
marci@301
   342
      } else { 
marci@301
   343
	cout << "invalid" << /*endl*/", " << 
marci@777
   344
	  node_name[bfs.tail()] << 
marci@301
   345
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@301
   346
	  
marci@301
   347
	  "invalid.";
marci@301
   348
      }
marci@301
   349
      cout << endl;
marci@301
   350
      ++bfs;
marci@301
   351
    }
marci@301
   352
marci@301
   353
    cout << "    /-->    ------------->            "<< endl;
marci@301
   354
    cout << "   / /-- v1 <-\\      /---- v3-\\      "<< endl;
marci@301
   355
    cout << "  / |          |    /  /->     \\     "<< endl;
marci@301
   356
    cout << " /  |          |   /  |    ^    \\  "<< endl;
marci@301
   357
    cout << "s   |          |  /   |    |     \\->  t "<< endl;
marci@301
   358
    cout << " \\  |          | /    |    |     /->  "<< endl;
marci@301
   359
    cout << "  \\ |       --/ /     |    |    /     "<< endl;
marci@301
   360
    cout << "   \\ \\-> v2 <--/       \\-- v4 -/      "<< endl;
marci@301
   361
    cout << "    \\-->    ------------->         "<< endl;
marci@301
   362
    
marci@301
   363
    cout << "dfs from t ..." << endl;
marci@360
   364
    DfsIterator< GW, GW::NodeMap<bool> > dfs(gw);
marci@301
   365
    dfs.pushAndSetReached(t);
marci@301
   366
    while (!dfs.finished()) {
marci@301
   367
      ++dfs;
marci@301
   368
      //cout << "edge: ";
marci@777
   369
      if (GW::Edge(dfs)!=INVALID) {
marci@777
   370
	cout << edge_name[GW::Edge(dfs)] << /*endl*/", " << 
alpar@774
   371
	  node_name[gw.tail(dfs)] << 
marci@301
   372
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
alpar@774
   373
	  node_name[gw.head(dfs)] << 
marci@301
   374
	  (dfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@301
   375
	   ": is not newly reached.");
marci@301
   376
      } else { 
marci@301
   377
	cout << "invalid" << /*endl*/", " << 
marci@777
   378
	  node_name[dfs.tail()] << 
marci@301
   379
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@301
   380
	  
marci@301
   381
	  "invalid.";
marci@301
   382
      }
marci@301
   383
      cout << endl;
marci@301
   384
    }
marci@301
   385
  }
marci@301
   386
marci@301
   387
  return 0;
marci@301
   388
}