Running Graph Algorithms
Use RelationalAI’s (RAI) built-in graph algorithms to explore structure and reveal hidden patterns in your data.
How to Run a Graph Algorithm
Section titled “How to Run a Graph Algorithm”Follow these steps to run a graph algorithm in RAI:
-
Create a graph.
For example, consider a graph where
Person
andProduct
entities are connected by an edge whenever aPerson
has purchased aProduct
:import relationalai as raifrom relationalai.std import graphs# Define a model.model = rai.Model("StoreSales")# Declare entity types.Person = model.Type("Person")Product = model.Type("Product")# Declare relationship typesPurchase = model.Type("Purchase")Purchase.customer.declare()Purchase.item.declare()14 collapsed lines# Define sample data.with model.rule():Person.add(name="Alice")Person.add(name="Bob")Person.add(name="Carol")with model.rule():alice = Person(name="Alice")Purchase.add(customer=alice, item=Product.add(description="Flashlight"))Purchase.add(customer=alice, item=Product.add(description="Batteries"))with model.rule():bob = Person(name="Bob")Purchase.add(customer=bob, item=Product.add(description="Batteries"))# Create a graph object from the model.graph = graphs.Graph(model)# Add edges to the graph from Purchase entities.with model.rule():purchase = Purchase()graph.Edge.add(from_=purchase.customer, to=purchase.item) -
Call the algorithm’s method from the graph’s
.compute
namespace.Instances of the
Graph
class have a.compute
namespace that provides access to all of the algorithms and other methods that can be computed on a graph:import relationalai as raifrom relationalai.std import alias, graphs # Edited34 collapsed lines# Define a model.model = rai.Model("StoreSales")# Declare entity types.Person = model.Type("Person")Product = model.Type("Product")# Declare relationship typesPurchase = model.Type("Purchase")Purchase.customer.declare()Purchase.item.declare()# Define sample data.with model.rule():Person.add(name="Alice")Person.add(name="Bob")Person.add(name="Carol")with model.rule():alice = Person(name="Alice")Purchase.add(customer=alice, item=Product.add(description="Flashlight"))Purchase.add(customer=alice, item=Product.add(description="Batteries"))with model.rule():bob = Person(name="Bob")Purchase.add(customer=bob, item=Product.add(description="Batteries"))# Create a graph object from the model.graph = graphs.Graph(model)# Add edges to the graph from Purchase entities.with model.rule():purchase = Purchase()graph.Edge.add(from_=purchase.customer, to=purchase.item)# Who has a similar purchase history to Alice?with model.query() as select:person = Person()person.name != "Alice"score = graph.compute.jaccard_similarity(person, Person(name="Alice"))response = select(person.name,alias(score, "similarity_to_alice"))print(response.results)# name similarity_to_alice# 0 Bob 0.5Not all graph algorithms are available for all types of graphs. For instance, the Louvain community detection algorithm is only available for undirected graphs and will raise an error if called from a directed graph:
with model.query() as select:node = graph.Node()# The following raises a DirectedGraphNotSupported error.community_id = graph.compute.louvain(node)response = select(node, community_id)Some algorithms, such as PageRank, may be fine-tuned using additional parameters to control the algorithm’s behavior:
with model.query() as select:node = graph.Node()# Calculate the PageRank of each node in the graph with a damping factor of 0.8.pagerank = graph.compute.page_rank(node, damping_factor=0.8)response = select(node, pagerank)Refer to the reference docs for details on the parameters they accept and the graph types they support. The full list of available algorithms is provided below.
Available Algorithms
Section titled “Available Algorithms”The following algorithms are available in the Graph.compute
namespace:
Basic Statistics
Section titled “Basic Statistics”Name | Returns | Description |
---|---|---|
.num_edges() | Expression | Get the number of edges in the graph. |
.num_nodes() | Expression | Get the number of nodes in the graph. |
Degree
Section titled “Degree”Name | Returns | Description |
---|---|---|
.degree() | Expression | Compute the degree of a node. |
.indegree() | Expression | Compute the indegree of a node. |
.outdegree() | Expression | Compute the outdegree of a node. |
Centrality Measures
Section titled “Centrality Measures”Name | Returns | Description |
---|---|---|
.betweenness_centrality() | Expression | Compute the betweenness centrality of a node. |
.degree_centrality() | Expression | Compute the degree centrality of a node. |
.eigenvector_centrality() | Expression | Compute the eigenvector centrality of the graph. |
.pagerank() | Expression | Compute the PageRank of a node. |
Ego-Networks
Section titled “Ego-Networks”Name | Returns | Description |
---|---|---|
.ego_network() Preview | tuple of two Expression objects | Get pairs of nodes from edges in the subgraph induced by all nodes within a specified distance from a given node. |
Similarity Measures
Section titled “Similarity Measures”Name | Returns | Description |
---|---|---|
.cosine_similarity() | Expression | Compute the cosine similarity between two nodes. |
.jaccard_similarity() | Expression | Compute the Jaccard similarity between two nodes. |
Link Prediction
Section titled “Link Prediction”Name | Returns | Description |
---|---|---|
.adamic_adar() Preview | Expression | Compute the Adamic-Adar index between two nodes. |
.common_neighbor() Preview | Expression | Find the common neighbors between two nodes. |
.preferential_attachment() Preview | Expression | Compute the preferential attachment score between two nodes. |
Community Detection
Section titled “Community Detection”Name | Returns | Description |
---|---|---|
.infomap() | Expression | Assign a community label to each node using the Infomap algorithm. |
.is_triangle() | Expression | Check if three nodes form a triangle. |
.label_propagation() | Expression | Assign a community label to node using the label propagation algorithm. |
.louvain() | Expression | Assign a community label to node using the Louvain algorithm. |
.num_triangles() | Expression | Compute the number of triangles in the graph. |
.triangles() | tuple of three Expression objects | Find all unique triangles in the graph. |
.triangle_community() | Expression | Assign a community label to node using the percolation method. |
Clustering
Section titled “Clustering”Name | Returns | Description |
---|---|---|
.avg_clustering_coefficient() | Expression | Compute the average clustering coefficient of the graph. |
.local_clustering_coefficient() | Expression | Compute the local clustering coefficient of a node. |
Connectivity
Section titled “Connectivity”Name | Returns | Description |
---|---|---|
.is_connected() Preview | Expression | Check if the graph is connected. |
.is_reachable() | Expression | Check if node2 is reachable from node1 . |
.reachable_from() | Expression | Find all nodes reachable from node . |
.weakly_connected_component() | Expression | Find the weakly connected component containing node . |
Distance
Section titled “Distance”Name | Returns | Description |
---|---|---|
.diameter_range() Preview | tuple of two Expression objects | Compute lower and upper bounds for the diameter of a graph. |
.distance() | Expression | Compute the shortest path length between two nodes. Ignores weights in weighted graphs. |