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#

NameDescriptionReturns
.num_edges()Get the number of edges in the graph.Expression
.num_nodes()Get the number of nodes in the graph.Expression

Degree#

NameDescriptionReturns
.avg_degree()Compute the average degree of the graph.Expression
.avg_indegree()Compute the average indegree of a directed graph. Alias for .avg_degree() in undirected graphs.Expression
.avg_outdegree()Compute the average outdegree of a directed graph. Alias for .avg_degree() in undirected graphs.Expression
.degree(node)Compute the degree of a node.Expression
.indegree(node)Compute the indegree of a node.Expression
.max_degree()Compute the maximum degree of a graph.Expression
.max_indegree()Compute the maximum indegree a directed graph. Alias for .max_degree() in undirected graphs.Expression
.max_outdegree()Compute the maximum outdegree of a directed graph graph. Alias for .max_degree() in undirected graphs.Expression
.min_degree()Compute the minimum degree of a graph.Expression
.min_indegree()Compute the minimum indegree of a directed graph. Alias for .min_degree() in undirected graphs.Expression
.min_outdegree()Compute the minimum outdegree of a directed graph. Alias for .min_degree() in undirected graphs.Expression
.outdegree(node)Compute the outdegree of a node.Expression

Centrality Measures#

NameDescriptionReturns
.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
.weighted_degree_centrality(node)Compute the weighted degree centrality of a node. Alias for degree_centrality in unweighted graphs.Expression

Similarity Measures#

NameDescriptionReturns
.cosine_similarity(node1, node2)Compute the cosine similarity between two nodes.Expression
.jaccard_similarity(node1, node2)Compute the Jaccard similarity between two nodes.Expression
.weighted_cosine_similarity(node1, node2)Compute the weighted cosine similarity between two nodes. Alias for cosine_similarity in unweighted graphs. Only undirected graphs are supported.Expression
.weighted_jaccard_similarity(node1, node2)Compute the weighted Jaccard similarity between two nodes. Alias for jaccard_similarity() in unweighted graphs.Expression
NameDescriptionReturns
.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#

NameDescriptionReturns
.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#

NameDescriptionReturns
.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#

NameDescriptionReturns
.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#

NameDescriptionReturns
.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
.weighted_distance(node1, node2)Compute the shortest path length between two nodes in a weighted graph. Alias for distance in unweighted 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.

See Also#