0% found this document useful (0 votes)
8 views8 pages

Biconnectedcomponent

Uploaded by

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

Biconnectedcomponent

Uploaded by

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

#include <stdio.

h>

#include <stdlib.h>

#include <stdbool.h>

#define MAX_VERTICES 8

// Node structure for adjacency list

struct Node {

int vertex;

struct Node* next;

};

// Graph structure using adjacency list

struct Graph {

struct Node* adjList[MAX_VERTICES];

int numVertices;

};

// Function to create a new node

struct Node* createNode(int v) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->vertex = v;

newNode->next = NULL;
return newNode;

// Function to initialize the graph

void initGraph(struct Graph* graph, int vertices) {

graph->numVertices = vertices;

for (int i = 0; i < vertices; i++) {

graph->adjList[i] = NULL;

// Function to add an edge to the graph (undirected)

void addEdge(struct Graph* graph, int u, int v) {

// Add edge from u to v

struct Node* newNode = createNode(v);

newNode->next = graph->adjList[u];

graph->adjList[u] = newNode;

// Add edge from v to u

newNode = createNode(u);

newNode->next = graph->adjList[v];

graph->adjList[v] = newNode;

}
// Helper function for DFS traversal

void DFS(struct Graph* graph, int u, int parent, int* disc, int* low, int* parentArray, bool*
articulation, int* bccId, int* bccCount) {

static int time = 0;

int children = 0;

disc[u] = low[u] = ++time;

struct Node* temp = graph->adjList[u];

while (temp) {

int v = temp->vertex;

if (disc[v] == -1) { // v is not visited

children++;

parentArray[v] = u;

DFS(graph, v, u, disc, low, parentArray, articulation, bccId, bccCount);

low[u] = (low[u] < low[v]) ? low[u] : low[v];

// u is an articulation point if

// (1) u is root of DFS tree and has two or more children

if (parent == -1 && children > 1)

articulation[u] = true;
// (2) u is not root and low value of one of its child is more

// than discovery value of u

if (parent != -1 && low[v] >= disc[u])

articulation[u] = true;

// If articulation point found, process the biconnected component (bcc)

if (low[v] >= disc[u]) {

*bccCount = *bccCount + 1;

printf("Biconnected component %d: ", *bccCount);

int x = -1, y = -1;

while (x != u || y != v) {

if (*bccId > 0 && bccId[u] == *bccId && bccId[v] == *bccId) {

break;

x = u;

y = v;

printf("%d-%d ", x, y);

printf("\n");

}
} else if (v != parent) {

low[u] = (low[u] < disc[v]) ? low[u] : disc[v];

temp = temp->next;

// Function to find biconnected components using DFS and Tarjan's algorithm

void findBCC(struct Graph* graph) {

int V = graph->numVertices;

int disc[MAX_VERTICES];

int low[MAX_VERTICES];

int parentArray[MAX_VERTICES];

bool articulation[MAX_VERTICES];

int bccId[MAX_VERTICES];

int bccCount = 0;

// Initialize arrays

for (int i = 0; i < V; i++) {

disc[i] = -1;

low[i] = -1;

parentArray[i] = -1;
articulation[i] = false;

bccId[i] = -1;

// Perform DFS traversal for each component

for (int i = 0; i < V; i++) {

if (disc[i] == -1) {

DFS(graph, i, -1, disc, low, parentArray, articulation, bccId, &bccCount);

int main() {

// Initialize the graph

int vertices = 8;

struct Graph graph;

initGraph(&graph, vertices);

// Add edges to the graph

addEdge(&graph, 0, 1);

addEdge(&graph, 0, 2);

addEdge(&graph, 1, 2);

addEdge(&graph, 1, 3);
addEdge(&graph, 1, 4);

addEdge(&graph, 3, 4);

addEdge(&graph, 3, 5);

addEdge(&graph, 4, 5);

addEdge(&graph, 5, 6);

addEdge(&graph, 5, 7);

// Display the adjacency list representation

printf("Graph Representation (Adjacency List):\n");

for (int i = 0; i < vertices; i++) {

struct Node* temp = graph.adjList[i];

printf("Vertex %d: ", i);

while (temp) {

printf("%d -> ", temp->vertex);

temp = temp->next;

printf("NULL\n");

// Find and print biconnected components

findBCC(&graph);

return 0;
}

You might also like