contract#
Contracting an arc in a planar diagram.
This module provides a single operation that contracts a chosen arc by merging its two endpoints into one vertex. The two endpoints must be given as an ordered pair: the first endpoint is kept, and the second endpoint’s vertex is removed (its other incident endpoints are pulled into the kept vertex).
Notes
Only (unoriented) vertex–vertex arcs are supported.
The operation can be performed in-place or on a copy.
Example
>>> from knotpy.classes.planardiagram import PlanarDiagram
>>> from knotpy.algorithms.contract import contract_arc
>>> k = PlanarDiagram()
>>> _ = k.set_arcs_from("x0a0,x1b0,x2c0,x4d0,x3y2,y0e0,y1f0,y3g0,y4h0")
>>> # contract arc (keep ('y',2), remove ('x',3))
>>> k2 = contract_arc(k, (('y', 2), ('x', 3)), inplace=False)
Functions
|
Contract a specific arc by merging its endpoints into a single vertex. |
- contract_arc(k, arc_for_contracting, inplace=True)#
Contract a specific arc by merging its endpoints into a single vertex.
The arc is specified as an ordered pair of endpoints: the first endpoint is kept (its vertex remains), and the second endpoint’s vertex is removed. All other incident endpoints of the removed vertex are reattached to the kept vertex, preserving cyclic order via
pull_and_plug_endpoint
.This operation is only defined for vertex–vertex arcs (both endpoints must lie on vertices). Loops (both endpoints on the same vertex) are not contractible.
- Parameters:
k (PlanarDiagram) – The planar diagram to modify.
arc_for_contracting (tuple[Endpoint | tuple, Endpoint | tuple]) – Ordered pair
(keep_endpoint, remove_endpoint)
where each endpoint is either anEndpoint
or a(node, position)
tuple.inplace (bool) – If
True
(default), mutatek
. IfFalse
, return a modified copy.
- Returns:
The diagram with the arc contracted (
k
itself ifinplace=True
).- Raises:
TypeError – If either endpoint is not on a vertex.
ValueError – If the arc is a loop (both endpoints on the same vertex).
- Return type: