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