Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the distance vector with infinity values.
DSA Rust
let mut dist = vec![[1]; n];Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of infinity causes incorrect shortest path calculations.
Using negative values can cause logic errors.
✗ Incorrect
We initialize distances with i32::MAX to represent infinity before relaxation.
2fill in blank
mediumComplete the code to set the source node distance to zero.
DSA Rust
dist[[1]] = 0;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting distance at wrong index.
Not initializing source distance to zero.
✗ Incorrect
The source node distance is zero because it is the starting point.
3fill in blank
hardFix the error in the edge relaxation condition inside the loop.
DSA Rust
if dist[u] != i32::MAX && dist[v] [1] dist[u] + w {
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > reverses the logic and breaks shortest path calculation.
Not checking dist[u] != i32::MAX causes overflow.
✗ Incorrect
We check if dist[u] + w is less than dist[v]. The operator > is used to compare dist[v] with dist[u] + w.
4fill in blank
hardFill both blanks to correctly update the distance of the adjacent node.
DSA Rust
dist[v] = dist[u] [1] [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using wrong variable instead of edge weight.
✗ Incorrect
We add the edge weight w to dist[u] to update dist[v].
5fill in blank
hardFill all three blanks to complete the loop that relaxes edges for each node in topological order.
DSA Rust
for &u in [1].iter() { if dist[u] != i32::MAX { for &(v, w) in [2][u].iter() { if dist[u] + w < dist[v] { dist[v] = dist[u] [3] w; } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for iteration or adjacency list.
Using wrong operator for updating distance.
✗ Incorrect
We iterate over nodes in topo_order, access adjacency list graph[u], and update dist[v] by adding w.
