Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the distance matrix with the given graph.
DSA Python
dist = [[[1] if i != j else 0 for j in range(V)] for i in range(V)]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 for all distances, which incorrectly assumes all nodes are connected.
Using -1 which can cause wrong calculations in shortest path.
✗ Incorrect
We use float('inf') to represent no direct edge between nodes, and 0 for the same node distance.
2fill in blank
mediumComplete the code to update the distance matrix with the direct edges from the graph.
DSA Python
for u, v, w in edges: dist[u][v] = [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 0 instead of the actual edge weight.
Assigning infinity which means no edge.
✗ 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 update condition to correctly check for shorter paths.
DSA Python
if dist[i][k] != float('inf') and dist[k][j] != float('inf') and dist[i][k] + dist[k][j] [1] dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j]
Attempts:
3 left
💡 Hint
Common Mistakes
Using > which would update only if new path is longer.
Using == which does not update for shorter paths.
✗ Incorrect
We update dist[i][j] only if the new path through k is shorter (less) than the current distance.
4fill in blank
hardFill both blanks to complete the nested loops for Floyd Warshall algorithm.
DSA Python
for [1] in range(V): for [2] in range(V): # update distances
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names that confuse the roles of loops.
Mixing up the order of loops.
✗ Incorrect
The outer loop uses k as the intermediate node, inner loop uses i as the start node.
5fill in blank
hardFill all three blanks to complete the triple nested loops for Floyd Warshall algorithm.
DSA Python
for [1] in range(V): for [2] in range(V): for [3] in range(V): if dist[[2]][[1]] != float('inf') and dist[[1]][[3]] != float('inf') and dist[[2]][[1]] + dist[[1]][[3]] < dist[[2]][[3]]: dist[[2]][[3]] = dist[[2]][[1]] + dist[[1]][[3]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect loop variable names.
Mixing the order of loops.
✗ Incorrect
The loops are for k (intermediate), i (start), and j (end) nodes respectively.
