DAA Lab
DAA Lab
DAA Lab
83 86 77 15 93 35 86 92 49 21 62 27 90
59 63 26 40 26
15 21 26 26 27 35 40 49 59 62 63 77 83
86 86 90 92 93
Ttime taken:2.000000
Week 2
Write and implement an algorithm determining articulation points and the biconnected
components in the given graph
#include<stdio.h>
#include<stdlib.h>
#define max 20
typedef struct
{ int u,v;
}edge;
int dfn[max],l[max],num=1,n;
int a[max][max];
edge stack[max];
int top=-1;
void push(edge e)
{
if(top==max-1)
printf("Stack is full");
else
stack[++top]=e;
}
edge pop()
{ edge e;
if(top==-1)
printf("stack is empty");
else
e=stack[top--];
return e;
}
void bicomp(int u,int v)
{ int w;
edge e;
dfn[u]=num;l[u]=num;num++;
for(w=1;w<=n;w++)
if(a[u][w]==1)
{ if((v!=w) && (dfn[w]<dfn[u]))
{ e.u=u; e.v=w;
printf("\nEdge %d -- %d is pushed to stack",e.u,e.v);
push(e);
}
if(dfn[w]==0)
{ bicomp(w,u);
printf("push2");
l[u]=(l[u]<l[w])?l[u]:l[w];
if(l[w]>=dfn[u])
{
printf("\n New Bicomponent for Articulation point %d:",u);
do
{ e=pop();
printf("%d--%d ",e.u,e.v);
}while(!((e.u==u && e.v==w)||(e.u==w && e.v==u)));
}
}
else
if(w!=v)
l[u]=(l[u]<dfn[w])?l[u]:dfn[w];
}
}
int main()
{ int i,j;
printf("enter the no. of in a given graph");
scanf("%d",&n);
printf("Enter the adjacency matrix of Graph G with %d vertices",n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d", &a[i][j]);
printf("The adjacency matrix of Graph G is:\n");
for(i=1;i<=n; printf("\n"),i++)
for(j=1;j<=n;j++)
printf("%d\t", a[i][j]);
printf("The biconnected components of given graph are:\n");
bicomp(1,0);
return 0;
}
OUTPUT:
enter the no. of in a given graph10
Enter the adjacency matrix of Graph G with 10 vertices
0101000000
1010101100
0101000011
1010000000
0100011100
0000100000
0100100100
0100101000
0010000000
0010000000
The adjacency matrix of Graph G is:
0 1 0 1 0 0 0 0 0 0
1 0 1 0 1 0 1 1 0 0
0 1 0 1 0 0 0 0 1 1
1 0 1 0 0 0 0 0 0 0
0 1 0 0 0 1 1 1 0 0
0 0 0 0 1 0 0 0 0 0
0 1 0 0 1 0 0 1 0 0
0 1 0 0 1 0 1 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
The biconnected components of given graph are:
Edge 1 -- 2 is pushed to stack
Edge 2 -- 3 is pushed to stack
Edge 3 -- 4 is pushed to stack
Edge 4 -- 1 is pushed to stack
Edge 3 -- 9 is pushed to stack
New Bicomponent for Articulation point 3:3--9
Edge 3 -- 10 is pushed to stack
New Bicomponent for Articulation point 3:3--10
Edge 2 -- 5 is pushed to stack
Edge 5 -- 6 is pushed to stack
New Bicomponent for Articulation point 5:5--6
Edge 5 -- 7 is pushed to stack
Edge 7 -- 2 is pushed to stack
Edge 7 -- 8 is pushed to stack
Edge 8 -- 2 is pushed to stack
Edge 8 -- 5 is pushed to stack
New Bicomponent for Articulation point 2:8--5 8--2 7--8 7--2 5--7 2--5
New Bicomponent for Articulation point 2:4--1 3--4 2--3 1--2