src/benchmark/hcube.cc
author jacint
Tue, 20 Jul 2004 14:31:24 +0000
changeset 715 665689d86225
parent 708 429dfcbbf47d
child 718 75d36edc6bc4
permissions -rw-r--r--
trying if without stl stack we are faster
alpar@708
     1
// -*- mode:C++ -*-
alpar@708
     2
alpar@708
     3
#include<math.h>
alpar@708
     4
#include<hugo/list_graph.h>
alpar@708
     5
#include<hugo/smart_graph.h>
alpar@708
     6
#include<hugo/dijkstra.h>
alpar@708
     7
#include<hugo/time_measure.h>
alpar@708
     8
#include<iostream>
alpar@711
     9
//#include<../work/jacint/max_flow.h>
alpar@711
    10
#include"bench_tools.h"
alpar@708
    11
alpar@708
    12
using namespace std;
alpar@708
    13
using namespace hugo;
alpar@708
    14
alpar@708
    15
template<class Graph>
alpar@708
    16
void addHiperCube(Graph &G,int dim,vector<typename Graph::Node> &nodes)
alpar@708
    17
{
alpar@708
    18
  GRAPH_TYPEDEF_FACTORY(Graph);
alpar@708
    19
  
alpar@708
    20
  vector<int> bits(dim+1);
alpar@708
    21
  bits[0]=1;
alpar@708
    22
  for(int i=1;i<=dim;i++) bits[i]=2*bits[i-1];
alpar@708
    23
  
alpar@708
    24
  for(int i=0;i<bits[dim];i++) {
alpar@708
    25
    nodes.push_back(G.addNode());
alpar@708
    26
    for(j=0;j<dim;j++) if(i&bits[j]) G.addEdge(nodes[i-bits[j]],nodes[i]);
alpar@708
    27
  }
alpar@708
    28
}
alpar@708
    29
alpar@708
    30
template<class Graph>
alpar@708
    31
void addBiDirHiperCube(Graph &G,int dim,vector<typename Graph::Node> &nodes)
alpar@708
    32
{
alpar@708
    33
  GRAPH_TYPEDEF_FACTORY(Graph);
alpar@708
    34
  
alpar@708
    35
  vector<int> bits(dim+1);
alpar@708
    36
  bits[0]=1;
alpar@708
    37
  for(int i=1;i<=dim;i++) bits[i]=2*bits[i-1];
alpar@708
    38
  
alpar@708
    39
  for(int i=0;i<bits[dim];i++) {
alpar@708
    40
    nodes.push_back(G.addNode());
alpar@708
    41
    for(int j=0;j<dim;j++) if(i&bits[j]) {
alpar@708
    42
      G.addEdge(nodes[i-bits[j]],nodes[i]);
alpar@708
    43
      G.addEdge(nodes[i],nodes[i-bits[j]]);
alpar@708
    44
    }
alpar@708
    45
    
alpar@708
    46
  }
alpar@708
    47
}
alpar@708
    48
alpar@708
    49
int main(int argc, char *argv[])
alpar@708
    50
{
alpar@708
    51
  //  typedef ListGraph Graph;
alpar@708
    52
  typedef SmartGraph Graph;
alpar@708
    53
alpar@708
    54
  ///\bug GRAPH_TYPEDEF_FACTORY(Graph);
alpar@708
    55
  GRAPH_TYPEDEF_FACTORY_NOTYPENAME(Graph);
alpar@708
    56
alpar@708
    57
  Graph G;
alpar@708
    58
  
alpar@708
    59
  Timer T;
alpar@708
    60
  
alpar@708
    61
  if(argc!=2) {
alpar@708
    62
    cout << "Usage: " << argv[0] << " dim\n";
alpar@708
    63
    return 1;
alpar@708
    64
  }
alpar@708
    65
  
alpar@708
    66
  int dim=atoi(argv[1]);
alpar@708
    67
  
alpar@708
    68
  cout << "Creating Hipercube ("<< (1<<dim) << " nodes, "
alpar@708
    69
       << dim*(1<<dim) << " edges):";
alpar@708
    70
alpar@708
    71
  vector<Node> nodes;
alpar@708
    72
  addBiDirHiperCube(G,dim,nodes);
alpar@708
    73
  cout << T;
alpar@708
    74
  cout << "\nGenerating the lengths: ";
alpar@708
    75
  T.reset();
alpar@708
    76
  Graph::EdgeMap<int> map(G);
alpar@708
    77
  {
alpar@708
    78
    Primes P;
alpar@708
    79
    for(int i=0;i<dim*(1<<dim);i++) P();
alpar@708
    80
    
alpar@708
    81
    //  for(EdgeIt e(G);G.valid(e);G.next(e)) map[e]=P();
alpar@708
    82
    for(int i=0;i<dim*(1<<dim);i++)
alpar@708
    83
      //    map[Edge(((long long int)(i)*2987)%(dim*(1<<dim)))]=P();
alpar@708
    84
      map[Edge(((long long int)(i)*93505)%(dim*(1<<dim)))]=P();
alpar@708
    85
  }
alpar@708
    86
  
alpar@708
    87
  cout << T;
alpar@708
    88
  cout << "\nRunning Dijkstra: ";
alpar@708
    89
  T.reset();
alpar@708
    90
  {
alpar@708
    91
    Dijkstra<Graph> Dij(G,map);
alpar@708
    92
    Dij.run(nodes[0]);
alpar@708
    93
  }
alpar@708
    94
  cout << T;
alpar@708
    95
//   cout << "\nRunning MaxFlow: ";
alpar@708
    96
//   T.reset();
alpar@708
    97
//   {
alpar@708
    98
//    Graph::EdgeMap<int> flow(G);
alpar@708
    99
   
alpar@708
   100
//     MaxFlow<Graph,int> MF(G,nodes[0],nodes[1<<dim-1],map,flow);
alpar@708
   101
//     MF.run(MF.NO_FLOW);
alpar@708
   102
//   }
alpar@708
   103
//   cout << T;
alpar@708
   104
  cout << "\n";
alpar@708
   105
}