Skip to content

Commit 74a87c7

Browse files
petercorkeclaude
andcommitted
refactor: vertex kinds supply _detach, the twin of _attach; removed vertices leave the graph
remove_edge knew how each vertex kind held an edge (a per-endpoint 'is it in this list?' check written for the directed case). That is now the vertex kind's job: UVertex._detach removes the edge from both ends (twice for a self-loop) and DVertex._detach from the source. remove_edge calls it once, before clearing the edge's endpoints. Behaviour is unchanged. Also fixes remove_vertex leaving vertex._graph pointing at the graph it had been removed from, which made a later connect() report 'different graphs' rather than 'not in a graph', and made vertex.remove() fail confusingly. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 0d9604d commit 74a87c7

1 file changed

Lines changed: 52 additions & 13 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,9 @@ def remove_edge(self, edge: Edge) -> None:
612612
613613
.. warning:: The connectivity of the network may be changed.
614614
615-
.. note:: A directed edge is tracked only by its source vertex's
616-
edge list, not its target's (see :attr:`BaseVertex.edges`), so
617-
membership is checked per endpoint rather than assumed for both
618-
-- removing a directed edge from an undirected-only
619-
implementation would otherwise raise ``ValueError`` on the
620-
target side.
615+
.. note:: An undirected edge is held by both of its vertices and a
616+
directed edge only by its source (see :meth:`BaseVertex.edges`).
617+
Each vertex kind knows which, so this method does not.
621618
622619
.. runblock:: pycon
623620
@@ -633,15 +630,16 @@ def remove_edge(self, edge: Edge) -> None:
633630
"""
634631
if edge not in self._edgelist:
635632
raise ValueError("edge does not belong to this graph")
636-
assert edge.v1 is not None and edge.v2 is not None
633+
v1, v2 = edge.v1, edge.v2
634+
if v1 is None or v2 is None:
635+
raise ValueError("edge does not connect two vertices")
637636

638-
if edge in edge.v1._edgelist:
639-
edge.v1._edgelist.remove(edge)
640-
if edge in edge.v2._edgelist:
641-
edge.v2._edgelist.remove(edge)
637+
# how the edge is held differs between undirected and directed
638+
# vertices, so the vertex kind undoes what its _attach did
639+
v1._detach(edge, v2)
642640

643-
edge.v1._connectivitychange = True
644-
edge.v2._connectivitychange = True
641+
v1._connectivitychange = True
642+
v2._connectivitychange = True
645643
self._connectivitychange = True
646644

647645
edge.v1 = None
@@ -688,6 +686,8 @@ def remove_vertex(self, vertex: BaseVertex) -> None:
688686

689687
self._vertexlist.remove(vertex)
690688
del self._vertexdict[vertex.name]
689+
vertex._graph = None # it is not in any graph now
690+
self._connectivitychange = True
691691

692692
def remove(self, x: Edge | BaseVertex) -> None:
693693
"""
@@ -3236,6 +3236,25 @@ def _attach(self, edge: Edge, dest: BaseVertex) -> None:
32363236
"subclass UVertex or DVertex"
32373237
)
32383238

3239+
def _detach(self, edge: Edge, dest: BaseVertex) -> None:
3240+
"""
3241+
Remove an edge from the vertices' edge lists (private method)
3242+
3243+
:param edge: the edge being removed, with ``edge.v1`` and ``edge.v2``
3244+
still set
3245+
:param dest: the vertex at the other end, ``edge.v2``
3246+
:raises NotImplementedError: not provided by this vertex kind
3247+
3248+
The twin of :meth:`_attach`: it undoes exactly what that recorded.
3249+
Called by :meth:`_BaseGraph.remove_edge` before the edge's endpoints
3250+
are cleared. An edge that is absent from a list is skipped rather
3251+
than treated as an error.
3252+
"""
3253+
raise NotImplementedError(
3254+
f"{type(self).__name__} does not define how an edge is detached; "
3255+
"subclass UVertex or DVertex"
3256+
)
3257+
32393258
def _endpoints_match(self, edge: Edge, dest: BaseVertex) -> bool:
32403259
"""
32413260
Test if an edge already connects this vertex to ``dest`` (private method)
@@ -3571,6 +3590,17 @@ def _attach(self, edge: Edge, dest: BaseVertex) -> None:
35713590
self._edgelist.append(edge)
35723591
dest._edgelist.append(edge)
35733592

3593+
def _detach(self, edge: Edge, dest: BaseVertex) -> None:
3594+
"""
3595+
Remove an undirected edge from both ends (private method)
3596+
3597+
A self-loop was recorded twice by :meth:`_attach`, so it is removed
3598+
twice.
3599+
"""
3600+
for vertex in (self, dest):
3601+
if edge in vertex._edgelist:
3602+
vertex._edgelist.remove(edge)
3603+
35743604
def _endpoints_match(self, edge: Edge, dest: BaseVertex) -> bool:
35753605
"""
35763606
Test if an edge already connects this vertex and ``dest`` (private method)
@@ -3616,6 +3646,15 @@ def _attach(self, edge: Edge, dest: BaseVertex) -> None:
36163646
"""
36173647
self._edgelist.append(edge)
36183648

3649+
def _detach(self, edge: Edge, dest: BaseVertex) -> None:
3650+
"""
3651+
Remove a directed edge from its source (private method)
3652+
3653+
``dest`` never held it.
3654+
"""
3655+
if edge in self._edgelist:
3656+
self._edgelist.remove(edge)
3657+
36193658

36203659
# UGraph/DGraph declare their _vertex_cls here, rather than in their own
36213660
# class body, because UVertex/DVertex are defined later in this file --

0 commit comments

Comments
 (0)