All Exams Test series for 1 year @ ₹349 only
Question

Let $G$ be a weighted directed acyclic graph with $m$ edges and $n$ vertices. Given $G$ and a source vertex $s$ in $G$, which one of the following options gives the worst case time complexity of the fastest algorithm to find the lengths of shortest paths from $s$ to all vertices that are reachable from $s$ in $G$?

The correct answer is
$\Theta(m + n)$

Shortest Path Algorithm for DAGs

To find the lengths of shortest paths from a source vertex $s$ in a weighted Directed Acyclic Graph (DAG), the most efficient algorithm utilizes topological sorting.

Algorithm Steps and Complexity

  1. Topological Sort: First, perform a topological sort on the vertices of the DAG. This process orders the vertices such that for every directed edge from vertex $u$ to vertex $v$, $u$ comes before $v$ in the ordering. The time complexity for topological sorting is $\Theta(n + m)$, where $n$ is the number of vertices and $m$ is the number of edges.
  2. Initialization: Initialize the distance to the source vertex $s$ as $0$ (i.e., $dist(s) = 0$) and the distances to all other vertices as infinity ($dist(v) = \infty$ for $v \neq s$).
  3. Edge Relaxation: Process the vertices in the topologically sorted order. For each vertex $u$, iterate through all its outgoing edges $(u, v)$. If a shorter path to $v$ is found through $u$, update $dist(v)$. This step is formally known as edge relaxation:
    If $dist(u) + weight(u, v) < dist(v)$ then $dist(v) = dist(u) + weight(u, v)$

Since each vertex is processed once in the topological order and each edge is relaxed exactly once (when its source vertex is processed), the edge relaxation phase takes $\Theta(m)$ time.

Overall Time Complexity

The total worst-case time complexity is the sum of the complexities of the topological sort and the edge relaxation steps:

$ \Theta(n + m) + \Theta(m) = \Theta(n + m) $

Therefore, the fastest algorithm for finding shortest paths in a weighted DAG has a time complexity of $\Theta(n + m)$.

Was this answer helpful?

Important Questions from Shortest Paths

  1. Let $G(V, E)$ be an undirected, edge-weighted graph with integer weights. The weight of a path is the sum of the weights of the edges in that path. The length of a path is the number of edges in that path.
    Let $s \in V$ be a vertex in $G$. For every $u \in V$ and for every $k \ge 0$, let $d_k(u)$ denote the weight of a shortest path (in terms of weight) from $s$ to $u$ of length at most $k$. If there is no path from $s$ to $u$ of length at most $k$, then $d_k(u) = \infty$.
    Consider the statements:
    S1: For every $k \ge 0$ and $u \in V$, $d_{k+1}(u) \le d_k(u)$.
    S2: For every $(u, v) \in E$, if $(u, v)$ is part of a shortest path (in terms of weight) from $s$ to $v$, then for every $k \ge 0$, $d_k(u) \le d_k(v)$.

    Which one of the following options is correct?
  2. Floyd-Warshall algorithm utilizes __________ to solve the all-pairs shortest paths problem on a directed graph in __________ time.

  3. An all-pairs shortest-paths problem is efficiently solved using :

  4. Match List - I with List - II.

    List - IList - II
    (A) Dijkstra’s Algorithms(I) Find the shortest path between all pairs of vertices in a graph with positive or negative edge weights.
    (B) Floyd-Warshall Algorithms(II) Finds the shortest path in a weighted graph with non-negative edge weights.
    (C) Bellman-frod Algorithms(III) Grows a spanning tree by repeatedly adding the smallest edge to a neighboring vertex.
    (D) Prim’s Algorithms(IV) Finds the shortest paths from a single source in a directed graph with negative edge weights.

    Choose the correct answer from the options given below :

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App