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

Exp 5 Aoa

The document describes an experiment conducted by Krishnacharan Mewada to implement the Minimum Spanning Tree using Prim's Algorithm. It includes the C code for the algorithm, which initializes a graph and computes the minimum spanning tree, printing the edges and their weights. The code demonstrates the use of arrays to track keys, parent nodes, and the set of included vertices.
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 views3 pages

Exp 5 Aoa

The document describes an experiment conducted by Krishnacharan Mewada to implement the Minimum Spanning Tree using Prim's Algorithm. It includes the C code for the algorithm, which initializes a graph and computes the minimum spanning tree, printing the edges and their weights. The code demonstrates the use of arrays to track keys, parent nodes, and the set of included vertices.
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/ 3

Experiment No:-5

Name:- Krishnacharan Mewada

Roll no:- 165 Batch:- A3

Aim:- Implementa on of Minimum Spanning Tree using Prims Algorithm

#Code:
#include <stdio.h>

#include <limits.h>

#define MAX_VERTICES 10

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

int min = INT_MAX, minIndex;

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

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

min = key[v];

minIndex = v;

return minIndex;

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

int parent[V];

int key[V];

int mstSet[V];

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

key[i] = INT_MAX;

mstSet[i] = 0;
}

key[0] = 0;

parent[0] = -1;

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

int u = minKey(key, mstSet, V);

mstSet[u] = 1;

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

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

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

parent[v] = u;

prin ("Edge \tWeight\n");

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

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

int main() {

prin ("\nKrishnacharan Mewada\n");

int graph[MAX_VERTICES][MAX_VERTICES] = {

{0, 2, 0, 6, 0, 0},

{2, 0, 3, 8, 5, 0},

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

{0, 5, 7, 9, 0, 4},

{0, 0, 0, 0, 4, 0}

};

int V = 6;

primMST(graph, V);

return 0;

Output:-

You might also like