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:
GraphStatic directed graph stored in compressed sparse row (CSR) format.
The out-neighbors of every node are stored consecutively in
ind, andptrrecords where each node’s neighbor list begins. This is the layout used byscipy.sparse.csr_array, which makes interoperability cheap: seeadja_to_graph().- Parameters:
ptr (
ndarrayofint32, shape (n + 1,)) – Row pointer array.ptr[i]is the position inindwhere the neighbors of node i start, andptr[i + 1]is where they end. Must be non-decreasing withptr[0] == 0.ind (
ndarrayofint32, shape (nnz,)) – Column indices.ind[ptr[i]:ptr[i + 1]]lists the neighbors of node i.
- Variables:
Notes
Because the class is decorated with
numba.jitclass(), ptr and ind must benumpyarrays 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._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()andring().- Return type:
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
Graphfrom 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 CSRindarray andptris 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:
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 - 1form the first side andn1, ..., n1 + n2 - 1the second; each of then1 n2pairs across the sides is an edge independently with probability p. The edges come frombipartite_edges(), in O(n + m) time.- Parameters:
- Returns:
A \(G(n_1, n_2, p)\) graph on
n1 + n2nodes.- Return type:
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 n2pairs across the sides are visited in row-major order and the gap to the next edge is drawn from its geometric law, as inerdos_renyi_edges().- Parameters:
- Returns:
One row
(u, v)per edge, withu < n1 <= v.- Return type:
ndarrayofint64, 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:
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 - 1edges of the tree.- Return type:
ndarrayofint64, 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
Graphfrom 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 withadja_to_graph(). Self-loops and repeated edges are not checked.- Parameters:
- Returns:
Graph in CSR form, with
2 mneighbor entries.- Return type:
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:
- Returns:
A G(n, p) graph.
- Return type:
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:
- Returns:
One row
(v, w)per edge, withw < v.- Return type:
ndarrayofint64, 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:
Examples
>>> g = line(5) >>> g.n 5 >>> g.neighbors(0).tolist() [1] >>> g.neighbors(1).tolist() [0, 2] >>> g.neighbors(4).tolist() [3]