Let be a weighted directed acyclic graph with edges and vertices. Given and a source vertex in , which one of the following options gives the worst case time complexity of the fastest algorithm to find the lengths of shortest paths from to all vertices that are reachable from in ?
A
Step-by-Step Solution
Key idea: Shortest paths in a Directed Acyclic Graph (DAG) can be found in linear time using topological sorting.
Step 1: Recognize that the graph is a DAG. This is the crucial condition that allows a faster algorithm than Dijkstra's or Bellman-Ford.
Step 2: Perform a topological sort of the vertices. This takes time using DFS or Kahn's algorithm.
Step 3: Initialize the distance to the source vertex as 0, and all other vertices as .
Step 4: Process each vertex in topological order. For each outgoing edge with weight , relax the edge: if , update .
Step 5: Since each vertex and each edge is processed exactly once in the topological order, the relaxation step takes time.
Answer: