src/work/jacint/graph_gen.h
author athos
Mon, 10 May 2004 16:41:27 +0000
changeset 600 09148a2c5ed2
parent 593 b83b36ee7f10
child 605 b3c57602c516
permissions -rw-r--r--
(none)
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@593
    64
  /// Generates a complete graph in the undirected sense 
marci@598
    65
  /// with n nodes.
marci@593
    66
  /// Before generating the random graph, \c g.clear() is called.
marci@593
    67
  template<typename Graph>
marci@593
    68
  void completeGraph(Graph& g, int n) {
marci@593
    69
    g.clear();
marci@593
    70
    std::vector<typename Graph::Node> nodes;
marci@593
    71
    for (int i=0; i<n; ++i)
marci@593
    72
      nodes.push_back(g.addNode());
marci@593
    73
    for (int i=0; i<n; ++i) 
marci@593
    74
      for (int j=i+1; j<n; ++j)
marci@593
    75
	g.addEdge(nodes[i], nodes[j]);
marci@593
    76
  }
marci@598
    77
marci@598
    78
  /// Generates a complete bidirected graph on n nodes.
marci@598
    79
  /// Before generating the random graph, \c g.clear() is called.
marci@598
    80
  template<typename Graph>
marci@598
    81
  void completeBidirectedGraph(Graph& g, int n) {
marci@598
    82
    g.clear();
marci@598
    83
    std::vector<typename Graph::Node> nodes;
marci@598
    84
    for (int i=0; i<n; ++i)
marci@598
    85
      nodes.push_back(g.addNode());
marci@598
    86
    for (int i=0; i<n; ++i) 
marci@598
    87
      for (int j=i+1; j<n; ++j) {
marci@598
    88
	g.addEdge(nodes[i], nodes[j]);	
marci@598
    89
	g.addEdge(nodes[j], nodes[i]);
marci@598
    90
      }
marci@598
    91
  }
marci@598
    92
marci@598
    93
  /// Generates a complete bipartite graph with a and b nodes 
marci@598
    94
  /// in the color classes.
marci@598
    95
  /// Before generating the random graph, \c g.clear() is called.
marci@598
    96
  template<typename Graph>
marci@598
    97
  void completeBipartiteGraph(Graph& g, int a, int b) {
marci@598
    98
    g.clear();
marci@598
    99
    std::vector<typename Graph::Node> s_nodes;
marci@598
   100
    std::vector<typename Graph::Node> t_nodes;
marci@598
   101
    for (int i=0; i<a; ++i)
marci@598
   102
      ///\bug g.addNode(g.S_CLASS) would be better.
marci@598
   103
      s_nodes.push_back(g.addNode(false));
marci@598
   104
    for (int i=0; i<b; ++i)
marci@598
   105
      ///\bug g.addNode(g.T_CLASS) would be better.
marci@598
   106
      t_nodes.push_back(g.addNode(true));
marci@598
   107
    for (int i=0; i<a; ++i) 
marci@598
   108
      for (int j=0; j<b; ++j)       
marci@598
   109
	g.addEdge(s_nodes[i], t_nodes[j]);
marci@598
   110
  }
marci@593
   111
  
marci@558
   112
} //namespace hugo