Generate an unweighted Erdős-Rényi random graph [*]_. References ---------- .. [*] Erdős, P. and Rényi, A. (1959). On Random Graphs, *Publ. Math. 6*, 290. Parameters ---------- n_vertices : int The number of vertices in the graph. edge_prob : float in [0, 1
(n_vertices, edge_prob=0.5, directed=False)
| 300 | |
| 301 | |
| 302 | def random_unweighted_graph(n_vertices, edge_prob=0.5, directed=False): |
| 303 | """ |
| 304 | Generate an unweighted Erdős-Rényi random graph [*]_. |
| 305 | |
| 306 | References |
| 307 | ---------- |
| 308 | .. [*] Erdős, P. and Rényi, A. (1959). On Random Graphs, *Publ. Math. 6*, 290. |
| 309 | |
| 310 | Parameters |
| 311 | ---------- |
| 312 | n_vertices : int |
| 313 | The number of vertices in the graph. |
| 314 | edge_prob : float in [0, 1] |
| 315 | The probability of forming an edge between two vertices. Default is |
| 316 | 0.5. |
| 317 | directed : bool |
| 318 | Whether the edges in the graph should be directed. Default is False. |
| 319 | |
| 320 | Returns |
| 321 | ------- |
| 322 | G : :class:`Graph` instance |
| 323 | The resulting random graph. |
| 324 | """ |
| 325 | vertices = list(range(n_vertices)) |
| 326 | candidates = permutations(vertices, 2) if directed else combinations(vertices, 2) |
| 327 | |
| 328 | edges = [] |
| 329 | for (fr, to) in candidates: |
| 330 | if np.random.rand() <= edge_prob: |
| 331 | edges.append(Edge(fr, to)) |
| 332 | |
| 333 | return DiGraph(vertices, edges) if directed else UndirectedGraph(vertices, edges) |
| 334 | |
| 335 | |
| 336 | def random_DAG(n_vertices, edge_prob=0.5): |