MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / remove_edge

Method remove_edge

easygraph/classes/multigraph.py:285–342  ·  view source on GitHub ↗

Remove an edge between u and v. Parameters ---------- u, v : nodes 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

(self, u, v, key=None)

Source from the content-addressed store, hash-verified

283 return keylist
284
285 def remove_edge(self, u, v, key=None):
286 """Remove an edge between u and v.
287
288 Parameters
289 ----------
290 u, v : nodes
291 Remove an edge between nodes u and v.
292 key : hashable identifier, optional (default=None)
293 Used to distinguish multiple edges between a pair of nodes.
294 If None remove a single (arbitrary) edge between u and v.
295
296 Raises
297 ------
298 EasyGraphError
299 If there is not an edge between u and v, or
300 if there is no edge with the specified key.
301
302 See Also
303 --------
304 remove_edges_from : remove a collection of edges
305
306 Examples
307 --------
308 For multiple edges
309
310 >>> G = eg.MultiGraph() # or MultiDiGraph, etc
311 >>> G.add_edges_from([(1, 2), (1, 2), (1, 2)]) # key_list returned
312 [0, 1, 2]
313 >>> G.remove_edge(1, 2) # remove a single (arbitrary) edge
314
315 For edges with keys
316
317 >>> G = eg.MultiGraph() # or MultiDiGraph, etc
318 >>> G.add_edge(1, 2, key="first")
319 'first'
320 >>> G.add_edge(1, 2, key="second")
321 'second'
322 >>> G.remove_edge(1, 2, key="second")
323
324 """
325 try:
326 d = self._adj[u][v]
327 except KeyError as err:
328 raise EasyGraphError(f"The edge {u}-{v} is not in the graph.") from err
329 # remove the edge with specified data
330 if key is None:
331 d.popitem()
332 else:
333 try:
334 del d[key]
335 except KeyError as err:
336 msg = f"The edge {u}-{v} with key {key} is not in the graph."
337 raise EasyGraphError(msg) from err
338 if len(d) == 0:
339 # remove the key entries if last edge
340 del self._adj[u][v]
341 if u != v: # check for selfloop
342 del self._adj[v][u]

Callers 1

remove_edges_fromMethod · 0.95

Calls 1

EasyGraphErrorClass · 0.90

Tested by

no test coverage detected