src/work/iterator_bfs_demo.cc
author athos
Fri, 19 Mar 2004 14:47:36 +0000
changeset 207 9910d5a5be7f
parent 158 4f54d89fa9d2
child 234 348f8fd374ee
permissions -rw-r--r--
M?g ?rtam bele 2 dolgot, meg a tesztelot is kibovitettem.
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@158
    81
//   CGW wG(G);
marci@158
    82
marci@158
    83
//   cout << "bfs and dfs demo on the directed graph" << endl;
marci@174
    84
//   for(CGW::NodeIt n=wG.first<CGW::NodeIt>(); n.valid(); ++n) { 
marci@158
    85
//     cout << n << ": ";
marci@158
    86
//     cout << "out edges: ";
marci@158
    87
//     for(CGW::OutEdgeIt e=wG.first<CGW::OutEdgeIt>(n); e.valid(); ++e) 
marci@158
    88
//       cout << e << " ";
marci@158
    89
//     cout << "in edges: ";
marci@158
    90
//     for(CGW::InEdgeIt e=wG.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@158
    97
    GW wG(G);
marci@158
    98
marci@174
    99
    EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(wG, node_name);
marci@158
   100
    
marci@158
   101
    cout << "bfs and dfs iterator demo on the directed graph" << endl;
marci@174
   102
    for(GW::NodeIt n=wG.first<GW::NodeIt>(); wG.valid(n); wG.next(n)) { 
marci@158
   103
      cout << node_name.get(n) << ": ";
marci@158
   104
      cout << "out edges: ";
marci@158
   105
      for(GW::OutEdgeIt e=wG.first<GW::OutEdgeIt>(n); wG.valid(e); wG.next(e)) 
marci@158
   106
	cout << edge_name.get(e) << " ";
marci@158
   107
      cout << "in edges: ";
marci@158
   108
      for(GW::InEdgeIt e=wG.first<GW::InEdgeIt>(n); wG.valid(e); wG.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@158
   114
    BfsIterator5< GW, GW::NodeMap<bool> > bfs(wG);
marci@75
   115
    bfs.pushAndSetReached(s);
marci@75
   116
    while (!bfs.finished()) {
marci@158
   117
      //cout << "edge: ";
marci@158
   118
      if (wG.valid(bfs)) {
marci@158
   119
	cout << edge_name.get(bfs) << /*endl*/", " << 
marci@158
   120
	  /*" aNode: " <<*/ node_name.get(wG.aNode(bfs)) << 
marci@158
   121
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   122
	  /*" bNode: " <<*/ node_name.get(wG.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@158
   127
	  /*" aNode: " <<*/ node_name.get(bfs.aNode()) << 
marci@158
   128
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   129
	  /*" bNode: " <<*/ 
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@158
   147
    DfsIterator5< GW, GW::NodeMap<bool> > dfs(wG);
marci@158
   148
    dfs.pushAndSetReached(s);
marci@158
   149
    while (!dfs.finished()) {
marci@158
   150
      ++dfs;
marci@158
   151
      //cout << "edge: ";
marci@158
   152
      if (wG.valid(dfs)) {
marci@158
   153
	cout << edge_name.get(dfs) << /*endl*/", " << 
marci@158
   154
	  /*" aNode: " <<*/ node_name.get(wG.aNode(dfs)) << 
marci@158
   155
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   156
	  /*" bNode: " <<*/ node_name.get(wG.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@158
   161
	  /*" aNode: " <<*/ node_name.get(dfs.aNode()) << 
marci@158
   162
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   163
	  /*" bNode: " <<*/ 
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@174
   172
    typedef RevGraphWrapper<const Graph> GW;
marci@158
   173
    GW wG(G);
marci@158
   174
    
marci@174
   175
    EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(wG, node_name);
marci@158
   176
    
marci@158
   177
    cout << "bfs and dfs iterator demo on the reversed directed graph" << endl;
marci@174
   178
    for(GW::NodeIt n=wG.first<GW::NodeIt>(); wG.valid(n); wG.next(n)) { 
marci@158
   179
      cout << node_name.get(n) << ": ";
marci@158
   180
      cout << "out edges: ";
marci@158
   181
      for(GW::OutEdgeIt e=wG.first<GW::OutEdgeIt>(n); wG.valid(e); wG.next(e)) 
marci@158
   182
	cout << edge_name.get(e) << " ";
marci@158
   183
      cout << "in edges: ";
marci@158
   184
      for(GW::InEdgeIt e=wG.first<GW::InEdgeIt>(n); wG.valid(e); wG.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@158
   190
    BfsIterator5< GW, GW::NodeMap<bool> > bfs(wG);
marci@158
   191
    bfs.pushAndSetReached(t);
marci@158
   192
    while (!bfs.finished()) {
marci@158
   193
      //cout << "edge: ";
marci@158
   194
      if (wG.valid(bfs)) {
marci@158
   195
	cout << edge_name.get(bfs) << /*endl*/", " << 
marci@158
   196
	  /*" aNode: " <<*/ node_name.get(wG.aNode(bfs)) << 
marci@158
   197
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   198
	  /*" bNode: " <<*/ node_name.get(wG.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@158
   203
	  /*" aNode: " <<*/ node_name.get(bfs.aNode()) << 
marci@158
   204
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   205
	  /*" bNode: " <<*/ 
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@158
   223
    DfsIterator5< GW, GW::NodeMap<bool> > dfs(wG);
marci@158
   224
    dfs.pushAndSetReached(t);
marci@158
   225
    while (!dfs.finished()) {
marci@158
   226
      ++dfs;
marci@158
   227
      //cout << "edge: ";
marci@158
   228
      if (wG.valid(dfs)) {
marci@158
   229
	cout << edge_name.get(dfs) << /*endl*/", " << 
marci@158
   230
	  /*" aNode: " <<*/ node_name.get(wG.aNode(dfs)) << 
marci@158
   231
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   232
	  /*" bNode: " <<*/ node_name.get(wG.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@158
   237
	  /*" aNode: " <<*/ node_name.get(dfs.aNode()) << 
marci@158
   238
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   239
	  /*" bNode: " <<*/ 
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@174
   247
    typedef UndirGraphWrapper<const Graph> GW;
marci@158
   248
    GW wG(G);
marci@158
   249
    
marci@174
   250
    EdgeNameMap< GW, Graph::NodeMap<string> > edge_name(wG, node_name);
marci@158
   251
    
marci@158
   252
    cout << "bfs and dfs iterator demo on the undirected graph" << endl;
marci@174
   253
    for(GW::NodeIt n=wG.first<GW::NodeIt>(); wG.valid(n); wG.next(n)) { 
marci@158
   254
      cout << node_name.get(n) << ": ";
marci@158
   255
      cout << "out edges: ";
marci@158
   256
      for(GW::OutEdgeIt e=wG.first<GW::OutEdgeIt>(n); wG.valid(e); wG.next(e)) 
marci@158
   257
	cout << edge_name.get(e) << " ";
marci@158
   258
      cout << "in edges: ";
marci@158
   259
      for(GW::InEdgeIt e=wG.first<GW::InEdgeIt>(n); wG.valid(e); wG.next(e)) 
marci@158
   260
	cout << edge_name.get(e) << " ";
marci@158
   261
      cout << endl;
marci@158
   262
    }
marci@158
   263
marci@158
   264
    cout << "bfs from t ..." << endl;
marci@158
   265
    BfsIterator5< GW, GW::NodeMap<bool> > bfs(wG);
marci@158
   266
    bfs.pushAndSetReached(t);
marci@158
   267
    while (!bfs.finished()) {
marci@158
   268
      //cout << "edge: ";
marci@158
   269
      if (wG.valid(GW::OutEdgeIt(bfs))) {
marci@158
   270
	cout << edge_name.get(GW::OutEdgeIt(bfs)) << /*endl*/", " << 
marci@158
   271
	  /*" aNode: " <<*/ node_name.get(wG.aNode(bfs)) << 
marci@158
   272
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   273
	  /*" bNode: " <<*/ node_name.get(wG.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@158
   278
	  /*" aNode: " <<*/ node_name.get(bfs.aNode()) << 
marci@158
   279
	  (bfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   280
	  /*" bNode: " <<*/ 
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@158
   298
    DfsIterator5< GW, GW::NodeMap<bool> > dfs(wG);
marci@158
   299
    dfs.pushAndSetReached(t);
marci@99
   300
    while (!dfs.finished()) {
marci@99
   301
      ++dfs;
marci@158
   302
      //cout << "edge: ";
marci@158
   303
      if (wG.valid(GW::OutEdgeIt(dfs))) {
marci@158
   304
	cout << edge_name.get(GW::OutEdgeIt(dfs)) << /*endl*/", " << 
marci@158
   305
	  /*" aNode: " <<*/ node_name.get(wG.aNode(dfs)) << 
marci@158
   306
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   307
	  /*" bNode: " <<*/ node_name.get(wG.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@158
   312
	  /*" aNode: " <<*/ node_name.get(dfs.aNode()) << 
marci@158
   313
	  (dfs.isANodeExamined() ? ": is examined, " : ": is not examined, ") << 
marci@158
   314
	  /*" bNode: " <<*/ 
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
}