knit_graphs.artin_wale_braids.Loop_Braid_Graph module

Module containing the Loop Braid Graph class.

This module provides the Loop_Braid_Graph class which tracks crossing relationships between loops in cable knitting patterns using a directed graph structure.

class Loop_Braid_Graph[source]

Bases: Directed_Loop_Graph[LoopT, Crossing_Direction]

A graph structure that tracks crossing braid edges between loops in cable patterns.

This class maintains a directed graph where nodes are loops and edges represent cable crossings between those loops. It provides methods to add crossings, query crossing relationships, and determine which loops cross with a given loop.

__init__() None[source]

Initialize an empty loop braid graph with no crossings.

get_crossing(left_loop: LoopT, right_loop: LoopT) Crossing_Direction[source]
Parameters:
  • left_loop (Loop) – The loop on the left side of the crossing.

  • right_loop (Loop) – The loop on the right side of the crossing.

Returns:

The crossing direction between the left and right loop. Defaults to No_Cross if no explicit crossing was previously defined.

Return type:

Crossing_Direction

add_crossing(left_loop: LoopT, right_loop: LoopT, crossing_direction: Crossing_Direction) None[source]

Add a crossing edge between two loops with the specified crossing direction.

If the opposite crossing edge has already been defined, it will be removed from the graph so that only one crossing relationship is ever defined for each pair of the loops.

Parameters:
  • left_loop (Loop) – The loop on the left side of the crossing.

  • right_loop (Loop) – The loop on the right side of the crossing.

  • crossing_direction (Crossing_Direction) – The direction of the crossing (over, under, or none) between the loops.

left_crossing_loops(left_loop: LoopT) set[LoopT][source]

Get all loops that cross with the given loop when it is on the left side.

Parameters:

left_loop (Loop) – The loop on the left side of potential crossings.

Returns:

Set of loops that this loop crosses over on the right side. Empty set if the loop is not in the graph or has no crossings.

Return type:

set[Loop]

right_crossing_loops(right_loop: LoopT) set[LoopT][source]

Get all loops that cross with the given loop when it is on the right side.

Parameters:

right_loop (Loop) – The loop on the right side of potential crossings.

Returns:

List of loops that cross this loop from the left side. Empty list if the loop is not in the graph or has no crossings.

Return type:

list[Loop]

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

__contains__(item: LoopT | int | tuple[LoopT | int, LoopT | int]) bool
Parameters:

item (LoopT | int | tuple[LoopT | int, LoopT | int]) – The loop, loop-id, or a pair of loops in a directed edge to search for.

Returns:

True if the graph contains the given loop or edge.

Return type:

bool

__getitem__(item: int | tuple[LoopT | int, LoopT | int]) LoopT | EdgeT
Parameters:

item (int | tuple[LoopT | int, LoopT | int]) – The id of the loop or the pair of loops that form an edge in the graph.

Returns:

The loop associated with the given id. EdgeT: The edge data associated with the given pair of loops/ loop_ids.

Return type:

LoopT

Raises:

KeyError – The given loop or edge does not exist in the graph.

__iter__() Iterator[LoopT]
Returns:

Iterator over all the loops in the graph.

Return type:

Iterator[LoopT]

Notes

No guarantees about order of loops. Expected to be insertion order.

__len__() int
Returns:

The number of loops in the graph.

Return type:

int

add_edge(u: LoopT | int, v: LoopT | int, edge_data: EdgeT) None

Connect the given loop u to the loop v with the given edge data. :param u: The loop to connect to. :type u: LoopT | int :param v: The loop to connect to. :type v: LoopT | int :param edge_data: The edge data to associate with the connection. :type edge_data: EdgeT

Raises:

KeyError – Either of the given loops does not exist in the graph.

add_loop(loop: LoopT) None

Add the given loop to the loop graph. :param loop: The loop to add to the graph. :type loop: LoopT

ancestors(loop: LoopT) set[LoopT]
Parameters:

loop (LoopT) – The loop to get the ancestors of from the graph.

Returns:

The ancestors of the given loop.

Return type:

set[LoopT]

property contains_loops: bool

Returns: bool: True if the graph has at least one loop, False otherwise.

dfs_edges(loop: LoopT) Iterator[tuple[LoopT, LoopT]]
Parameters:

loop (LoopT) – The loop to start iteration over edges from.

Returns:

The depth-first-search ordering of edges starting from the given loop.

dfs_preorder_loops(loop: LoopT) Iterator[LoopT]
Parameters:

loop (LoopT) – The loop to start iteration from.

Returns:

The depth-first-search ordering of loops in the graph starting from the given loop.

Return type:

Iterator[LoopT]

property edge_count: int

Returns: int: The number of edges in the graph.

property edge_iter: Iterator[tuple[LoopT, LoopT, EdgeT]]
Returns:

Iterator over the edges and edge-data in the graph.

Return type:

Iterator[tuple[LoopT, LoopT, EdgeT]]

Notes

No guarantees about the order of the edges.

get_child_loop(loop: LoopT) LoopT | None
Parameters:

loop (LoopT) – The loop to look for a child loop from.

Returns:

The child loop if one exists, or None if no child loop is found.

Return type:

LoopT | None

get_edge(u: LoopT | int, v: LoopT | int) EdgeT
Parameters:
  • u (LoopT | int) – The loop or id of the first loop in the edge.

  • v (LoopT | int) – The loop or id of the second loop in the edge.

Returns:

The data about the edge from loop u to v.

Return type:

EdgeT

get_loop(loop_id: int) LoopT
Parameters:

loop_id (int) – The loop id of the loop to get from the graph.

Returns:

The loop node in the graph.

Return type:

LoopT

has_child_loop(loop: LoopT) bool
Parameters:

loop (Loop) – The loop to check for child connections.

Returns:

True if the loop has a child loop, False otherwise.

Return type:

bool

has_edge(u: LoopT | int, v: LoopT | int) bool
Parameters:
  • u (LoopT | int) – The loop or id of the first loop in the edge.

  • v (LoopT | int) – The loop or id of the second loop in the edge.

Returns:

True if the graph has an edge from loop u to v. False, otherwise.

Return type:

bool

has_loop(loop: int | LoopT) bool
Parameters:

loop (int | LoopT) – The loop or loop id to check for in the graph.

Returns:

True if the loop id is in the graph. False, otherwise.

Return type:

bool

is_descendant(ancestor: LoopT, descendant: LoopT) bool
Parameters:
  • ancestor (LoopT) – The loop to check if it is an ancestor of the other loop.

  • descendant (LoopT) – The loop to check if it is a descendant of the other loop.

Returns:

True if there is a directed path from the ancestor to the descendant, False otherwise.

Return type:

bool

is_terminal_loop(loop: LoopT) bool
Parameters:

loop (LoopT) – The loop to check for terminal status.

Returns:

True if the loop has no child loops, False otherwise.

Return type:

bool

property loop_count: int

Returns: int: The number of loops in the graph.

predecessors(loop: int | LoopT) set[LoopT]
Parameters:

loop (int | LoopT) – The loop to get the predecessors of from the graph.

Returns:

The successors of the loop.

Return type:

set[LoopT]

remove_edge(u: LoopT | int, v: LoopT | int) None

Removes the edge from loop u to the loop v from the graph. :param u: The loop to connect to. :type u: LoopT | int :param v: The loop to connect to. :type v: LoopT | int

Raises:

KeyError – The given edge does not exist in the graph.

remove_loop(loop: LoopT | int) None

Remove the given loop from the graph. :param loop: The loop or id of the loop to remove. :type loop: LoopT | int

Raises:

KeyError – The given loop does not exist in the graph.

property sorted_loops: list[LoopT]

Returns: list[Loop]: The list of loops in the graph sorted from the earliest formed loop to the latest formed loop.

source_loops(loop: LoopT) set[LoopT]
Parameters:

loop (LoopT) – The loop to get the sources of.

Returns:

The source loops of the given loop. These are the loops that are the source of all ancestors to the given loop.

Return type:

set[LoopT]

successors(loop: int | LoopT) set[LoopT]
Parameters:

loop (int | LoopT) – The loop to get the successors of from the graph.

Returns:

The successors of the loop.

Return type:

set[LoopT]

property terminal_loops: Iterator[LoopT]

Returns: Iterator[Loop]: An iterator over all terminal loops in the graph.