【问题标题】:A node in a 2-hub neighborhood of a node has zero degree?节点的 2-hub 邻域中的节点度数为零?
【发布时间】:2014-06-18 12:38:47
【问题描述】:

我有一个networkxG,我使用此代码计算了距离特定节点 2-hubs 的节点:

def node_neighborhood(G, node, n=2):
    """
    Returns a list of nodes which are the n-neighborhood of the input node.

    Parameters
    ----------
    G: networkx graph object.
    node: the node to get the neighborhood for.
    n: the neighborhood degree.
    """
    path_lengths = nx.single_source_dijkstra_path_length(G, node)
    return [node for node, length in path_lengths.iteritems()
                    if length == n]

所以这段代码返回的节点是距离指定节点 2-hubs 的节点。

接下来,我使用以下代码从G 中删除了不在node_neighborhood 返回列表中的所有节点:

            for n in G.nodes():
                if (n not in node_2_neiborhood) and (n != node):
                    G.remove_node(n)
                else:
                    if G.degree(n) == 0:
                        raise Exception("a node has zero degree!!!")

但是问题是节点的度数为零的异常被抛出。我的问题是为什么会这样?如果一个节点距离 X 2-hubs,那么该节点必须至少有一条边!那么邻域中的一个节点怎么可能是零度呢?!

【问题讨论】:

    标签: python algorithm graph networkx


    【解决方案1】:

    这种行为是如此正常和意料之中,因为通过这种方法,我正在删除距离目标节点 1-hub 的节点。所以我要删除 2-hubs away 节点和目标节点之间的路径。

    【讨论】:

      猜你喜欢
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多