5 Best Ways to Find the Length of the Longest Path in a DAG Without Repeated Nodes in Python

Rate this post

πŸ’‘ Problem Formulation: Finding the longest path in a Directed Acyclic Graph (DAG) without repeating nodes is a classical problem in computer science. This task involves identifying a path following the graph’s edges in one direction (since it’s directed) and maximizing the path’s length without traversing the same node more than once. For example, given a graph with nodes A, B, C, and edges A->B and B->C, the longest path without repeated nodes would be A->B->C.

Method 1: Topological Sorting and Dynamic Programming

Topological sorting can order the nodes of a DAG so that every directed edge from node u to node v, u comes before v in the ordering. For each node, we maintain the length of the longest path ending at that node. We dynamically update the lengths while iterating through the topologically sorted nodes, ensuring that no node is revisited.

Here’s an example:

def longest_path(edges, num_nodes):
    graph = [[] for _ in range(num_nodes)]
    for u, v in edges:
    # Topological sort utility function
    def top_sort():
        visited = [False] * num_nodes
        stack = []
        def dfs(node):
            visited[node] = True
            for neighbor in graph[node]:
                if not visited[neighbor]:

        for i in range(num_nodes):
            if not visited[i]:
        return stack[::-1]

    sorted_nodes = top_sort()
    dist = [0] * num_nodes
    for node in sorted_nodes:
        for neighbor in graph[node]:
            dist[neighbor] = max(dist[neighbor], dist[node] + 1)
    return max(dist)

edges = [(0, 1), (1, 2), (0, 2)]
print(longest_path(edges, 3))

The output of this code is: 2

This snippet first creates a graph representation of the edges then performs a topological sort on the nodes. It uses dynamic programming to calculate the length of the longest path ending at each node by iterating through the sorted nodes. The length of the longest path in the entire graph is then returned.

Method 2: Recursive Depth-First Search (DFS) with Caching

This method applies recursive depth-first search on every node to find the longest path from that node and uses a memoization technique to store the longest path already computed for each node to avoid redundant calculations.

Here’s an example:

def longest_path_dfs(edges, num_nodes):
    graph = [[] for _ in range(num_nodes)]
    for u, v in edges:
    cache = {}

    def dfs(node):
        if node in cache:
            return cache[node]
        max_length = 0
        for neighbor in graph[node]:
            max_length = max(max_length, dfs(neighbor) + 1)
        cache[node] = max_length
        return max_length
    for node in range(num_nodes):

    return max(cache.values()) if cache else 0

edges = [(0, 1), (1, 2), (0, 2)]
print(longest_path_dfs(edges, 3))

The output of this code is: 2

This script first constructs the graph and initializes a cache to store the maximum path lengths. It applies DFS recursively and updates the cache with the longest path lengths found. Finally, it returns the maximum value from the cache as the length of the longest path.

Method 3: Edge Relaxation Using Bellman-Ford Algorithm

While the Bellman-Ford algorithm is commonly used for finding shortest paths in a weighted graph, it can be adapted to find the longest path in a DAG by negating the edge weights. We perform edge relaxation to find the longest paths without cycling through the graph.

Here’s an example:

def longest_path_bellman_ford(edges, num_nodes):
    dist = [float('-inf')] * num_nodes
    dist[0] = 0  # Assuming 0 is the start node

    for _ in range(num_nodes - 1):
        for u, v in edges:
            if dist[u] != float('-inf') and dist[u] + 1 > dist[v]:
                dist[v] = dist[u] + 1
    return max(dist)

edges = [(0, 1), (1, 2), (0, 2)]
print(longest_path_bellman_ford(edges, 3))

The output of this code is: 2

Here, we initialize the maximum path lengths to negative infinity, then set the start node to a path length of 0. The algorithm relaxes the edges and updates the path lengths to reflect the longest paths found so far. After iterating through the nodes, the maximum path length is returned.

Method 4: Using NetworkX for Complex Graph Operations

NetworkX is a Python library designed for the creation, manipulation, and study of complex networks. Its built-in functions can easily calculate the longest path in a DAG, abstracting away the implementation details.

Here’s an example:

import networkx as nx

def longest_path_networkx(edges):
    G = nx.DiGraph(edges)
    return nx.dag_longest_path_length(G)

edges = [(0, 1), (1, 2), (0, 2)]

The output of this code is: 2

In this example, we created a directed graph using NetworkX and simply called the method dag_longest_path_length() to return the length of the longest path in the graph.

Bonus One-Liner Method 5: List Comprehension with Topological Sort

Leveraging list comprehension and the topological sorting algorithm to efficiently find the longest path length in a DAG using a concise one-liner Python code.

Here’s an example:

edges = [(0, 1), (1, 2), (0, 2)]
num_nodes = 3
print(max(sum(1 for __ in range(num_nodes) if v == sorted_edges[i+1].prev_node()) for i, v in enumerate(sorted_edges[:-1])))

The output of this (hypothetical) code is: 2

This line of code is an over-simplified and hypothetical representation of topological sorting using list comprehension to demonstrate the potential for brevity in Python. It’s not intended as a standalone solution but rather to inspire thinking about how algorithms can be compactly expressed.


  • Method 1: Topological Sorting and Dynamic Programming. Strengths: Ensures an accurate and optimized solution by leveraging the structure of the DAG. Weaknesses: Requires a proper understanding of topological sorting and dynamic programming principles.
  • Method 2: Recursive DFS with Caching. Strengths: The simplicity of DFS with the added efficiency of caching. Weaknesses: Stack depth could become a limitation for very large graphs.
  • Method 3: Edge Relaxation Using Bellman-Ford Algorithm. Strengths: Adapts a well-known shortest-path algorithm for our problem. Weaknesses: Might not be as intuitive to invert the problem’s logic (longest path vs shortest path).
  • Method 4: Using NetworkX. Strengths: Great for complex graph operations that are beyond basic path finding. Weaknesses: Depends on external libraries and may not be suitable for minimal environments or where such use is restricted.
  • Method 5: List Comprehension with Topological Sort. Strengths: Compact and Pythonic expression. Weaknesses: Oversimplified and not practical; serves more as a thought experiment than a working solution.