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.
- get_crossing(left_loop: LoopT, right_loop: LoopT) Crossing_Direction[source]
- Parameters:
- Returns:
The crossing direction between the left and right loop. Defaults to No_Cross if no explicit crossing was previously defined.
- Return type:
- 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.
- 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.
- 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]): ….
- __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.
- 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_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_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
- 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:
- 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:
- 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]