src/benchmark/graph-bench.cc
author deba
Thu, 15 Jul 2004 12:15:58 +0000
changeset 703 32f280a5ed7d
child 708 429dfcbbf47d
permissions -rw-r--r--
(none)
alpar@699
     1
#include<math.h>
alpar@699
     2
#include<hugo/list_graph.h>
alpar@699
     3
#include<hugo/time_measure.h>
alpar@699
     4
#include<iostream>
alpar@699
     5
#include<sage_graph.h>
alpar@699
     6
alpar@699
     7
using namespace hugo;
alpar@699
     8
alpar@699
     9
///An experimental typedef factory
alpar@699
    10
#define GRAPH_TYPEDEF_FACTORY(Graph) \
alpar@699
    11
   typedef typename Graph::   Node      Node;\
alpar@699
    12
   typedef typename Graph::   NodeIt    NodeIn;\
alpar@699
    13
   typedef typename Graph::   Edge      Edge;\
alpar@699
    14
   typedef typename Graph::   EdgeIt    EdgeIt;\
alpar@699
    15
   typedef typename Graph:: InEdgeIt  InEdgeIt;\
alpar@699
    16
   typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@699
    17
 
alpar@699
    18
alpar@699
    19
///A primitive primtest
alpar@699
    20
bool isPrim(int n)
alpar@699
    21
{
alpar@699
    22
  if(n%2) {
alpar@699
    23
    for(int k=3;n/k>=k;k+=2)
alpar@699
    24
      if(!(n%k)) return false;
alpar@699
    25
    return true;
alpar@699
    26
  }
alpar@699
    27
  return false;
alpar@699
    28
}
alpar@699
    29
alpar@699
    30
///Finds the smallest prime not less then \c n.
alpar@699
    31
int nextPrim(int n)
alpar@699
    32
{
alpar@699
    33
  for(n+=!(n%2);!isPrim(n);n+=2) ;
alpar@699
    34
  return n;
alpar@699
    35
}
alpar@699
    36
alpar@699
    37
///Makes a full graph by adding and deleting a lot of edges;
alpar@699
    38
alpar@699
    39
///\param n Number of nodes.
alpar@699
    40
///\param rat The funcion will make \f$rat\timesn^2\f$ edge addition and
alpar@699
    41
///\f$(rat-1)\timesn^2\f$ deletion.
alpar@699
    42
///\param p Tuning parameters.
alpar@699
    43
///\warning \c rat, \c p, and \c n must be pairwise relative primes. 
alpar@699
    44
template <class Graph>
alpar@699
    45
void makeFullGraph(int n, int rat, int p)
alpar@699
    46
{
alpar@699
    47
  GRAPH_TYPEDEF_FACTORY(Graph);
alpar@699
    48
alpar@699
    49
  Graph G;
alpar@699
    50
  
alpar@699
    51
  Node nodes[n];
alpar@699
    52
  for(int i=0;i<n;i++) nodes[i]=G.addNode();
alpar@699
    53
  
alpar@699
    54
  Edge equ[rat];
alpar@699
    55
  
alpar@699
    56
  unsigned long long int count;
alpar@699
    57
  
alpar@699
    58
  for(count=0;count<rat;count++) {
alpar@699
    59
    equ[count%rat]=G.addEdge(nodes[(count*p)%n],nodes[(count*p/n)%n]);
alpar@699
    60
  }
alpar@699
    61
  for(;(count%rat)||((count*p)%n)||((count*p/n)%n);count++) {
alpar@699
    62
    //    if(!(count%1000000)) fprintf(stderr,"%d\r",count);
alpar@699
    63
    if(count%rat) G.erase(equ[count%rat]);
alpar@699
    64
    equ[count%rat]=G.addEdge(nodes[(count*p)%n],nodes[(count*p/n)%n]);
alpar@699
    65
  }
alpar@699
    66
  std::cout << "Added " << count
alpar@699
    67
	    << " ( " << n << "^2 * " << rat << " ) edges\n";
alpar@699
    68
  //  for(int i=0;1;i++) ;
alpar@699
    69
}
alpar@699
    70
alpar@699
    71
int main()
alpar@699
    72
{
alpar@699
    73
  std::cout << "START: n="  << nextPrim(1000) << " rat="
alpar@699
    74
	    << nextPrim(300) << " p=" << nextPrim(100) << '\n';
alpar@699
    75
  hugo::Timer T;
alpar@699
    76
  makeFullGraph<ListGraph>(nextPrim(1000),nextPrim(300),nextPrim(100));
alpar@699
    77
  std::cout << T  << '\n';
alpar@699
    78
  std::cout << "START: n="  << nextPrim(1000) << " rat="
alpar@699
    79
	    << nextPrim(300) << " p=" << nextPrim(100) << '\n';
alpar@699
    80
  T.reset();
alpar@699
    81
  makeFullGraph<ListGraph>(nextPrim(100),nextPrim(30000),nextPrim(150));
alpar@699
    82
  std::cout << T  << '\n';
alpar@699
    83
}