【问题标题】:Getting nodes without edges (When the N is larger than 60)获取没有边的节点(当 N 大于 60 时)
【发布时间】:2022-01-24 18:59:33
【问题描述】:

首先,我使用 NumPy 生成了一个由 0 和 1 组成的 NxN 矩阵。之后,我从前一个矩阵生成了一个复制矩阵,我用边的权重替换了第一个矩阵中的那些。 (矩阵是对称的、连通的、无向的,它的对角线和原始矩阵一样为零),我用 BSF 检查它是否连通,我发现它每次都连通。然后我使用 SciPy 找到 MST(最小生成树)。之后,我使用 Network X 说明了 MST

用于生成 NxN 零和一矩阵

base = np.zeros((shape,shape))
for _ in range(100):
    a = np.random.randint(shape)
    b = np.random.randint(shape)
    if a != b:
        base[a, b] = 1
        base[b, a] = 1

用于生成具有边权重的 NxN 矩阵

# Fetch the location of the 1s.
Weightofedges = base
ones = np.argwhere(Weightofedges == 1)
ones = ones[ones[:, 0] < ones[:, 1], :]

# Assign random values.
for a, b in ones:
    Weightofedges[a, b] = Weightofedges[b, a] = np.random.randint(100)

使用 SciPy 查找 MST

from scipy.sparse.csgraph import minimum_spanning_tree
X = minimum_spanning_tree(Weightofedges)
print("The Output Of The MST By Kruskal Algorithm:")
print("  Edges:    Weights:")
print(X)
print("-----------------------")
my_matrix3 = X.toarray().astype(int)

问题:当我输入一个包含大量节点的矩阵时,我得到一些节点没有与边连接 例如 节点数等于 75 边数等于 65 在 MST 中,边必须是 N-1,其中 N 是节点数

这是使用 N = 75 的图(如图所示有没有边的节点) enter image description here

【问题讨论】:

    标签: python numpy scipy networkx minimum-spanning-tree


    【解决方案1】:

    您已经创建了 Erdős–Rényi model 的加权版本 - 确切地说是具有 n 个节点和 M 个边的 ER 模型 G(n,M)。目前,您已固定 M=100 并且您观察到 n>60 时您的连接断开。这是典型的并且(至少对于具有 n 个节点和边缘概率 p 的第二个 ER 模型变体 G(n,p)),您甚至可以计算(几乎可以肯定)获得单个/大型连接组件的阈值。但即使没有数学,您也可以直观地看到,仅使用 100 条随机边连接 75 个节点变得很困难。

    如果您想在 python 上使用图表做更多事情,我建议您查看networkx 包。比如G(n,p)变体的实现:erdos_renyi_graph

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多