Compute#
relationalai.std.graphs
#class relationalai.std.graphs.Compute()
The Compute
class serves as a namespace for various graph algorithms.
This class is automatically instantiated when you create a Graph
object
and is accessible via the graph’s .compute
attribute.
It provides methods for computing basic graph statistics, centrality and similarity measures, community detection, and more.
Methods#
Basic Statistics#
Name | Description | Returns |
---|---|---|
.num_edges() | Get the number of edges in the graph. | Expression |
.num_nodes() | Get the number of nodes in the graph. | Expression |
Degree#
Name | Description | Returns |
---|---|---|
.degree(node) | Compute the degree of a node. | Expression |
.indegree(node) | Compute the indegree of a node. | Expression |
.outdegree(node) | Compute the outdegree of a node. | Expression |
Centrality Measures#
Name | Description | Returns |
---|---|---|
.betweenness_centrality(node) | Compute the betweenness centrality of a node. | Expression |
.degree_centrality(node) | Compute the degree centrality of a node. | Expression |
.eigenvector_centrality(node) | Compute the eigenvector centrality of the graph. | Expression |
.pagerank(node) | Compute the PageRank of a node. | Expression |
Similarity Measures#
Name | Description | Returns |
---|---|---|
.cosine_similarity(node1, node2) | Compute the cosine similarity between two nodes. | Expression |
.jaccard_similarity(node1, node2) | Compute the Jaccard similarity between two nodes. | Expression |
Link Prediction#
Name | Description | Returns |
---|---|---|
.adamic_adar(node1, node2) | Compute the Adamic-Adar index between two nodes. | Expression |
.common_neighbor(node1, node2) | Find the common neighbors between two nodes. | Expression |
.preferential_attachment(node1, node2) | Compute the preferential attachment score between two nodes. | Expression |
Community Detection#
Name | Description | Returns |
---|---|---|
.infomap() | Assign a community label to each node using the Infomap algorithm. | Expression |
.is_triangle(node1, node2, node3) | Check if three nodes form a triangle. | Expression |
.label_propagation(node) | Assign a community label to node using the label propagation algorithm. | Expression |
.louvain(node) | Assign a community label to node using the Louvain algorithm. | Expression |
.num_triangles() | Compute the number of triangles in the graph. | Expression |
.triangles() | Find all unique triangles in the graph. | tuple of three Expression objects. |
.triangle_community(node) | Assign a community label to node using the percolation method. | Expression |
Clustering#
Name | Description | Returns |
---|---|---|
.avg_clustering_coefficient() | Compute the average clustering coefficient of the graph. | Expression |
.local_clustering_coefficient(node) | Compute the local clustering coefficient of a node. | Expression |
Connectivity#
Name | Description | Returns |
---|---|---|
.is_connected() | Check if the graph is connected. | Expression |
.is_reachable(node1, node2) | Check if node2 is reachable from node1 . | Expression |
.reachable_from(node) | Find all nodes reachable from node . | Expression |
.weakly_connected_component(node) | Find the weakly connected component containing node . | Expression |
Distance#
Name | Description | Returns |
---|---|---|
.diameter_range() | Compute lower and upper bounds for the diameter of a graph. | tuple of two Expression objects. |
.distance(node1, node2) | Compute the shortest path length between two nodes. Ignores weights in weighted graphs. | Expression |
Example#
Graph algorithms are executed by calling the appropriate method from a Graph
object’s
.compute
attribute.
The following example demonstrates how to compute the PageRank of each person in a social network graph:
#import relationalai as rai
from relationalai.std import alias
from relationalai.std.graphs import Graph
# Create a model named "socialNetwork" with a Person type.
model = rai.Model("socialNetwork")
Person = model.Type("Person")
# Add some people to the model and connect them with a multi-valued 'friends' property.
with model.rule():
alice = Person.add(name="Alice")
bob = Person.add(name="Bob")
carol = Person.add(name="Carol")
daniel = Person.add(name="Daniel")
alice.friends.extend([bob, carol])
carol.friends.add(daniel)
# Create an undirected graph with Person nodes and edges from people to their friends.
# This graph has three edges: one from Alice to Bob, Alice to Carol, and Carol to Daniel.
graph = Graph(model, undirected=True)
graph.Node.extend(Person)
graph.Edge.extend(Person.friends)
with model.query() as select:
# Get all person objects.
person = Person()
# Compute the PageRank of each person.
pagerank = graph.compute.pagerank(person)
# Select the each person's name and their PageRank value.
response = select(person.name, alias(pagerank, "pagerank"))
print(response.results)
# Output:
# name pagerank
# 0 Alice 0.324562
# 1 Bob 0.175438
# 2 Carol 0.324562
# 3 Daniel 0.175438
See the documentation for each method in the Methods section for more examples.