π Quick Study Guide: Shortest Path Algorithms
- π‘ What is the Shortest Path Problem? It's finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized. These weights can represent distance, time, cost, etc.
- π Graph Terminology: A graph consists of vertices (nodes) and edges (connections between nodes). Edges can be directed (one-way) or undirected (two-way) and often have weights.
- π£οΈ Dijkstra's Algorithm:
- π Finds the single-source shortest path to all other nodes.
- π« Works only with non-negative edge weights.
- π§ A greedy algorithm that iteratively visits the unvisited node with the smallest known distance from the source.
- Formula for edge relaxation: $d[v] = \min(d[v], d[u] + w(u,v))$ where $d[v]$ is the current shortest distance to $v$, $d[u]$ is the shortest distance to $u$, and $w(u,v)$ is the weight of the edge from $u$ to $v$.
- π§ Bellman-Ford Algorithm:
- π Also finds the single-source shortest path.
- β Can handle negative edge weights.
- π Can detect negative cycles (a path where summing the edge weights results in a negative value, implying an infinitely short path).
- π Time complexity is higher than Dijkstra's, typically $O(VE)$ where $V$ is the number of vertices and $E$ is the number of edges.
- πΊοΈ Floyd-Warshall Algorithm:
- π― Finds the all-pairs shortest paths (shortest path between every pair of vertices).
- β
Can handle negative edge weights (but not negative cycles).
- π A dynamic programming algorithm.
- Formula: $dist[i][j] = \min(dist[i][j], dist[i][k] + dist[k][j])$ where $k$ is an intermediate vertex.
- π A* Search Algorithm:
- π An informed search algorithm that uses a heuristic function to estimate the cost from the current node to the goal node.
- π Often much faster than Dijkstra's for a single-source, single-destination shortest path by prioritizing paths that seem more promising.
- π Real-world Applications: GPS navigation, network routing protocols (e.g., OSPF), logistics and supply chain optimization, game AI pathfinding, urban planning, and more!
β Practice Quiz
- Which shortest path algorithm is NOT suitable for graphs containing negative edge weights?
A. Bellman-Ford
B. Floyd-Warshall
C. Dijkstra's
D. A* Search - What is the primary advantage of the Bellman-Ford algorithm over Dijkstra's algorithm?
A. It is faster in all scenarios.
B. It can find all-pairs shortest paths.
C. It can handle graphs with negative edge weights and detect negative cycles.
D. It uses a heuristic function for faster convergence. - The Floyd-Warshall algorithm is best suited for which of the following problems?
A. Finding the shortest path from a single source to a single destination.
B. Finding the shortest path from a single source to all other vertices.
C. Finding the shortest paths between all pairs of vertices in a graph.
D. Finding the minimum spanning tree of a graph. - In the context of shortest path algorithms, what does a 'negative cycle' imply?
A. A path with only negative edge weights.
B. A path where the sum of edge weights is zero.
C. A path where summing the edge weights results in an infinitely decreasing value, making a shortest path undefined.
D. A cycle that contains an even number of negative edges. - Which algorithm uses a heuristic function to guide its search and often finds a path to a single destination more efficiently than Dijkstra's in large graphs?
A. Bellman-Ford
B. Floyd-Warshall
C. Dijkstra's
D. A* Search - Consider a graph where nodes represent cities and edge weights represent the cost of traveling between them. If some travel costs can be negative (e.g., a subsidy for taking a specific route), which algorithm would be most appropriate to find the cheapest route from one city to all others?
A. Dijkstra's Algorithm
B. Prim's Algorithm
C. Bellman-Ford Algorithm
D. Kruskal's Algorithm - The relaxation formula $d[v] = \min(d[v], d[u] + w(u,v))$ is a core component of which greedy shortest path algorithm for graphs with non-negative edge weights?
A. Floyd-Warshall
B. Bellman-Ford
C. Dijkstra's
D. A* Search
Click to see Answers
1. C
2. C
3. C
4. C
5. D
6. C
7. C