ADVANCED DATA STRUCTURES PROGRAM • LEVEL 20 — ADVANCED GRAPH ALGORITHMS

Find Bridges with Tarjan Low-Link Values

Learn how to find bridges with tarjan low-link values using a clear C program.

AdvancedAdvanced graphsGraph optimization

PROBLEM UNDERSTANDING

Input and expected output

Sample input
No input required
Sample output
Bridge = 3-4
Bridge = 1-3

COMPLETE C PROGRAM

Complete C implementation

ads-tarjan-bridge-detection.c
Open in compiler
#include <stdio.h>

void dfs(int node,int parent,int graph[5][5],int visited[],int tin[],int low[],int*timer){visited[node]=1;tin[node]=low[node]=(*timer)++;for(int next=0;next<5;next++)if(graph[node][next]){if(next==parent)continue;if(visited[next]){if(tin[next]<low[node])low[node]=tin[next];}else{dfs(next,node,graph,visited,tin,low,timer);if(low[next]<low[node])low[node]=low[next];if(low[next]>tin[node])printf("Bridge = %d-%d\n",node,next);}}}

int main(void)
{
    int graph[5][5]={{0,1,1,0,0},{1,0,1,1,0},{1,1,0,0,0},{0,1,0,0,1},{0,0,0,1,0}},visited[5]={0},tin[5],low[5],timer=0;dfs(0,-1,graph,visited,tin,low,&timer);return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Bridge = 3-4
Bridge = 1-3
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. An edge to a DFS child is a bridge when the child subtree cannot reach the parent's discovery time.
  3. Display the computed result.

An edge to a DFS child is a bridge when the child subtree cannot reach the parent's discovery time.

EFFICIENCY

Time and space complexity

Time complexity

O(V + E)

Auxiliary space

O(V + E)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.