【问题标题】:Using formula to count number of cycles in undirected graph in python在python中使用公式计算无向图中的循环数
【发布时间】:2019-08-05 11:47:33
【问题描述】:

我遇到了这个公式来获得无向图中的 4 循环数:

 [![formula to calculate the number of 4-cycle][1]][1]

 Ck = 1/2k Tr(Pk-1 A)

 k stands for k-cycle, and A is the adjacency matrix, Pk-1 is the path count matrix

网页链接:

[数学世界]http://mathworld.wolfram.com/GraphCycle.html

我现在正在尝试在 python 中编写这个简单的公式,我可以使用 NetworkX 的 adjacency_matrix 函数来获取邻接矩阵,并且我也可以得到矩阵迹线。我只是不确定所谓的路径计数 Pk 矩阵,我用谷歌搜索了一段时间,但没有找到对此的直接解释。专家能否建议这个 Pk 矩阵是什么,我想知道是否可以用 Python 对其进行编码?

提前致谢!

【问题讨论】:

    标签: python graph networkx cycle discrete-mathematics


    【解决方案1】:

    我的猜测是P_k = A^k。但由于您对 4 个周期的数量感兴趣,您可以简单地使用 link 的公式 (3):

    8c_4 = Tr(A^4) - 2m - 2 \sum_i{i\ne j} a_{ij}^{(2)}
    

    其中a_{ij}^{(2)}A^2 的元素。

    这个公式应该可以通过numpy 轻松实现。

    【讨论】:

    • 谢谢。但我不确定 A^2 是什么......它是邻接矩阵的一半吗?
    • 这是矩阵幂,例如A^2 = A*A,其中后者是矩阵乘法(A^4 = A*A*A*A),A 是邻接矩阵。
    • 谢谢您,这对您有很大帮助!
    猜你喜欢
    • 2021-09-21
    • 2016-01-27
    • 2021-09-23
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多