簡體   English   中英

如何跟隨有向圖的向后邊?

[英]How to follow the edges backwards of directed graph?

我想創建代碼以跟隨圖向后的邊以從目標圖(有向圖)構造子圖。

為了更好的解釋,我畫了一個例子。 這是示例。

首先,從目標圖中隨機選擇初始節點。 (示例圖中的綠色)

然后,我想獲得初始節點的“向后”鄰居節點集。(可能的“向后”鄰居節點被紅線包圍)

這是從有向圖中提取子圖的代碼。 在這個子圖構建過程中,根據邊的方向取鄰居節點。

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

def RWS(G, r=0.5, S=4):
    #initialize subgraph
    Gk = nx.DiGraph()
    #initialize nodes 
    Vk = [] 
    #randomly select the initial node from G
    vs = np.random.randint(0, G.size()) 
    print(vs)
    #add vs to Gk
    Gk.add_node(vs) 
    Vk.append(vs)

    while len(Vk) < S:
        #get neighbor nodes set of Vk (step 4) (Also appending j just for the purpose of adding edge)
        NS = [(n, j) for j in Vk for n in G.neighbors(j) if n not in Vk]
        print("{} {} {} {}".format('length of NS is', len(NS), 'and vs =', vs))
        # randomly select r of nodes in NS, add them into the Vk
        if not len(NS) == 0:
            for node, j in NS:
                if np.random.uniform() < r:
                    Vk.append(node)
                    Gk.add_edge(j, node)
                    if len(Vk) == S or len(NS) < S:
                        break
        else:
            break
    return Gk

if __name__ == '__main__':
    # "Undirected" graph adjacency matrix
    m = np.matrix([
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0], 
    [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 1, 0, 1, 1, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]])

    # G = nx.from_numpy_matrix(m, create_using=nx.MultiDiGraph())
    G =  nx.from_numpy_matrix(m, create_using=nx.DiGraph)
    #expansion ratio
    r  = 0.5
    #subgraph size
    S  = 4

    Gk = RWS(G, r, S)

    # VISUALIZATION
    pos = nx.spring_layout(G)
    nx.draw_networkx_nodes(G, pos)
    nx.draw_networkx_nodes(G, pos, nodelist=list(Gk.nodes()), node_color='r')
    nx.draw_networkx_labels(G, pos)
    nx.draw_networkx_edges(G, pos, edge_color='b', width=0.5)
    nx.draw_networkx_edges(G, pos, edgelist=list(Gk.edges()), edge_color='g', width=1, arrowstyle='->')

    plt.axis('off')
    plt.show() 

結果

是的,您已經明白我想要做與此代碼相反的操作,以獲取向后鄰居節點的子圖。

也許我的這個問題將有助於更好地理解。

您可以通過以下方式以最少的修改修復您的算法

  1. 用反轉圖運行它。 您可以使用DiGraph.reverse反轉圖形。 在您的情況下,您可以執行Gi = G.reverse()並在RWS使用它而不是G
  2. 再次使用DiGraph.reverse反轉結果。

RWS的修改實現可能如下所示:

def RWS(G, r=0.5, S=4):
    #initialize subgraph
    Gk = nx.DiGraph()
    #initialize nodes 
    Vk = [] 
    #randomly select the initial node from G
    vs = np.random.randint(0, G.size()) 
    print(vs)
    #add vs to Gk
    Gk.add_node(vs) 
    Vk.append(vs)
    Gi = G.reverse()  # reverse input graph so we can just follow the edges
    while len(Vk) < S:
        #get neighbor nodes set of Vk (step 4) (Also appending j just for the purpose of adding edge)
        NS = [(n, j) for j in Vk for n in Gi.neighbors(j) if n not in Vk]
        print("{} {} {} {}".format('length of NS is', len(NS), 'and vs =', vs))
        # randomly select r of nodes in NS, add them into the Vk
        if not len(NS) == 0:
            for node, j in NS:
                if np.random.uniform() < r:
                    Vk.append(node)
                    Gk.add_edge(j, node)
                    if len(Vk) == S or len(NS) < S:
                        break
        else:
            break
    return Gk.reverse()  # Reverse result

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM