Prims Weighted
Module: terminaltexteffects.utils.spanningtree.algo.primsweighted
Weighted Prim-style spanning tree generator.
This module provides the PrimsWeighted spanning tree generator. The
algorithm builds a spanning tree over a graph of EffectCharacter
nodes using a Prim-style approach with randomly assigned per-character
weights.
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 pending link whose target character has the lowest assigned weight is chosen and the process repeats.
PrimsWeighted
Bases: SpanningTreeGenerator
Weighted Prim-style spanning tree generator.
Attributes:
| Name | Type | Description |
|---|---|---|
char_last_linked |
EffectCharacter | None
|
Character most recently linked into the tree. During initialization this is the starting character. On later steps it may retain its previous value when completion is reached through stale pending links. |
char_link_order |
list[EffectCharacter]
|
Characters in the order they were linked into the tree, beginning with the starting character. |
neighbors_last_added |
list[EffectCharacter]
|
Characters most recently added to the pending weighted-link pool. During initialization this is populated from the starting character's eligible neighbors. |
complete |
bool
|
Whether the algorithm is complete. |
Source code in terminaltexteffects/utils/spanningtree/algo/primsweighted.py
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
__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 character for the tree
generation. When |
None
|
limit_to_text_boundary
|
bool
|
If True, the graph will not link to neighbors outside the text boundary, and random starting-character selection is limited to the text boundary. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unable to find a starting character. |
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.
Stale pending links whose target character has already been linked are discarded while searching for the next valid link.
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()
Advance the tree generation by one step.
Each step chooses the pending weighted link with the lowest target weight, links its target character into the tree, and adds the new character's eligible neighbors to the pending pool.
Note
If the pending pool starts empty, this method sets complete to
True, clears neighbors_last_added, and sets char_last_linked
to None.
If pending links exist but all remaining candidates are stale,
get_lowest_weight_link() returns None and this method sets
complete to True without resetting char_last_linked or
neighbors_last_added.
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. |