#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;
};
// Edge structure to store edges during DFS
struct Edge{
int u,v;
};
//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 initilize 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 (since undirected)
newNode = createNode(u);
newNode->next = graph->adjlist[v];
graph->adjlist [v] = newNode;
}
//DFS function to find articulation points and biconnected components
void DFS (struct Graph* graph, int u, int parent, int* disc, int* low, int* parentArray, bool* articulation, struct Edge* stack, int* stackTop, int* bccCount
){
static int time = 0;
int children = 0;
disc [u] = low [u]= ++time; // Set discovery time and low value
// Traverse all adjacency vertices of u
struct Node* temp=graph->adjlist[u];
while(temp){
int v=temp->vertex;
if (disc[v]==-1){ //is not visited yel
children++;
parentArray[v] = u;
// push edge (u,v) to stack
stack[(*stackTop)++] = (struct Edge){u,v};
//Recur for the child vertex
DFS (graph, v, u, disc, low, parentArray, articulation,stack, stackTop,bccCount);
//Check if the subtree rooted at v has a back Connection to an ancestor of u
low[u]=(low[u]<low [v])? low[u]: low[v];
//If u is root and has more than one child, its an anticulation point
if(parent == -1&&children >1)
articulation[u] = true;
//If u is not root and low value of one of it's child is more than or equal to u's discovery time
if (parent!=-1&& low[v]>=disc[u]){
articulation [u] = true;
//u is an articulation point, print the edges forming the biconnected component
printf("Biconnected component %d \n", ++(*bccCount));
struct Edge edge;
do{
edge=stack[--(*stackTop)];
printf("%d-%d", edge.u, edge.v);
}while(edge.u!= u || edge.v!= v);
printf("\n");
}
}else if (v!=parent&& disc[v] < disc[u]){
//update low value of u for back edge
low[u] = (low [u]<disc[v])? low[u]: disc[v];
// Push back edge (u,v) to stack
stack[(*stackTop)++] = (struct Edge){u, v};
}
temp = temp->next;
}
}
// Function to find and paint all biconnected components using DFS
void findBCC(struct Graph* graph){
int V= graph->numVertices;
int disc[MAX_VERTICES], low[MAX_VERTICES], parentArray[MAX_VERTICES];
bool articulation [MAX_VERTICES];
struct Edge stack[MAX_VERTICES*MAX_VERTICES];
//Stack to store edges
int stackTop=0, bccCount=0;
// Initilize discovery and low values
for(int i=0; i<V;i++){
disc[i]=-1;
low[i]=-1;
parentArray[i] =-1;
articulation[i] = false;
}
//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, stack,&stackTop,&bccCount);
}
}
// Print articulation points
printf("Articulation points: \n");
for (int i=0; i<V; i++){
if (articulation[i]){
printf("%d",i);
}
}
printf("\n");
}
int main(){
// Initialize the graph with 8 vertices
int vertices = 8;
struct Graph graph;
initGraph(&graph,vertices);
//Add edges to the graph (undirected)
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 of the graph
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 and articulation points
findBCC(&graph);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPHN0ZGJvb2wuaD4KI2RlZmluZ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5nIHRoZSBiaWNvbm5lY3RlZCBjb21wb25lbnQKICAgICAgICAgICAgcHJpbnRmKCJCaWNvbm5lY3RlZCBjb21wb25lbnQgJWQgXG4iLCArKygqYmNjQ291bnQpKTsKICAgICAgICAgICAgc3RydWN0IEVkZ2UgZWRnZTsKICAgICAgICAgICAgZG97CiAgICAgICAgICAgICAgICBlZGdlPXN0YWNrWy0tKCpzdGFja1RvcCldOwogICAgICAgICAgICAgICAgcHJpbnRmKCIlZC0lZCIsIGVkZ2UudSwgZWRnZS52KTsgCiAgICAgICAgICAgICAgICB9d2hpbGUoZWRnZS51IT0gdSB8fCBlZGdlLnYhPSB2KTsKICAgICAgICAgICAgICAgIHByaW50ZigiXG4iKTsKICAgICAgICB9CiAgICAgICAgICAgICAgICB9ZWxzZSBpZiAodiE9cGFyZW50JiYgZGlzY1t2XSA8IGRpc2NbdV0pewogICAgICAgICAgICAgICAgICAgIC8vdXBkYXRlIGxvdyB2YWx1ZSBvZiB1IGZvciBiYWNrIGVkZ2UKICAgICAgICAgICAgICAgIGxvd1t1XSA9IChsb3cgW3VdPGRpc2Nbdl0pPyBsb3dbdV06IGRpc2Nbdl07CiAgICAgICAgICAgICAgICAvLyBQdXNoIGJhY2sgZWRnZSAodSx2KSB0byBzdGFjayAKICAgICAgICAgICAgICAgIHN0YWNrWygqc3RhY2tUb3ApKytdID0gKHN0cnVjdCBFZGdlKXt1LCB2fTsKICAgICAgICAgICAgfQogICAgICAgIHRlbXAgPSB0ZW1wLT5uZXh0OwogICAgfQp9CiAgICAgICAgICAgICAgICAgICAgCi8vIEZ1bmN0aW9uIHRvIGZpbmQgYW5kIHBhaW50IGFsbCBiaWNvbm5lY3RlZCBjb21wb25lbnRzIHVzaW5nIERGUwoKdm9pZCBmaW5kQkNDKHN0cnVjdCBHcmFwaCogZ3JhcGgpewppbnQgVj0gZ3JhcGgtPm51bVZlcnRpY2VzOwppbnQgZGlzY1tNQVhfVkVSVElDRVNdLCBsb3dbTUFYX1ZFUlR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