src/work/marci/bfsit_vs_byhand.cc
author alpar
Thu, 02 Sep 2004 15:13:21 +0000
changeset 785 a9b0863c2265
parent 773 ce9438c5a82d
child 921 818510fa3d99
permissions -rw-r--r--
Changes in doc. (New module name for array/vector maps added.)
marci@358
     1
// -*- c++ -*-
marci@358
     2
#include <iostream>
marci@358
     3
#include <fstream>
marci@358
     4
marci@642
     5
#include <sage_graph.h>
marci@773
     6
#include <hugo/smart_graph.h>
marci@555
     7
#include <hugo/dimacs.h>
marci@555
     8
#include <hugo/time_measure.h>
marci@762
     9
//#include <hugo/for_each_macros.h>
marci@602
    10
#include <bfs_dfs.h>
marci@358
    11
marci@358
    12
using namespace hugo;
marci@358
    13
marci@773
    14
using std::cout;
marci@773
    15
using std::endl;
marci@773
    16
marci@358
    17
int main() {
marci@642
    18
  typedef SageGraph Graph; 
marci@358
    19
  typedef Graph::Node Node;
marci@358
    20
  typedef Graph::NodeIt NodeIt;
marci@358
    21
  typedef Graph::Edge Edge;
marci@358
    22
  typedef Graph::EdgeIt EdgeIt;
marci@358
    23
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@358
    24
marci@577
    25
  Graph g;
marci@773
    26
  //Node s;
marci@762
    27
  //Graph::EdgeMap<int> cap(g);
marci@577
    28
  //readDimacsMaxFlow(std::cin, g, s, t, cap);
marci@577
    29
  readDimacs(std::cin, g);
marci@773
    30
  NodeIt s;
marci@773
    31
  g.first(s);
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@358
    39
  {
marci@358
    40
    ts.reset();
marci@577
    41
    Graph::NodeMap<bool> reached(g);
marci@358
    42
    reached.set(s, true);
marci@358
    43
    pred.set(s, INVALID);
marci@358
    44
    std::queue<Node> bfs_queue;
marci@773
    45
    bfs_queue.push(s);
marci@358
    46
    while (!bfs_queue.empty()) {
marci@358
    47
      Node v=bfs_queue.front();	
marci@358
    48
      bfs_queue.pop();
marci@358
    49
      OutEdgeIt e;
marci@577
    50
      for(g.first(e,v); g.valid(e); g.next(e)) {
marci@577
    51
	Node w=g.head(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@358
    60
    std::cout << ts << std::endl;
marci@358
    61
  }
marci@358
    62
marci@773
    63
  cout << "iteration time with bfs iterator..." << endl;
marci@358
    64
  {
marci@358
    65
    ts.reset();      
marci@577
    66
    BfsIterator< Graph, Graph::NodeMap<bool> > bfs(g);
marci@358
    67
    bfs.pushAndSetReached(s);
marci@358
    68
    pred.set(s, INVALID);
marci@358
    69
    while (!bfs.finished()) { 
marci@358
    70
      ++bfs; 
marci@577
    71
      if (g.valid(bfs) && bfs.isBNodeNewlyReached()) 
marci@777
    72
	pred.set(bfs.head(), Graph::Edge(bfs));
marci@358
    73
    }
marci@358
    74
    std::cout << ts << std::endl;
marci@358
    75
  }
marci@358
    76
marci@358
    77
  return 0;
marci@358
    78
}