0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.