Prims Weighted
Module: terminaltexteffects.utils.spanningtree.algo.primsweighted
Prims weighted minimum spanning tree algorithm implementation.
This module provides the PrimsWeighted spanning tree generator
implementation. The algorithm builds a spanning tree over a graph of
EffectCharacter nodes using Prim's algorithm with weighted edges.
The algorithm starts from a chosen starting character (or a random one from the terminal) and grows the tree by linking to the unlinked neighbor with the lowest weight.
When a neighbor is linked, its unlinked neighbors are added to the pool of potential links. On each step, the unlinked neighbor with the lowest weight is chosen and the process repeats.
PrimsWeighted
Bases: SpanningTreeGenerator
Prims weighted minimum spanning tree 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. |
neighbors_last_added |
list[EffectCharacter]
|
Characters added as neighbors on the last step. |
complete |
bool
|
Whether the algorithm is complete. |
Source code in terminaltexteffects/utils/spanningtree/algo/primsweighted.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
__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
|
Source code in terminaltexteffects/utils/spanningtree/algo/primsweighted.py
add_weighted_links(char)
Add weighted links for the given character's unlinked neighbors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
char
|
EffectCharacter
|
Character for which weighted links are added. |
required |
Source code in terminaltexteffects/utils/spanningtree/algo/primsweighted.py
get_lowest_weight_link()
Get the weighted link with the lowest weight.
Returns:
| Type | Description |
|---|---|
WeightedLink | None
|
WeightedLink | None: The weighted link with the lowest weight, or None if no links are available. |
Source code in terminaltexteffects/utils/spanningtree/algo/primsweighted.py
step()
Perform a single step of the algorithm.
Source code in terminaltexteffects/utils/spanningtree/algo/primsweighted.py
WeightedLink
dataclass
Weighted link.
Attributes:
| Name | Type | Description |
|---|---|---|
char_a |
EffectCharacter
|
One end of the link. |
char_b |
EffectCharacter
|
The other end of the link. |
weight |
int
|
Weight of the link. |