Definition
The shortest-path problem is a fundamental problem in graph theory that seeks to find the path with the minimum total weight (or cost) between two vertices in a graph.
There are variations of this problem, including:
- Single-source shortest paths: Finding the shortest paths from a single source vertex to all other vertices in the graph.
- Single-destination shortest paths: Finding the shortest paths from all vertices in the graph to a single destination vertex.
- All-pairs shortest paths: Finding the shortest paths between every pair of vertices in the graph.
Dijkstra's Algorithm
Dijkstra's Algorithm is a classic algorithm for solving the single-source shortest-path problem in a weighted graph where all edge weights are non-negative. It is a greedy algorithm that works by iteratively exploring the graph, maintaining a set of visited vertices and a set of unvisited vertices and updating the estimated shortest distance from the source to each vertex.
Warning
Dijkstra's Algorithm finds the shortest path only if all edge weights are non-negative.
Floyd's Algorithm
Floyd's algorithm, also known as the Floyd-Warshall algorithm, is a dynamic programming algorithm used to find the shortest paths between all pairs of vertices in a weighted graph. Unlike Dijkstra's algorithm, which computes the shortest paths from a single source vertex, Floyd's algorithm calculates the shortest paths between every possible source-destination pair.
Note
Floyd's algorithm can handle graphs with negative edge weights but does not work if there are negative cycles (a cycle where the sum of edge weights is negative), as shortest paths would be undefined in that case.