Explore key concepts of All-Pairs Shortest Path algorithms, focusing on Floyd-Warshall and Johnson’s methods, including their features, use cases, and differences. This easy quiz helps solidify your grasp on algorithm steps, graph characteristics, and practical considerations for these popular graph algorithms.
Which fundamental strategy does the Floyd-Warshall algorithm use to find shortest paths between all pairs of vertices in a weighted graph?
Explanation: Floyd-Warshall algorithm applies dynamic programming by building solutions to subproblems and combining them. Greedy choice is common in algorithms like Dijkstra, not Floyd-Warshall. Divide and conquer breaks problems into independent subproblems, which Floyd-Warshall does not do. Backtracking is unrelated to shortest path computation here, making dynamic programming the correct answer.
When is it appropriate to use the Floyd-Warshall algorithm considering negative edge weights in a graph?
Explanation: Floyd-Warshall can handle negative edge weights as long as there are no negative cycles. Using it on graphs with only positive weights is possible but not its main distinction. If negative cycles are present, Floyd-Warshall will not produce correct shortest paths. All zero edge weights are an unrelated scenario.
What is the time complexity of the Floyd-Warshall algorithm for a graph with n vertices?
Explanation: The time complexity of Floyd-Warshall is O(n^3) because it has three nested loops over the vertices. O(n^2) suggests a simpler algorithm, which isn't the case here. O(n log n) is typical for efficient single-source algorithms. O(2^n) is exponential and does not apply to Floyd-Warshall.
What unique feature of Johnson’s algorithm allows it to work with graphs having negative edge weights?
Explanation: Johnson’s algorithm cleverly reweights all edges to remove negative weights without altering actual shortest paths. Cycling or repeating edges does not address negative weights. Ignoring negative edges produces incorrect results, and splitting edges is not part of the algorithm.
Which algorithm is typically more efficient for finding all-pairs shortest paths in large, sparse graphs?
Explanation: Johnson’s algorithm is more efficient for sparse graphs thanks to its combination of reweighting and using Dijkstra's algorithm for each node. Floyd-Warshall has cubic complexity regardless of graph density. Bellman-Ford is slower for all-pairs, and Depth-First Search does not calculate path lengths suitable for this problem.
Which step in the Floyd-Warshall algorithm can detect the presence of a negative weight cycle in the graph?
Explanation: After running Floyd-Warshall, negative values on the diagonal of the distance matrix indicate a negative cycle. Counting edges or comparing matrices does not detect negative cycles directly. Listing all possible paths is unnecessary for cycle detection.
What is the first major step in Johnson’s algorithm before running Dijkstra’s algorithm from each vertex?
Explanation: The algorithm adds a new vertex, connecting it to every vertex with zero-weight edges, to facilitate reweighting via Bellman-Ford. Reversing edges or replacing weights changes the graph's structure, which is not required. Labeling by degree is not part of the Johnson’s approach.
Compared to Dijkstra's algorithm, what is the main advantage of Floyd-Warshall for all-pairs shortest path problems?
Explanation: Floyd-Warshall is designed to deliver all shortest paths between every pair of nodes in one run. Dijkstra is more efficient for single-source cases. Memory usage is not necessarily less, and Floyd-Warshall works with weighted graphs, not solely unweighted graphs.
In which scenario is Floyd-Warshall usually a poor choice for finding all-pairs shortest paths?
Explanation: Floyd-Warshall's cubic time makes it inefficient on large, sparse graphs. For small or dense graphs, the running time is manageable. The algorithm supports positive edge weights and can handle small node counts effectively.
After running Floyd-Warshall, what does a distance matrix entry of infinity (∞) between two vertices indicate?
Explanation: An infinity in the result means that there is no reachable path from one vertex to another. Presence of a direct edge would yield a finite distance. A negative cycle would show up on the diagonal as a negative value, and strong connectivity does not guarantee absence of infinity between any two nodes.