最近研究知识图谱相关的构建与推理应用,一个坑的问题在于neo4j中可以存储图数据,查询与可视化也做得不错,但是计算功能比较弱,而neo4j的查询结果不能直接导入networkx进行计算,需要根据查询结果构建图ε=(´ο`*)))
1. networkx创建图
import networkx as nx
G = nx.Graph()
G.add_node(node)
G.add_edge(link[0], link[1])
2. networkx常用计算
- 获取节点/边列表
G.nodes()
G.edges()
- 获取节点的度/入度、出度(有向图)
G.degree(node_id)
G.in_degree(node_id)
- 获取连通分量数量、连通分量的节点列表
nx.number_connected_components(G)
nx.connected_components(G)
- 获取 连通分量图
for c in nx.connected_components(G):
g = G.subgraph(c)