Assignment5-1
Assignment5-1
#include<stdio.h>
int main()
{
int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;
printf("Enter the no of vertices:\n");
scanf("%d",&n);
printf("Enter the adjacency matrix:\n");
for(i=0;i<n;i++)
{
printf("Enter row %d\n",i+1);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<n;i++)
{
indeg[i]=0;
flag[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
indeg[i]=indeg[i]+a[j][i];
}
}
printf("\nThe topological order is:\t");
while(count<n)
{
for(k=0;k<n;k++)
{
if((indeg[k]==0) && (flag[k]==0))
{
printf("%d \t",(k+1));
flag [k]=1;
}
for(i=0;i<n;i++)
{
if(a[i][k]==1)
indeg[k]--;
}
}
count++;
}
return 0;
}
b) Write a C program for the Implementation of Prim’s Minimum spanning tree algorithm.
//b) Write a C program for the Implementation of Prim’s Minimum spanning tree algorithm.
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
int main()
{
int numVertices,i,j,graph[MAX_VERTICES][MAX_VERTICES];
printf("Enter the number of vertices: ");
scanf("%d", &numVertices);
printf("Enter Graph\n");
for (i = 0; i < numVertices; i++)
{
for (j = 0; j < numVertices; j++)
{
scanf("%d",&graph[i][j]);
}
}
return 0;
}
Set B
a) Write a C program for the Implementation of Kruskal’s Minimum spanning tree algorithm.
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
const int inf = 999999;
int k, a, b, u, v, n, ne = 1;
int mincost = 0;
int cost[MAX_VERTICES][MAX_VERTICES],numVertices;
int i, j;
}
}