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

Practical-10: Implementation of Prim's Algorithm:-: Ada-Lab Poradhiya Chirag I en - No:180280107082

The document implements Prim's algorithm to find the minimum spanning tree (MST) of a graph. It includes code to find the minimum key value not yet included in the MST, update the parent list and key values, and print the edges of the MST. The code takes a graph as input, initializes the key and parent lists, iterates V-1 times to build up the MST by selecting the minimum key value at each step, and finally prints the edges of the MST.

Uploaded by

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

Practical-10: Implementation of Prim's Algorithm:-: Ada-Lab Poradhiya Chirag I en - No:180280107082

The document implements Prim's algorithm to find the minimum spanning tree (MST) of a graph. It includes code to find the minimum key value not yet included in the MST, update the parent list and key values, and print the edges of the MST. The code takes a graph as input, initializes the key and parent lists, iterates V-1 times to build up the MST by selecting the minimum key value at each step, and finally prints the edges of the MST.

Uploaded by

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

ADA-LAB Poradhiya Chirag I

En.no:180280107082

Practical-10: Implementation of prim’s Algorithm:-

 Input code:
#include <limits.h>

#include <stdbool.h>

#include <stdio.h>

#define V 5

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

int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)

if (mstSet[v] == false && key[v] < min)

min = key[v], min_index = v;

return min_index;

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

int parent[V];

int key[V];

bool mstSet[V];

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

Computer Engineering
L D College of Engineering Page 1
ADA-LAB Poradhiya Chirag I
En.no:180280107082
key[i] = INT_MAX, mstSet[i] = false;
key[0] = 0;

parent[0] = -1;

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

int u = minKey(key, mstSet);

mstSet[u] = true;

for (int v = 0; v < V; v++)

if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])

parent[v] = u, key[v] = graph[u][v];

printf("Edge \tWeight\n");

for (int i = 1; i < V; i++)

printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);

void main()

int graph[V][V] = { { 0, 3, 1, 7, 4 },

{ 3, 0, 4, 8, 5 },

{ 1, 4, 0, 6, 7 },

{ 7, 8, 6, 0, 9 },

Computer Engineering
L D College of Engineering Page 2
ADA-LAB Poradhiya Chirag I
En.no:180280107082
{ 4, 5, 7, 9, 0 } };

primMST(graph);

Output snapshot:

*****

Computer Engineering
L D College of Engineering Page 3
ADA-LAB Poradhiya Chirag I
En.no:180280107082

Computer Engineering
L D College of Engineering Page 4
ADA-LAB Poradhiya Chirag I
En.no:180280107082

Computer Department
L D College of Engineering Page 5

You might also like