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

Update multigraph docstrings to reflect remove_edges_from behavior. #5699

Merged
merged 3 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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