![](/img/trans.png)
[英]How to find edges hierarchy graph from directed acyclic graph in python
[英]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()
是的,您已經明白我想要做與此代碼相反的操作,以獲取向后鄰居節點的子圖。
也許我的這個問題將有助於更好地理解。
您可以通過以下方式以最少的修改修復您的算法
Gi = G.reverse()
並在RWS
使用它而不是G
;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.