Skip to content

Commit

Permalink
Update multigraph docstrings to reflect remove_edges_from behavior. (
Browse files Browse the repository at this point in the history
…networkx#5699)

* Update MG docstring to reflect rm_edges_from behavior.

Also adds example.

* Update remove_edge docstring in MG and MDG.

* Fix MDG examples.
  • Loading branch information
rossbar authored and MridulS committed Feb 4, 2023
1 parent 4c0fccc commit 1e23db2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
16 changes: 13 additions & 3 deletions networkx/classes/multidigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ def remove_edge(self, u, v, key=None):
Remove an edge between nodes u and v.
key : hashable identifier, optional (default=None)
Used to distinguish multiple edges between a pair of nodes.
If None remove a single (arbitrary) edge between u and v.
If None, remove a single edge between u and v. If there are
multiple edges, removes the last edge added in terms of
insertion order.
Raises
------
Expand All @@ -529,7 +531,13 @@ def remove_edge(self, u, v, key=None):
>>> G = nx.MultiDiGraph()
>>> G.add_edges_from([(1, 2), (1, 2), (1, 2)]) # key_list returned
[0, 1, 2]
>>> G.remove_edge(1, 2) # remove a single (arbitrary) edge
When ``key=None`` (the default), edges are removed in the opposite
order that they were added:
>>> G.remove_edge(1, 2)
>>> G.edges(keys=True)
OutMultiEdgeView([(1, 2, 0), (1, 2, 1)])
For edges with keys
Expand All @@ -538,7 +546,9 @@ def remove_edge(self, u, v, key=None):
'first'
>>> G.add_edge(1, 2, key="second")
'second'
>>> G.remove_edge(1, 2, key="second")
>>> G.remove_edge(1, 2, key="first")
>>> G.edges(keys=True)
OutMultiEdgeView([(1, 2, 'second')])
"""
try:
Expand Down
36 changes: 31 additions & 5 deletions networkx/classes/multigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,9 @@ def remove_edge(self, u, v, key=None):
Remove an edge between nodes u and v.
key : hashable identifier, optional (default=None)
Used to distinguish multiple edges between a pair of nodes.
If None remove a single (arbitrary) edge between u and v.
If None, remove a single edge between u and v. If there are
multiple edges, removes the last edge added in terms of
insertion order.
Raises
------
Expand All @@ -621,17 +623,27 @@ def remove_edge(self, u, v, key=None):
>>> G = nx.MultiGraph() # or MultiDiGraph, etc
>>> G.add_edges_from([(1, 2), (1, 2), (1, 2)]) # key_list returned
[0, 1, 2]
>>> G.remove_edge(1, 2) # remove a single (arbitrary) edge
When ``key=None`` (the default), edges are removed in the opposite
order that they were added:
>>> G.remove_edge(1, 2)
>>> G.edges(keys=True)
MultiEdgeView([(1, 2, 0), (1, 2, 1)])
>>> G.remove_edge(2, 1) # edges are not directed
>>> G.edges(keys=True)
MultiEdgeView([(1, 2, 0)])
For edges with keys
>>> G = nx.MultiGraph() # or MultiDiGraph, etc
>>> G = nx.MultiGraph()
>>> G.add_edge(1, 2, key="first")
'first'
>>> G.add_edge(1, 2, key="second")
'second'
>>> G.remove_edge(1, 2, key="second")
>>> G.remove_edge(1, 2, key="first")
>>> G.edges(keys=True)
MultiEdgeView([(1, 2, 'second')])
"""
try:
Expand Down Expand Up @@ -662,7 +674,7 @@ def remove_edges_from(self, ebunch):
Each edge given in the list or container will be removed
from the graph. The edges can be:
- 2-tuples (u, v) All edges between u and v are removed.
- 2-tuples (u, v) A single edge between u and v is removed.
- 3-tuples (u, v, key) The edge identified by key is removed.
- 4-tuples (u, v, key, data) where data is ignored.
Expand Down Expand Up @@ -690,6 +702,20 @@ def remove_edges_from(self, ebunch):
>>> G.remove_edges_from([(1, 2), (1, 2)]) # silently ignore extra copy
>>> list(G.edges) # now empty graph
[]
When the edge is a 2-tuple ``(u, v)`` but there are multiple edges between
u and v in the graph, the most recent edge (in terms of insertion
order) is removed.
>>> G = nx.MultiGraph()
>>> for key in ("x", "y", "a"):
... k = G.add_edge(0, 1, key=key)
>>> G.edges(keys=True)
MultiEdgeView([(0, 1, 'x'), (0, 1, 'y'), (0, 1, 'a')])
>>> G.remove_edges_from([(0, 1)])
>>> G.edges(keys=True)
MultiEdgeView([(0, 1, 'x'), (0, 1, 'y')])
"""
for e in ebunch:
try:
Expand Down

0 comments on commit 1e23db2

Please sign in to comment.