Recursive Backtracker
Module: terminaltexteffects.utils.spanningtree.algo.recursivebacktracker
Recursive Backtracker minimum spanning tree algorithm.
This module provides the RecursiveBacktracker spanning tree generator
implementation. The algorithm builds a spanning tree over a graph of
EffectCharacter nodes using the depth-first recursive backtracker
approach (implemented iteratively with an explicit stack).
The algorithm starts from a chosen starting character (or a random one from the terminal) and grows the tree by repeatedly linking to a randomly selected unvisited neighbor. Linked cells are added to the stack.
When a node has no unvisited neighbors the algorithm backtracks by popping the stack until it finds a node with unvisited neighbors.
RecursiveBacktracker
Bases: SpanningTreeGenerator
Recursive Backtracker tree generator algorithm.
Attributes:
| Name | Type | Description |
|---|---|---|
char_last_linked |
EffectCharacter | None
|
Character linked into the tree on the last step. None, if no character was linked during the last step. |
char_link_order |
list[EffectCharacter]
|
Characters in linked order. |
stack |
list[EffectCharacter]
|
Characters on the stack. |
stack_last_popped |
EffectCharacter | None
|
Character popped off the stack on the last step. None if no character was popped during the last step. |
complete |
bool
|
Whether the algorithm is complete. |
Source code in terminaltexteffects/utils/spanningtree/algo/recursivebacktracker.py
__init__(terminal, starting_char=None, *, limit_to_text_boundary=False)
Initialize the algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
terminal
|
Terminal
|
TTE Terminal. |
required |
starting_char
|
EffectCharacter | None
|
Starting EffectCharacter. Defaults to None. |
None
|
limit_to_text_boundary
|
bool
|
If True, the graph will not link to neighbors outside the text boundary. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unable to find a starting character. |
Source code in terminaltexteffects/utils/spanningtree/algo/recursivebacktracker.py
step()
Progress the algorithm by one step.