This library provides NetworkX API for Neo4j Graph Data Science. You should be able to use it as you would NetworkX but algorithms will run against Neo4j.
Here’s how you use it.
First let’s import our libraries and create an instance of the Neo4j driver:
>>> from neo4j import GraphDatabase
>>> import nxneo4j as nx
>>> driver = GraphDatabase.driver(uri="bolt://localhost",auth=("neo4j","your_password"))
For undirected Graphs:
>>> G = nx.Graph(driver)
For directed Graphs:
>>> G = nx.DiGraph(driver)
The available functions in nxneo4j
are:
# ADD ONE NODE
G.add_node(node)
node: str, int
>>> G.add_node(1)
# ADD MULTIPLE NODES
G.add_nodes_from(value)
values: list
>>> G.add_nodes_from([1, 2, 3, 4])
# ADD ONE EDGE
G.add_edge(node1,node2)
node1: str, int
node2: str, int
>>> G.add_edge(1,2)
#ADD MULTIPLE EDGES
G.add_edges_from(values)
values: list of tuples
>>> G.add_edges_from([(1, 2),(2, 3),(3, 4)])
The available algoritms in nxneo4j
are:
>>> nx.betweenness_centrality(G)
{3: 4.0, 4: 3.0, 1: 0.0, 2: 0.0, 5: 0.0}
>>> nx.closeness_centrality(G)
{3: 0.8, 4: 0.6666666666666666, 1: 0.5714285714285714, 2: 0.5714285714285714, 5: 0.4444444444444444}
>>> nx.pagerank(G)
{3: 1.4170146573314513, 4: 1.0629939728840803, 1: 0.9591085771210682, 2: 0.9591085771210682, 5: 0.6017724112363687}
>>> nx.triangles(G)
{1: 1, 2: 1, 3: 1, 4: 0, 5: 0}
>>> nx.clustering(G)
{1: 1.0, 2: 1.0, 3: 0.3333333333333333, 4: 0.0, 5: 0.0}
>>> list(nx.community.label_propagation_communities(G))
[{1, 2, 3, 4, 5}]
>>> nx.shortest_path(G, source=1, target=5)
[1, 3, 4, 5]