Skip to content

Running Graph Algorithms

Use RelationalAI’s (RAI) built-in graph algorithms to explore structure and reveal hidden patterns in your data.

Follow these steps to run a graph algorithm in RAI:

  1. Create a graph.

    For example, consider a graph where Person and Product entities are connected by an edge whenever a Person has purchased a Product:

    import relationalai as rai
    from relationalai.std import graphs
    # Define a model.
    model = rai.Model("StoreSales")
    # Declare entity types.
    Person = model.Type("Person")
    Product = model.Type("Product")
    # Declare relationship types
    Purchase = 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)
  2. 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 rai
    from relationalai.std import alias, graphs # Edited
    34 collapsed lines
    # Define a model.
    model = rai.Model("StoreSales")
    # Declare entity types.
    Person = model.Type("Person")
    Product = model.Type("Product")
    # Declare relationship types
    Purchase = 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.5

    Not 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.

The following algorithms are available in the Graph.compute namespace:

NameReturnsDescription
.num_edges()ExpressionGet the number of edges in the graph.
.num_nodes()ExpressionGet the number of nodes in the graph.
NameReturnsDescription
.degree()ExpressionCompute the degree of a node.
.indegree()ExpressionCompute the indegree of a node.
.outdegree()ExpressionCompute the outdegree of a node.
NameReturnsDescription
.betweenness_centrality()ExpressionCompute the betweenness centrality of a node.
.degree_centrality()ExpressionCompute the degree centrality of a node.
.eigenvector_centrality()ExpressionCompute the eigenvector centrality of the graph.
.pagerank()ExpressionCompute the PageRank of a node.
NameReturnsDescription
.ego_network() Previewtuple of two Expression objectsGet pairs of nodes from edges in the subgraph induced by all nodes within a specified distance from a given node.
NameReturnsDescription
.cosine_similarity()ExpressionCompute the cosine similarity between two nodes.
.jaccard_similarity()ExpressionCompute the Jaccard similarity between two nodes.
NameReturnsDescription
.adamic_adar() PreviewExpressionCompute the Adamic-Adar index between two nodes.
.common_neighbor() PreviewExpressionFind the common neighbors between two nodes.
.preferential_attachment() PreviewExpressionCompute the preferential attachment score between two nodes.
NameReturnsDescription
.infomap()ExpressionAssign a community label to each node using the Infomap algorithm.
.is_triangle()ExpressionCheck if three nodes form a triangle.
.label_propagation()ExpressionAssign a community label to node using the label propagation algorithm.
.louvain()ExpressionAssign a community label to node using the Louvain algorithm.
.num_triangles()ExpressionCompute the number of triangles in the graph.
.triangles()tuple of three Expression objectsFind all unique triangles in the graph.
.triangle_community()ExpressionAssign a community label to node using the percolation method.
NameReturnsDescription
.avg_clustering_coefficient()ExpressionCompute the average clustering coefficient of the graph.
.local_clustering_coefficient()ExpressionCompute the local clustering coefficient of a node.
NameReturnsDescription
.is_connected() PreviewExpressionCheck if the graph is connected.
.is_reachable()ExpressionCheck if node2 is reachable from node1.
.reachable_from()ExpressionFind all nodes reachable from node.
.weakly_connected_component()ExpressionFind the weakly connected component containing node.
NameReturnsDescription
.diameter_range() Previewtuple of two Expression objectsCompute lower and upper bounds for the diameter of a graph.
.distance()ExpressionCompute the shortest path length between two nodes. Ignores weights in weighted graphs.