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