Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest Path For A Dag

I have a graph with an s and t vertex that I need to find the shortest path between. The graph has a lot of special properties that I would like to capitalize on:

  • The graph is a DAG (directed acyclic graph).
  • I can create a topological sort in O(|V|) time, faster than the traditional O(|V + E|).
  • Within the topological sort, s is the first item in the list and t is the last.

I was told that once I have a topological sort of the vertices I could find the shortest path faster than my current standard of Dijkstra's Uniform Cost, but I cannot seem to find the algorithm for it.

Pseudo code would be greatly appreciated.

EDITS: All paths from s to t have the same number of edges. Edges have weights. I am searching for lowest cost path.

like image 575
user108088 Avatar asked Sep 27 '09 02:09

user108088


People also ask

What is shortest path in DAG?

Given an Weighted DAG and a source point, the task is to find the shortest path between the source node and every other node in the graph. 3 Methods to solve this- Using Bellman-Ford [ TC = O(VE) ] Using Dijkstra's Algorithm [ TC = O(E + Vlog(V)) ]

What's the best algorithm for the shortest path between two nodes in a DAG?

Dijkstra's algorithm Dijkstra's algorithm for shortest paths does this almost exactly like Prim's algorithm. Remember that in Prim's algorithm, we add vertices and edges one a a time to a tree, at each step choosing the shortest possible edge to add.

Why topological sort is needed for shortest path in DAG?

Topological sorts works only for Directed Acyclic Graph(DAG). So Basically in topological sorting of a graph the vertices which have fewer dependencies are printed before the vertices which have relatively greater dependencies. We can use both DFS/BFS for implementing Topological Sorting.

How do you find the longest path in a DAG?

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.


1 Answers

I'm going to go against my intuition and assume this isn't homework. You have to take advantage of the information that a topological ordering gives you. Whenever you examine the node n in a topological ordering, you have the guarantee that you've already traversed every possible path to n. Using this it's clear to see that you can generate the shortest path with one linear scan of a topological ordering (pseudocode):

Graph g
Source s
top_sorted_list = top_sort(g)

cost = {} // A mapping between a node, the cost of its shortest path, and 
          //its parent in the shortest path

for each vertex v in top_sorted_list:
  cost[vertex].cost = inf
  cost[vertex].parent = None

cost[s] = 0

for each vertex v in top_sorted_list:
   for each edge e in adjacensies of v:
      if cost[e.dest].cost > cost[v].cost + e.weight:
        cost[e.dest].cost =  cost[v].cost + e.weight
        e.dest.parent = v

Now you can look up any shortest path from s to a destination. You'd just need to look up the destination in the cost mapping, get it's parent, and repeat this process until you get a node whose parent is s, then you have the shortest path.

like image 173
Falaina Avatar answered Oct 06 '22 14:10

Falaina