General graphs#

Quantities valid on every graph: the peak probability of a node (Theorem 1) and the exact first two moments of the stabilization time (Theorems 1 and 3).

The exact functions work in rational arithmetic: with lo = 1 - 1/k, every integral below is an integral of a polynomial over [lo, 1], evaluated as a Fraction.

The graph functions read a graph through graph.n and graph.neighbors(i) only (a Graph qualifies); this module imports nothing from tinto. The exception is monochrome_variance(), compiled and in floating point, which reads an adjacency matrix: it serves loops over millions of graphs.

tinto.theory.general.fixed_coloring_mean(graph, colors)[source]#

Exact mean stabilization time from a fixed initial coloring.

From a fixed coloring \(\gamma\), only the labeling is random: a node is a peak iff its label is the largest among itself and its \(d_v(\gamma)\) same-colored neighbors, so

\[\mathbb E\, T(G, \gamma) = \sum_v \frac{d_v(\gamma)}{d_v(\gamma) + 1}.\]
Parameters:
  • graph (Graph) – Undirected graph.

  • colors (array_like) – Initial color of each node.

Return type:

Fraction

Examples

>>> from tinto.graphs import line
>>> fixed_coloring_mean(line(3), [0, 0, 1])
Fraction(1, 1)
tinto.theory.general.mean(graph, k=1)[source]#

Exact mean of the stabilization time T = n - S on a graph.

Parameters:
  • graph (Graph) – Undirected graph.

  • k (int, default=1) – Number of colors.

Return type:

Fraction

Examples

>>> from tinto.graphs import line
>>> mean(line(3))
Fraction(5, 3)
tinto.theory.general.moments(graph, k=1)[source]#

Exact \(\mathbb E S\) and \(\mathbb E S(S-1)\) on a graph (Theorems 1 and 3).

\(\mathbb E S = \sum_v \rho(d_v, k)\), and \(\mathbb E S(S-1)\) sums over ordered pairs of distinct nodes pi_dist(), plus pi_same() when the pair is not an edge (two adjacent nodes of the same color cannot both be peaks).

Parameters:
  • graph (Graph) – Undirected graph.

  • k (int, default=1) – Number of colors.

Returns:

(E S, E S(S-1)).

Return type:

tuple of Fraction

Examples

On a monochrome triangle exactly one node is a peak:

>>> from tinto.graphs import ring
>>> moments(ring(3))
(Fraction(1, 1), Fraction(0, 1))
tinto.theory.general.monochrome_variance(adj)[source]#

Monochrome variance of the stabilization time, in floating point and compiled.

The pairwise decomposition \(\mathrm{Var}\, T = \sum_v \mathrm{Var}\, X_v + \sum_{u \ne v} \mathrm{Cov}(X_u, X_v)\), with \(X_v\) the peak indicator of v, \(\mathrm{Var}\, X_v = d_v / (d_v + 1)^2\), and the covariances of the lemma “Pairwise covariance” of the exact stabilization-time paper: \(-1/((d_u + 1)(d_v + 1))\) for adjacent nodes, and

\[\mathrm{Cov}(X_u, X_v) = \frac{b}{(a + b + 1)(b + c + 1)(a + b + c + 2)}\]

for non-adjacent nodes with a private neighbors of u, b common neighbors and c private neighbors of v. Same value as variance() at k = 1, without rational arithmetic: meant for loops over many graphs (exhaustive enumeration, local search).

Parameters:

adj (ndarray of bool, shape (n, n)) – Adjacency matrix: symmetric, with a zero diagonal.

Return type:

float

Examples

>>> import numpy as np
>>> from tinto.graphs import line
>>> round(monochrome_variance(line(3).to_adja().astype(np.bool_)), 12)
0.222222222222
tinto.theory.general.pair_parameters(graph, u, v)[source]#

Private and common neighbors of two nodes.

Parameters:
  • graph (Graph) – Undirected graph.

  • u (int) – Two distinct nodes.

  • v (int) – Two distinct nodes.

Returns:

(a, b, c, adjacent): neighbors of u only, common neighbors, neighbors of v only (u and v themselves excluded), and whether u and v are adjacent.

Return type:

tuple

Examples

>>> from tinto.graphs import line
>>> pair_parameters(line(4), 0, 2)
(0, 1, 1, False)
tinto.theory.general.pi_dist(a, b, c, k=1)[source]#

Distinct-color term of Theorem 3, exact.

For two nodes u and v (adjacent or not) with a private neighbors of u, b common neighbors and c private neighbors of v,

\[\pi_{dist}(a, b, c) = (k-1) k \iint_{[1-1/k, 1]^2} s^a t^c (s + t - 1)^b \, ds\, dt\]

is the probability that both are peaks with distinct colors. The power of s + t - 1 is expanded by the trinomial formula.

Parameters:
  • a (int) – Private neighbors of u, common neighbors, private neighbors of v.

  • b (int) – Private neighbors of u, common neighbors, private neighbors of v.

  • c (int) – Private neighbors of u, common neighbors, private neighbors of v.

  • k (int, default=1) – Number of colors.

Returns:

Zero when k = 1.

Return type:

Fraction

Examples

Two isolated nodes with two colors: distinct colors with probability 1/2, and then both are peaks:

>>> pi_dist(0, 0, 0, 2)
Fraction(1, 2)
tinto.theory.general.pi_same(a, b, c, k=1)[source]#

Same-color term of Theorem 3, exact.

For two non-adjacent nodes u and v with a private neighbors of u, b common neighbors and c private neighbors of v,

\[\pi_{same}(a, b, c) = k \iint_{[1-1/k, 1]^2} s^a t^c \min(s, t)^b \, ds\, dt\]

is the probability that both are peaks and share their color (the color is shared with probability 1/k, which cancels one factor k). The integral is split on s <= t and s >= t.

Parameters:
  • a (int) – Private neighbors of u, common neighbors, private neighbors of v.

  • b (int) – Private neighbors of u, common neighbors, private neighbors of v.

  • c (int) – Private neighbors of u, common neighbors, private neighbors of v.

  • k (int, default=1) – Number of colors.

Return type:

Fraction

Examples

Two isolated nodes of the same color are both peaks:

>>> pi_same(0, 0, 0)
Fraction(1, 1)
tinto.theory.general.rho(d, k=1, exact=False)[source]#

Probability that a node of degree d is a peak, under k colors.

A node of degree d is a peak when its label exceeds those of its same-colored neighbors. With a uniform label s and each neighbor same-colored with probability 1/k, this gives (Theorem 1 of the exact stabilization-time paper)

\[\rho(d, k) = k \int_{1-1/k}^1 s^d \, ds = \frac{k}{d+1}\left(1 - \left(1 - \frac1k\right)^{d+1}\right).\]
Parameters:
  • d (int) – Degree of the node.

  • k (int, default=1) – Number of colors.

  • exact (bool, default=False) – Return a Fraction instead of a float.

Return type:

float or Fraction

Examples

Monochrome, a node of degree d is a peak iff its label is the largest of d + 1:

>>> rho(2)
0.3333333333333333

With two colors, a leaf is beaten only when its neighbor shares its color and has the larger label:

>>> rho(1, 2)
0.75
>>> rho(2, 3, exact=True)
Fraction(19, 27)
tinto.theory.general.variance(graph, k=1)[source]#

Exact variance of the stabilization time on a graph.

\(\mathrm{Var}\, T = \mathrm{Var}\, S = \mathbb E S(S-1) + \mathbb E S - (\mathbb E S)^2\).

Parameters:
  • graph (Graph) – Undirected graph.

  • k (int, default=1) – Number of colors.

Return type:

Fraction

Examples

>>> from tinto.graphs import line
>>> variance(line(3))
Fraction(2, 9)