Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the discovery time for the current node.
DSA C++
disc[u] = [1];
low[u] = time;
time++;Attempts:
3 left
💡 Hint
Common Mistakes
Setting discovery time to node index instead of time.
Initializing discovery time to zero or -1 incorrectly.
✗ Incorrect
The discovery time for node u is set to the current time counter before incrementing it.
2fill in blank
mediumComplete the code to update the low value of the current node after visiting a neighbor.
DSA C++
low[u] = std::min(low[u], [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using discovery time of neighbor instead of low value.
Using the current time or node index incorrectly.
✗ Incorrect
The low value of u is updated to the minimum between its current low and the low value of neighbor v.
3fill in blank
hardFix the error in the condition that checks if an edge is a bridge.
DSA C++
if (low[v] [1] disc[u]) { bridges.push_back({u, v}); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or < instead of > in the condition.
Confusing the order of comparison.
✗ Incorrect
An edge (u, v) is a bridge if low[v] > disc[u], meaning v cannot reach an ancestor of u.
4fill in blank
hardFill both blanks to correctly skip the parent node and update low value for back edges.
DSA C++
for (int v : adj[u]) { if (v == [1]) continue; if (disc[v] != -1) { low[u] = std::min(low[u], [2]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Not skipping the parent node causing incorrect cycles.
Using low[v] instead of disc[v] for back edge updates.
✗ Incorrect
We skip the parent node to avoid trivial cycles, and update low[u] with the discovery time of v for back edges.
5fill in blank
hardFill all three blanks to complete the DFS function header and recursive call with correct parameters.
DSA C++
void dfs(int [1], int [2], std::vector<int>& disc, std::vector<int>& low, std::vector<std::vector<int>>& adj, std::vector<std::pair<int,int>>& bridges, int& time) { disc[u] = time; low[u] = time; time++; for (int v : adj[u]) { if (disc[v] == -1) { dfs([3], u, disc, low, adj, bridges, time); low[u] = std::min(low[u], low[v]); if (low[v] > disc[u]) { bridges.push_back({u, v}); } } else if (v != parent) { low[u] = std::min(low[u], disc[v]); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping parameter order in function or recursive call.
Using incorrect variable names in recursive call.
✗ Incorrect
The DFS function takes current node u and its parent. The recursive call explores neighbor v with u as parent.
