0% found this document useful (0 votes)
2 views1 page

Prim s Algorithm

The document presents a C program implementing Prim's algorithm to find the minimum spanning tree of a graph. It prompts the user to input the number of vertices and the cost matrix, then calculates and displays the edges included in the minimum spanning tree along with the total minimum cost. The example output demonstrates the algorithm's functionality with a sample input of three vertices.

Uploaded by

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

Prim s Algorithm

The document presents a C program implementing Prim's algorithm to find the minimum spanning tree of a graph. It prompts the user to input the number of vertices and the cost matrix, then calculates and displays the edges included in the minimum spanning tree along with the total minimum cost. The example output demonstrates the algorithm's functionality with a sample input of three vertices.

Uploaded by

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

PRIM'S ALGORITHM

#include<stdio.h>
int a,b,u,n,v,i,j,ne;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
printf("Enter the no of vertex:");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
}
}
visited[1]=1;
printf("\n");
for(ne=1;ne<n;ne++)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]!=0&&visited[j]==0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
}
if(visited[u]==0||visited[v]==0)
{
printf("Edge %d:(%d to %d)min cost is %d\n",ne,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("Min cost= %d",mincost);
}

OUTPUT:

Enter the no of vertex:3


Enter the cost matrix
999 1 2
1 999 2
4 2 999

Edge 1:(1 to 2)min cost is 1


Edge 2:(1 to 3)min cost is 2
Min cost= 3

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