Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views5 pages

DAA Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Week 1

/*To sort a set of elements using Quick sort algorithm.*/


#include<stdio.h>
#include<time.h> #define max 500
void qsort(int [],int,int); int partition(int [],int,int);
void main()
{
int a[max],i,n; clock_t s,e; clrscr();
printf("Enter the value of n:"); scanf("%d",&n); for(i=0;i<n;i++)
a[i]=rand()%100;
printf("\nThe array elements before\n"); for(i=0;i<n;i++)
printf("%d\t",a[i]);
s=clock();
delay(100); qsort(a,0,n-1); e=clock();
printf("\nElements of the array after sorting are:\n"); for(i=0;i<n;i++)
printf("%d\t",a[i]); printf("\nTime taken:%f",(e-s)/CLK_TCK); getch();
}
void qsort(int a[],int low,int high)
{
int j; if(low<high)
{
j=partition(a,low,high); qsort(a,low,j-1);
qsort(a,j+1,high);
}
}
int partition(int a[], int low,int high)
{
int pivot,i,j,temp;
pivot=a[low];
i=low+1;
j=high;
while(1)
{
while(pivot>a[i] && i<=high) i++;
while(pivot<a[j]) j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[j];
a[j]=a[low];
a[low]=temp; return j; }}}
OUTPUT

Enter the value of n:18


The array elements before Sorting are:

83 86 77 15 93 35 86 92 49 21 62 27 90
59 63 26 40 26

Elements of the array after sorting are:

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

You might also like