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