Running Graph Algorithms
Tutorial
Running Graph Algorithms
Execute centrality, community detection, and pathfinding algorithms
What You'll Learn
- Algorithm Categories - Understand centrality, community, pathfinding
- Running Algorithms - Execute algorithms via SDK
- Accessing Results - Query algorithm results from graph properties
- NetworkX Integration - Use 100+ NetworkX algorithms
# Cell 1 — ParametersUSERNAME = "_FILL_ME_IN_" # Set your email before running# Cell 2 — Connectfrom graph_olap import GraphOLAPClientclient = GraphOLAPClient(username=USERNAME)
# Cell 3 — Provisionfrom notebook_setup import provisionpersonas, conn = provision(USERNAME)analyst = personas["analyst"]admin = personas["admin"]ops = personas["ops"]client = analyst
print(f"Connected | {conn.query_scalar('MATCH (n) RETURN count(n)')} nodes")
1
Algorithm Overview
Categories and when to use them
# Run PageRank to find the most important customersresult = conn.algo.pagerank( node_label="Customer", property_name="pagerank_score", edge_type="SHARES_ACCOUNT",)print(f"Status: {result.status}, Nodes updated: {result.nodes_updated}")
# Query results — scores are stored as node propertiestop_customers = conn.query(""" MATCH (c:Customer) RETURN c.id AS name, round(c.pagerank_score, 4) AS score ORDER BY c.pagerank_score DESC""")top_customers.show()
2
Centrality Algorithms
Find important nodes
# Run Louvain community detectionresult = conn.algo.louvain( node_label="Customer", property_name="community_id", edge_type="SHARES_ACCOUNT",)print(f"Status: {result.status}, Nodes updated: {result.nodes_updated}")
# Find community assignmentscommunities = conn.query(""" MATCH (c:Customer) RETURN c.community_id AS community, collect(c.id) AS members ORDER BY community""")communities.show()
3
Community Detection
Discover clusters and groups
# NetworkX algorithms — extended analytics library# Degree centrality: normalized count of connectionsresult = conn.networkx.degree_centrality( node_label="Customer", property_name="degree_cent",)print(f"Degree centrality: {result.status}")
# Betweenness centrality: how often a node bridges pathsresult = conn.networkx.betweenness_centrality( node_label="Customer", property_name="betweenness",)print(f"Betweenness centrality: {result.status}")
# Clustering coefficient: how connected a node's neighbors areresult = conn.networkx.clustering_coefficient( node_label="Customer", property_name="clustering",)print(f"Clustering coefficient: {result.status}")
# Query the NetworkX resultsdf = conn.query_df(""" MATCH (p:Customer) RETURN p.id AS name, round(p.degree_cent, 4) AS degree, round(p.betweenness, 4) AS betweenness, round(p.clustering, 4) AS clustering ORDER BY p.degree_cent DESC""")df
4
NetworkX Algorithms
Extended algorithm library
# Compare multiple centrality measures side by sidedf = conn.query_df(""" MATCH (c:Customer) RETURN c.id AS name, round(c.pagerank_score, 3) AS pagerank, round(c.degree_cent, 3) AS degree, round(c.betweenness, 3) AS betweenness, round(c.clustering, 3) AS clustering ORDER BY c.pagerank_score DESC""")
dfKey Takeaways
- Centrality algorithms rank node importance (
conn.algo.pagerank(),conn.networkx.betweenness_centrality()) - Community detection finds clusters (
conn.algo.louvain()) - Results stored as node properties, queryable via Cypher
- NetworkX provides additional centrality and structural algorithms via
conn.networkx.*