0% found this document useful (0 votes)
20 views7 pages

Prims Kruskals RK

The document presents implementations of Prim's and Kruskal's algorithms in C for finding the Minimum Spanning Tree (MST) of a graph with 8 vertices. It includes code for adding edges, calculating the MST, and printing the results, along with the summation of edge weights. The algorithms are applied to the same graph, demonstrating their functionality and correctness through code execution.

Uploaded by

Raghav Kohli
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)
20 views7 pages

Prims Kruskals RK

The document presents implementations of Prim's and Kruskal's algorithms in C for finding the Minimum Spanning Tree (MST) of a graph with 8 vertices. It includes code for adding edges, calculating the MST, and printing the results, along with the summation of edge weights. The algorithms are applied to the same graph, demonstrating their functionality and correctness through code execution.

Uploaded by

Raghav Kohli
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/ 7

Digital Assignment

1. Prim’s Algorithm:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
//Raghav Kohli – 23BCI0219
#define vertices 8

void addEdge (int graph[vertices][vertices], int u, int v, int weight)


{
graph[u][v] = weight;
graph[v][u] = weight;
}

int minKey (int key[], bool mstSet[]) {


// Intialize min
int min = INT_MAX, min_index;

for (int i = 1 ; i < vertices; i++) {


if (mstSet[i] == false && key[i] < min) {
min = key[i];
min_index = i;
}
} return min_index;
}

void printMST (int parent[], int graph[vertices][vertices]) {


int sum_wts = 0;
printf("Edges \tWeight\n");

for (int i = 2; i < vertices; i++) {


printf("%d - %d \t%d \n", i, parent[i], graph[i][parent[i]]);
sum_wts += graph[i][parent[i]];
} printf("Summation of edge weights of the MST is: %d", sum_wts);
}

void primMST (int graph[vertices][vertices]) {


int parent[vertices];
int key[vertices];
bool mstSet[vertices];
for (int i = 1; i < vertices; i++) {
key[i] = INT_MAX;
mstSet[i] = false;
}

key[1] = 0; // printing will start from 2


parent[1] = -1;

for (int count = 1; count < vertices - 1; count++) {


int u = minKey(key, mstSet); // picking min key vertex
mstSet[u] = true;

for (int i = 1; i < vertices; i++) {


if (graph[u][i] && mstSet[i] == false && graph[u][i] <
key[i]) {
parent[i] = u;
key[i] = graph[u][i];
}
}
} printMST(parent, graph);
}

int main (void) {


int graph[vertices][vertices];
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++)
graph[i][j] = 0;
}

addEdge(graph, 1, 2, 28);
addEdge(graph, 1, 6, 10);
addEdge(graph, 2, 3, 16);
addEdge(graph, 2, 7, 14);
addEdge(graph, 3, 4, 12);
addEdge(graph, 4, 5, 22);
addEdge(graph, 4, 7, 18);
addEdge(graph, 5, 6, 25);
addEdge(graph, 5, 7, 24);

primMST(graph);
}
Output:

2. Kruskal’s Algorithm:

//23BCI0219 - Raghav Kohli


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

#define vertices 8
#define edges 9

typedef struct
{
int u, v, weight;
} Edge;
Edge graph[edges];
int edgeCount = 0;

int parent[vertices];

void appendEdge(int u, int v, int weight)


{
graph[edgeCount].u = u;
graph[edgeCount].v = v;
graph[edgeCount].weight = weight;
edgeCount++;
}

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


Edge *edgeA = (Edge *)a;
Edge *edgeB = (Edge *)b;
return edgeA->weight - edgeB->weight;
}

int find(int i)
{
if (parent[i] == i)
return i;
return parent[i] = find(parent[i]);
}

void unionSets(int u, int v)


{
int rootU = find(u);
int rootV = find(v);
if (rootU != rootV)
parent[rootU] = rootV;
}

void kruskalMST()
{
// sorting in ascending order- the edge wts.
qsort(graph, edgeCount, sizeof(Edge), compare);

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


parent[i] = i;
int mstwt = 0;
printf("Edges\tWeights\n");

for (int i = 0; i < edgeCount; i++) {


int u = graph[i].u;
int v = graph[i].v;
int weight = graph[i].weight;

//checking cyclicity
if (find(u) != find(v)) {
printf("%d - %d %d\n", u, v, weight);
mstwt += weight;
unionSets(u, v);
}
}

printf("Summation of the edge wts of the MST is: %d\n", mstwt);


}

int main (void) {


appendEdge(1, 2, 28);
appendEdge(1, 6, 10);
appendEdge(2, 3, 16);
appendEdge(2, 7, 14);
appendEdge(3, 4, 12);
appendEdge(4, 5, 22);
appendEdge(4, 7, 18);
appendEdge(5, 6, 25);
appendEdge(5, 7, 24);

kruskalMST();
}

Ouput:
So, we have successfully applied Prim’s and Kruskal’s Algo to the given graph manually, and verified our results
through code in C.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy