benchmark/bfs-bench.cc
author alpar
Fri, 03 Feb 2006 09:18:17 +0000
changeset 1949 5db4ff8d69de
parent 1756 b1f441f24d08
child 1956 a055123339d5
permissions -rw-r--r--
Fight with Doxygen.
Victory hasn't been reached yet, but it's on the horizon.
alpar@742
     1
// -*- mode:C++ -*-
alpar@742
     2
alpar@1632
     3
#include<queue>
alpar@1632
     4
#include<cmath>
alpar@921
     5
#include<lemon/smart_graph.h>
alpar@742
     6
#include"bench_tools.h"
alpar@742
     7
alpar@742
     8
using namespace std;
alpar@921
     9
using namespace lemon;
alpar@742
    10
alpar@742
    11
inline int numOfOnes(int n,int dim)
alpar@742
    12
{
alpar@742
    13
  int s=0;
alpar@742
    14
  for(int i=0;i<dim;i++) {
alpar@742
    15
    s+=n%2;
alpar@742
    16
    n>>=1;
alpar@742
    17
  }
alpar@742
    18
  return s;
alpar@742
    19
}
alpar@742
    20
alpar@742
    21
inline int numOfZeros(int n,int dim)
alpar@742
    22
{
alpar@742
    23
  int s=dim;
alpar@742
    24
  for(int i=0;i<dim;i++) {
alpar@742
    25
    s-=n&1;
alpar@742
    26
    n>>=1;
alpar@742
    27
  }
alpar@742
    28
  return s;
alpar@742
    29
}
alpar@742
    30
alpar@742
    31
template<class Graph>
alpar@742
    32
void bfsStlQueue(Graph &G,typename Graph::Node source)
alpar@742
    33
{
alpar@1756
    34
  GRAPH_TYPEDEFS(typename Graph);
alpar@742
    35
alpar@742
    36
  using namespace std;
alpar@742
    37
  
alpar@751
    38
  typename Graph::template NodeMap<bool> visited(G,false);
alpar@742
    39
    
alpar@742
    40
  queue<typename Graph::Node> Q;
alpar@742
    41
    
alpar@742
    42
  Q.push(source);
alpar@742
    43
  visited[source]=true;
alpar@742
    44
  do {
alpar@742
    45
    Node n(Q.front());
alpar@742
    46
    Node m;
alpar@742
    47
    Q.pop();
alpar@774
    48
    for(OutEdgeIt e(G,n);e!=INVALID;++e)
alpar@986
    49
      if(!visited[m=G.target(e)]) {
alpar@742
    50
	Q.push(m);
alpar@742
    51
	visited.set(m,true);
alpar@742
    52
      }
alpar@742
    53
  } while(!Q.empty());
alpar@742
    54
}
alpar@742
    55
alpar@742
    56
template<class Graph>
alpar@742
    57
void bfsOwnQueue(Graph &G,typename Graph::Node source)
alpar@742
    58
{
alpar@1756
    59
  GRAPH_TYPEDEFS(typename Graph);
alpar@742
    60
alpar@742
    61
  using namespace std;
alpar@742
    62
  
alpar@751
    63
  typename Graph::template NodeMap<bool> visited(G,false);
alpar@742
    64
  
alpar@742
    65
  int N=G.nodeNum();
alpar@742
    66
  vector<typename Graph::Node> Q(N);
alpar@742
    67
  int Qh=0;
alpar@742
    68
  int Qt=0;
alpar@742
    69
  
alpar@742
    70
alpar@742
    71
  Q[Qh++]=source;
alpar@742
    72
  visited.set(source,true);
alpar@742
    73
  do {
alpar@742
    74
    Node m;
alpar@742
    75
    Node n=Q[Qt++];
alpar@774
    76
    for(OutEdgeIt e(G,n);e!=INVALID;++e)
alpar@986
    77
      if(!visited[m=G.target(e)]) {
alpar@742
    78
	Q[Qh++]=m;
alpar@742
    79
	visited.set(m,true);
alpar@742
    80
      }
alpar@742
    81
  } while(Qt!=Qh);
alpar@742
    82
}
alpar@742
    83
alpar@751
    84
template<class Graph>
alpar@751
    85
void iteratorBench(Graph &G)
alpar@751
    86
{
alpar@1756
    87
  GRAPH_TYPEDEFS(typename Graph);
alpar@751
    88
alpar@751
    89
  int i=0;
alpar@751
    90
  
alpar@774
    91
  for(NodeIt n(G);n!=INVALID;++n)
alpar@774
    92
    for(OutEdgeIt e(G,n);e!=INVALID;++e)
alpar@751
    93
      i++;
alpar@751
    94
}
alpar@751
    95
alpar@742
    96
int main(int argc, char *argv[])
alpar@742
    97
{
alpar@742
    98
  typedef SmartGraph Graph;
alpar@742
    99
alpar@1756
   100
  GRAPH_TYPEDEFS(Graph);
alpar@742
   101
alpar@742
   102
  Graph G;
alpar@742
   103
  
alpar@742
   104
  Timer T;
alpar@742
   105
  
alpar@751
   106
  if(argc!=3) {
alpar@751
   107
    cout << "Usage: " << argv[0] << " dim mul\n";
alpar@742
   108
    return 1;
alpar@742
   109
  }
alpar@742
   110
  
alpar@742
   111
  int dim=atoi(argv[1]);
alpar@751
   112
  int mul=atoi(argv[2]);
alpar@742
   113
  
alpar@742
   114
//   cout << "Creating Hipercube ("<< (1<<dim) << " nodes, "
alpar@742
   115
//        << dim*(1<<dim) << " edges):";
alpar@742
   116
alpar@1847
   117
  T.restart();
alpar@742
   118
  vector<Node> nodes;
alpar@1689
   119
  addBiDirHyperCube(G,dim,nodes);
alpar@742
   120
alpar@742
   121
  PrintTime("GENGRAPH",T);
alpar@742
   122
alpar@1847
   123
  T.restart();
alpar@742
   124
  {
alpar@751
   125
    for(int i=0;i<mul;i++)
alpar@742
   126
      bfsStlQueue(G,nodes[0]);
alpar@742
   127
  }
alpar@742
   128
  PrintTime("BFS-STL",T);
alpar@1847
   129
  T.restart();
alpar@742
   130
  {
alpar@751
   131
    for(int i=0;i<mul;i++)
alpar@742
   132
      bfsOwnQueue(G,nodes[0]);
alpar@742
   133
  }
alpar@742
   134
  PrintTime("BFS-OWN",T);
alpar@1847
   135
  T.restart();
alpar@751
   136
  {
alpar@751
   137
    for(int i=0;i<mul;i++)
alpar@751
   138
      iteratorBench(G);
alpar@751
   139
  }
alpar@751
   140
  PrintTime("ITERATE",T);
alpar@751
   141
alpar@751
   142
alpar@742
   143
}