Guards#
Guard predicates enabling nodes under the current coloring.
A guard is a numba-compatible predicate guard(config, i) -> bool
deciding whether node i of a Config is currently
enabled, i.e. eligible for recoloring. The guard is captured by
tinto.configs.make() at class-definition time and evaluated by
check().
Four ready-made guards are provided, expressing increasingly conservative
enabling rules. color_clash() is the default used by
tinto.configs.make() when no guard is passed.
- tinto.guards.color_clash(config, i)[source]#
Enable node i iff it shares its color with at least one neighbor.
- Parameters:
config (
Config) – Coloring configuration whosecolorsandgraphare read.i (
int) – Node index, with0 <= i < config.graph.n.
- Returns:
Trueiff at least one neighbor of i currently shares the color of i.- Return type:
Examples
On a 3-node line with every node at color
0, every node clashes with a neighbor:>>> from tinto.graphs import line >>> from tinto.configs import make >>> Config = make() >>> c = Config(line(3)) >>> color_clash(c, 0) True
With a proper coloring, no node clashes:
>>> c.colors[:] = [0, 1, 0] >>> color_clash(c, 1) False
- tinto.guards.color_clash_tie(config, i)[source]#
Enable node i iff it clashes with a neighbor and owns the lowest ID among the clashing cluster.
Identical to
color_clash()except that, among a group of nodes sharing the same color, only the one with the smallestidsentry is enabled. This breaks ties deterministically, which matters for solvers that would otherwise recolor several conflicting nodes simultaneously into the same new color.- Parameters:
config (
Config) – Coloring configuration.i (
int) – Node index.
- Returns:
Trueiff i clashes with a neighbor and no clashing neighbor has a smalleridsvalue.- Return type:
Examples
>>> from tinto.graphs import line >>> from tinto.configs import make >>> import numpy as np >>> Config = make() >>> c = Config(line(3))
Let’s cheat and set the ids to the natural order:
>>> c.ids = np.arange(3) >>> c.ids.tolist() [0, 1, 2]
All three nodes share color 0. Node 0 wins the tie (smallest id):
>>> color_clash_tie(c, 0) True
Node 1 clashes with node 0 whose id is smaller, so it yields:
>>> color_clash_tie(c, 1) False
With a proper coloring no node clashes at all:
>>> c.colors[:] = [0, 1, 0] >>> color_clash_tie(c, 0) False
- tinto.guards.color_min(config, i)[source]#
Enable node i iff its own color is not the lowest one missing among its neighbors.
More precisely, walk
range(k)in order and stop at the first color c absent from the neighbor histogram; the node is enabled iffcdiffers from its current color.- Parameters:
config (
Config) – Coloring configuration.i (
int) – Node index.
- Returns:
Trueiff there exists a color strictly lower than the first one missing from the neighbor histogram that i currently holds, or equivalently iff i is not already using that minimum available color.- Return type:
Examples
>>> from tinto.graphs import line >>> from tinto.configs import make >>> Config = make() >>> c = Config(line(3))
With
colors = [0, 0, 0], node 1 has two neighbors colored 0. The smallest missing color is 1, which differs fromme = 0:>>> color_min(c, 1) True
Swap node 1 to color 1 ; now it holds the smallest missing color:
>>> c.colors[:] = [0, 1, 0] >>> c.check_all() >>> color_min(c, 1) False
On a complete graph K4 with
k = 3colors, every color is represented in the neighborhood of node 0, so no color is “missing” at all:>>> import numpy as np >>> from tinto.graphs import Graph >>> ptr = np.array([0, 3, 6, 9, 12], dtype=np.int32) >>> ind = np.array([1, 2, 3, 0, 2, 3, 0, 1, 3, 0, 1, 2], dtype=np.int32) >>> g = Graph(ptr, ind) >>> c = Config(g, k=3) >>> c.colors[:] = [0, 1, 2, 0] >>> c.check_all() >>> color_min(c, 0) False
- tinto.guards.color_min_tie(config, i)[source]#
Enable node i under
color_min()with a tie-break onids.Same rule as
color_min(), except that when a neighbor sharing the color of i has a smallerids, i yields priority and is disabled.- Parameters:
config (
Config) – Coloring configuration.i (
int) – Node index.
- Returns:
Trueiff i satisfiescolor_min()and owns the smallestidsamong same-colored neighbors.- Return type:
Examples
>>> from tinto.graphs import line >>> from tinto.configs import make >>> import numpy as np >>> Config = make() >>> c = Config(line(3)) >>> c.ids = np.arange(3)
With uniform color 0, node 0 wins the tie and is enabled:
>>> color_min_tie(c, 0) True
Node 1 clashes with node 0 whose id is smaller, and yields:
>>> color_min_tie(c, 1) False
Holding already the minimum missing color makes the node disabled, whatever its id:
>>> c.colors[:] = [0, 1, 0] >>> c.check_all() >>> color_min_tie(c, 1) False
And on a saturated neighborhood where no color is missing at all, the node is also disabled:
>>> import numpy as np >>> from tinto.graphs import Graph >>> ptr = np.array([0, 3, 6, 9, 12], dtype=np.int32) >>> ind = np.array([1, 2, 3, 0, 2, 3, 0, 1, 3, 0, 1, 2], dtype=np.int32) >>> g = Graph(ptr, ind) >>> c = Config(g, k=3) >>> c.colors[:] = [0, 1, 2, 0] >>> c.check_all() >>> color_min_tie(c, 0) False