Removes the given Node (and all its edges) or Edge from the graph. Note: removing Edge a->b does not remove Edge b->a.
(self, x)
| 385 | return e2 |
| 386 | |
| 387 | def remove(self, x): |
| 388 | """ Removes the given Node (and all its edges) or Edge from the graph. |
| 389 | Note: removing Edge a->b does not remove Edge b->a. |
| 390 | """ |
| 391 | if isinstance(x, Node) and x.id in self: |
| 392 | self.pop(x.id) |
| 393 | self.nodes.remove(x); x.graph = None |
| 394 | # Remove all edges involving the given node. |
| 395 | for e in list(self.edges): |
| 396 | if x in (e.node1, e.node2): |
| 397 | if x in e.node1.links: e.node1.links.remove(x) |
| 398 | if x in e.node2.links: e.node2.links.remove(x) |
| 399 | self.edges.remove(e) |
| 400 | if isinstance(x, Edge): |
| 401 | self.edges.remove(x) |
| 402 | # Clear adjacency cache. |
| 403 | self._adjacency = None |
| 404 | |
| 405 | def node(self, id): |
| 406 | """ Returns the node in the graph with the given id. |