@@ -2761,8 +2761,9 @@ def connect(
27612761 :param data: reference to arbitrary data associated with the edge,
27622762 defaults to None
27632763 :type data: Any, optional
2764- :raises ValueError: either vertex has not been added to a graph, or
2765- the vertices belong to different graphs
2764+ :raises ValueError: either vertex has not been added to a graph, the
2765+ vertices belong to different graphs, or ``edge`` already connects
2766+ other vertices
27662767 :return: the edge connecting the vertices
27672768 :rtype: Edge
27682769
@@ -2772,6 +2773,9 @@ def connect(
27722773
27732774 - If the vertices subclass ``UVertex`` the edge is undirected, and if
27742775 they subclass ``DVertex`` the edge is directed.
2776+ - A supplied ``edge`` built without vertices has its ``v1`` and
2777+ ``v2`` set to the vertices being connected. One built with
2778+ vertices must agree with them (in either order if undirected).
27752779 - Both vertices must already have been added to the same graph,
27762780 e.g. via :meth:`PGraph.add_vertex` -- since a graph only ever
27772781 accepts its own vertex subclass (see :meth:`UGraph.add_vertex`,
@@ -2798,6 +2802,7 @@ def connect(
27982802 raise ValueError ("vertices must belong to the same graph" )
27992803 elif isinstance (edge , Edge ):
28002804 e = edge
2805+ self ._bind_edge (e , dest )
28012806 else :
28022807 e = Edge (self , dest , cost = cost , data = data )
28032808
@@ -2807,6 +2812,38 @@ def connect(
28072812
28082813 return e
28092814
2815+ def _endpoints_match (self , edge : Edge , dest : BaseVertex ) -> bool :
2816+ """
2817+ Test if an edge already connects this vertex to ``dest`` (private method)
2818+
2819+ A directed edge must run from this vertex to ``dest``; see
2820+ :meth:`UVertex._endpoints_match` for the undirected relaxation.
2821+ """
2822+ return edge .v1 is self and edge .v2 is dest
2823+
2824+ def _bind_edge (self , edge : Edge , dest : BaseVertex ) -> None :
2825+ """
2826+ Make a caller-supplied edge connect this vertex to ``dest`` (private method)
2827+
2828+ :param edge: edge passed to :meth:`connect`
2829+ :param dest: vertex being connected to
2830+ :raises ValueError: ``edge`` already connects other vertices
2831+
2832+ An edge built without vertices has its ``v1`` and ``v2`` set here.
2833+ One built with vertices is left alone provided they agree with the
2834+ connection being made; otherwise it is rejected before anything is
2835+ changed, rather than leaving the graph holding an edge whose
2836+ endpoints disagree with the vertices' edge lists.
2837+ """
2838+ if edge .v1 is None and edge .v2 is None :
2839+ edge .v1 = self
2840+ edge .v2 = dest
2841+ elif not self ._endpoints_match (edge , dest ):
2842+ raise ValueError (
2843+ f"edge already connects { edge .v1 } and { edge .v2 } , "
2844+ f"not { self } and { dest } "
2845+ )
2846+
28102847 def edgeto (self , dest : BaseVertex ) -> Edge :
28112848 """
28122849 Get edge connecting vertex to specific neighbour
@@ -3122,6 +3159,16 @@ def connect(
31223159
31233160 return e
31243161
3162+ def _endpoints_match (self , edge : Edge , dest : BaseVertex ) -> bool :
3163+ """
3164+ Test if an edge already connects this vertex and ``dest`` (private method)
3165+
3166+ An undirected edge has no direction, so either order is accepted.
3167+ """
3168+ return (edge .v1 is self and edge .v2 is dest ) or (
3169+ edge .v1 is dest and edge .v2 is self
3170+ )
3171+
31253172
31263173class DVertex (BaseVertex ):
31273174 """
0 commit comments