Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Longest path in a DAG

To find the longest path in a DAG, I'm aware of 2 algorithms: algo 1: do a topological sort + use dynamic programming on the result of the sort ~ or ~ algo 2: enumerate all the paths in the DAG using DFS, and record the longest. It seems like enumerating all the paths with DFS has better complexity than algo 1. Is that true?

like image 977
Frank Avatar asked May 23 '12 01:05

Frank


People also ask

How do you find the longest path in a DAG?

The recursive formula will be: dp[node] = max(dp[node], 1 + max(dp[child1], dp[child2], dp[child3]..)) At the end check for the maximum value in dp[] array, which will be the longest path in the DAG.

What is a path in a DAG?

A path in a directed graph is a sequence of edges having the property that the ending vertex of each edge in the sequence is the same as the starting vertex of the next edge in the sequence; a path forms a cycle if the starting vertex of its first edge equals the ending vertex of its last edge.

What is longest simple path problem?

The longest simple path problem (LPP) consists of finding a path p ∈ 乡 such that w(p) is maximal. Clearly, LPP is NP-hard because the problem of deciding whether or not there exists a Hamilton cycle in a given graph which is NP-complete can be reduced to this problem [4].


2 Answers

Your second option is incorrect: DFS does not explore all possible paths, unless your graph is a tree or a forest, and you start from the roots. The second algorithm that I know is negating the weights and finding the shortest path, but it is somewhat slower than the top sort + DP algorithm that you listed as #1.

like image 174
Sergey Kalinichenko Avatar answered Sep 29 '22 00:09

Sergey Kalinichenko


Enumerate all paths in a DAG using "DFS":

import numpy as np

def enumerate_dag(g):

    def enumerate_r(n, paths, visited, a_path = []):
        a_path += [n]
        visited[n] = 1
        if not g[n]:
            paths += [list(a_path)]
        else:
            for nn in g[n]:
                enumerate_r(nn, paths, visited, list(a_path))

    paths, N = [], len(g)
    visited = np.zeros((N), dtype='int32')

    for root in range(N):
        if visited[root]: continue
        enumerate_r(root, paths, visited, [])

    return paths
like image 38
Frank Avatar answered Sep 28 '22 23:09

Frank