src/work/iterator_bfs_demo.cc
author klao
Sat, 03 Apr 2004 18:41:46 +0000
changeset 284 2d4684f76aac
parent 265 bf7aea53635a
permissions -rw-r--r--
Superfluous parts removed
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@75
    36
 
marci@174
    37
  Graph G;
marci@75
    38
marci@174
    39
  Node s=G.addNode();
marci@174
    40
  Node v1=G.addNode();
marci@174
    41
  Node v2=G.addNode();
marci@174
    42
  Node v3=G.addNode();
marci@174
    43
  Node v4=G.addNode();
marci@174
    44
  Node t=G.addNode();
marci@75
    45
  
marci@174
    46
  Graph::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@174
    75
//   typedef TrivGraphWrapper<const Graph> CGW;
marci@234
    76
//   CGW gw(G);
marci@158
    77
marci@158
    78
//   cout << "bfs and dfs demo on the directed graph" << endl;
marci@234
    79
//   for(CGW::NodeIt n=gw.first<CGW::NodeIt>(); n.valid(); ++n) { 
marci@158
    80
//     cout << n << ": ";
marci@158
    81
//     cout << "out edges: ";
marci@234
    82
//     for(CGW::OutEdgeIt e=gw.first<CGW::OutEdgeIt>(n); e.valid(); ++e) 
marci@158
    83
//       cout << e << " ";
marci@158
    84
//     cout << "in edges: ";
marci@234
    85
//     for(CGW::InEdgeIt e=gw.first<CGW::InEdgeIt>(n); e.valid(); ++e) 
marci@158
    86
//       cout << e << " ";
marci@158
    87
//     cout << endl;
marci@158
    88
//   }
marci@158
    89
marci@158
    90
  {
marci@174
    91
    typedef TrivGraphWrapper<const Graph> GW;
marci@234
    92
    GW gw(G);
marci@158
    93
marci@234
    94
    EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
marci@158
    95
    
marci@158
    96
    cout << "bfs and dfs iterator demo on the directed graph" << endl;
marci@279
    97
    for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { 
marci@158
    98
      cout << node_name.get(n) << ": ";
marci@158
    99
      cout << "out edges: ";
marci@279
   100
      for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 
marci@158
   101
	cout << edge_name.get(e) << " ";
marci@158
   102
      cout << "in edges: ";
marci@279
   103
      for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 
marci@158
   104
	cout << edge_name.get(e) << " ";
marci@158
   105
      cout << endl;
marci@158
   106
    }
marci@158
   107
marci@158
   108
    cout << "bfs from s ..." << endl;
marci@234
   109
    BfsIterator5< GW, GW::NodeMap<bool> > bfs(gw);
marci@75
   110
    bfs.pushAndSetReached(s);
marci@75
   111
    while (!bfs.finished()) {
marci@158
   112
      //cout << "edge: ";
marci@234
   113
      if (gw.valid(bfs)) {
marci@158
   114
	cout << edge_name.get(bfs) << /*endl*/", " << 
marci@234
   115
	  node_name.get(gw.aNode(bfs)) << 
marci@158
   116
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   117
	  node_name.get(gw.bNode(bfs)) << 
marci@158
   118
	  (bfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@158
   119
	   ": is not newly reached.");
marci@75
   120
      } else { 
marci@158
   121
	cout << "invalid" << /*endl*/", " << 
marci@234
   122
	  node_name.get(bfs.aNode()) << 
marci@158
   123
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   124
	  
marci@158
   125
	  "invalid.";
marci@75
   126
      }
marci@158
   127
      cout << endl;
marci@158
   128
      ++bfs;
marci@158
   129
    }
marci@158
   130
marci@158
   131
    cout << "    /-->    ------------->            "<< endl;
marci@158
   132
    cout << "   / /-- v1 <-\\      /---- v3-\\      "<< endl;
marci@158
   133
    cout << "  / |          |    /  /->     \\     "<< endl;
marci@158
   134
    cout << " /  |          |   /  |    ^    \\  "<< endl;
marci@158
   135
    cout << "s   |          |  /   |    |     \\->  t "<< endl;
marci@158
   136
    cout << " \\  |          | /    |    |     /->  "<< endl;
marci@158
   137
    cout << "  \\ |       --/ /     |    |    /     "<< endl;
marci@158
   138
    cout << "   \\ \\-> v2 <--/       \\-- v4 -/      "<< endl;
marci@158
   139
    cout << "    \\-->    ------------->         "<< endl;
marci@158
   140
marci@158
   141
    cout << "dfs from s ..." << endl;
marci@234
   142
    DfsIterator5< GW, GW::NodeMap<bool> > dfs(gw);
marci@158
   143
    dfs.pushAndSetReached(s);
marci@158
   144
    while (!dfs.finished()) {
marci@158
   145
      ++dfs;
marci@158
   146
      //cout << "edge: ";
marci@234
   147
      if (gw.valid(dfs)) {
marci@158
   148
	cout << edge_name.get(dfs) << /*endl*/", " << 
marci@234
   149
	  node_name.get(gw.aNode(dfs)) << 
marci@158
   150
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   151
	  node_name.get(gw.bNode(dfs)) << 
marci@158
   152
	  (dfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@158
   153
	   ": is not newly reached.");
marci@75
   154
      } else { 
marci@158
   155
	cout << "invalid" << /*endl*/", " << 
marci@234
   156
	  node_name.get(dfs.aNode()) << 
marci@158
   157
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   158
	  
marci@158
   159
	  "invalid.";
marci@158
   160
      }
marci@158
   161
      cout << endl;
marci@158
   162
    }
marci@158
   163
  }
marci@158
   164
marci@158
   165
marci@158
   166
  {
marci@234
   167
    typedef RevGraphWrapper<const TrivGraphWrapper<const Graph> > GW;
marci@234
   168
    GW gw(G);
marci@158
   169
    
marci@234
   170
    EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
marci@158
   171
    
marci@158
   172
    cout << "bfs and dfs iterator demo on the reversed directed graph" << endl;
marci@279
   173
    for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { 
marci@158
   174
      cout << node_name.get(n) << ": ";
marci@158
   175
      cout << "out edges: ";
marci@279
   176
      for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 
marci@158
   177
	cout << edge_name.get(e) << " ";
marci@158
   178
      cout << "in edges: ";
marci@279
   179
      for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 
marci@158
   180
	cout << edge_name.get(e) << " ";
marci@158
   181
      cout << endl;
marci@158
   182
    }
marci@158
   183
marci@158
   184
    cout << "bfs from t ..." << endl;
marci@234
   185
    BfsIterator5< GW, GW::NodeMap<bool> > bfs(gw);
marci@158
   186
    bfs.pushAndSetReached(t);
marci@158
   187
    while (!bfs.finished()) {
marci@158
   188
      //cout << "edge: ";
marci@234
   189
      if (gw.valid(bfs)) {
marci@158
   190
	cout << edge_name.get(bfs) << /*endl*/", " << 
marci@234
   191
	  node_name.get(gw.aNode(bfs)) << 
marci@158
   192
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   193
	  node_name.get(gw.bNode(bfs)) << 
marci@158
   194
	  (bfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@158
   195
	   ": is not newly reached.");
marci@75
   196
      } else { 
marci@158
   197
	cout << "invalid" << /*endl*/", " << 
marci@234
   198
	  node_name.get(bfs.aNode()) << 
marci@158
   199
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   200
	  
marci@158
   201
	  "invalid.";
marci@158
   202
      }
marci@158
   203
      cout << endl;
marci@75
   204
      ++bfs;
marci@75
   205
    }
marci@158
   206
marci@158
   207
    cout << "    /-->    ------------->            "<< endl;
marci@158
   208
    cout << "   / /-- v1 <-\\      /---- v3-\\      "<< endl;
marci@158
   209
    cout << "  / |          |    /  /->     \\     "<< endl;
marci@158
   210
    cout << " /  |          |   /  |    ^    \\  "<< endl;
marci@158
   211
    cout << "s   |          |  /   |    |     \\->  t "<< endl;
marci@158
   212
    cout << " \\  |          | /    |    |     /->  "<< endl;
marci@158
   213
    cout << "  \\ |       --/ /     |    |    /     "<< endl;
marci@158
   214
    cout << "   \\ \\-> v2 <--/       \\-- v4 -/      "<< endl;
marci@158
   215
    cout << "    \\-->    ------------->         "<< endl;
marci@158
   216
    
marci@158
   217
    cout << "dfs from t ..." << endl;
marci@234
   218
    DfsIterator5< GW, GW::NodeMap<bool> > dfs(gw);
marci@158
   219
    dfs.pushAndSetReached(t);
marci@158
   220
    while (!dfs.finished()) {
marci@158
   221
      ++dfs;
marci@158
   222
      //cout << "edge: ";
marci@234
   223
      if (gw.valid(dfs)) {
marci@158
   224
	cout << edge_name.get(dfs) << /*endl*/", " << 
marci@234
   225
	  node_name.get(gw.aNode(dfs)) << 
marci@158
   226
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   227
	  node_name.get(gw.bNode(dfs)) << 
marci@158
   228
	  (dfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@158
   229
	   ": is not newly reached.");
marci@158
   230
      } else { 
marci@158
   231
	cout << "invalid" << /*endl*/", " << 
marci@234
   232
	  node_name.get(dfs.aNode()) << 
marci@158
   233
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   234
	  
marci@158
   235
	  "invalid.";
marci@158
   236
      }
marci@158
   237
      cout << endl;
marci@158
   238
    }
marci@75
   239
  }
marci@75
   240
marci@99
   241
  {
marci@236
   242
    //typedef UndirGraphWrapper<const Graph> GW;
marci@236
   243
    typedef UndirGraphWrapper<const TrivGraphWrapper<const Graph> > GW;
marci@234
   244
    GW gw(G);
marci@158
   245
    
marci@234
   246
    EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(gw, node_name);
marci@158
   247
    
marci@158
   248
    cout << "bfs and dfs iterator demo on the undirected graph" << endl;
marci@279
   249
    for(GW::NodeIt n(gw); gw.valid(n); gw.next(n)) { 
marci@158
   250
      cout << node_name.get(n) << ": ";
marci@158
   251
      cout << "out edges: ";
marci@279
   252
      for(GW::OutEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 
marci@158
   253
	cout << edge_name.get(e) << " ";
marci@158
   254
      cout << "in edges: ";
marci@279
   255
      for(GW::InEdgeIt e(gw, n); gw.valid(e); gw.next(e)) 
marci@158
   256
	cout << edge_name.get(e) << " ";
marci@158
   257
      cout << endl;
marci@158
   258
    }
marci@238
   259
//     for(GW::EdgeIt e=gw.first<GW::EdgeIt>(); gw.valid(e); gw.next(e)) { 
marci@238
   260
//       cout << edge_name.get(e) << " ";
marci@238
   261
//     }
marci@238
   262
//     cout << endl;
marci@158
   263
marci@158
   264
    cout << "bfs from t ..." << endl;
marci@234
   265
    BfsIterator5< GW, GW::NodeMap<bool> > bfs(gw);
marci@158
   266
    bfs.pushAndSetReached(t);
marci@158
   267
    while (!bfs.finished()) {
marci@158
   268
      //cout << "edge: ";
marci@234
   269
      if (gw.valid(GW::OutEdgeIt(bfs))) {
marci@158
   270
	cout << edge_name.get(GW::OutEdgeIt(bfs)) << /*endl*/", " << 
marci@234
   271
	  node_name.get(gw.aNode(bfs)) << 
marci@158
   272
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   273
	  node_name.get(gw.bNode(bfs)) << 
marci@158
   274
	  (bfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@158
   275
	   ": is not newly reached.");
marci@158
   276
      } else { 
marci@158
   277
	cout << "invalid" << /*endl*/", " << 
marci@234
   278
	  node_name.get(bfs.aNode()) << 
marci@158
   279
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   280
	  
marci@158
   281
	  "invalid.";
marci@158
   282
      }
marci@158
   283
      cout << endl;
marci@158
   284
      ++bfs;
marci@158
   285
    }
marci@158
   286
marci@158
   287
    cout << "    /-->    ------------->            "<< endl;
marci@158
   288
    cout << "   / /-- v1 <-\\      /---- v3-\\      "<< endl;
marci@158
   289
    cout << "  / |          |    /  /->     \\     "<< endl;
marci@158
   290
    cout << " /  |          |   /  |    ^    \\  "<< endl;
marci@158
   291
    cout << "s   |          |  /   |    |     \\->  t "<< endl;
marci@158
   292
    cout << " \\  |          | /    |    |     /->  "<< endl;
marci@158
   293
    cout << "  \\ |       --/ /     |    |    /     "<< endl;
marci@158
   294
    cout << "   \\ \\-> v2 <--/       \\-- v4 -/      "<< endl;
marci@158
   295
    cout << "    \\-->    ------------->         "<< endl;
marci@158
   296
    
marci@158
   297
    cout << "dfs from t ..." << endl;
marci@234
   298
    DfsIterator5< GW, GW::NodeMap<bool> > dfs(gw);
marci@158
   299
    dfs.pushAndSetReached(t);
marci@99
   300
    while (!dfs.finished()) {
marci@99
   301
      ++dfs;
marci@158
   302
      //cout << "edge: ";
marci@234
   303
      if (gw.valid(GW::OutEdgeIt(dfs))) {
marci@158
   304
	cout << edge_name.get(GW::OutEdgeIt(dfs)) << /*endl*/", " << 
marci@234
   305
	  node_name.get(gw.aNode(dfs)) << 
marci@158
   306
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   307
	  node_name.get(gw.bNode(dfs)) << 
marci@158
   308
	  (dfs.isBNodeNewlyReached() ? ": is newly reached." : 
marci@158
   309
	   ": is not newly reached.");
marci@99
   310
      } else { 
marci@158
   311
	cout << "invalid" << /*endl*/", " << 
marci@234
   312
	  node_name.get(dfs.aNode()) << 
marci@158
   313
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@234
   314
	  
marci@158
   315
	  "invalid.";
marci@99
   316
      }
marci@158
   317
      cout << endl;
marci@99
   318
    }
marci@99
   319
  }
marci@99
   320
marci@75
   321
  return 0;
marci@75
   322
}