1.10.0
User Documentation for MADlib
Single Source Shortest Path

Given a graph and a source vertex, the single source shortest path (SSSP) algorithm finds a path from the source vertex to every other vertex in the graph, such that the sum of the weights of the path edges is minimized.

SSSP
graph_sssp( vertex_table,
            vertex_id,
            edge_table,
            edge_args,
            source_vertex,
            out_table
          )

Arguments

vertex_table

TEXT. Name of the table containing the vertex data for the graph. Must contain the column specified in the 'vertex_id' parameter below.

vertex_id

TEXT, default = 'id'. Name of the column in 'vertex_table' containing vertex ids. The vertex ids are of type INTEGER with no duplicates. They do not need to be contiguous.

edge_table

TEXT. Name of the table containing the edge data. The edge table must contain columns for source vertex, destination vertex and edge weight. Column naming convention is described below in the 'edge_args' parameter.

edge_args

TEXT. A comma-delimited string containing multiple named arguments of the form "name=value". The following parameters are supported for this string argument:

  • src (INTEGER): Name of the column containing the source vertex ids in the edge table. Default column name is 'src'.
  • dest (INTEGER): Name of the column containing the destination vertex ids in the edge table. Default column name is 'dest'.
  • weight (FLOAT8): Name of the column containing the edge weights in the edge table. Default column name is 'weight'.

source_vertex

INTEGER. The source vertex id for the algorithm to start. This vertex id must exist in the 'vertex_id' column of 'vertex_table'.

out_table
TEXT. Name of the table to store the result of SSSP. It will contain a row for every vertex from 'vertex_table' and have the following columns:
  • vertex_id : The id for the destination. Will use the input parameter 'vertex_id' for column naming.
  • weight : The total weight of the shortest path from the source vertex to this particular vertex. Will use the input parameter (weight) for column naming.
  • parent : The parent of this vertex in the shortest path from source. Will use 'parent' for column naming.
Path Retrieval

The path retrieval function returns the shortest path from the source vertex to a specified desination vertex.

graph_sssp( sssp_table,
            dest_vertex
          )

Arguments

sssp_table

TEXT. Name of the table that contains the SSSP output.

dest_vertex
INTEGER. The vertex that will be the destination of the desired path.

Notes

The Bellman-Ford algorithm [1] is used to implement SSSP. This algorithm allows negative edges but not negative cycles. In the case of graphs with negative cycles, an error will be given and no output table will be generated.

Also see the Grail project [2] for more background on graph analytics processing in relational databases.

Examples
  1. Create vertex and edge tables to represent the graph:
    DROP TABLE IF EXISTS vertex, edge;
    CREATE TABLE vertex(
            id INTEGER
            );
    CREATE TABLE edge(
            src INTEGER,
            dest INTEGER,
            weight FLOAT8
            );
    INSERT INTO vertex VALUES
    (0),
    (1),
    (2),
    (3),
    (4),
    (5),
    (6),
    (7);
    INSERT INTO edge VALUES
    (0, 1, 1.0),
    (0, 2, 1.0),
    (0, 4, 10.0),
    (1, 2, 2.0),
    (1, 3, 10.0),
    (2, 3, 1.0),
    (2, 5, 1.0),
    (2, 6, 3.0),
    (3, 0, 1.0),
    (4, 0, -2.0),
    (5, 6, 1.0),
    (6, 7, 1.0);
    
  2. Calculate the shortest paths from vertex 0:
    DROP TABLE IF EXISTS out;
    SELECT madlib.graph_sssp(
                             'vertex',      -- Vertex table
                             NULL,          -- Vertix id column (NULL means use default naming)
                             'edge',        -- Edge table
                             NULL,          -- Edge arguments (NULL means use default naming)
                             0,             -- Source vertex for path calculation
                             'out');        -- Output table of shortest paths
    SELECT * FROM out ORDER BY id;
    
     id | weight | parent
    ----+--------+--------
      0 |      0 |      0
      1 |      1 |      0
      2 |      1 |      0
      3 |      2 |      2
      4 |     10 |      0
      5 |      2 |      2
      6 |      3 |      5
      7 |      4 |      6
    (8 rows)
    
  3. Get the shortest path to vertex 6:
    SELECT madlib.graph_sssp_get_path('out',6) AS spath;
    
       spath
    -----------
     {0,2,5,6}
    
  4. Now let's do a similar example except using different column names in the tables (i.e., not the defaults). Create the vertex and edge tables:
    DROP TABLE IF EXISTS vertex_alt, edge_alt;
    CREATE TABLE vertex_alt AS SELECT id AS v_id FROM vertex;
    CREATE TABLE edge_alt AS SELECT src AS e_src, dest, weight AS e_weight FROM edge;
    
  5. Get the shortest path from vertex 1:
    DROP TABLE IF EXISTS out_alt;
    SELECT madlib.graph_sssp(
                             'vertex_alt',                  -- Vertex table
                             'v_id',                        -- Vertix id column (NULL means use default naming)
                             'edge_alt',                    -- Edge table
                             'src=e_src, weight=e_weight',  -- Edge arguments (NULL means use default naming)
                             1,                             -- Source vertex for path calculation
                             'out_alt');                    -- Output table of shortest paths
    SELECT * FROM out_alt ORDER BY v_id;
    
     v_id | e_weight | parent
    ------+----------+--------
        0 |        4 |      3
        1 |        0 |      1
        2 |        2 |      1
        3 |        3 |      2
        4 |       14 |      0
        5 |        3 |      2
        6 |        4 |      5
        7 |        5 |      6
    (8 rows)
    

Literature

[1] Bellman–Ford algorithm. https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm

[2] The case against specialized graph analytics engines, J. Fan, G. Soosai Raj, and J. M. Patel. CIDR 2015. http://cidrdb.org/cidr2015/Papers/CIDR15_Paper20.pdf