Which algorithm is used to find the shortest path from a single source vertex to all other vertices in a weighted graph? Which algorithm is used to…

Which algorithm is used to find the shortest path from a single source vertex to all other vertices in a weighted graph? Which algorithm is used to find the shortest path in a graph containing negative edge weights?

From the BUET MSC (CSE) October 2026 – Written exam, question 12 of 14.

Answers (1)

  • Non-negative weights: Dijkstra’s algorithm — single source to all vertices, O((V + E) log V) with a binary heap.
  • Negative edge weights: Bellman–Ford algorithm — relaxes every edge V − 1 times, O(V·E); one more pass detects a negative cycle, in which case shortest paths are undefined.

Dijkstra can fail with negative edges because it finalises a vertex’s distance too early. (For all pairs: Floyd–Warshall, or Johnson’s algorithm with negative edges.)

— rhs

More from this paper