Skip to content

Overview

This document introduces you to using the Graph Library to perform operations on graphs.

All of the algorithms in the Graph Library are contained in the rel:graphlib module. Once you have created a graph, you can instantiate rel:graphlib on the graph to make available all of the library relations that perform operations and compute measures and statistics on that graph.

Instantiating rel:graphlib

To instantiate rel:graphlib, apply the library module to a graph module, such as a graph constructed using one of the create_graph, undirected_graph, or directed_graph constructors.

// Construct a directed graph with two nodes and one edge.
def my_graph = directed_graph[{(1, 2)}]
 
// Instantiate `rel:graphlib` on `my_graph`.
// The module should always be instantiated with `@inline`.
@inline
def my_graphlib = rel:graphlib[my_graph]

Always instantiate rel:graphlib with the @inline annotation. Without it, you may experience errors when using certain relations.

For the best performance, persist the instantiated rel:graphlib module in a model. See Performance Tips for more information.

Using Library Relations

Once rel:graphlib has been instantiated, you can run algorithms and perform analyses by using relations in the instantiated module. To use a relation, import the relation from the instantiated module (for example, with my_graphlib use degree) or use the relation’s qualified name (for example, my_graphlib:degree):

// read query
 
// Construct a directed graph with two nodes and one edge.
def my_graph = directed_graph[{(1, 2)}]
 
// Instantiate `rel:graphlib` on `my_graph`.
@inline
def my_graphlib = rel:graphlib[my_graph]
 
// Display the degree of each node in `my_graph`.
// Here, degree is accessed by its qualified name.
def output = my_graphlib:degree
 
// Alternatively, you may import the `degree` relation first.
with my_graphlib use degree
def output = degree

Learn more about the degree relation in the Degrees guide. The Neighbors guide describes how to work with the Graph Library’s neighbor relations.

Performance Tips

Here are two tips for getting the best performance out of the Graph Library’s relations:

  1. Persist the graph module and instantiated rel:graphlib module to a model. When algorithms are run, the materialized relations are persisted so that they do not have to be recalculated for each query. See Create a Graph From a CSV File for an example.

    Installing the library module has another advantage: If the base data for the graph changes, any materialized library relations can be updated incrementally, allowing updates to stored computations to finish much faster in some cases. See Ingestion Workflows: ELT for details.

  2. Use integer nodes. Many of the algorithms implemented by the Graph Library perform better when all of the nodes in a graph are integers. See Graphs: Performance Tips for details on mapping nodes to integers.

Was this doc helpful?