src/work/jacint/graph_gen.h
author marci
Fri, 07 May 2004 11:57:34 +0000
changeset 577 e8703f0a6e2f
parent 534 22ce98f7d0f1
child 593 b83b36ee7f10
permissions -rw-r--r--
top-sort, dimacs mods.
jacint@534
     1
// -*- c++ -*-
jacint@534
     2
#include <vector>
jacint@534
     3
#include <cstdlib>
jacint@534
     4
jacint@534
     5
namespace hugo {
jacint@534
     6
jacint@534
     7
jacint@534
     8
  /**
jacint@534
     9
   * Inicializalja a veletlenszamgeneratort.
jacint@534
    10
   * Figyelem, ez nem jo igazi random szamokhoz,
jacint@534
    11
   * erre ne bizzad a titkaidat!
jacint@534
    12
   */
jacint@534
    13
  void random_init()
jacint@534
    14
  {
jacint@534
    15
    unsigned int seed = getpid();
jacint@534
    16
    seed |= seed << 15;
jacint@534
    17
    seed ^= time(0);
jacint@534
    18
jacint@534
    19
    srand(seed);
jacint@534
    20
  }
jacint@534
    21
jacint@534
    22
jacint@534
    23
  /**
jacint@534
    24
   * Egy veletlen int-et ad vissza 0 es m-1 kozott.
jacint@534
    25
   */
jacint@534
    26
  int random(int m)
jacint@534
    27
  {
jacint@534
    28
    return int( double(m) * rand() / (RAND_MAX + 1.0) );
jacint@534
    29
  }
jacint@534
    30
jacint@534
    31
marci@558
    32
  /// Generates a random graph with n nodes and m edges.
marci@558
    33
  /// Before generating the random graph, \c g.clear() is called.
jacint@534
    34
  template<typename Graph>
marci@558
    35
  void randomGraph(Graph& g, int n, int m) {
jacint@534
    36
    g.clear();
marci@558
    37
    std::vector<typename Graph::Node> nodes;
jacint@534
    38
    for (int i=0; i<n; ++i)
jacint@534
    39
      nodes.push_back(g.addNode());
jacint@534
    40
    for (int i=0; i<m; ++i) 
jacint@534
    41
      g.addEdge(nodes[random(n)], nodes[random(n)]);
jacint@534
    42
  }
jacint@534
    43
marci@558
    44
  /// Generates a random bipartite graph with a and b nodes 
marci@558
    45
  /// in the color classes and m edges.
marci@558
    46
  /// According to the bipartite graph concept, the resulting 
marci@558
    47
  /// graph is directed from the first class to the second one.
marci@558
    48
  /// Before generating the random graph, \c g.clear() is called.
marci@558
    49
  template<typename Graph>
marci@558
    50
  void randomBipartiteGraph(Graph& g, int a, int b, int m) {
marci@558
    51
    g.clear();
marci@558
    52
    std::vector<typename Graph::Node> s_nodes;
marci@558
    53
    std::vector<typename Graph::Node> t_nodes;
marci@558
    54
    for (int i=0; i<a; ++i)
marci@558
    55
      ///\bug g.addNode(g.S_CLASS) would be better.
marci@558
    56
      s_nodes.push_back(g.addNode(false));
marci@558
    57
    for (int i=0; i<b; ++i)
marci@558
    58
      ///\bug g.addNode(g.T_CLASS) would be better.
marci@558
    59
      t_nodes.push_back(g.addNode(true));
marci@558
    60
    for (int i=0; i<m; ++i) 
marci@558
    61
      g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
marci@558
    62
  }
marci@558
    63
marci@558
    64
} //namespace hugo