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