src/work/marci/bfsit_vs_byhand.cc
author athos
Thu, 03 Mar 2005 17:18:27 +0000
changeset 1182 a1abe9452199
parent 944 4f064aff855e
permissions -rw-r--r--
Added two demo programs: of course they are not considered to be complete or finished in any sense.
marci@358
     1
// -*- c++ -*-
marci@358
     2
#include <iostream>
marci@358
     3
#include <fstream>
marci@358
     4
marci@944
     5
//#include <sage_graph.h>
alpar@921
     6
#include <lemon/smart_graph.h>
marci@944
     7
#include <lemon/list_graph.h>
alpar@921
     8
#include <lemon/dimacs.h>
alpar@921
     9
#include <lemon/time_measure.h>
alpar@921
    10
//#include <lemon/for_each_macros.h>
marci@944
    11
#include <bfs_mm.h>
marci@944
    12
#include <lemon/bfs.h>
marci@358
    13
alpar@921
    14
using namespace lemon;
marci@358
    15
marci@773
    16
using std::cout;
marci@773
    17
using std::endl;
marci@773
    18
marci@358
    19
int main() {
marci@944
    20
  //  typedef SageGraph Graph; 
marci@944
    21
  typedef SmartGraph Graph ;
marci@944
    22
  //typedef ListGraph Graph; 
marci@358
    23
  typedef Graph::Node Node;
marci@358
    24
  typedef Graph::NodeIt NodeIt;
marci@358
    25
  typedef Graph::Edge Edge;
marci@358
    26
  typedef Graph::EdgeIt EdgeIt;
marci@358
    27
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@358
    28
marci@577
    29
  Graph g;
marci@577
    30
  readDimacs(std::cin, g);
marci@944
    31
  NodeIt s(g);
marci@773
    32
marci@773
    33
  cout << g.nodeNum() << endl;
marci@773
    34
  cout << g.edgeNum() << endl;
marci@577
    35
marci@777
    36
  Graph::NodeMap<Edge> pred(g);
marci@773
    37
  cout << "iteration time of bfs written by hand..." << endl;
marci@358
    38
  Timer ts;
marci@944
    39
  ts.reset();
marci@944
    40
  for (int i=0; i<100; ++i)
marci@358
    41
  {
marci@577
    42
    Graph::NodeMap<bool> reached(g);
marci@358
    43
    reached.set(s, true);
marci@358
    44
    pred.set(s, INVALID);
marci@358
    45
    std::queue<Node> bfs_queue;
marci@773
    46
    bfs_queue.push(s);
marci@358
    47
    while (!bfs_queue.empty()) {
marci@358
    48
      Node v=bfs_queue.front();	
marci@358
    49
      bfs_queue.pop();
marci@944
    50
      for(OutEdgeIt e(g,v); e!=INVALID; ++e) {
alpar@986
    51
	Node w=g.target(e);
marci@358
    52
	if (!reached[w]) {
marci@358
    53
	  bfs_queue.push(w);
marci@358
    54
	  reached.set(w, true);
marci@358
    55
	  pred.set(w, e);
marci@358
    56
	}
marci@358
    57
      }
marci@358
    58
    }
marci@358
    59
  }
marci@944
    60
  std::cout << ts << std::endl;
marci@358
    61
marci@773
    62
  cout << "iteration time with bfs iterator..." << endl;
marci@944
    63
  ts.reset();      
marci@944
    64
  for (int i=0; i<100; ++i)
marci@358
    65
  {
marci@944
    66
    Graph::NodeMap<bool> reached(g);
marci@944
    67
    marci::BfsIterator< Graph, Graph::NodeMap<bool> > bfs(g, reached);
marci@358
    68
    bfs.pushAndSetReached(s);
marci@358
    69
    pred.set(s, INVALID);
marci@358
    70
    while (!bfs.finished()) { 
marci@358
    71
      ++bfs; 
marci@944
    72
      if (Graph::Edge(bfs)!=INVALID && bfs.isBNodeNewlyReached()) 
alpar@986
    73
	pred.set(bfs.target(), Graph::Edge(bfs));
marci@358
    74
    }
marci@358
    75
  }
marci@944
    76
  std::cout << ts << std::endl;
marci@944
    77
marci@944
    78
  cout << "iteration time with bfs aplar..." << endl;
marci@944
    79
  ts.reset();      
marci@944
    80
  for (int i=0; i<100; ++i)
marci@944
    81
  {
marci@944
    82
    Bfs<Graph> bfs(g);
marci@944
    83
    bfs.setPredMap(pred);
marci@944
    84
    bfs.run(s);
marci@944
    85
  }
marci@944
    86
  std::cout << ts << std::endl;
marci@944
    87
marci@358
    88
marci@358
    89
  return 0;
marci@358
    90
}