Longest Path for DAG

Glossary

Directed

Directed trait. The algorithm is well-defined on a directed graph.

Directed

Directed trait. The algorithm ignores the direction of the graph.

Directed

Directed trait. The algorithm does not run on a directed graph.

Undirected

Undirected trait. The algorithm is well-defined on an undirected graph.

Undirected

Undirected trait. The algorithm ignores the undirectedness of the graph.

Heterogeneous nodes

Heterogeneous nodes fully supported. The algorithm has the ability to distinguish between nodes of different types.

Heterogeneous nodes

Heterogeneous nodes allowed. The algorithm treats all selected nodes similarly regardless of their label.

Heterogeneous relationships

Heterogeneous relationships fully supported. The algorithm has the ability to distinguish between relationships of different types.

Heterogeneous relationships

Heterogeneous relationships allowed. The algorithm treats all selected relationships similarly regardless of their type.

Weighted relationships

Weighted trait. The algorithm supports a relationship property to be used as weight, specified via the relationshipWeightProperty configuration parameter.

Weighted relationships

Weighted trait. The algorithm treats each relationship as equally important, discarding the value of any relationship weight.

This feature is in the alpha tier. For more information on feature tiers, see API Tiers.

Introduction

Finding the longest path that leads to a node in a graph is possible to do in linear time for the special case of DAGs, that is graphs which do not contain cycles.

The GDS implementation for this problem is based on topological sort and operates in linear time. When the graph is not a DAG, any node that belongs to component containing at least one cycle will be excluded from the results. That is, the implementation will only give results for those components of the graph that form DAGs.

You can use topological sort to make sure the graph is a DAG.

The algorithm supports weighted and unweighted graphs. Negative weights are currently unsupported.

Usage

One example for usage of this algorithm is in the context of a supply chain graph. If edges indicate the time to supply, then the distance of the longest path to a target node is the time required to manufacture the node from decision to completion.

Syntax

This section covers the syntax used to execute the DAG Longest Path algorithm in each of its execution modes. We are describing the named graph variant of the syntax. To learn more about general syntax variants, see Syntax overview.

Example 1. Longest Path syntax per mode
Run DAG Longest Path in stream mode on a named graph.
CALL gds.dag.longestPath.stream(
  graphName: String,
  configuration: Map
) YIELD
  index: Integer,
  sourceNode: Integer,
  targetNode: Integer,
  totalCost: Float,
  nodeIds: List of Integer,
  costs: List of Float,
  path: Path
Table 1. Parameters
Name Type Default Optional Description

graphName

String

n/a

no

The name of a graph stored in the catalog.

configuration

Map

{}

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 2. Configuration
Name Type Default Optional Description

nodeLabels

List of String

['*']

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

['*']

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm.

jobId

String

Generated internally

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

true

yes

If disabled the progress percentage will not be logged.

relationshipWeightProperty

String

null

yes

Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted.

Table 3. Results
Name Type Description

index

Integer

0-based index of the found path.

sourceNode

Integer

Source node of the path.

targetNode

Integer

Target node of the path.

totalCost

Float

Total cost from source to target.

nodeIds

List of Integer

Node ids on the path in traversal order.

costs

List of Float

Accumulated costs for each node on the path.

path

Path

The path represented as Cypher entity.

Examples

In this section we will show examples of running the DAG Longest Path algorithm on a concrete graph. The intention is to illustrate what the results look like and to provide a guide in how to make use of the algorithm in a real setting. We will do this on a small supply chain graph of a handful nodes connected in a particular pattern. The example graph looks like this:

Visualization of the example graph
The following Cypher statement will create the example graph in the Neo4j database:
CREATE
       (n0:Goods {name: 'Timber'}),
       (n1:Goods {name: 'Lumber'}),
       (n2:Goods {name: 'Screws'}),
       (n3:Workshop {name: 'Table Maker'}),
       (n4:Product {name: 'Table'}),

       (n0)-[:Processing {time: 1}]->(n1),
       (n1)-[:Shipment {time: 0}]->(n3),
       (n2)-[:Shipment {time: 3}]->(n3),
       (n3)-[:Processing {time: 1}]->(n4)

This graph describes a simple supply chain of constructing a table in the Table Maker workshop. In order to have lumber for the table, the workshop processes timber, which takes 1 day to complete. Once the lumber is ready, it is already in the workshop, therefor it takes zero time to ship it. However, the screws take 3 days to be shipped to the workshop. Only after the workshop has all the requirements met, the table can be constructed, a process that takes 1 day.

The longest path to the table node starts with the screws, then the workshop and then the table, in total: 4 days. This is the bottleneck path, and total time that takes to manufacture the table.

The following Cypher statement will project the graph to GDS:
MATCH (n)
OPTIONAL MATCH (n)-[r:Processing|Shipment]->(target)
RETURN gds.graph.project("g", n, target, {relationshipProperties: r {.time}})

Stream

The stream procedure streams every node in the graph and the distance of the longest path that leads to it.

For more details on the stream mode in general, see Stream.

The following will run the Longest Path algorithm in stream mode with weights:
CALL gds.dag.longestPath.stream("g", {relationshipWeightProperty: "time"})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs, path
RETURN
    index,
    gds.util.asNode(sourceNode).name AS sourceNode,
    gds.util.asNode(targetNode).name AS targetNode,
    totalCost,
    [nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames,
    costs,
    nodes(path) as path
ORDER BY index

We use the utility function asNode to return the name of node instead of its ID to make results more readable.

Table 4. Results
index sourceNode targetNode totalCost nodeNames costs path

0

"Timber"

"Timber"

0.0

["Timber"]

[0.0]

[Node[0]]

1

"Timber"

"Lumber"

1.0

["Timber", "Lumber"]

[0.0, 1.0]

[Node[0], Node[1]]

2

"Screws"

"Table Maker"

3.0

["Screws", "Table Maker"]

[0.0, 3.0]

[Node[2], Node[3]]

3

"Screws"

"Screws"

0.0

["Screws"]

[0.0]

[Node[2]]

4

"Screws"

"Table"

4.0

["Screws", "Table Maker", "Table"]

[0.0, 3.0, 4.0]

[Node[2], Node[3], Node[4]]