igraph
2024-05-09 本文已影响0人
hehehehe
from datetime import datetime
import igraph as ig
import networkx as nx
from tool import db_util, config_util
sql1 = f"""
select link_id,snode_id,enode_id,direction,st_astext(pts) as link_wkt
from sd_plus_link_tmp
where adcode = 110113 and link_type != 2
"""
dbcfg = config_util.get_cfg_by_key("gd_sdmap_20230616")
links = db_util.fetch_pg(dbcfg, sql1)
print(f"{len(links)=}")
def igraph_t():
start = datetime.now()
g = ig.Graph(directed=True)
# 首先,收集所有独特的节点ID
node_ids = set()
for link in links:
link_id, snode_id, enode_id, direction, link_wkt = link
node_ids.add(snode_id)
node_ids.add(enode_id)
node_ids_sorted = sorted(list(node_ids))
g.add_vertices(node_ids_sorted)
id_to_index = {nid: index for index, nid in enumerate(node_ids_sorted)}
edges_to_add = []
link_ids = []
for i, link in enumerate(links):
link_id, snode_id, enode_id, direction, link_wkt = link
src_index = id_to_index[snode_id]
dest_index = id_to_index[enode_id]
if direction == 1:
edges_to_add.append((src_index, dest_index))
elif direction == 2:
edges_to_add.append((dest_index, src_index))
link_ids.append(link_id)
g.add_edges(edges_to_add)
g.es['link_id'] = link_ids
node_id_to_query = 7550147220000001902
node_index_to_query = id_to_index[node_id_to_query]
in_incident_edges = g.es[g.incident(node_index_to_query, mode=ig.IN)]
out_incident_edges = g.es[g.incident(node_index_to_query, mode=ig.OUT)]
in_incident_link_ids = in_incident_edges['link_id']
out_incident_link_ids = out_incident_edges['link_id']
print(f'节点 {node_id_to_query} 的所有邻边 link_ids: {in_incident_link_ids}')
print(f'节点 {node_id_to_query} 的所有邻边 link_ids: {out_incident_link_ids}')
print((datetime.now() - start).total_seconds())
def nx_t():
start = datetime.now()
digraph = nx.DiGraph()
for i, link in enumerate(links):
link_id, snode_id, enode_id, direction, link_wkt = link
if direction == 2:
digraph.add_edge(enode_id, snode_id, link_id=link_id)
else:
digraph.add_edge(snode_id, enode_id, link_id=link_id)
in_edges = list(digraph.in_edges(7550147220000001902, data=True))
in_link_ids = [in_edge[2]['link_id'] for in_edge in in_edges]
print(f"{in_link_ids=}")
out_edges = list(digraph.out_edges(7550147220000001902, data=True))
out_link_ids = [out_edge[2]['link_id'] for out_edge in out_edges]
print(f"{out_link_ids=}")
print((datetime.now() - start).total_seconds())
if __name__ == '__main__':
igraph_t()
nx_t()
import igraph as ig
# 创建一个无向图示例
g = ig.Graph()
g.add_vertices(6) # 添加6个顶点
g.add_edges([
(0, 1), (1, 2), # 第一个连通分量
(3, 4), (4, 5) # 第二个连通分量
])
# 查找图中的连通分量
clustering = g.clusters()
# 打印连通分量的数量
print(f"连通分量的数量: {len(clustering)}")
# 遍历每个连通分量并打印其中的顶点
for i, component in enumerate(clustering):
print(f"连通分量 {i+1}: {component}")
import igraph as ig
# 创建图并添加属性
g = ig.Graph()
g.add_vertices(6)
g.add_edges([(0, 1), (0, 2), (3, 4), (4, 5)])
g.es['link_id'] = range(1, g.ecount() + 1) # 假设link_id从1开始递增
# 找到所有连通分量
components = g.clusters()
# 对于每个连通分量,收集属于该分量的边的link_id
connected_edges_link_ids = []
for component in components:
# 从连通分量中获取全部顶点ID
vertex_ids = component
# 获取该连通分量中所有边
edges = g.es.select(_within=vertex_ids)
# 收集边的link_id
link_ids = edges['link_id']
connected_edges_link_ids.append(link_ids)
# 打印每个连通分量的边的link_id
for i, link_ids in enumerate(connected_edges_link_ids):
print(f"连通分量 {i + 1} 的边的link_id: {link_ids}")