Challenge - 5 Problems
Floyd Warshall Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Floyd Warshall on a simple graph
What is the output distance matrix after running Floyd Warshall on this graph?
DSA Go
package main import "fmt" func floydWarshall(graph [][]int) [][]int { n := len(graph) dist := make([][]int, n) for i := range dist { dist[i] = make([]int, n) copy(dist[i], graph[i]) } for k := 0; k < n; k++ { for i := 0; i < n; i++ { for j := 0; j < n; j++ { if dist[i][k] != 9999 && dist[k][j] != 9999 && dist[i][k]+dist[k][j] < dist[i][j] { dist[i][j] = dist[i][k] + dist[k][j] } } } } return dist } func main() { graph := [][]int{ {0, 3, 9999, 7}, {8, 0, 2, 9999}, {5, 9999, 0, 1}, {2, 9999, 9999, 0}, } dist := floydWarshall(graph) fmt.Println(dist) }
Attempts:
2 left
💡 Hint
Look for the shortest paths updated through intermediate nodes.
✗ Incorrect
The Floyd Warshall algorithm updates the distance matrix by checking if going through an intermediate node k gives a shorter path from i to j. The final matrix shows the shortest distances between all pairs.
🧠 Conceptual
intermediateUnderstanding Floyd Warshall's time complexity
What is the time complexity of the Floyd Warshall algorithm for a graph with n vertices?
Attempts:
2 left
💡 Hint
Consider the three nested loops over vertices.
✗ Incorrect
Floyd Warshall uses three nested loops each running n times, resulting in O(n^3) time complexity.
🔧 Debug
advancedIdentify the error in this Floyd Warshall implementation
What error will this code produce when run?
DSA Go
func floydWarshall(graph [][]int) [][]int { n := len(graph) dist := make([][]int, n) for i := range dist { dist[i] = make([]int, n) copy(dist[i], graph[i]) } for k := 0; k < n; k++ { for i := 0; i < n; i++ { for j := 0; j < n; j++ { if dist[i][k]+dist[k][j] < dist[i][j] { dist[i][j] = dist[i][k] + dist[k][j] } } } } return dist }
Attempts:
2 left
💡 Hint
Check how infinite distances are handled in the if condition.
✗ Incorrect
The code does not check if dist[i][k] or dist[k][j] is infinite (e.g., 9999) before adding, which can cause incorrect updates.
🚀 Application
advancedUsing Floyd Warshall to detect negative cycles
How can Floyd Warshall algorithm be used to detect if a graph contains a negative weight cycle?
Attempts:
2 left
💡 Hint
Think about what a negative cycle means for shortest path from a node to itself.
✗ Incorrect
If dist[i][i] is negative, it means there is a cycle with negative total weight reachable from i.
❓ Predict Output
expertOutput of Floyd Warshall on a graph with unreachable nodes
What is the output distance matrix after running Floyd Warshall on this graph with unreachable nodes?
DSA Go
package main import "fmt" func floydWarshall(graph [][]int) [][]int { n := len(graph) dist := make([][]int, n) for i := range dist { dist[i] = make([]int, n) copy(dist[i], graph[i]) } for k := 0; k < n; k++ { for i := 0; i < n; i++ { for j := 0; j < n; j++ { if dist[i][k] != 9999 && dist[k][j] != 9999 && dist[i][k]+dist[k][j] < dist[i][j] { dist[i][j] = dist[i][k] + dist[k][j] } } } } return dist } func main() { graph := [][]int{ {0, 9999, 3, 9999}, {9999, 0, 9999, 7}, {9999, 2, 0, 9999}, {6, 9999, 9999, 0}, } dist := floydWarshall(graph) fmt.Println(dist) }
Attempts:
2 left
💡 Hint
Check how unreachable nodes get updated through intermediate nodes.
✗ Incorrect
The algorithm updates unreachable distances when a path exists through intermediate nodes, resulting in the matrix shown in option A.
