dhg.random

Random Seed

dhg.random.seed()[source]

Return current random seed of DHG. Defaultly, the random seed is synchronized with the value of int(time.time()).

dhg.random.set_seed(seed)[source]

Set the random seed of DHG.

Note

When you call this function, the random seeds of random, numpy, and pytorch will be set, simultaneously.

Parameters

seed (int) – The specified random seed.

Generating Features

dhg.random.normal_features(labels, noise=1.0)[source]

Generate random features that are satisfying the normal distribution.

Parameters
  • labels (Union[list, np.ndarray, torch.Tensor]) – The label list.

  • noise (float, optional) – The noise of the normal distribution. Defaults to 1.0.

Examples

>>> import dhg
>>> label = [1, 3, 5, 2, 1, 5]
>>> dhg.random.normal_features(label)
tensor([[ 0.3204, -0.3059, -0.3103, -0.6558],
        [-1.0128,  0.0846,  0.4317, -0.1427],
        [ 0.0776, -0.6265, -0.7592, -0.5559],
        [ 0.8282, -0.5076, -1.1508,  0.6998],
        [ 0.4600, -0.8477,  0.8881,  0.7426],
        [-0.4456,  0.8452, -1.2390,  2.3204]])

Generating Graph

dhg.random.graph_Gnp(num_v, prob)[source]

Return a random graph with num_v vertices and probability prob of choosing an edge.

Parameters
  • num_v (int) – The Number of vertices.

  • prob (float) – Probability of choosing an edge.

Examples

>>> import dhg.random as random
>>> g = random.graph_Gnp(4, 0.5)
>>> g.e
([(0, 1), (0, 2), (0, 3)], [1.0, 1.0, 1.0])
dhg.random.graph_Gnp_fast(num_v, prob)[source]

Return a random graph with num_v vertices and probability prob of choosing an edge. This function is an implementation of Efficient generation of large random networks paper.

Parameters
  • num_v (int) – The Number of vertices.

  • prob (float) – Probability of choosing an edge.

Examples

>>> import dhg.random as random
>>> g = random.graph_Gnp_fast(4, 0.8)
>>> g.e
([(0, 1), (0, 2), (1, 2), (0, 3), (1, 3), (2, 3)], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
dhg.random.graph_Gnm(num_v, num_e)[source]

Return a random graph with num_v verteices and num_e edges. Edges are drawn uniformly from the set of possible edges.

Parameters
  • num_v (int) – The Number of vertices.

  • num_e (int) – The Number of edges.

Examples

>>> import dhg.random as random
>>> g = random.graph_Gnm(4, 5)
>>> g.e
([(1, 2), (0, 3), (2, 3), (0, 2), (1, 3)], [1.0, 1.0, 1.0, 1.0, 1.0])

Generating Directed Graph

dhg.random.digraph_Gnp(num_v, prob)[source]

Return a random directed graph with num_v vertices and probability prob of choosing an edge.

Parameters
  • num_v (int) – The Number of vertices.

  • prob (float) – Probability of choosing an edge.

Examples

>>> import dhg.random as random
>>> g = random.digraph_Gnp(4, 0.5)
>>> g.e
([(0, 1), (0, 2), (1, 2), (2, 1), (3, 0)], [1.0, 1.0, 1.0, 1.0, 1.0])
dhg.random.digraph_Gnp_fast(num_v, prob)[source]

Return a random directed graph with num_v vertices and probability prob of choosing an edge. This function is an implementation of Efficient generation of large random networks paper.

Parameters
  • num_v (int) – The Number of vertices.

  • prob (float) – Probability of choosing an edge.

Examples

>>> import dhg.random as random
>>> g = random.digraph_Gnp_fast(4, 0.6)
>>> g.e
([(0, 1), (0, 3), (1, 3), (2, 3), (1, 0), (2, 1)], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
dhg.random.digraph_Gnm(num_v, num_e)[source]

Return a random directed graph with num_v verteices and num_e edges. Edges are drawn uniformly from the set of possible edges.

Parameters
  • num_v (int) – The Number of vertices.

  • num_e (int) – The Number of edges.

Examples

>>> import dhg.random as random
>>> g = random.digraph_Gnm(4, 6)
>>> g.e
([(1, 2), (2, 1), (0, 3), (2, 0), (2, 3), (0, 2)], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0])

Generating Bipartite Graph

dhg.random.bigraph_Gnp(num_u, num_v, prob)[source]

Return a random bipartite graph with num_u vertices in set \(\mathcal{U}\) and num_v vertices in set \(\mathcal{V}\) and probability prob of choosing an edge.

Parameters
  • num_u (int) – The Number of vertices in set \(\mathcal{U}\).

  • num_v (int) – The Number of vertices in set \(\mathcal{V}\).

  • prob (float) – Probability of choosing an edge.

Examples

>>> import dhg.random as random
>>> g = random.bigraph_Gnp(2, 3, 0.6)
>>> g.e
([(0, 1), (1, 0), (1, 2)], [1.0, 1.0, 1.0])
dhg.random.bigraph_Gnm(num_u, num_v, num_e)[source]

Return a random bipartite graph with num_u vertices in set \(\mathcal{U}\) and num_v vertices in set \(\mathcal{V}\) and num_e edges. Edges are drawn uniformly from the set of possible edges.

Parameters
  • num_u (int) – The Number of vertices in set \(\mathcal{U}\).

  • num_v (int) – The Number of vertices in set \(\mathcal{V}\).

  • num_e (int) – The Number of edges.

Examples

>>> import dhg.random as random
>>> g = random.bigraph_Gnm(3, 3, 5)
>>> g.e
([(1, 2), (2, 1), (1, 1), (2, 0), (1, 0)], [1.0, 1.0, 1.0, 1.0, 1.0])

Generating Hypergraph

dhg.random.uniform_hypergraph_Gnp(k, num_v, prob)[source]

Return a random k-uniform hypergraph with num_v vertices and probability prob of choosing a hyperedge.

Parameters
  • num_v (int) – The Number of vertices.

  • k (int) – The Number of vertices in each hyperedge.

  • prob (float) – Probability of choosing a hyperedge.

Examples

>>> import dhg.random as random
>>> hg = random.uniform_hypergraph_Gnp(3, 5, 0.5)
>>> hg.e
([(0, 1, 3), (0, 1, 4), (0, 2, 4), (1, 3, 4), (2, 3, 4)], [1.0, 1.0, 1.0, 1.0, 1.0])
dhg.random.uniform_hypergraph_Gnm(k, num_v, num_e)[source]

Return a random k-uniform hypergraph with num_v vertices and num_e hyperedges.

Parameters
  • k (int) – The Number of vertices in each hyperedge.

  • num_v (int) – The Number of vertices.

  • num_e (int) – The Number of hyperedges.

Examples

>>> import dhg.random as random
>>> hg = random.uniform_hypergraph_Gnm(3, 5, 4)
>>> hg.e
([(0, 1, 2), (0, 1, 3), (0, 3, 4), (2, 3, 4)], [1.0, 1.0, 1.0, 1.0])
dhg.random.hypergraph_Gnm(num_v, num_e, method='low_order_first', prob_k_list=None)[source]

Return a random hypergraph with num_v vertices and num_e hyperedges. The method argument determines the distribution of the hyperedge degree. The method can be one of "uniform", "low_order_first", "high_order_first".

  • If set to "uniform", the number of hyperedges with the same degree will approximately to the capacity of each hyperedge degree. For example, the num_v is \(10\). The capacity of hyperedges with degree \(2\) is \(C^2_{10} = 45\).

  • If set to "low_order_first", the generated hyperedges will tend to have low degrees.

  • If set to "high_order_first", the generated hyperedges will tend to have high degrees.

Parameters
  • num_v (int) – The Number of vertices.

  • num_e (int) – The Number of hyperedges.

  • method (str) – The method to generate hyperedges must be one of "uniform", "low_order_first", "high_order_first", "custom". Defaults to "uniform".

Examples

>>> import dhg.random as random
>>> hg = random.hypergraph_Gnm(5, 4)
>>> hg.e
([(0, 1, 3, 4), (0, 2, 3, 4), (0, 2, 3), (0, 2, 4)], [1.0, 1.0, 1.0, 1.0])
>>> hg = dhg.random.hypergraph_Gnm(5, 4, 'custom', [0, 0, 0.8, 0.2])
>>> hg.e
([(1, 2, 3, 4), (0, 2, 3, 4), (0, 1, 2, 3), (0, 1, 2, 3, 4)], [1.0, 1.0, 1.0, 1.0])