Skip to content

Running Graph Algorithms

Tutorial

Running Graph Algorithms

Execute centrality, community detection, and pathfinding algorithms

35 min Intermediate
AlgorithmsPageRankCommunityCentrality

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 — Parameters
USERNAME = "_FILL_ME_IN_" # Set your email before running
# Cell 2 — Connect
from graph_olap import GraphOLAPClient
client = GraphOLAPClient(username=USERNAME)
# Cell 3 — Provision
from notebook_setup import provision
personas, 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 customers
result = 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 properties
top_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 detection
result = 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 assignments
communities = 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 connections
result = conn.networkx.degree_centrality(
node_label="Customer",
property_name="degree_cent",
)
print(f"Degree centrality: {result.status}")
# Betweenness centrality: how often a node bridges paths
result = conn.networkx.betweenness_centrality(
node_label="Customer",
property_name="betweenness",
)
print(f"Betweenness centrality: {result.status}")
# Clustering coefficient: how connected a node's neighbors are
result = conn.networkx.clustering_coefficient(
node_label="Customer",
property_name="clustering",
)
print(f"Clustering coefficient: {result.status}")
# Query the NetworkX results
df = 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 side
df = 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
""")
df

Key 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.*