【问题标题】:Find a connected subgraph with n nodes and e edges in python在python中查找具有n个节点和e条边的连通子图
【发布时间】:2021-12-11 08:36:45
【问题描述】:

我有一个非常大的 .txt 格式的无向图数据集,我将其转换为 networkx 图。我需要提取一个包含 N 个节点的连接子图(如果可能,还有 E 个边,但不是必需的)。我该怎么做?这是我为查找最大连通子图而编写的代码:

def find_subgraph(graph):
  connected_component_subgraphs = (graph.subgraph(c) for c in nx.connected_components(graph))
  largest_subgraph = max(connected_component_subgraphs, key=len)
  return largest_subgraph

我想要的子图位于最小子图和最大子图的中间。任何帮助将不胜感激。

编辑:可以是任意N个节点,不需要特定节点

【问题讨论】:

    标签: python-3.x graph networkx connected-components subgraph


    【解决方案1】:

    找到一个正好有 N 个节点的连通子图

    Select connected subgraph with node count closest but greater than N
    delete_success = true
    WHILE( delete_success )
        delete_success = false
        Loop n over nodes in subgraph
            Delete n from subgraph
            IF subgraph no longer connected
                restore n
                continue
            IF subgraph contains N nodes
                DONE
            delete_success = true
        END LOOP
    END WHILE
    report failure.
    

    请注意,这并不保证一定会成功。例如,如果两个连接在一起的节点一起被移除,那么剩余的节点可能仍然是连接的并且大于或等于 N。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多