Many modern business problems involve connections and relationships between entities, and are not solely based on discrete data. Graphs are powerful at representing complex interconnections, and graph data modeling is very effective and flexible when the number and depth of relationships increase exponentially.

The use cases for graph analytics are diverse: social networks, transportation routes, autonomous vehicles, cyber security, criminal networks, fraud detection, health research, epidemiology, and so forth.

This chapter contains the following information:

What is a Graph?

Graphs represent the interconnections between objects (vertices) and their relationships (edges). Example objects could be people, locations, cities, computers, or components on a circuit board. Example connections could be roads, circuits, cables, or interpersonal relationships. Edges can have directions and weights, for example the distance between towns.

Graph connection example

Graphs can be small and easily traversed - as with a small group of friends - or extremely large and complex, similar to contacts in a modern-day social network.

Graph Analytics on Greenplum

Efficient processing of very large graphs can be challenging. Greenplum offers a suitable environment for this work for these key reasons:

  1. Using MADlib graph functions in Greenplum brings the graph computation close to where the data lives. Otherwise, large data sets need to be moved to a specialized graph database, requiring additional time and resources.
  2. Specialized graph databases frequently use purpose-built languages. With Greenplum, you can invoke graph functions using the familiar SQL interface. For example, for the PageRank graph algorithm:

    1. SELECT madlib.pagerank('vertex', -- Vertex table
    2. 'id', -- Vertex id column
    3. 'edge', -- Edge table
    4. 'src=src, dest=dest', -- Comma delimited string of edge arguments
    5. 'pagerank_out', -- Output table of PageRank
    6. 0.5); -- Damping factor
    7. SELECT * FROM pagerank_out ORDER BY pagerank DESC;
  3. A lot of data science problems are solved using a combination of models, with graphs being just one. Regression, clustering, and other methods available in Greenplum, make for a powerful combination.

  4. Greenplum offers great benefits of scale, taking advantage of years of query execution and optimization research focused on large data sets.

Using Graph

Installing Graph Modules

To use the MADlib graph modules, install the version of MADlib corresponding to your Greenplum Database version. To download the software, access the VMware Tanzu Network. For Greenplum 6.x, see Installing MADlib.

Graph modules on MADlib support many algorithms.

Creating a Graph in Greenplum

To represent a graph in Greenplum, create tables that represent the vertices, edges, and their properties.

Vertex edge table

Using SQL, create the relevant tables in the database you want to use. This example uses testdb:

  1. gpadmin@mdw ~]$ psql
  2. dev=# \c testdb

Create a table for vertices, called vertex, and a table for edges and their weights, called edge:

  1. testdb=# DROP TABLE IF EXISTS vertex, edge;
  2. testdb=# CREATE TABLE vertex(id INTEGER);
  3. testdb=# CREATE TABLE edge(
  4. src INTEGER,
  5. dest INTEGER,
  6. weight FLOAT8
  7. );

Insert values related to your specific use case. For example :

  1. testdb#=> INSERT INTO vertex VALUES
  2. (0),
  3. (1),
  4. (2),
  5. (3),
  6. (4),
  7. (5),
  8. (6),
  9. (7);
  10. testdb#=> INSERT INTO edge VALUES
  11. (0, 1, 1.0),
  12. (0, 2, 1.0),
  13. (0, 4, 10.0),
  14. (1, 2, 2.0),
  15. (1, 3, 10.0),
  16. (2, 3, 1.0),
  17. (2, 5, 1.0),
  18. (2, 6, 3.0),
  19. (3, 0, 1.0),
  20. (4, 0, -2.0),
  21. (5, 6, 1.0),
  22. (6, 7, 1.0);

Now select the Graph Module that suits your analysis.

Graph Modules

This section lists the graph functions supported in MADlib. They include: All Pairs Shortest Path (APSP), Breadth-First Search, Hyperlink-Induced Topic Search (HITS), PageRank and Personalized PageRank, Single Source Shortest Path (SSSP), Weakly Connected Components, and Measures. Explore each algorithm using the example edge and vertex tables already created.

All Pairs Shortest Path (APSP)

The all pairs shortest paths (APSP) algorithm finds the length (summed weights) of the shortest paths between all pairs of vertices, such that the sum of the weights of the path edges is minimized.

The function is:

  1. graph_apsp( vertex_table,
  2. vertex_id,
  3. edge_table,
  4. edge_args,
  5. out_table,
  6. grouping_cols
  7. )

For details on the parameters, with examples, see the All Pairs Shortest Path in the Apache MADlib documentation.

Given a graph and a source vertex, the breadth-first search (BFS) algorithm finds all nodes reachable from the source vertex by searching / traversing the graph in a breadth-first manner.

The function is:

  1. graph_bfs( vertex_table,
  2. vertex_id,
  3. edge_table,
  4. edge_args,
  5. source_vertex,
  6. out_table,
  7. max_distance,
  8. directed,
  9. grouping_cols
  10. )

For details on the parameters, with examples, see the Breadth-First Search in the Apache MADlib documentation.

The all pairs shortest paths (APSP) algorithm finds the length (summed weights) of the shortest paths between all pairs of vertices, such that the sum of the weights of the path edges is minimized.

The function is:

  1. graph_apsp( vertex_table,
  2. vertex_id,
  3. edge_table,
  4. edge_args,
  5. out_table,
  6. grouping_cols
  7. )

For details on the parameters, with examples, see the Hyperlink-Induced Topic Search in the Apache MADlib documentation.

PageRank and Personalized PageRank

Given a graph, the PageRank algorithm outputs a probability distribution representing a person’s likelihood to arrive at any particular vertex while randomly traversing the graph.

MADlib graph also includes a personalized PageRank, where a notion of importance provides personalization to a query. For example, importance scores can be biased according to a specified set of graph vertices that are of interest or special in some way.

The function is:

  1. pagerank( vertex_table,
  2. vertex_id,
  3. edge_table,
  4. edge_args,
  5. out_table,
  6. damping_factor,
  7. max_iter,
  8. threshold,
  9. grouping_cols,
  10. personalization_vertices
  11. )

For details on the parameters, with examples, see the PageRank in the Apache MADlib documentation.

Single Source Shortest Path (SSSP)

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.

The function is:

  1. graph_sssp ( vertex_table,
  2. vertex_id,
  3. edge_table,
  4. edge_args,
  5. source_vertex,
  6. out_table,
  7. grouping_cols
  8. )

For details on the parameters, with examples, see the Single Source Shortest Path in the Apache MADlib documentation.

Weakly Connected Components

Given a directed graph, a weakly connected component (WCC) is a subgraph of the original graph where all vertices are connected to each other by some path, ignoring the direction of edges.

The function is:

  1. weakly_connected_components(
  2. vertex_table,
  3. vertex_id,
  4. edge_table,
  5. edge_args,
  6. out_table,
  7. grouping_cols
  8. )

For details on the parameters, with examples, see the Weakly Connected Components in the Apache MADlib documentation.

Measures

These algorithms relate to metrics computed on a graph and include: Average Path Length, Closeness Centrality , Graph Diameter, and In-Out Degree.

Average Path Length

This function computes the shortest path average between pairs of vertices. Average path length is based on “reachable target vertices”, so it averages the path lengths in each connected component and ignores infinite-length paths between unconnected vertices. If the user requires the average path length of a particular component, the weakly connected components function may be used to isolate the relevant vertices.

The function is:

  1. graph_avg_path_length( apsp_table,
  2. output_table
  3. )

This function uses a previously run APSP (All Pairs Shortest Path) output. For details on the parameters, with examples, see the Average Path Length in the Apache MADlib documentation.

Closeness Centrality

The closeness centrality algorithm helps quantify how much information passes through a given vertex. The function returns various closeness centrality measures and the k-degree for a given subset of vertices.

The function is:

  1. graph_closeness( apsp_table,
  2. output_table,
  3. vertex_filter_expr
  4. )

This function uses a previously run APSP (All Pairs Shortest Path) output. For details on the parameters, with examples, see the Closeness in the Apache MADlib documentation.

Graph Diameter

Graph diameter is defined as the longest of all shortest paths in a graph. The function is:

  1. graph_diameter( apsp_table,
  2. output_table
  3. )

This function uses a previously run APSP (All Pairs Shortest Path) output. For details on the parameters, with examples, see the Graph Diameter in the Apache MADlib documentation.

In-Out Degree

This function computes the degree of each node. The node degree is the number of edges adjacent to that node. The node in-degree is the number of edges pointing in to the node and node out-degree is the number of edges pointing out of the node.

The function is:

  1. graph_vertex_degrees( vertex_table,
  2. vertex_id,
  3. edge_table,
  4. edge_args,
  5. out_table,
  6. grouping_cols
  7. )

For details on the parameters, with examples, see the In-out Degree page in the Apache MADlib documentation.

References

MADlib on Greenplum is at Machine Learning and Deep Learning using MADlib.

MADlib Apache web site and MADlib release notes are at http://madlib.apache.org/.

MADlib user documentation is at http://madlib.apache.org/documentation.html.