src/work/jacint/graph_gen.h
author marci
Thu, 06 May 2004 15:39:31 +0000
changeset 553 8e5102790d4d
child 558 4cbfb435ec2b
permissions -rw-r--r--
SubGraphWrapper bug ok
     1 // -*- c++ -*-
     2 //randomGraph(i,j) gives a random graph on i nodes and j edges.
     3 #include <vector>
     4 #include <cstdlib>
     5 
     6 //#include <list_graph.h>
     7 //#include <time_measure.h>
     8 //#include <for_each_macros.h>
     9 //#include <bfs_iterator.h>
    10 //#include <bipartite_graph_wrapper.h>
    11 //#include <maps.h>
    12 //#include <max_flow.h>
    13 
    14 namespace hugo {
    15 
    16 
    17   /**
    18    * Inicializalja a veletlenszamgeneratort.
    19    * Figyelem, ez nem jo igazi random szamokhoz,
    20    * erre ne bizzad a titkaidat!
    21    */
    22   void random_init()
    23   {
    24     unsigned int seed = getpid();
    25     seed |= seed << 15;
    26     seed ^= time(0);
    27 
    28     srand(seed);
    29   }
    30 
    31 
    32   /**
    33    * Egy veletlen int-et ad vissza 0 es m-1 kozott.
    34    */
    35   int random(int m)
    36   {
    37     return int( double(m) * rand() / (RAND_MAX + 1.0) );
    38   }
    39 
    40 
    41   template<typename Graph>
    42   void randomGraph (Graph& g, int n, int m) {
    43     typedef typename Graph::Node Node;
    44     g.clear();
    45     std::vector<Node> nodes;
    46     for (int i=0; i<n; ++i)
    47       nodes.push_back(g.addNode());
    48     for (int i=0; i<m; ++i) 
    49       g.addEdge(nodes[random(n)], nodes[random(n)]);
    50   }
    51 
    52 }