0

This is part of my Master thesis which is about designing hydrogen pipeline networks.

For a particular graph of 135 nodes and 157 edges and (see figure below), I need to compute the number of spanning trees (spanning all nodes). I used the Kirchhoff's theorem and implemented it (using scipy and networkx) this way:

import networkx as nx
from scipy import linalg


def nbr_spanning_trees(a_G):
    L = nx.laplacian_matrix(a_G)  # compute the laplacian matrix which is equal to degree matrix - adjacency matrix
    L = L.toarray()  # convert the sparse matrix output of the previous line to an array
    L_cof = L[1:, 1:]  # extract the submatrix associated with the (1,1)-entry's cofactor from the Laplacian matrix
    # (any cofactor can be taken)
    det = linalg.det(L_cof)  # calculate the cofactor
    return det

The returned result is: 1.879759212930661e+16 spanning trees.

This number seems gigantesque, is it reasonable/correct? Is my implementation correct?

As an addition, the graph has at least 23 cycles (but not much more I think). I know this since I have identified the minimum spanning tree and it has 23 edges less than the underlying graph.

enter image description here

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Khalilbs
  • 71
  • 1
  • 3
  • 2
    What do you get as result of your function if you test it with graphs where you know the correct number of spanning trees in advance and/or are able to count them by hand? – mkrieger1 Mar 14 '22 at 10:29
  • You could also test various identities, eg two smaller graphs separately and then combined – Jiří Baum Mar 14 '22 at 11:40
  • @mkrieger1 yes indeed, I did that and the method yielded the expected result, However for the "big" graph, the result seemed to me stupidly big. – Khalilbs Mar 21 '22 at 14:34
  • 1
    @JiříBaum I tried to separate the network at a bottleneck node (in the left bottom quadrant) to two smaller nets. The product of the number of spanning trees calculated for each net is almost equal (711 units of difference for a number of the magnitude of 10^16) to the total. – Khalilbs Mar 21 '22 at 14:40
  • You could gain more confidence by writing tests that generate multiple graphs of different sizes and check identities like that; or ones where the number of spanning trees can be calculated by construction – Jiří Baum Mar 22 '22 at 03:04

0 Answers0