Challenge your skills on advanced shortest path algorithms including Dijkstra, Bellman-Ford, and Floyd-Warshall. Assess your understanding of algorithm mechanics, use cases, and differences important for graph analysis and computational optimization.
Which type of weighted directed graph is unsuitable for finding shortest paths with Dijkstra's algorithm, for example, one containing a cycle with edge weights of -2, 3, and 4?
Explanation: Dijkstra's algorithm assumes all edge weights are non-negative, so negative weight edges can lead to incorrect results, especially in graphs with negative cycles. Acyclic graphs with positive weights and graphs where all weights are zero can be handled by Dijkstra's algorithm. Undirected graphs can work with Dijkstra’s as long as the weights are non-negative. Only the presence of negative weights disrupts its reliability.
After applying the Bellman-Ford algorithm to a graph with a negative weight cycle reachable from the source, such as one with edges (A→B:2, B→C:-4, C→A:1), what result will it produce?
Explanation: Bellman-Ford can detect negative weight cycles during the relaxation process. If such a cycle is reachable from the source, it indicates the presence and typically halts or reports an error. It won't compute correct shortest paths in this scenario. Unlike Dijkstra’s, it does not ignore negative weights, and returning all distances is only correct when all cycles have non-negative total weight.
For which type of problem is the Floyd-Warshall algorithm particularly suited, for instance, calculating shortest paths between every pair of cities in a country?
Explanation: Floyd-Warshall is specifically designed for finding shortest paths between all pairs of vertices in a weighted graph, making it ideal for scenarios like city-to-city route evaluation. Single-source or single-destination problems are better solved by Dijkstra or Bellman-Ford. Minimum spanning tree problems are unrelated and require different algorithms entirely.
How does the edge relaxation step differ between Dijkstra’s and Bellman-Ford algorithms, specifically in the way they revisit edges?
Explanation: Dijkstra's algorithm selects the shortest current path and relaxes outgoing edges only once per vertex. Bellman-Ford can relax the same edge multiple times in multiple passes to account for negative cycles or paths. The options suggesting both algorithms relax edges only once or that Bellman-Ford never revisits edges are inaccurate.
What is the memory complexity for storing intermediate distances in the Floyd-Warshall algorithm when run on a graph with n vertices?
Explanation: Floyd-Warshall uses an n by n matrix to store shortest distances between every vertex pair, resulting in O(n^2) memory usage. The O(n^3) term relates to time complexity, not memory. O(n) and O(n log n) are too low for the matrix needed to store all-pairs distances.