src/work/marci/bfsit_vs_byhand.cc
author jacint
Thu, 13 May 2004 10:29:13 +0000
changeset 629 6620dfc606af
parent 577 e8703f0a6e2f
child 640 d426dca0aaf7
permissions -rw-r--r--
max_flow interface changes
marci@358
     1
// -*- c++ -*-
marci@358
     2
#include <iostream>
marci@358
     3
#include <fstream>
marci@358
     4
marci@358
     5
#include <list_graph.h>
marci@389
     6
//#include <smart_graph.h>
marci@555
     7
#include <hugo/dimacs.h>
marci@555
     8
#include <hugo/time_measure.h>
marci@358
     9
#include <for_each_macros.h>
marci@602
    10
#include <bfs_dfs.h>
marci@358
    11
marci@358
    12
using namespace hugo;
marci@358
    13
marci@358
    14
int main() {
marci@358
    15
  typedef ListGraph Graph; 
marci@358
    16
  typedef Graph::Node Node;
marci@358
    17
  typedef Graph::NodeIt NodeIt;
marci@358
    18
  typedef Graph::Edge Edge;
marci@358
    19
  typedef Graph::EdgeIt EdgeIt;
marci@358
    20
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@358
    21
marci@577
    22
  Graph g;
marci@358
    23
  Node s, t;
marci@577
    24
  Graph::EdgeMap<int> cap(g);
marci@577
    25
  //readDimacsMaxFlow(std::cin, g, s, t, cap);
marci@577
    26
  readDimacs(std::cin, g);
marci@577
    27
marci@577
    28
  Graph::NodeMap<OutEdgeIt> pred(g);
marci@358
    29
  Timer ts;
marci@358
    30
  {
marci@358
    31
    ts.reset();
marci@577
    32
    Graph::NodeMap<bool> reached(g);
marci@358
    33
    reached.set(s, true);
marci@358
    34
    pred.set(s, INVALID);
marci@358
    35
    std::queue<Node> bfs_queue;
marci@358
    36
    bfs_queue.push(t);
marci@358
    37
    while (!bfs_queue.empty()) {
marci@358
    38
      Node v=bfs_queue.front();	
marci@358
    39
      bfs_queue.pop();
marci@358
    40
      OutEdgeIt e;
marci@577
    41
      for(g.first(e,v); g.valid(e); g.next(e)) {
marci@577
    42
	Node w=g.head(e);
marci@358
    43
	if (!reached[w]) {
marci@358
    44
	  bfs_queue.push(w);
marci@358
    45
	  reached.set(w, true);
marci@358
    46
	  pred.set(w, e);
marci@358
    47
	}
marci@358
    48
      }
marci@358
    49
    }
marci@358
    50
marci@358
    51
    std::cout << ts << std::endl;
marci@358
    52
  }
marci@358
    53
marci@358
    54
  {
marci@358
    55
    ts.reset();      
marci@577
    56
    BfsIterator< Graph, Graph::NodeMap<bool> > bfs(g);
marci@358
    57
    bfs.pushAndSetReached(s);
marci@358
    58
    pred.set(s, INVALID);
marci@358
    59
    while (!bfs.finished()) { 
marci@358
    60
      ++bfs; 
marci@577
    61
      if (g.valid(bfs) && bfs.isBNodeNewlyReached()) 
marci@358
    62
	pred.set(bfs.bNode(), bfs);
marci@358
    63
    }
marci@358
    64
    std::cout << ts << std::endl;
marci@358
    65
  }
marci@358
    66
marci@358
    67
  return 0;
marci@358
    68
}