Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #5012 implementation of newman watts strogatz graph inconsistent with model #5015

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/developer/about_us.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ to add your name to the bottom of the list.
- James Trimble, Github: `jamestrimble <https://github.com/jamestrimble>`_
- Matthias Bruhns, Github `<https://github.com/mbruhns>`_
- Philip Boalch
- Mingxuan He, Github `mingxuan-he <https://github.com/mingxuan-he>`_

A supplementary (but still incomplete) list of contributors is given by the
list of names that have commits in ``networkx``'s
Expand Down
21 changes: 8 additions & 13 deletions networkx/generators/random_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ def newman_watts_strogatz_graph(n, k, p, seed=None):
each edge $(u, v)$ in the underlying "$n$-ring with $k$ nearest
neighbors" with probability $p$ add a new edge $(u, w)$ with
randomly-chosen existing node $w$. In contrast with
:func:`watts_strogatz_graph`, no edges are removed.
:func:`watts_strogatz_graph`, no edges are removed, and multi-edges and
self-loops are allowed.

See Also
--------
Expand All @@ -325,9 +326,9 @@ def newman_watts_strogatz_graph(n, k, p, seed=None):
References
----------
.. [1] M. E. J. Newman and D. J. Watts,
Renormalization group analysis of the small-world network model,
Physics Letters A, 263, 341, 1999.
https://doi.org/10.1016/S0375-9601(99)00757-4
Scaling and percolation in the small-world network model,
Phys. Rev. E60(6), 7332-7342, 1999.
https://doi.org/10.1103/PhysRevE.60.7332
"""
if k > n:
raise nx.NetworkXError("k>=n, choose smaller k or larger n")
Expand All @@ -336,7 +337,7 @@ def newman_watts_strogatz_graph(n, k, p, seed=None):
if k == n:
return nx.complete_graph(n)

G = empty_graph(n)
G = empty_graph(n, create_using=nx.MultiGraph)
nlist = list(G.nodes())
fromv = nlist
# connect the k/2 neighbors
Expand All @@ -350,14 +351,8 @@ def newman_watts_strogatz_graph(n, k, p, seed=None):
for (u, v) in e:
if seed.random() < p:
w = seed.choice(nlist)
# no self-loops and reject if edge u-w exists
# is that the correct NWS model?
while w == u or G.has_edge(u, w):
w = seed.choice(nlist)
if G.degree(u) >= n - 1:
break # skip this rewiring
else:
G.add_edge(u, w)
# self-loops and multi-edges are allowed
G.add_edge(u, w)
return G


Expand Down
4 changes: 4 additions & 0 deletions networkx/generators/tests/test_random_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def test_random_graph(self):
assert len(G) == 10
assert G.number_of_edges() >= 20

G = nx.newman_watts_strogatz_graph(10, 2, 1.0, seed)
assert len(G) == 10
assert G.number_of_edges() == 20

G = nx.barabasi_albert_graph(100, 1, seed)
G = nx.barabasi_albert_graph(100, 3, seed)
assert G.number_of_edges() == (97 * 3)
Expand Down