networkx笔记

# 将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])

你可能感兴趣的:(networkx,python)