COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/iterator_bfs_demo.cc @ 281:3fefabfd00b7

Last change on this file since 281:3fefabfd00b7 was 279:be43902fadb7, checked in by marci, 20 years ago

minor changes

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