Challenge - 5 Problems
Tarjan Bridges Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tarjan's Bridge Finding on a Simple Graph
What is the output (list of bridges) of the following C++ code that uses Tarjan's algorithm to find bridges in an undirected graph?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<vector<int>> graph; vector<int> disc, low; vector<bool> visited; int time_counter = 0; vector<pair<int,int>> bridges; void dfs(int u, int parent) { visited[u] = true; disc[u] = low[u] = ++time_counter; for (int v : graph[u]) { if (v == parent) continue; if (!visited[v]) { dfs(v, u); low[u] = min(low[u], low[v]); if (low[v] > disc[u]) { bridges.push_back({u, v}); } } else { low[u] = min(low[u], disc[v]); } } } int main() { int n = 5; graph.assign(n, vector<int>()); disc.assign(n, -1); low.assign(n, -1); visited.assign(n, false); graph[0].push_back(1); graph[1].push_back(0); graph[1].push_back(2); graph[2].push_back(1); graph[1].push_back(3); graph[3].push_back(1); graph[3].push_back(4); graph[4].push_back(3); for (int i = 0; i < n; i++) { if (!visited[i]) dfs(i, -1); } sort(bridges.begin(), bridges.end()); for (auto &b : bridges) { cout << b.first << " - " << b.second << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
Remember that a bridge is an edge which, if removed, increases the number of connected components.
✗ Incorrect
The edges (0-1), (1-2), (1-3), and (3-4) are bridges because removing any of them disconnects the graph. The code outputs edges (0-1), (1-2), and (3-4) as bridges. Edge (1-3) is also a bridge but depending on DFS traversal order it may appear. The correct bridges are (0-1), (1-2), and (3-4). So the correct answer is C.
🧠 Conceptual
intermediateUnderstanding Low and Discovery Times in Tarjan's Algorithm
In Tarjan's algorithm for finding bridges, what does the 'low' value of a node represent?
Attempts:
2 left
💡 Hint
Think about how 'low' helps detect back edges and cycles.
✗ Incorrect
The 'low' value of a node is the smallest discovery time reachable from that node by following zero or more tree edges and then possibly one back edge. It helps identify if there is a back edge connecting to an ancestor, which is crucial to find bridges.
🔧 Debug
advancedIdentify the Bug in Tarjan's Bridge Detection Code
What is the bug in the following snippet of Tarjan's algorithm for bridges?
void dfs(int u, int parent) {
visited[u] = true;
disc[u] = low[u] = ++time_counter;
for (int v : graph[u]) {
if (v == parent) continue;
if (!visited[v]) {
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= disc[u]) {
bridges.push_back({u, v});
}
} else {
low[u] = min(low[u], disc[v]);
}
}
}
Attempts:
2 left
💡 Hint
Check the condition that distinguishes bridges from articulation points.
✗ Incorrect
For bridges, the condition is 'low[v] > disc[u]'. Using '>=' incorrectly marks edges that are not bridges. The '>=' condition is used for articulation points, not bridges.
🚀 Application
advancedNumber of Bridges in a Given Graph
Given a graph with 6 nodes and edges: (0-1), (1-2), (2-0), (1-3), (3-4), (4-5), (5-3), how many bridges does Tarjan's algorithm find?
Attempts:
2 left
💡 Hint
Look for edges whose removal increases the number of connected components.
✗ Incorrect
The graph has a triangle (0-1-2) and a cycle (3-4-5). The only bridge is edge (1-3) connecting these two cycles. Removing (1-3) disconnects the graph, so there is exactly 1 bridge.
❓ Predict Output
expertOutput of Tarjan's Algorithm on a Complex Graph
What is the output (list of bridges) of the following C++ code using Tarjan's algorithm on the given graph?
Graph edges:
0-1, 1-2, 2-0, 1-3, 3-4, 4-5, 5-3, 4-6
Code:
#include
#include
#include
using namespace std;
vector> graph;
vector disc, low;
vector visited;
int time_counter = 0;
vector> bridges;
void dfs(int u, int parent) {
visited[u] = true;
disc[u] = low[u] = ++time_counter;
for (int v : graph[u]) {
if (v == parent) continue;
if (!visited[v]) {
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > disc[u]) {
bridges.push_back({min(u,v), max(u,v)});
}
} else {
low[u] = min(low[u], disc[v]);
}
}
}
int main() {
int n = 7;
graph.assign(n, vector());
disc.assign(n, -1);
low.assign(n, -1);
visited.assign(n, false);
graph[0].push_back(1); graph[1].push_back(0);
graph[1].push_back(2); graph[2].push_back(1);
graph[2].push_back(0); graph[0].push_back(2);
graph[1].push_back(3); graph[3].push_back(1);
graph[3].push_back(4); graph[4].push_back(3);
graph[4].push_back(5); graph[5].push_back(4);
graph[5].push_back(3); graph[3].push_back(5);
graph[4].push_back(6); graph[6].push_back(4);
for (int i = 0; i < n; i++) {
if (!visited[i]) dfs(i, -1);
}
sort(bridges.begin(), bridges.end());
for (auto &b : bridges) {
cout << b.first << " - " << b.second << "\n";
}
return 0;
}
Attempts:
2 left
💡 Hint
Identify edges that connect cycles to other parts of the graph.
✗ Incorrect
The graph has two cycles: (0-1-2) and (3-4-5). Edge (1-3) connects these cycles, so (1-3) is a bridge. Edge (4-6) connects node 6 to the cycle, so (4-6) is also a bridge. Edges inside cycles are not bridges. So the bridges are (1-3) and (4-6).
