0% found this document useful (0 votes)
3 views6 pages

Shalini DAA assignment

The document contains two C programs implementing Prim's and Kruskal's algorithms for finding the Minimum Spanning Tree (MST) of a graph. The first program prompts the user for the number of vertices and the adjacency matrix, while the second program asks for the number of edges and their details. Both programs output the edges included in the MST along with the total weight of the tree.

Uploaded by

royrohan310306
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)
3 views6 pages

Shalini DAA assignment

The document contains two C programs implementing Prim's and Kruskal's algorithms for finding the Minimum Spanning Tree (MST) of a graph. The first program prompts the user for the number of vertices and the adjacency matrix, while the second program asks for the number of edges and their details. Both programs output the edges included in the MST along with the total weight of the tree.

Uploaded by

royrohan310306
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/ 6

Design Analysis & Algorithm Lab

PCA-2 (P (PCC-CS494)
CSE – 4th Sem. – 2nd Year

1. Write a C program to implement PRIM’S ALGORITHM .

Program:

#include <stdio.h>
#include <limits.h>
#define MAX_VERTICES 100

int minKey(int key[], int mstSet[], int V);


void printMST(int parent[], int V, int graph[MAX_VERTICES][MAX_VERTICES]);
void primMST(int V, int graph[MAX_VERTICES][MAX_VERTICES]);

int main()
{
int V;
printf("\t\t\t ----# PRIM'S ALGORITHM #----\n");
printf("\nEnter the number of vertices: ");
scanf("%d", &V);
int graph[MAX_VERTICES][MAX_VERTICES];
printf("\nEnter the adjacency matrix of the graph: \n\n");
int i, j;
for (i = 0; i < V; i++)
{
printf("Enter row %d:\n", i + 1);
for (j = 0; j < V; j++)
{
scanf("%d", &graph[i][j]);
}
}
primMST(V, graph);
return 0;
}

int minKey(int key[], int mstSet[], int V)


{
int min = INT_MAX, min_index, v;
for (v = 0; v < V; v++)
if (mstSet[v] == 0 && key[v] < min)
min = key[v], min_index = v;
return min_index;
}

Assignment No. – I Page - 1 SHALINI DAS, 23/CSE/023, </> CSE Dept, FIEM
Design Analysis & Algorithm Lab
PCA-2 (P (PCC-CS494)
CSE – 4th Sem. – 2nd Year

void printMST(int parent[], int V, int graph[MAX_VERTICES][MAX_VERTICES])


{
printf("\nEdge Weight\n");
int i;
int maxW = 0;
for (i = 1; i < V; i++)
{
printf("%d - %d %d \n", parent[i], i, graph[i][parent[i]]);
maxW += graph[i][parent[i]];
}
printf("\nMaximum weight of MST is : %d", maxW);
}

void primMST(int V, int graph[MAX_VERTICES][MAX_VERTICES])


{
int parent[V];
int key[V];
int mstSet[V], i;
for (i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = 0;
key[0] = 0;
parent[0] = -1;

int count;
for (count = 0; count < V - 1; count++)
{
int u = minKey(key, mstSet, V), v;
mstSet[u] = 1;
for (v = 0; v < V; v++)
if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}
printMST(parent, V, graph);
}

Assignment No. – I Page - 2 SHALINI DAS, 23/CSE/023, </> CSE Dept, FIEM
Design Analysis & Algorithm Lab
PCA-2 (P (PCC-CS494)
CSE – 4th Sem. – 2nd Year

Output:

----# PRIM'S ALGORITHM #----

Enter the number of vertices: 4

Enter the adjacency matrix of the graph:

Enter row 1:
11 22 33 44
Enter row 2:

10 20 30 40
Enter row 3:
5
10 15 20
Enter row 4:
20 25 30 35

Edge Weight
0 - 1 10
1 - 2 10
2 - 3 30

Maximum weight of MST is : 50


--------------------------------
Process exited after 61.39 seconds with return value 0
Press any key to continue . . .

Assignment No. – I Page - 3 SHALINI DAS, 23/CSE/023, </> CSE Dept, FIEM
Design Analysis & Algorithm Lab
PCA-2 (P (PCC-CS494)
CSE – 4th Sem. – 2nd Year

2. Write a C program to implement KRUSKAL’S ALGORITHM .

Program:

#include <stdio.h>
#include <stdlib.h>

#define MAX_VERTICES 100


#define MAX_EDGES 1000

typedef struct {
int src, dest, weight;
} Edge;

typedef struct {
int parent;
int rank;
} Subset;

Edge edges[MAX_EDGES];

int find(Subset subsets[], int i);


void unionSets(Subset subsets[], int x, int y);
int compareEdges(const void *a, const void *b);
void kruskalMST(int V, int E);

int find(Subset subsets[], int i) {


if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}

void unionSets(Subset subsets[], int x, int y) {


int xroot = find(subsets, x);
int yroot = find(subsets, y);

if (subsets[xroot].rank < subsets[yroot].rank)


subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}

Assignment No. – I Page - 4 SHALINI DAS, 23/CSE/023, </> CSE Dept, FIEM
Design Analysis & Algorithm Lab
PCA-2 (P (PCC-CS494)
CSE – 4th Sem. – 2nd Year

int compareEdges(const void *a, const void *b) {


Edge *e1 = (Edge *)a;
Edge *e2 = (Edge *)b;
return e1->weight - e2->weight;
}

void kruskalMST(int V, int E) {


qsort(edges, E, sizeof(edges[0]), compareEdges);

Subset *subsets = (Subset *)malloc(V * sizeof(Subset));


int v;
for (v = 0; v < V; ++v) {
subsets[v].parent = v;
subsets[v].rank = 0;
}

Edge result[MAX_VERTICES];
int e = 0, i = 0;
int totalWeight = 0;

if (E < V - 1) {
printf("\nError: Not enough edges to form a spanning tree.\n");
free(subsets);
return;
}

while (e < V - 1 && i < E) {


Edge next_edge = edges[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);

if (x != y) {
result[e++] = next_edge;
unionSets(subsets, x, y);
}
}

printf("\nEdge Weight\n");
for (i = 0; i < e; ++i) {
printf("%d - %d %d\n", result[i].src, result[i].dest, result[i].weight);
totalWeight += result[i].weight;
}
printf("\nTotal weight of MST is : %d\n", totalWeight);
free(subsets);
}

Assignment No. – I Page - 5 SHALINI DAS, 23/CSE/023, </> CSE Dept, FIEM
Design Analysis & Algorithm Lab
PCA-2 (P (PCC-CS494)
CSE – 4th Sem. – 2nd Year

int main() {
int V, E, i;
printf("\t\t\t ----# KRUSKAL'S ALGORITHM #----\n");
printf("\nEnter the number of vertices: ");
scanf("%d", &V);
printf("Enter the number of edges: ");
scanf("%d", &E);

printf("\nEnter each edge in the format: <source> <destination> <weight>\n\n");


for (i = 0; i < E; i++) {
printf("Edge %d: ", i + 1);
scanf("%d %d %d", &edges[i].src, &edges[i].dest, &edges[i].weight);
}

kruskalMST(V, E);
return 0;
}

Output:

----# KRUSKAL'S ALGORITHM #----

Enter the number of vertices: 4


Enter the number of edges: 4

Enter each edge in the format: <source> <destination> <weight>

Edge 1: 1 2 10
Edge 2: 2 3 20
Edge 3: 3 4 30
Edge 4: 4 5 40

Edge Weight
1 - 2 10
2 - 3 20
3 - 4 30

Total weight of MST is : 60

--------------------------------
Process exited after 25.88 seconds with return value 0
Press any key to continue . . .

Assignment No. – I Page - 6 SHALINI DAS, 23/CSE/023, </> CSE Dept, FIEM

You might also like