2014-06-18 113 views
-1

我有一個networkx圖表G和我計算是2-集線器遠離使用此代碼的特定節點中的節點:節點的2-hub鄰居中的節點具有零度?

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-集線器遠節點。然而問題是,一個節點具有零度異常得到投擲

  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!!!") 

接下來,我用這個代碼G刪除不在返回列表中的所有節點從node_neighborhood。我的問題是爲什麼呢?如果一個節點與X相距2個集線器,那麼該節點必須至少有一個邊緣!那麼鄰居節點的零度如何呢?

回答

0

這種行爲是如此正常和預期,因爲使用這種方法,我將從目標節點中刪除1-hub的節點。因此,我將刪除2個集線器之間的路徑與節點和目標節點之間的路徑。

相關問題