COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/jacint/graph_gen.h @ 539:fb261e3a9a0f

Last change on this file since 539:fb261e3a9a0f was 534:22ce98f7d0f1, checked in by jacint, 21 years ago

primitive random graph generator

File size: 1.0 KB
Line 
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
14namespace 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}
Note: See TracBrowser for help on using the repository browser.