Experiment 5
BI-CONNECTED COMPONENTS
Aim:
To develop a program that identifies and lists the bi-connected components (biconnected
components) of a given undirected graph using Depth First Search (DFS) and Tarjan's algorithm.
Description:
Identifying Biconnected Components in an Undirected Graph Overview
The program aims to identify and list the biconnected components (BCCs) of a given undirected
graph using Depth First Search (DFS) and Tarjan's algorithm. A biconnected component is a
maximal subgraph in which any two vertices are connected to each other by at least two distinct
paths, ensuring the component remains connected even if any single vertex is removed.
Key Features
Input: The program accepts an undirected graph defined by its vertices and edges.
Algorithm: It implements Tarjan's algorithm, which leverages DFS to efficiently find all BCCs in
linear time.
Output: The program outputs a list of all biconnected components, each represented by a list of its
vertices.
Program:
#include <iostream>
#include <vector>
#include <stack>
#include <list>
using namespace std;
class Graph
{
int V;
list<int> *adj;
void BCCUtil(int u, int disc[], int low[], stack<pair<int, int>> &st, int parent[]);
public:
Graph(int V);
void addEdge(int v, int w);
void BCC();
};
Graph::Graph(int V) : V(V)
{
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
void Graph::BCCUtil(int u, int disc[], int low[], stack<pair<int, int>> &st, int parent[])
{
static int time = 0;
disc[u] = low[u] = ++time;
int children = 0;
for (int v : adj[u])
{
if (disc[v] == -1)
{
children++;
parent[v] = u;
st.push({u, v});
BCCUtil(v, disc, low, st, parent);
low[u] = min(low[u], low[v]);
if ((disc[u] == 1 && children > 1) || (disc[u] > 1 && low[v] >= disc[u]))
{
while (st.top() != make_pair(u, v))
{
cout << st.top().first << "--" << st.top().second << " ";
st.pop();
}
cout << st.top().first << "--" << st.top().second << endl;
st.pop();
}
}
else if (v != parent[u] && disc[v] < disc[u])
{
low[u] = min(low[u], disc[v]);
st.push({u, v});
}
}
}
void Graph::BCC()
{
int *disc = new int[V], *low = new int[V], *parent = new int[V];
stack<pair<int, int>> st;
fill(disc, disc + V, -1);
fill(low, low + V, -1);
fill(parent, parent + V, -1);
for (int i = 0; i < V; ++i)
if (disc[i] == -1)
{
BCCUtil(i, disc, low, st, parent);
while (!st.empty())
{
cout << st.top().first << "--" << st.top().second << " ";
st.pop();
}
cout << endl;
}
delete[] disc;
delete[] low;
delete[] parent;
}
int main()
{
Graph g(7);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(2, 3);
g.addEdge(3, 4);
g.addEdge(4, 5);
g.addEdge(5, 6);
g.addEdge(4, 6);
cout << "Biconnected components in the graph:\n";
g.BCC();
return 0;
}
Output:
Biconnected components in the graph:
6--4 5--6 4--5
3--4
3--1 2--3 1--2
0--1
Result :
Hence, developing a program that identities and list bi-connected components of undirected
graph using DFS and Tarjan's algorithm successfully completed