src/work/iterator_bfs_demo.cc
author marci
Thu, 04 Mar 2004 19:45:06 +0000
changeset 156 a34e5a909e97
parent 99 f26897fb91fd
child 158 4f54d89fa9d2
permissions -rw-r--r--
.
marci@75
     1
#include <iostream>
marci@75
     2
#include <vector>
marci@75
     3
#include <string>
marci@75
     4
marci@75
     5
#include <list_graph.hh>
marci@75
     6
#include <bfs_iterator.hh>
marci@75
     7
#include <graph_wrapper.h>
marci@75
     8
alpar@107
     9
using namespace hugo;
marci@75
    10
marci@75
    11
int main (int, char*[])
marci@75
    12
{
marci@75
    13
  typedef ListGraph::NodeIt NodeIt;
marci@75
    14
  typedef ListGraph::EdgeIt EdgeIt;
marci@75
    15
  typedef ListGraph::EachNodeIt EachNodeIt;
marci@75
    16
  typedef ListGraph::EachEdgeIt EachEdgeIt;
marci@75
    17
  typedef ListGraph::OutEdgeIt OutEdgeIt;
marci@75
    18
  typedef ListGraph::InEdgeIt InEdgeIt;
marci@75
    19
  typedef ListGraph::SymEdgeIt SymEdgeIt;
marci@75
    20
 
marci@75
    21
  ListGraph G;
marci@75
    22
marci@75
    23
  NodeIt s=G.addNode();
marci@75
    24
  NodeIt v1=G.addNode();
marci@75
    25
  NodeIt v2=G.addNode();
marci@75
    26
  NodeIt v3=G.addNode();
marci@75
    27
  NodeIt v4=G.addNode();
marci@75
    28
  NodeIt t=G.addNode();
marci@75
    29
  
marci@75
    30
  G.addEdge(s, v1);
marci@75
    31
  G.addEdge(s, v2);
marci@75
    32
  G.addEdge(v1, v2);
marci@75
    33
  G.addEdge(v2, v1);
marci@75
    34
  G.addEdge(v1, v3);
marci@75
    35
  G.addEdge(v3, v2);
marci@75
    36
  G.addEdge(v2, v4);
marci@75
    37
  G.addEdge(v4, v3);
marci@75
    38
  G.addEdge(v3, t);
marci@75
    39
  G.addEdge(v4, t);
marci@75
    40
marci@75
    41
  std::cout << "bfs and dfs demo on the directed graph" << std::endl;
marci@75
    42
  for(EachNodeIt i=G.first<EachNodeIt>(); i.valid(); ++i) { 
marci@75
    43
    std::cout << i << ": ";
marci@75
    44
    std::cout << "out edges: ";
marci@75
    45
    for(OutEdgeIt j=G.first<OutEdgeIt>(i); j.valid(); ++j) 
marci@75
    46
      std::cout << j << " ";
marci@75
    47
    std::cout << "in edges: ";
marci@75
    48
    for(InEdgeIt j=G.first<InEdgeIt>(i); j.valid(); ++j) 
marci@75
    49
      std::cout << j << " ";
marci@75
    50
    std::cout << std::endl;
marci@75
    51
  }
marci@75
    52
  
marci@75
    53
  {
marci@75
    54
    std::cout << "iterator bfs demo 4 ..." << std::endl;
marci@75
    55
    BfsIterator4< ListGraph, ListGraph::OutEdgeIt, ListGraph::NodeMap<bool> > bfs(G);
marci@75
    56
    bfs.pushAndSetReached(s);
marci@75
    57
    while (!bfs.finished()) {
marci@75
    58
      if (OutEdgeIt(bfs).valid()) {
marci@75
    59
	std::cout << "OutEdgeIt: " << bfs; 
marci@75
    60
	std::cout << " aNode: " << G.aNode(bfs); 
marci@75
    61
	std::cout << " bNode: " << G.bNode(bfs) << " ";
marci@75
    62
      } else { 
marci@75
    63
	std::cout << "OutEdgeIt: " << "invalid"; 
marci@75
    64
	std::cout << " aNode: " << bfs.aNode(); 
marci@75
    65
	std::cout << " bNode: " << "invalid" << " ";
marci@75
    66
      }
marci@75
    67
      if (bfs.isBNodeNewlyReached()) { 
marci@75
    68
	std::cout << "bNodeIsNewlyReached ";
marci@75
    69
      } else { 
marci@75
    70
	std::cout << "bNodeIsNotNewlyReached ";
marci@75
    71
      } 
marci@75
    72
      if (bfs.isANodeExamined()) { 
marci@75
    73
	std::cout << "aNodeIsExamined ";
marci@75
    74
      } else { 
marci@75
    75
	std::cout << "aNodeIsNotExamined ";
marci@75
    76
      } 
marci@75
    77
      std::cout<<std::endl;
marci@75
    78
      ++bfs;
marci@75
    79
    }
marci@75
    80
  }
marci@75
    81
marci@99
    82
  {
marci@99
    83
    std::cout << "iterator dfs demo 4 ..." << std::endl;
marci@99
    84
    DfsIterator4< ListGraph, ListGraph::OutEdgeIt, ListGraph::NodeMap<bool> > dfs(G);
marci@99
    85
    dfs.pushAndSetReached(s);
marci@99
    86
    while (!dfs.finished()) {
marci@99
    87
      ++dfs;
marci@99
    88
      if (OutEdgeIt(dfs).valid()) {
marci@99
    89
	std::cout << "OutEdgeIt: " << dfs; 
marci@99
    90
	std::cout << " aNode: " << G.aNode(dfs); 
marci@99
    91
	std::cout << " bNode: " << G.bNode(dfs) << " ";
marci@99
    92
      } else { 
marci@99
    93
	std::cout << "OutEdgeIt: " << "invalid"; 
marci@99
    94
	std::cout << " aNode: " << dfs.aNode(); 
marci@99
    95
	std::cout << " bNode: " << "invalid" << " ";
marci@99
    96
      }
marci@99
    97
      if (dfs.isBNodeNewlyReached()) { 
marci@99
    98
	std::cout << "bNodeIsNewlyReached ";
marci@99
    99
      } else { 
marci@99
   100
	std::cout << "bNodeIsNotNewlyReached ";
marci@99
   101
      } 
marci@99
   102
      if (dfs.isANodeExamined()) { 
marci@99
   103
	std::cout << "aNodeIsExamined ";
marci@99
   104
      } else { 
marci@99
   105
	std::cout << "aNodeIsNotExamined ";
marci@99
   106
      } 
marci@99
   107
      std::cout<<std::endl;
marci@99
   108
      //++dfs;
marci@99
   109
    }
marci@99
   110
  }
marci@99
   111
marci@99
   112
marci@75
   113
  typedef ConstTrivGraphWrapper<ListGraph> CTGW;
marci@75
   114
  CTGW wG(G);
marci@75
   115
marci@75
   116
  std::cout << "bfs and dfs demo on the directed graph" << std::endl;
marci@75
   117
  for(CTGW::EachNodeIt i=wG.first<CTGW::EachNodeIt>(); i.valid(); ++i) { 
marci@75
   118
    std::cout << i << ": ";
marci@75
   119
    std::cout << "out edges: ";
marci@75
   120
    for(CTGW::OutEdgeIt j=wG.first<CTGW::OutEdgeIt>(i); j.valid(); ++j) 
marci@75
   121
      std::cout << j << " ";
marci@75
   122
    std::cout << "in edges: ";
marci@75
   123
    for(CTGW::InEdgeIt j=wG.first<CTGW::InEdgeIt>(i); j.valid(); ++j) 
marci@75
   124
      std::cout << j << " ";
marci@75
   125
    std::cout << std::endl;
marci@75
   126
  }
marci@75
   127
marci@75
   128
marci@75
   129
  {
marci@75
   130
    std::cout << "iterator bfs demo 5 ..." << std::endl;
marci@75
   131
    BfsIterator5< CTGW, CTGW::NodeMap<bool> > bfs(wG);
marci@75
   132
    bfs.pushAndSetReached(s);
marci@75
   133
    while (!bfs.finished()) {
marci@75
   134
      if (OutEdgeIt(bfs).valid()) {
marci@75
   135
	std::cout << "OutEdgeIt: " << bfs; 
marci@75
   136
	std::cout << " aNode: " << wG.aNode(bfs); 
marci@75
   137
	std::cout << " bNode: " << wG.bNode(bfs) << " ";
marci@75
   138
      } else { 
marci@75
   139
	std::cout << "OutEdgeIt: " << "invalid"; 
marci@75
   140
	std::cout << " aNode: " << bfs.aNode(); 
marci@75
   141
	std::cout << " bNode: " << "invalid" << " ";
marci@75
   142
      }
marci@75
   143
      if (bfs.isBNodeNewlyReached()) { 
marci@75
   144
	std::cout << "bNodeIsNewlyReached ";
marci@75
   145
      } else { 
marci@75
   146
	std::cout << "bNodeIsNotNewlyReached ";
marci@75
   147
      } 
marci@75
   148
      if (bfs.isANodeExamined()) { 
marci@75
   149
	std::cout << "aNodeIsExamined ";
marci@75
   150
      } else { 
marci@75
   151
	std::cout << "aNodeIsNotExamined ";
marci@75
   152
      } 
marci@75
   153
      std::cout<<std::endl;
marci@75
   154
      ++bfs;
marci@75
   155
    }
marci@75
   156
  }
marci@75
   157
marci@75
   158
marci@75
   159
  return 0;
marci@75
   160
}