num_spanning_trees

num_spanning_trees#

graph_tool.spectral.num_spanning_trees(g, weight=None, log=False)[git][source]#

Return the number of spanning trees of an undirected graph.

Parameters:
gGraph

Undirected graph to be used.

weightEdgePropertyMap (optional, default: None)

Edge weights. If given, the sum over spanning trees of the products of their edge weights is returned.

logbool (optional, default: False)

If True, the natural logarithm is returned, which avoids overflows for large graphs.

Returns:
taufloat

The number of spanning trees (zero if the graph is disconnected).

See also

laplacian

Laplacian matrix

graph_tool.structure.random_spanning_tree

Random spanning tree

graph_tool.structure.minimum_spanning_tree

Minimum spanning tree

graph_tool.structure.tutte_polynomial

Tutte polynomial

Notes

By Kirchhoff’s matrix-tree theorem [kirchhoff-ueber-1847], the number is the determinant of the Laplacian with one row and column removed, which is computed from a sparse LU factorization (scipy.sparse.linalg.splu()) as the product of the pivots. With edge weights, the same determinant gives

\[\tau(G) = \sum_{T}\prod_{e\in T}w_e,\]

where the sum runs over all spanning trees \(T\) of the graph and \(w_e\) is the weight of edge \(e\), which reduces to the number of spanning trees for unit weights. Parallel edges count as distinct, and self-loops are ignored.

References

[kirchhoff-ueber-1847]

G. Kirchhoff, “Ueber die Auflösung der Gleichungen, auf welche man bei der Untersuchung der linearen Vertheilung galvanischer Ströme geführt wird”, Annalen der Physik 148, 497-508 (1847), DOI: 10.1002/andp.18471481202 [sci-hub, @tor]

Examples

>>> g = gt.complete_graph(5)
>>> print(int(np.round(gt.num_spanning_trees(g))))
125