MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / graph_Gnm

Function graph_Gnm

easygraph/random/hypergraph.py:9–43  ·  view source on GitHub ↗

r"""Return a random graph with ``num_v`` verteices and ``num_e`` edges. Edges are drawn uniformly from the set of possible edges. Args: ``num_v`` (``int``): The Number of vertices. ``num_e`` (``int``): The Number of edges. Examples: >>> import easygraph.random as ra

(num_v: int, num_e: int)

Source from the content-addressed store, hash-verified

7
8
9def graph_Gnm(num_v: int, num_e: int):
10 r"""Return a random graph with ``num_v`` verteices and ``num_e`` edges. Edges are drawn uniformly from the set of possible edges.
11
12 Args:
13 ``num_v`` (``int``): The Number of vertices.
14 ``num_e`` (``int``): The Number of edges.
15
16 Examples:
17 >>> import easygraph.random as random
18 >>> g = random.graph_Gnm(4, 5)
19 >>> g.e
20 ([(1, 2), (0, 3), (2, 3), (0, 2), (1, 3)], [1.0, 1.0, 1.0, 1.0, 1.0])
21 """
22 assert num_v > 1, "num_v must be greater than 1"
23 assert (
24 num_e < num_v * (num_v - 1) // 2
25 ), "the specified num_e is larger than the possible number of edges"
26
27 v_list = list(range(num_v))
28 cur_num_e, e_set = 0, set()
29 while cur_num_e < num_e:
30 v = random.choice(v_list)
31 w = random.choice(v_list)
32 if v > w:
33 v, w = w, v
34 if v == w or (v, w) in e_set:
35 continue
36 e_set.add((v, w))
37 cur_num_e += 1
38 g = Graph()
39 g.add_nodes(list(range(0, num_v)))
40 for ee in list(e_set):
41 g.add_edge(ee[0], ee[1], weight=1.0)
42
43 return g

Callers

nothing calls this directly

Calls 4

add_nodesMethod · 0.95
add_edgeMethod · 0.95
GraphClass · 0.90
addMethod · 0.80

Tested by

no test coverage detected