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