Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the distance matrix with the input graph.
DSA C#
int V = graph.GetLength(0); int[,] dist = new int[V, V]; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = graph[i, j] == 0 && i != j ? [1] : graph[i, j]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of a large number causes incorrect shortest path calculations.
Using -1 can cause confusion with valid edge weights.
✗ Incorrect
We use int.MaxValue to represent no direct edge between nodes (infinity).
2fill in blank
mediumComplete the code to update the distance matrix using the Floyd Warshall algorithm.
DSA C#
for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i, k] != int.MaxValue && dist[k, j] != int.MaxValue && dist[i, k] + dist[k, j] < [1]) { dist[i, j] = dist[i, k] + dist[k, j]; } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with dist[i, k] or dist[k, j] instead of dist[i, j].
Using int.MaxValue in comparison causes wrong logic.
✗ Incorrect
We update dist[i, j] only if the new path through k is shorter than the current dist[i, j].
3fill in blank
hardFix the error in the code that prints the final shortest path matrix.
DSA C#
for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i, j] == [1]) Console.Write("INF "); else Console.Write(dist[i, j] + " "); } Console.WriteLine(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 instead of int.MaxValue causes wrong output.
Using null causes compilation errors in C#.
✗ Incorrect
Distances with int.MaxValue mean no path, so print 'INF' for clarity.
4fill in blank
hardFill both blanks to complete the nested loops for Floyd Warshall algorithm.
DSA C#
for (int [1] = 0; [1] < V; [1]++) { for (int [2] = 0; [2] < V; [2]++) { // inner logic } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using same variable name for both loops causes errors.
Using j in outer loop breaks the algorithm logic.
✗ Incorrect
The outer loop uses k as intermediate node, inner uses i for source nodes.
5fill in blank
hardFill all three blanks to complete the triple nested loops for Floyd Warshall algorithm.
DSA C#
for (int [1] = 0; [1] < V; [1]++) { for (int [2] = 0; [2] < V; [2]++) { for (int [3] = 0; [3] < V; [3]++) { // update dist logic } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing loop variable names breaks the algorithm.
Using same variable for multiple loops causes errors.
✗ Incorrect
The loops iterate over k (intermediate), i (source), and j (destination) nodes respectively.
