Graphs#

Compressed sparse row (CSR) graph representation.

This module defines Graph, a lightweight static directed graph backed by two integer arrays in CSR format and compiled with numba so that it can be manipulated inside other JIT-compiled routines. The module also exposes two builders, line() and ring(), that return common benchmark topologies.

class tinto.graphs.Graph(*args, **kwargs)[source]#

Bases: Graph

Static directed graph stored in compressed sparse row (CSR) format.

The out-neighbors of every node are stored consecutively in ind, and ptr records where each node’s neighbor list begins. This is the layout used by scipy.sparse.csr_array, which makes interoperability cheap: see adja_to_graph().

Parameters:
  • ptr (ndarray of int32, shape (n + 1,)) – Row pointer array. ptr[i] is the position in ind where the neighbors of node i start, and ptr[i + 1] is where they end. Must be non-decreasing with ptr[0] == 0.

  • ind (ndarray of int32, shape (nnz,)) – Column indices. ind[ptr[i]:ptr[i + 1]] lists the neighbors of node i.

Variables:
  • n (int64) – Number of nodes, equal to len(ptr) - 1.

  • ptr (ndarray of int32) – See the corresponding parameter.

  • ind (ndarray of int32) – See the corresponding parameter.

  • max_degree (int64) – Maximum out-degree of any node, computed from ptr and used by Config to set the number of colors.

Notes

Because the class is decorated with numba.jitclass(), ptr and ind must be numpy arrays with the exact dtypes declared above; Python lists are not accepted.

Examples

Build a small directed triangle graph directly from its CSR arrays:

>>> ptr = np.array([0, 2, 4, 6], dtype=np.int32)
>>> ind = np.array([1, 2, 0, 2, 0, 1], dtype=np.int32)
>>> g = Graph(ptr, ind)
>>> g.n
3
>>> g.neighbors(0).tolist()
[1, 2]
_ctor(ind)#
_ctor_sig = <Signature (self, ptr, ind)>#
class_type = jitclass.Graph#7c3c2966fb30<n:int64,ptr:array(int32, 1d, A),ind:array(int32, 1d, A),max_degree:int64>#
tinto.graphs._grow(edges, m)[source]#

Double the capacity of an edge buffer holding m edges.

tinto.graphs._skip(lq)[source]#

Gap to the next success of a Bernoulli sequence, log(1 - p) = lq.

The number of failures before the next success is geometric; drawing it directly is what makes the sparse generators run in O(n + m).

tinto.graphs.adja_line(n)[source]#

Return the adjacency matrix of the undirected line on n nodes.

Parameters:

n (int) – Number of nodes.

Returns:

Logical or of the two off-diagonal identity matrices at offsets +1 and -1. Used as a building block for line() and ring().

Return type:

ndarray of bool, shape (n, n)

Examples

>>> adja_line(3).astype(int).tolist()
[[0, 1, 0], [1, 0, 1], [0, 1, 0]]
tinto.graphs.adja_to_graph(adja)[source]#

Build a Graph from a dense adjacency matrix.

Non-zero entries are read in row-major order with numpy.nonzero(), which already groups them by row, so the column indices are exactly the CSR ind array and ptr is the cumulative count of entries per row. Any dtype works: a non-zero entry denotes an edge.

Parameters:

adja (ndarray, shape (n, n)) – Square adjacency matrix. A non-zero entry at row i and column j creates an edge from i to j.

Returns:

Equivalent graph in CSR form.

Return type:

Graph

Examples

>>> adja = np.array([[0, 1, 1],
...                  [1, 0, 0],
...                  [1, 0, 0]], dtype=np.int64)
>>> g = adja_to_graph(adja)
>>> g.n
3
>>> g.neighbors(0).tolist()
[1, 2]
>>> g.neighbors(1).tolist()
[0]

A node with no out-edges, including trailing ones, yields an empty neighbor list:

>>> adja = np.array([[0, 1, 0],
...                  [0, 0, 0],
...                  [0, 0, 0]], dtype=np.int64)
>>> g = adja_to_graph(adja)
>>> g.neighbors(0).tolist()
[1]
>>> g.neighbors(2).tolist()
[]
tinto.graphs.bipartite(n1=50, n2=50, p=0.5)[source]#

Draw a random bipartite \(G(n_1, n_2, p)\) graph.

Nodes 0, ..., n1 - 1 form the first side and n1, ..., n1 + n2 - 1 the second; each of the n1 n2 pairs across the sides is an edge independently with probability p. The edges come from bipartite_edges(), in O(n + m) time.

Parameters:
  • n1 (int, default=50) – Size of the first side.

  • n2 (int, default=50) – Size of the second side.

  • p (float, default=0.5) – Probability that an edge exists between two nodes of different sides.

Returns:

A \(G(n_1, n_2, p)\) graph on n1 + n2 nodes.

Return type:

Graph

Examples

>>> g = bipartite(2, 3, 1.0)
>>> [g.neighbors(i).tolist() for i in range(5)]
[[2, 3, 4], [2, 3, 4], [0, 1], [0, 1], [0, 1]]
tinto.graphs.bipartite_edges(n1=50, n2=50, p=0.5)[source]#

Draw the edges of a random bipartite \(G(n_1, n_2, p)\) graph in O(n + m) time.

The n1 n2 pairs across the sides are visited in row-major order and the gap to the next edge is drawn from its geometric law, as in erdos_renyi_edges().

Parameters:
  • n1 (int, default=50) – Size of the first side (nodes 0, ..., n1 - 1).

  • n2 (int, default=50) – Size of the second side (nodes n1, ..., n1 + n2 - 1).

  • p (float, default=0.5) – Probability that an edge exists between two nodes of different sides.

Returns:

One row (u, v) per edge, with u < n1 <= v.

Return type:

ndarray of int64, shape (m, 2)

Examples

>>> bipartite_edges(2, 2, 1.0).tolist()
[[0, 2], [0, 3], [1, 2], [1, 3]]
>>> bipartite_edges(2, 2, 0.0).shape
(0, 2)
tinto.graphs.cayley(n=100)[source]#

Draw a random Cayley graph with n nodes. Uses the bijection described in https://arxiv.org/abs/2107.09726

Parameters:

n (int, default=100) – Number of nodes of the tree.

Returns:

A Cayley tree with n nodes.

Return type:

Graph

Examples

>>> from tinto.io import set_seed
>>> set_seed(42)
>>> g = cayley(5)
>>> g.to_adja()
array([[False, False,  True, False, False],
       [False, False, False, False,  True],
       [ True, False, False, False,  True],
       [False, False, False, False,  True],
       [False,  True,  True,  True, False]])

The degenerate single-node tree has no edges:

>>> cayley(1).to_adja()
array([[False]])
tinto.graphs.cayley_edges(n=100)[source]#

Draw the edges of a uniform random labeled tree on n nodes.

Same draws as cayley(), which builds its graph from this list (the bijection of https://arxiv.org/abs/2107.09726).

Parameters:

n (int, default=100) – Number of nodes of the tree.

Returns:

The n - 1 edges of the tree.

Return type:

ndarray of int64, shape (n - 1, 2)

Examples

>>> from tinto.io import set_seed
>>> set_seed(42)
>>> sorted(sorted(e) for e in cayley_edges(5).tolist())
[[0, 2], [1, 4], [2, 4], [3, 4]]
>>> cayley_edges(1).shape
(0, 2)
tinto.graphs.edges_to_graph(n, edges)[source]#

Build an undirected Graph from an edge list.

Each row (u, v) of edges is an undirected edge, listed once; the graph stores it in both directions. Neighbor lists come out sorted, as with adja_to_graph(). Self-loops and repeated edges are not checked.

Parameters:
  • n (int) – Number of nodes.

  • edges (ndarray of int, shape (m, 2)) – Edge list, with node indices in range(n).

Returns:

Graph in CSR form, with 2 m neighbor entries.

Return type:

Graph

Examples

The bull, a triangle with two pendant nodes:

>>> g = edges_to_graph(5, np.array([[0, 1], [1, 2], [0, 2], [1, 3], [2, 4]]))
>>> [g.neighbors(i).tolist() for i in range(5)]
[[1, 2], [0, 2, 3], [0, 1, 4], [1], [2]]

Isolated nodes, trailing ones included, get empty neighbor lists:

>>> g = edges_to_graph(4, np.array([[1, 0]]))
>>> [g.neighbors(i).tolist() for i in range(4)]
[[1], [0], [], []]
tinto.graphs.erdos_renyi(n=100, p=0.5)[source]#

Draw a random undirected \(G(n, p)\) Erdős-Rényi graph.

Every unordered pair of distinct nodes is connected independently with probability p. The edges come from erdos_renyi_edges(), in O(n + m) time.

Parameters:
  • n (int, default=100) – Number of nodes in the erdos-renyi graph.

  • p (float, default=0.5) – Probability that an edge exists between two distinct nodes.

Returns:

A G(n, p) graph.

Return type:

Graph

Examples

>>> from tinto.io import set_seed
>>> set_seed(42)
>>> g = erdos_renyi(5, p=.6)
>>> g.to_adja()
array([[False,  True, False, False,  True],
       [ True, False, False,  True,  True],
       [False, False, False, False,  True],
       [False,  True, False, False,  True],
       [ True,  True,  True,  True, False]])
tinto.graphs.erdos_renyi_edges(n=100, p=0.5)[source]#

Draw the edges of a random \(G(n, p)\) graph in O(n + m) time.

The pairs are visited in the order (1, 0), (2, 0), (2, 1), (3, 0)... and the gap to the next edge is drawn from its geometric law instead of testing every pair (Batagelj and Brandes, Efficient generation of large random networks, Phys. Rev. E 71, 2005).

Parameters:
  • n (int, default=100) – Number of nodes.

  • p (float, default=0.5) – Probability that an edge exists between two distinct nodes.

Returns:

One row (v, w) per edge, with w < v.

Return type:

ndarray of int64, shape (m, 2)

Examples

>>> erdos_renyi_edges(4, 0.0).shape
(0, 2)
>>> erdos_renyi_edges(4, 1.0).tolist()
[[1, 0], [2, 0], [2, 1], [3, 0], [3, 1], [3, 2]]
tinto.graphs.line(n=100)[source]#

Return a line graph with n nodes.

Parameters:

n (int, default=100) – Number of nodes in the line graph.

Returns:

Line graph with n nodes.

Return type:

Graph

Examples

>>> g = line(5)
>>> g.n
5
>>> g.neighbors(0).tolist()
[1]
>>> g.neighbors(1).tolist()
[0, 2]
>>> g.neighbors(4).tolist()
[3]
tinto.graphs.ring(n=100)[source]#

Return a ring graph with n nodes.

Parameters:

n (int, default=100) – Number of nodes in the ring graph.

Returns:

Ring graph with n nodes.

Return type:

Graph

Examples

>>> g = ring(5)
>>> g.neighbors(0).tolist()
[1, 4]
>>> g.neighbors(4).tolist()
[0, 3]