Bird
Raised Fist0
DSA Cprogramming~20 mins

Bridges in Graph Tarjan's Algorithm in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tarjan Bridges Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Tarjan's Algorithm on a Simple Graph
What is the output (list of bridges) of the following Tarjan's algorithm code on the given graph?
DSA C
/* Graph edges: 0-1, 1-2, 2-0, 1-3 */
#include <stdio.h>
#include <stdlib.h>
#define MAX 4

int time_counter = 0;
int disc[MAX], low[MAX];
int visited[MAX];
int parent[MAX];

int graph[MAX][MAX] = {
    {0,1,1,0},
    {1,0,1,1},
    {1,1,0,0},
    {0,1,0,0}
};

void dfs(int u) {
    visited[u] = 1;
    disc[u] = low[u] = ++time_counter;
    for (int v = 0; v < MAX; v++) {
        if (graph[u][v]) {
            if (!visited[v]) {
                parent[v] = u;
                dfs(v);
                if (low[v] > disc[u]) {
                    printf("Bridge found: %d - %d\n", u, v);
                }
                if (low[v] < low[u]) low[u] = low[v];
            } else if (v != parent[u]) {
                if (disc[v] < low[u]) low[u] = disc[v];
            }
        }
    }
}

int main() {
    for (int i = 0; i < MAX; i++) {
        visited[i] = 0;
        parent[i] = -1;
    }
    for (int i = 0; i < MAX; i++) {
        if (!visited[i]) dfs(i);
    }
    return 0;
}
ANo bridges found
BBridge found: 0 - 1\nBridge found: 1 - 3
CBridge found: 2 - 0
DBridge found: 1 - 3
Attempts:
2 left
💡 Hint
Focus on edges that, if removed, increase the number of connected components.
🧠 Conceptual
intermediate
1:30remaining
Understanding Low and Discovery Times
In Tarjan's algorithm for finding bridges, what does the 'low' value of a node represent?
AThe earliest discovery time reachable from the node including itself and its descendants
BThe total number of edges connected to the node
CThe maximum depth of the node in the DFS tree
DThe number of connected components in the graph
Attempts:
2 left
💡 Hint
Think about what 'low' helps to detect in the graph.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Tarjan's Bridge Detection Code
What is the bug in this snippet of Tarjan's algorithm for bridges? void dfs(int u) { visited[u] = 1; disc[u] = low[u] = ++time_counter; for (int v = 0; v < MAX; v++) { if (graph[u][v]) { if (!visited[v]) { parent[v] = u; dfs(v); if (low[v] >= disc[u]) { printf("Bridge found: %d - %d\n", u, v); } if (low[v] < low[u]) low[u] = low[v]; } else if (v != parent[u]) { if (disc[v] < low[u]) low[u] = disc[v]; } } } }
AThe graph adjacency matrix is not symmetric
BThe parent array is not initialized before DFS
CThe condition 'low[v] >= disc[u]' should be 'low[v] > disc[u]' to detect bridges correctly
DThe visited array is not updated correctly
Attempts:
2 left
💡 Hint
Check the condition that identifies bridges carefully.
🚀 Application
advanced
1:30remaining
Number of Bridges in a Complex Graph
Given a graph with edges: 0-1, 1-2, 2-3, 3-4, 2-4, 4-5, 5-6, 6-4, how many bridges does Tarjan's algorithm find?
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Look for edges that, if removed, increase the number of connected components.
❓ Predict Output
expert
2:30remaining
Output of Tarjan's Algorithm on a Disconnected Graph
What is the output of the following code that runs Tarjan's algorithm on a disconnected graph with edges: 0-1, 1-2, 3-4? #include #include #define MAX 5 int time_counter = 0; int disc[MAX], low[MAX]; int visited[MAX]; int parent[MAX]; int graph[MAX][MAX] = { {0,1,0,0,0}, {1,0,1,0,0}, {0,1,0,0,0}, {0,0,0,0,1}, {0,0,0,1,0} }; void dfs(int u) { visited[u] = 1; disc[u] = low[u] = ++time_counter; for (int v = 0; v < MAX; v++) { if (graph[u][v]) { if (!visited[v]) { parent[v] = u; dfs(v); if (low[v] > disc[u]) { printf("Bridge found: %d - %d\n", u, v); } if (low[v] < low[u]) low[u] = low[v]; } else if (v != parent[u]) { if (disc[v] < low[u]) low[u] = disc[v]; } } } } int main() { for (int i = 0; i < MAX; i++) { visited[i] = 0; parent[i] = -1; } for (int i = 0; i < MAX; i++) { if (!visited[i]) dfs(i); } return 0; }
ABridge found: 1 - 2\nBridge found: 0 - 1\nBridge found: 3 - 4
BBridge found: 0 - 1\nBridge found: 1 - 2
CBridge found: 3 - 4
DNo bridges found
Attempts:
2 left
💡 Hint
Remember to run DFS on all disconnected components.

Practice

(1/5)
1. What is the main purpose of Tarjan's algorithm when finding bridges in a graph?
easy
A. To detect cycles in a graph
B. To find edges that, if removed, increase the number of connected components
C. To find the shortest path between two nodes
D. To count the total number of edges in the graph

Solution

  1. Step 1: Understand the definition of a bridge

    A bridge is an edge that, when removed, increases the number of connected components in a graph.
  2. Step 2: Relate Tarjan's algorithm to bridges

    Tarjan's algorithm efficiently finds such edges by using DFS and low-link values.
  3. Final Answer:

    To find edges that, if removed, increase the number of connected components -> Option B
  4. Quick Check:

    Bridges = edges increasing components when removed [OK]
Hint: Bridges disconnect graph parts when removed [OK]
Common Mistakes:
  • Confusing bridges with shortest paths
  • Thinking bridges detect cycles
  • Assuming bridges count edges
2. Which of the following is the correct way to declare the array that stores discovery times in C for Tarjan's algorithm?
easy
A. int disc = new int[MAX];
B. int disc[];
C. int disc[MAX];
D. int[] disc = new int[MAX];

Solution

  1. Step 1: Recall C array declaration syntax

    In C, arrays are declared with type and size like int arr[size];
  2. Step 2: Match with discovery time array

    So, int disc[MAX]; is correct for fixed size MAX.
  3. Final Answer:

    int disc[MAX]; -> Option C
  4. Quick Check:

    C arrays use type name[size] [OK]
Hint: Use type name[size] for arrays in C [OK]
Common Mistakes:
  • Using Java or C++ style array declarations in C
  • Not specifying array size
  • Using dynamic allocation syntax incorrectly
3. Consider the following code snippet for Tarjan's algorithm to find bridges. What will be printed?
void dfs(int u, int parent) {
  disc[u] = low[u] = ++time;
  for (int v : adj[u]) {
    if (disc[v] == -1) {
      dfs(v, u);
      low[u] = min(low[u], low[v]);
      if (low[v] > disc[u])
        printf("Bridge: %d - %d\n", u, v);
    } else if (v != parent) {
      low[u] = min(low[u], disc[v]);
    }
  }
}

// Graph edges:
// 0-1, 1-2, 2-0, 1-3
// Start dfs(0, -1);
medium
A. Bridge: 1 - 3
B. Bridge: 0 - 1
C. Bridge: 2 - 0
D. No bridges

Solution

  1. Step 1: Analyze the graph structure

    The graph has a triangle 0-1-2 and an edge 1-3. Triangle edges are not bridges because removing them doesn't disconnect the graph.
  2. Step 2: Apply Tarjan's condition for bridges

    Edge 1-3 is a bridge because removing it disconnects node 3.
  3. Final Answer:

    Bridge: 1 - 3 -> Option A
  4. Quick Check:

    Only edge 1-3 disconnects graph [OK]
Hint: Bridges disconnect nodes; triangle edges are safe [OK]
Common Mistakes:
  • Thinking all edges in a cycle are bridges
  • Ignoring parent check in DFS
  • Misinterpreting low and disc values
4. The following code snippet is intended to find bridges using Tarjan's algorithm. What is the bug?
void dfs(int u, int parent) {
  disc[u] = low[u] = ++time;
  for (int v : adj[u]) {
    if (disc[v] == -1) {
      dfs(v, u);
      low[u] = min(low[u], low[v]);
      if (low[v] >= disc[u])
        printf("Bridge: %d - %d\n", u, v);
    } else if (v != parent) {
      low[u] = min(low[u], disc[v]);
    }
  }
}
medium
A. The condition should be low[v] > disc[u], not >=.
B. The disc array is not initialized.
C. The parent parameter is not used correctly.
D. The time variable is not incremented.

Solution

  1. Step 1: Understand bridge condition in Tarjan's algorithm

    Bridge condition is low[v] > disc[u], meaning no back edge from v or its descendants to u or above.
  2. Step 2: Identify the bug in condition

    Using >= incorrectly marks edges that are not bridges (like parent edges in cycles).
  3. Final Answer:

    The condition should be low[v] > disc[u], not >=. -> Option A
  4. Quick Check:

    Bridge if low[v] > disc[u] [OK]
Hint: Use strict greater than for bridge check [OK]
Common Mistakes:
  • Using >= instead of > in bridge condition
  • Not initializing discovery times
  • Ignoring parent node in DFS
5. Given a connected undirected graph with 5 nodes and edges: 0-1, 1-2, 2-3, 3-4, 1-4, which edges are bridges according to Tarjan's algorithm?
hard
A. [0-1, 2-3]
B. [2-3, 3-4]
C. [1-4]
D. [0-1]

Solution

  1. Step 1: Visualize the graph

    Edges: 0-1, 1-2, 2-3, 3-4, 1-4. The edge 1-4 creates a cycle with 1-2-3-4.
  2. Step 2: Identify bridges

    Removing 0-1 disconnects node 0, so 0-1 is a bridge. Edges 2-3 and 3-4 are in cycle with 1-4, so not bridges.
  3. Final Answer:

    [0-1] -> Option D
  4. Quick Check:

    Only 0-1 disconnects graph [OK]
Hint: Edges in cycles are not bridges [OK]
Common Mistakes:
  • Marking cycle edges as bridges
  • Ignoring connectivity after edge removal
  • Confusing bridge with articulation point
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard