0% found this document useful (0 votes)
10 views3 pages

22 (1)

The document contains a C++ implementation of a graph class that identifies biconnected components using Depth First Search (DFS). It includes methods for adding edges and finding biconnected components, as well as a main function that demonstrates its usage with a sample graph. The program outputs the biconnected components found in the graph.

Uploaded by

resoce3697
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

22 (1)

The document contains a C++ implementation of a graph class that identifies biconnected components using Depth First Search (DFS). It includes methods for adding edges and finding biconnected components, as well as a main function that demonstrates its usage with a sample graph. The program outputs the biconnected components found in the graph.

Uploaded by

resoce3697
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <vector>
#include <stack>
using namespace std;

class Graph {
private:
int V; // Number of vertices
vector<vector<int>> adj; // Adjacency list

// Helper function for DFS


void DFS(int u, int& time, vector<int>& disc, vector<int>& low, stack<pair<int,
int>>& st,
vector<bool>& articulation, vector<vector<int>>& bcc) {
disc[u] = low[u] = ++time;
int children = 0;

// Traverse all vertices adjacent to this vertex


for (int v : adj[u]) {
if (disc[v] == -1) { // If v is not visited
children++;
st.push({u, v});
DFS(v, time, disc, low, st, articulation, bcc);

// Check if the subtree rooted at v has a connection back to one of the ancestors
of
low[u] = min(low[u], low[v]);

// u is an articulation point in following cases


// (1) u is root of DFS tree and has two or more children.
if(disc[u]==1&&children>1)
articulation[u] = true;

// (2) If u is not root and low value of one of its child is more than discovery
value of

if (disc[u]>1&&low[v]>=disc[u]) {
articulation[u] = true;
vector<int> temp;
while (st.top() != make_pair(u, v)) {
temp.push_back(st.top().first);
temp.push_back(st.top().second);
st.pop();
}
temp.push_back(st.top().first);
temp.push_back(st.top().second);
st.pop();
bcc.push_back(temp);
}
} else if (v != st.top().first && disc[v] < low[u]) {
low[u] = disc[v];
st.push({u, v});
}
}
}

public:
Graph(int vertices) : V(vertices) {
adj.resize(V);
}

// Function to add an edge in the graph


void addEdge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);

// Function to find biconnected components


vector<vector<int>> findBCC() {
vector<int> disc(V, -1), low(V, -1);
vector<bool> articulation(V, false);
stack<pair<int, int>> st;
vector<vector<int>> bcc;
int time = 0;

// Perform DFS to find articulation points and bridges


for (int i = 0; i < V; i++) {
if (disc[i] == -1) // Call DFS for each component if not visited
DFS(i, time, disc, low, st, articulation, bcc);

// Check for remaining edges in stack


vector<int> temp;
while (!st.empty()) {
temp.push_back(st.top().first);
temp.push_back(st.top().second);
st.pop();
}
if (!temp.empty())
bcc.push_back(temp);
}

return bcc;
}
};

int main() {
// Create a graph given in the example
Graph g(12);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(2, 3);
g.addEdge(2, 4);
g.addEdge(3, 5);
g.addEdge(4, 5);
g.addEdge(4, 6);
g.addEdge(5, 7);
g.addEdge(6, 7);
g.addEdge(7, 8);
g.addEdge(8, 9);
g.addEdge(8, 10);
g.addEdge(9, 10);
g.addEdge(9, 11);
g.addEdge(10, 11);

// Find and print all biconnected components


vector<vector<int>> bcc = g.findBCC();
cout << "Biconnected Components:\n";
for (int i = 0; i < bcc.size(); i++) {
cout << "Component " << i + 1 << ": ";
for (int j = 0; j < bcc[i].size(); j += 2) {
cout << "{" << bcc[i][j] << "," << bcc[i][j + 1] << "} ";
}

cout << endl;


}

return 0;
}

You might also like