gpt4 book ai didi

python - 匹配两个节点并保持其他各自节点之间的路径 networkx

转载 作者:行者123 更新时间:2023-12-04 15:29:55 25 4
gpt4 key购买 nike

已编辑

df1

From   description    To      priority 
10 Start 20,30 1
20 Left 40 2
30 Right 40 2
40 End - 1

我的第二个数据框

df2

From   description    To      priority 
50 Start 60,70 1
60 Left 80 2
70 Right 80 2
80 End - 1

当我使用 Netwokrx Python 库将两个数据帧转换为图形时,我得到以下图形,df1 为 graph1,df2 为 graph2。节点的颜色基于它们的优先级。

enter image description here

我想匹配(组合)两个颜色相似的节点,例如 10/50、40/80、20/60 和 30/70。为了清楚起见,10 和 50 具有 Start 属性,而 40 和 80 具有 End。除了这些之外,10、50、40 和 80 还具有 priority ==1 属性。节点 20 和 60 具有“Left”属性,30 和 70 具有 Right。除了这些,20、60、30 和 70 的 priority ==2

我设法同时匹配两个图中所有节点的节点。但是我无法一步一步(使用一种循环)。这意味着首先匹配具有 blue 颜色的节点,然后添加具有 orange 颜色的节点之一,依此类推。我希望像下面这样。

enter image description here

为了达到上面的结果,我是这样尝试的。

for n1, n2 in g1.nodes(data ='True'):
for x1, x2 in g2.nodes(data ='True'):
if ((g1.node[n1]['description']) == (g2.node[x1]['description'])&
if ((g1.node[n1]['priority']) == (g2.node[x1]['priority'])
):
name1 = str(n1) + '/' + str(x1)
mapping1 = {n1: name1, x1:name1}
mapping1 = nx.relabel_nodes(g1, mapping1, copy =True)

任何人都可以延长上述试用期或找到新的解决方案来获得我希望看到的结果吗?

最佳答案

使用以下代码,您可以重新标记节点:

关于 sorted 的更多提示功能。

import networkx as nx

df1_graph = nx.DiGraph()
# add edges
df1_graph.add_edges_from([(10, 20), (10, 30), (20, 40), (30, 40)])
# add node information
nodes = [10, 20, 30, 40]
descriptions = ["Start", "Left", "Right", "End"]
priorities = [1, 2, 2, 1]
for node, (description, priority) in zip(nodes, zip(descriptions, priorities)):
df1_graph.nodes[node]["description"] = description
df1_graph.nodes[node]["priority"] = priority

df2_graph = nx.DiGraph()
# add edges
df2_graph.add_edges_from([(50, 60), (50, 70), (60, 80), (60, 80)])
nodes = [50, 60, 70, 80]
for node, (description, priority) in zip(nodes, zip(descriptions, priorities)):
df2_graph.nodes[node]["description"] = description
df2_graph.nodes[node]["priority"] = priority

# creating new graph
mappings = []
for node_1, data_1 in df1_graph.nodes(data=True):
for node_2, data_2 in df2_graph.nodes(data=True):
if data_1["description"] == data_2["description"] and data_1["priority"] == data_2["priority"]:
name = str(node_1) + '/' + str(node_2)
# add found mapping (node_1, name)
# together with information about sorting order
mappings.append((data_2["priority"], data_2["description"], node_1, name))

new_graph = df1_graph.copy()
# save the relabelled graphs in a lists
graphs_stepwise = []
# sort nodes according to priority and description (secondary key)
# we sort first by description to ensure in this example Left is replaced first
mappings = sorted(mappings, key=lambda x: x[1])
# sort by priority
mappings = sorted(mappings, key=lambda x: x[0])
# relabel one node at a time
for priority, description, node, new_label in mappings:
new_graph = nx.relabel_nodes(new_graph, {node: new_label}, copy=True)
graphs_stepwise.append(new_graph)

# print node information of saved graphs
for graph in graphs_stepwise:
print(graph.nodes(data=True))
#[(10, {'description': 'Start', 'priority': 1}), (20, {'description': 'Left', 'priority': 2}), (30, {'description': 'Right', 'priority': 2}), ('40/80', {'description': 'End', 'priority': 1})]
#[('10/50', {'description': 'Start', 'priority': 1}), (20, {'description': 'Left', 'priority': 2}), (30, {'description': 'Right', 'priority': 2}), ('40/80', {'description': 'End', 'priority': 1})]
#[('10/50', {'description': 'Start', 'priority': 1}), ('20/60', {'description': 'Left', 'priority': 2}), (30, {'description': 'Right', 'priority': 2}), ('40/80', {'description': 'End', 'priority': 1})]
#[('10/50', {'description': 'Start', 'priority': 1}), ('20/60', {'description': 'Left', 'priority': 2}), ('30/70', {'description': 'Right', 'priority': 2}), ('40/80', {'description': 'End', 'priority': 1})]

关于python - 匹配两个节点并保持其他各自节点之间的路径 networkx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61424081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com