| 163 | @patch_docstring(nxa.gnm_random_graph) |
| 164 | @py_random_state(2) |
| 165 | def gnm_random_graph(n, m, seed=None, directed=False): |
| 166 | if directed: |
| 167 | G = nx.DiGraph() |
| 168 | else: |
| 169 | G = nx.Graph() |
| 170 | G.add_nodes_from(range(n)) |
| 171 | |
| 172 | if n == 1: |
| 173 | return G |
| 174 | max_edges = n * (n - 1) |
| 175 | if not directed: |
| 176 | max_edges /= 2.0 |
| 177 | if m >= max_edges: |
| 178 | return complete_graph(n, create_using=G) |
| 179 | |
| 180 | nlist = list(G) |
| 181 | edge_count = 0 |
| 182 | while edge_count < m: |
| 183 | # generate random edge,u,v |
| 184 | u = seed.choice(nlist) |
| 185 | v = seed.choice(nlist) |
| 186 | if u == v: |
| 187 | continue |
| 188 | else: |
| 189 | G.add_edge(u, v) |
| 190 | edge_count = edge_count + 1 |
| 191 | return G |
| 192 | |
| 193 | |
| 194 | @patch_docstring(nxa.newman_watts_strogatz_graph) |