Sei sulla pagina 1di 13

5: Prims Algorithm

Aim:
To write a C program for Prims algorithm

Theory:
Prim's algorithm is a greedy algorithm that finds a
minimum spanning tree for a connected weighted undirected
graph. This means it finds a subset of the edges that tree
that includes every vertex, where the total weight of all the
edges in the tree is minimized.

Algorithm:
Step1: Select the edge which has lowest weight.
Step2: Next lowest weight edge selected and choose any one
edge if there is more than one edge having same weight.
Step3: Leave those edges which cause cycle in graph and
select next lower edge.
Step4: Process is repeated till all the edges are covered and
largest tree is formed.

Input Graph:

Program:
#include<stdio.h>
#include<conio.h>
void dfs(int);
int a[20][20],vst[20],n;
void main( )
{
int i,j,count=0;
clrscr();
printf("Enter the no: of nodes:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
vst[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);

for(i=1;i<=n;i++)
{
if(vst[i])
count++;
}
if(count==n)
printf("Graph is connected\n");
else
printf("Graph is not connected\n");
getch();
}
void dfs ( int v)
{
int i;
vst[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !vst[i ])
{
printf("\n%d->%d\n",v,i);
dfs(i);
}
}

Results:
Enter the no of vertices:5
Enter the cost adjacency matrix:
0 1 0 5 4
1 0 6 0 3

0 6 0 2 0
5 0 2 0 0
4 3 0 0 0
The edges of minimum Cost spanning tree are
1 edge (1,2)=1
2 edge (3,4)=2
3 edge (2,5)=3
4 edge (1,4)=5
Minimum Cost=11

Minimum Spanning Tree:

6: KL Algorithm
Aim:
To write a C program for KL algorithm.

Theory:
KL algorithm is stands forKernighan-Lin algorithm that
finds a minimum cut cost cut by the cut set. This algorithm has
important applications in the layout of digital circuits and
components in VLSI.

Algorithm:
Step1: V= set of 2n elements;
{A,B} is an initial partition such that |A| = |B|.
Step2: i=1.
Compute D(v) for all v .
Step3:
Choose a and b such that g(a, b) = D(a) + D(b) 2*C(a, b) is
maximum. Swap and fix a and b.
Step4:
If all nodes are fixed, go to step5. Otherwise Compute and
update D values for all nodes that are connected to a and b and
are fixed.
i=i+1 and go to step3.

Step5:
Find the move sequence 1m (1<=m<=i), such that G (m)
= g1+g2+g3+..+gm.
If G(m) >0, go to step6. OtherwiseEND.
Step6
Execute m swaps, reset remaining node.
Go to step2.

Input Circuit:

Input Graph:

8
6

9
6

1
0
1
1
1
2

Program:
#include<stdio.h>
#include<conio.h>
int a[20][20], D[20]={0}, fixed[20], G[20], n, i, j, cut, uncut, k,
l, temp, iter=1, count,
swp,p[20]={0},z=1,m[20]={0},q[20]={0},s[20]={0},f;
void kl ( int c)
{
printf ("iteration = %d\n",iter);
for (i=1;i<=n;i++)
{
cut=0; uncut=0;
for (j=1;j<=n;j++)
{
if ( i<=c&&!fixed[i])
{
if (j<=c&&a[i][j]==1)
{
uncut++;
}

else if (j>c&&a[i][j]==1)
{
cut++;
}
}
else if ( i>c&&!fixed[i] )
{
if ( j<=c&&a[i][j]==1 )
{
cut++;
}
else if ( j>c&&a[i][j]==1)
{
uncut++;
}
}
}
if (!fixed[i])
{
D[i]= cut - uncut;
}
else
{
D[i]=-10;
}
}
/*maximum gain computation*/
temp=-10;
for ( i=1;i<=c;i++)
{
for ( j=c+1;j<=n;j++)
{
if ( D[i]+D[j]-2*a[i][j]>temp )
{
temp=D[i]+D[j]-2*a[i][j]; k=i; l=j;
}
}

}
printf ("gain = %d\n",temp);
G[iter]=G[iter-1]+temp;
p[z]=G[iter];
m[z]=k;
q[z]=l;
z=z+1;
printf ( "Gain = %d\n",G[iter++] );
printf ( "swapping %d and %d\n",k,l );
for ( i=1;i<=n;i++)
{
swp=a[i][k]; a[i][k]=a[i][l]; a[i][l]=swp;
}
/* Fixing the swapped nodes*/
fixed[k]=fixed[l]=1;
count=count+2;
if (count!=n)
{
kl( c );
}
}
void main ( )
{
int o[20]={0},max=0,c=0;
clrscr ( );
printf ( "\n Enter the number of vertices:" );
scanf ( "%d",&n );
for(i=1;i<=n;i++)
{
o[i]=i;
}
for ( i=1;i<=n;i++)
{
fixed[i]=0;
}
printf ( "\n Enter graph data in matrix form:\n" );
getch ( );

for ( i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
scanf ( "%d", &a[i][j]);
}
}
c=n/2;
kl(c);
printf("the optimum partition is obtained by swapping\n");
for(z=1;z<=iter;z++)
{
if(p[z]>max)
{
max=s[z]=p[z];
k=z;
}
}
for(z=1;z<=k;z++)
printf("%d and %d\n",m[z],q[z]);
for(z=1;z<=c;z++)
{
for(l=1;l<=k;l++)
{
if(o[z]==m[l])
o[z]=q[l];
}
}
for(z=c+1;z<=n;z++)
{
for(l=1;l<=k;l++)
{
if(o[z]==q[l])
o[z]=m[l];
}
}
printf("the groups after partion are\n");

for(i=1;i<=c;i++)
printf("%d\t",o[i]);
printf("\nand\n");
for(i=c+1;i<=n;i++)
printf("%d\t",o[i]);
getch();
}

Results:
Enter the no of vertices 12
Enter the graph data in matrix form
010000011000
100000100000
000000010010
000000001000
000000000101
000000000100
010000000000
101000000000
100100000000
000011000000
001000000000
000010000000
Iteration =1
gain=3
Gain=3
Swapping 3 and 9
Iteration=2
gain=4
Gain=7
Swapping 5 and 7
Iteration=3
gain=1
Gain=8
Swapping 6 and 8

Iteration=4
gain=-2
Gain=6
Swapping 4 and 11
Iteration=5
gain=-3
Gain= 3
Swapping 2 and 12
Iteration=6
gain= -3
Gain= 0
Swapping 1 and 10
The optimum partition is obtained by swapping
3 and 9
5 and 7
6 and 8
The groups after partition are
1

10

11

12

And
5

The resultant graph is

6
6

3
6

1
0
1
1
1
2

Potrebbero piacerti anche