计算n*n网格生成树的数目,网格,[Python]代码Py


[Python]代码

Python语言: 计算n*n网格生成树的数目from math import cos"""[1] http://en.wikipedia.org/wiki/Kirchhoff's_theorem[2] http://www.research.att.com/~njas/sequences/A007341[3] Direct Methods for Computing Eigenvalues of the Finite-Difference LaplacianJ. R. KuttlerSIAM Journal on Numerical Analysis, Vol. 11, No. 4 (Sep., 1974), pp. 732-740 """def eigenvalues_of_laplacian(n):    ew = [2*(2-cos(i*pi/n)-cos(j*pi/n)) for i in range(n) for j in range(n)]    return ewdef num_of_spanning_trees(n):    ew = eigenvalues_of_laplacian(n)    return reduce(lambda x,y:x*y, ew[1:])/n**2

评论关闭