0% found this document useful (0 votes)
4 views5 pages

Assignment5-1

The document contains C programs for implementing three algorithms: Topological Sorting, Prim's Minimum Spanning Tree, and Kruskal's Minimum Spanning Tree. Each program includes user input for the number of vertices and the adjacency matrix or weights of the graph, followed by the implementation of the respective algorithm to compute and display the results. The code snippets are structured to handle graph representation and the necessary computations for each algorithm.

Uploaded by

gangurdesahil52
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Assignment5-1

The document contains C programs for implementing three algorithms: Topological Sorting, Prim's Minimum Spanning Tree, and Kruskal's Minimum Spanning Tree. Each program includes user input for the number of vertices and the adjacency matrix or weights of the graph, followed by the implementation of the respective algorithm to compute and display the results. The code snippets are structured to handle graph representation and the necessary computations for each algorithm.

Uploaded by

gangurdesahil52
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Set A

a) Write a C program for the implementation of Topological sorting.


// C Program to implement Topological Sort
//Slip 2 Q1,Slip4 Q2 Slip12 Q2

#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>

#define MAX_VERTICES 100

int minweight(int weight[], bool visited[],int numVertices)


{
int min = INT_MAX, min_index;

for (int v = 0; v < numVertices; v++) {


if (visited[v] == false && weight[v] < min)
{
min = weight[v];
min_index = v;
}
}
return min_index;
}

// Function to print the constructed MST and its total cost


void printMST(int parent[], int graph[MAX_VERTICES][MAX_VERTICES],int numVertices)
{
int totalCost = 0;

printf("MST for the graph:\n");


printf("Edge \tWeight\n");
for (int i = 1; i < numVertices; i++)
{
printf("%d - %d \t%d \n", parent[i]+1,i+1,graph[i][parent[i]]);
totalCost += graph[i][parent[i]];
}
printf("Total cost of MST: %d\n", totalCost);
}

// Function to construct and print MST for a graph


// represented using adjacency matrix representation
void primMST(int graph[MAX_VERTICES][MAX_VERTICES],int numVertices)
{
// Array to store constructed MST
int parent[numVertices];

// Array to store the weights


int weight[numVertices];

// To represent set of vertices included in MST


bool visited[numVertices];

// Initialize all weights as INFINITE


for (int i = 0; i < numVertices; i++)
{
weight[i] = INT_MAX;
visited[i] = false;
}

// Always include the first 1st vertex in MST.


weight[0] = 0;
parent[0] = -1;

// The MST will have V vertices


for (int count = 0; count < numVertices - 1; count++)
{
// Pick the minimum weight vertex from the set of
// vertices not yet included in MST
int u = minweight(weight, visited,numVertices);

// Add the picked vertex to the MST Set


visited[u] = true;

// Consider only those vertices which are not yet


// included in MST
for (int v = 0; v < numVertices; v++)
{
if (graph[u][v] && visited[v] == false && graph[u][v] < weight[v])
{
parent[v] = u;
weight[v] = graph[u][v];
}
}
}
printMST(parent,graph,numVertices);
}

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]);
}
}

// Print the MST


primMST(graph,numVertices);

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 p[MAX_VERTICES] = {0};


int applyfind(int i)
{
while(p[i] != 0)
i=p[i];
return i;
}
int applyunion(int i,int j)
{
if(i!=j)
{
p[j]=i;
return 1;
}
return 0;
}
int main()
{

int cost[MAX_VERTICES][MAX_VERTICES],numVertices;
int i, j;

printf("Enter the number of vertices: ");


scanf("%d", &numVertices);

printf("Enter Weights graph\n");


for (int i = 0; i < numVertices; i++)
{
for (int j = 0; j < numVertices; j++)
{
scanf("%d",&cost[i][j]);

}
}

for (int i = 0; i < numVertices; i++)


{
for (int j = 0; j < numVertices; j++)
{
if (cost[i][j] == 0) {
cost[i][j] = inf;
}
}
}
printf("Minimum Cost Spanning Tree: \n");
while(ne < numVertices)
{
int min_val = inf;
for(i=0; i<numVertices; i++)
{
for(j=0; j <numVertices; j++)
{
if(cost[i][j] < min_val)
{
min_val = cost[i][j];
a = u = i;
b = v = j;
}
}
}
u = applyfind(u);
v = applyfind(v);
if(applyunion(u, v) != 0)
{
printf("%d -> %d\n", a, b);
mincost +=min_val;
}
cost[a][b] = cost[b][a] = 999;
ne++;
}
printf("Minimum cost = %d",mincost);
return 0;
}

You might also like