primitive random graph generator
authorjacint
Wed, 05 May 2004 17:23:04 +0000
changeset 53422ce98f7d0f1
parent 533 04eb0d9022c8
child 535 bd79aa43f299
primitive random graph generator
src/work/jacint/graph_gen.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/jacint/graph_gen.h	Wed May 05 17:23:04 2004 +0000
     1.3 @@ -0,0 +1,52 @@
     1.4 +// -*- c++ -*-
     1.5 +//randomGraph(i,j) gives a random graph on i nodes and j edges.
     1.6 +#include <vector>
     1.7 +#include <cstdlib>
     1.8 +
     1.9 +//#include <list_graph.h>
    1.10 +//#include <time_measure.h>
    1.11 +//#include <for_each_macros.h>
    1.12 +//#include <bfs_iterator.h>
    1.13 +//#include <bipartite_graph_wrapper.h>
    1.14 +//#include <maps.h>
    1.15 +//#include <max_flow.h>
    1.16 +
    1.17 +namespace hugo {
    1.18 +
    1.19 +
    1.20 +  /**
    1.21 +   * Inicializalja a veletlenszamgeneratort.
    1.22 +   * Figyelem, ez nem jo igazi random szamokhoz,
    1.23 +   * erre ne bizzad a titkaidat!
    1.24 +   */
    1.25 +  void random_init()
    1.26 +  {
    1.27 +    unsigned int seed = getpid();
    1.28 +    seed |= seed << 15;
    1.29 +    seed ^= time(0);
    1.30 +
    1.31 +    srand(seed);
    1.32 +  }
    1.33 +
    1.34 +
    1.35 +  /**
    1.36 +   * Egy veletlen int-et ad vissza 0 es m-1 kozott.
    1.37 +   */
    1.38 +  int random(int m)
    1.39 +  {
    1.40 +    return int( double(m) * rand() / (RAND_MAX + 1.0) );
    1.41 +  }
    1.42 +
    1.43 +
    1.44 +  template<typename Graph>
    1.45 +  void randomGraph (Graph& g, int n, int m) {
    1.46 +    typedef typename Graph::Node Node;
    1.47 +    g.clear();
    1.48 +    std::vector<Node> nodes;
    1.49 +    for (int i=0; i<n; ++i)
    1.50 +      nodes.push_back(g.addNode());
    1.51 +    for (int i=0; i<m; ++i) 
    1.52 +      g.addEdge(nodes[random(n)], nodes[random(n)]);
    1.53 +  }
    1.54 +
    1.55 +}