no> [!summary] Finding the shortest path between a pair of vertices.

ConditionAlgorithmTime Complexity
Unweighted GraphBFS
No Negative Weight CyclesBellman-Ford
No Negative WeightsDijkstra’sdepends..
TreeBFS/DFS
Directed Acyclic GraphDAG SSSP

DAG SSSP

This algorithm

  • does a topological sort on the graph, while relaxing the edges.
  • now, in topological order, each vertex is processed.

This works for DAGs because of the property that at vertex v, all the shortest paths to all its parents have been found.

ABCDE11315ABCDE11315ABCDE11315ABCDE11315Shortest path: B E (3)Finding shortest path of B to CShortest path: B C E (2)