0% found this document useful (0 votes)
10 views2 pages

Exp 7

The document outlines the steps to find the minimum cost spanning tree of an undirected graph using Prim's algorithm. It provides a detailed algorithmic approach and includes a C code implementation demonstrating the algorithm. The code initializes a graph, computes the minimum spanning tree, and prints the resulting edges and their weights.

Uploaded by

Sathish Kumar
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)
10 views2 pages

Exp 7

The document outlines the steps to find the minimum cost spanning tree of an undirected graph using Prim's algorithm. It provides a detailed algorithmic approach and includes a C code implementation demonstrating the algorithm. The code initializes a graph, computes the minimum spanning tree, and prints the resulting edges and their weights.

Uploaded by

Sathish Kumar
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/ 2

Find the minimum cost spanning tree of a given undirected graph using Prim’s algorithm.

Aim:
To Find the minimum costs panning tree of a given undirected graph using Prim’s algorithm.
Algorithm:
Step1:Determine an arbitrary vertex as the starting vertex of the MST.
Step2:Follow steps3to5tillthereareverticesthatarenotincludedintheMST(knownasfringe vertex).
Step3:Find edges connecting any tree vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step5:Add the chosen edge to the MST if it does not for many cycle.
Step 6: Return the MST and exit.

#include <limits.h>
#include <stdio.h>
#include<conio.h>
#define V 5
int minKey(int key[],int mstSet[])
{
int v,min=INT_MAX, min_index;
for (v = 0; v < V; v++)
if (mstSet[v] == 0 && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
int printMST(int parent[], int graph[V][V])
{
int i;
printf("Edge \tWeight\n");
for ( i = 1; i < V; i++)
printf("%d - %d \t%d \n", parent[i], i,graph[parent[i]][i]);
return 0;
}
void primMST(int graph[V][V])
{
int parent[V];
int key[V],i,count;
int mstSet[V];
for ( i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = 0;
key[0] = 0;
parent[0] = -1;
for ( count = 0; count < V - 1; count++)
{
int u = minKey(key, mstSet),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, graph);
}

int main()
{
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
clrscr();
primMST(graph);
getch();
return 0;
}

You might also like