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:
- g
Graph Undirected graph to be used.
- weight
EdgePropertyMap(optional, default:None) Edge weights. If given, the sum over spanning trees of the products of their edge weights is returned.
- log
bool(optional, default:False) If
True, the natural logarithm is returned, which avoids overflows for large graphs.
- g
- Returns:
- tau
float The number of spanning trees (zero if the graph is disconnected).
- tau
See also
laplacianLaplacian matrix
graph_tool.structure.random_spanning_treeRandom spanning tree
graph_tool.structure.minimum_spanning_treeMinimum spanning tree
graph_tool.structure.tutte_polynomialTutte 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