# 将sorce、target生成图,找出最大深度
pipe_gid_sql = self.repair_sql.pipe_data_select(table_name='dt_pipeline', projectid=self.project_id)
pipe_res = self.query(pipe_gid_sql)
pipe_data = pd.DataFrame(pipe_res)
source_list = pipe_data['source'].tolist()
target_list = pipe_data['target'].tolist()
source_target_list = list(zip(tuple(source_list), tuple(target_list)))
self.sub_graph.add_edges_from(source_target_list)
depth_max = len(min(nx.connected_components(self.sub_graph), key=len))
print(depth_max)
# 查看某个点的相邻点
for i in self.graph.neighbors(pipe_source):
print(i)
# nx.from_pandas_edgelist用法(输入source,返回以target为key,其他为values的值)
import pandas as pd
import networkx as nx
def graph_connection(G):
"""孤立区域"""
return sorted(nx.connected_components(G), key=len, reverse=True)
edges = pd.DataFrame(
{
"gid": [5, 6, 8, 1],
"source": [0, 1, 7, 9],
"target": [1, 8, 3, 2],
"aaaa": [88, 66, 99, 77]
}
)
G = nx.from_pandas_edgelist(
edges,
edge_attr=["gid", "source", "target"],
)
sub_g_edges_ = [G.subgraph(com_set) for com_set in graph_connection(G)]
print(graph_connection(G))
print(sub_g_edges_)
for i, j in enumerate(sub_g_edges_):
print(j.nodes)
if 1 in j.nodes:
print(pd.DataFrame(G.subgraph(j.nodes).edges(data=True)).iloc[:, -1].tolist())
break
print(edges)
print(G[0])
# 提取网络最大连通子图
Gc = max(nx.connected_component_subgraphs(graph), key=len)
# 获取所有的子图
for sub_graph in sorted(nx.connected_component_subgraphs(graph), key=len, reverse=True):
pass
networkx笔记
于 2022-11-28 17:37:06 首次发布
本文介绍了如何从数据管道中提取源目标对构建子图,并通过`nx.connected_components`找出最大连通组件的深度。还展示了如何使用`networkx`库处理边缘数据,以及如何提取最大连通子图和遍历邻居节点。
摘要由CSDN通过智能技术生成