Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the distance matrix with the given graph edges.
DSA C++
for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i == j) dist[i][j] = 0; else dist[i][j] = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 for no edge causes incorrect shortest path calculations.
Using negative values can cause wrong results.
✗ Incorrect
We use INT_MAX to represent no direct edge between nodes, meaning infinite distance initially.
2fill in blank
mediumComplete the code to update the distance matrix with the direct edges from the graph.
DSA C++
for (auto edge : edges) { int u = edge.first; int v = edge.second.first; int w = edge.second.second; dist[u][v] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning node indices instead of weight.
Assigning zero which ignores edge weight.
✗ Incorrect
We assign the weight w of the edge from u to v to the distance matrix.
3fill in blank
hardFix the error in the Floyd Warshall triple nested loop condition to avoid integer overflow.
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] != [1] && dist[k][j] != [1] && dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking for INT_MAX causes overflow and wrong results.
Checking for zero or negative values is incorrect here.
✗ Incorrect
We check that dist[i][k] and dist[k][j] are not INT_MAX to avoid overflow when adding.
4fill in blank
hardFill both blanks to print the shortest distance matrix after Floyd Warshall completes.
DSA C++
for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i][j] == [1]) std::cout << "INF" << [2]; else std::cout << dist[i][j] << " "; } std::cout << std::endl; }
Attempts:
3 left
💡 Hint
Common Mistakes
Printing INT_MAX as a number confuses output.
Using space instead of tab may reduce readability.
✗ Incorrect
We print "INF" for infinite distances and separate values with a tab for readability.
5fill in blank
hardFill all three blanks to complete the Floyd Warshall update step with correct indices and condition.
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] != [1] && dist[k][j] != [1] && dist[i][k] + dist[k][j] < dist[[2]][[3]]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices in dist update condition.
Not checking for INT_MAX before addition.
✗ Incorrect
We check for INT_MAX to avoid overflow and update dist[i][j] if a shorter path is found.
