UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Introduction
  • Running Algorithms
    • Degree Centrality
    • Closeness Centrality
    • Harmonic Centrality
    • Eccentricity Centrality
    • Betweenness Centrality
    • Bridges
    • Articulation Points
    • Eigenvector Centrality
    • Katz Centrality
    • CELF
    • PageRank
    • ArticleRank
    • TextRank
    • HITS
    • SybilRank
    • Jaccard Similarity
    • Overlap Similarity
    • Cosine Similarity
    • Pearson Correlation Coefficient
    • Euclidean Distance
    • KNN
    • Vector Similarity
    • Bipartite Graph
    • HyperANF
    • Weakly Connected Components (WCC)
    • Strongly Connected Components (SCC)
    • k-Edge Connected Components
    • Local Clustering Coefficient
    • Triangle Count
    • Clique Count
    • k-Core
    • k-Truss
    • p-Cohesion
    • Induced Subgraph
    • Topological Sort
    • Breadth-First Search (BFS)
    • Depth-First Search (DFS)
    • Dijkstra's Shortest Path
    • A* Shortest Path
    • Yen's K-Shortest Paths
    • Shortest Path (BFS)
    • Delta-Stepping SSSP
    • Shortest Path Faster Algorithm (SPFA)
    • All-Pairs Shortest Path (APSP)
    • Minimum Spanning Tree (MST)
    • K-Spanning Tree
    • Steiner Tree
    • Prize-Collecting Steiner Tree (PCST)
    • Minimum Cost Flow
    • Maximum Flow
    • K-Hop Fast
    • Longest Path (DAG)
    • Random Walk
    • Adamic-Adar Index
    • Common Neighbors
    • Preferential Attachment
    • Resource Allocation
    • Total Neighbors
    • Same Community
    • Louvain
    • Leiden
    • Modularity Optimization
    • Label Propagation
    • HANP
    • SLPA
    • k-Means
    • HDBSCAN
    • K-1 Coloring
    • Modularity
    • Conductance
    • Max k-Cut
      • Node2Vec
      • Struc2Vec
      • LINE
      • Fast Random Projection
      • Summary of Graph Embedding
      • Gradient Descent
      • Backpropagation
      • Skip-gram
      • Skip-gram Optimization
  1. Docs
  2. /
  3. Graph Algorithms
  4. /
  5. Pathfinding

Longest Path (DAG)

Overview

The Longest Path (DAG) algorithm finds the longest path from any source node (in-degree 0) to every other node in a directed acyclic graph (DAG). It uses topological sort followed by dynamic programming to compute the longest distance efficiently.

Applications:

  • Project scheduling: Finding the critical path — the longest chain of dependent tasks that determines the minimum project duration.
  • Pipeline depth: Determining the maximum number of sequential stages in a data processing pipeline.
  • Dependency analysis: Identifying the deepest dependency chain in build systems or package managers.

Concepts

DAG

See Topological Sort.

Longest Path in DAG

Finding the longest path in a general graph is NP-hard. However, in a DAG, it can be solved in linear time because the absence of cycles guarantees a topological ordering — nodes can be processed in a sequence where every edge goes from an earlier node to a later one.

The algorithm works in two steps:

1. Topological sort: Order all nodes so that for every edge u→v, node u comes before v. See Topological Sort.

NodeLevel
A, F, H0
B, C, D, E1
G2

2. Dynamic programming: Process nodes in topological order. For each node v, compute longestDistance(v) = max(longestDistance(u) + 1) for all predecessors (incoming neighbors) u of v.

Source nodes (in-degree 0) have longestDistance = 0.

Node vIncoming From (u)longestDistance(v)
A, F, HNone0
BAmax(0) + 1 = 1
CAmax(0) + 1 = 1
DA or Fmax(0, 0) + 1 = 1
EA or Fmax(0, 0) + 1 = 1
GE or Hmax(1, 0) + 1 = 2

Longest path distance = 2 (A→E→G or F→E→G).

Considerations

  • The algorithm only works on directed acyclic graphs (DAGs). If the graph contains a cycle, an error is returned.
  • The algorithm follows outgoing edges only.

Example Graph

GQL
INSERT (A:default {_id: "A"}), (B:default {_id: "B"}),
       (C:default {_id: "C"}), (D:default {_id: "D"}),
       (E:default {_id: "E"}), (F:default {_id: "F"}),
       (G:default {_id: "G"}), (H:default {_id: "H"}),
       (A)-[:default]->(B), (A)-[:default]->(D),
       (B)-[:default]->(C), (B)-[:default]->(E),
       (C)-[:default]->(F), (D)-[:default]->(E),
       (E)-[:default]->(F), (F)-[:default]->(G),
       (H)-[:default]->(D), (H)-[:default]->(G)

Parameters

This algorithm accepts no parameters.

Run Mode

Returns:

ColumnTypeDescription
nodeIdSTRINGNode identifier (_id)
longestDistanceINTLongest distance from any source node
predecessorSTRINGPredecessor node on the longest path
GQL
CALL algo.longestpathdag() YIELD nodeId, longestDistance, predecessor

Result:

nodeIdlongestDistancepredecessor
A0
H0
B1A
D1A
C2B
E2B
F3C
G4F

Stream Mode

Returns the same columns as run mode, streamed for memory efficiency.

GQL
CALL algo.longestpathdag.stream() YIELD nodeId, longestDistance
ORDER BY longestDistance DESC
RETURN longestDistance LIMIT 1

Result:

longestDistance
4

Stats Mode

Returns:

ColumnTypeDescription
nodeCountINTTotal number of nodes in the DAG
maxDistanceINTMaximum longest path distance
GQL
CALL algo.longestpathdag.stats() YIELD nodeCount, maxDistance

Result:

nodeCountmaxDistance
84