python - 匹配两个节点并保持其他各自节点之间的路径networkx
问题描述
已编辑
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
当我使用 Python 库将两个数据框转换为图形Netwokrx
时,我得到以下图形作为 df1 的 graph1 和 df2 的 graph2。节点的颜色基于它们的优先级。
我想匹配(组合)两个具有相似颜色的节点,例如 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
颜色的节点,依此类推。我希望像下面这样。
为了达到上述结果,我是这样尝试的。
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})]
推荐阅读
- r - 调整 R Markdown gt 输出的大小
- c++ - OpenCV C++:在没有 matchTemplate 的情况下实现标准化互相关 (TM_CCORR_NORMED)
- reactjs - Instagram - 兑换代币
- javascript - 尝试通过 React 对 websocket 的一个消息请求设置多个状态时导致浏览器滞后/锁定的原因是什么?
- yarnpkg - 如何使用 Yarn 2+ 列出每个公共工作区?
- c++ - 使用 C++14 使用正则表达式处理字符串
- python - Django __init__() 得到了一个意外的关键字参数“用户”
- powerbi - 对数据流使用 DirectQuery
- android - KMM:如何解决不兼容的 abi 版本。当前默认为“1.4.2”,找到“1.5.0”。1.5.20编译器产生的库?
- python - 获取用户输入然后输出