0% found this document useful (0 votes)
46 views8 pages

Prim's Algorithm: Theory

Prim's algorithm finds a minimum spanning tree (MST) of a weighted undirected graph. It operates by building the MST one edge at a time, from a starting node. At each step it adds the minimum weight edge that connects the MST to another node. The algorithm repeats until all nodes are connected. The code implements Prim's algorithm to find the MST and minimum cost for a given graph. It takes input for the number of nodes and adjacency matrix, runs Prim's algorithm, and outputs the edges in the MST and total minimum cost.

Uploaded by

manash hamal
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)
46 views8 pages

Prim's Algorithm: Theory

Prim's algorithm finds a minimum spanning tree (MST) of a weighted undirected graph. It operates by building the MST one edge at a time, from a starting node. At each step it adds the minimum weight edge that connects the MST to another node. The algorithm repeats until all nodes are connected. The code implements Prim's algorithm to find the MST and minimum cost for a given graph. It takes input for the number of nodes and adjacency matrix, runs Prim's algorithm, and outputs the edges in the MST and total minimum cost.

Uploaded by

manash hamal
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/ 8

Prim’s Algorithm

Theory:
Code:
#include<stdio.h>
#include <time.h>

int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];

int main()
{
printf("\nEnter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
double starttime=clock();
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]< min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
double endtime=clock();
printf("\n Minimun cost=%d",mincost);
printf("\nThe time taken is %f\n",(endtime-starttime)/CLOCKS_PER_SEC);

return 0;
}

Output:
Krushkal’s Algorithm:

Theory:
Code:
#include<stdio.h>
#include<time.h>
struct edge
{
int u,v,cost;
};
typedef struct edge edge;

int find(int v,int parent[])


{
while(parent[v]!=v)
{
v=parent[v];
}
return v;
}

void union_ij(int i,int j,int parent[])


{
if(i<j)
parent[j]=i;
else
parent[i]=j;
}

void kruskal(int n,edge e[],int m)


{
int count,k,i,sum,u,v,j,t[10][10],p,parent[10];
edge temp;
count=0;
k=0;
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<m-1;j++)
{
if(e[j].cost>e[j+1].cost)
{
temp.u=e[j].u;
temp.v=e[j].v;
temp.cost=e[j].cost;

e[j].u=e[j+1].u;
e[j].v=e[j+1].v;
e[j].cost=e[j+1].cost;
e[j+1].u=temp.u;
e[j+1].v=temp.v;
e[j+1].cost=temp.cost;
}
}
}
for(i=0;i<n;i++)
parent[i]=i;
p=0;
while(count!=n-1)
{
u=e[p].u;
v=e[p].v;
i=find(u,parent);
j=find(v,parent);
if(i!=j)
{
t[k][0]=u;
t[k][1]=v;
k++;
count++;
sum+=e[p].cost;
union_ij(i,j,parent);
}
p++;
}
if(count==n-1)
{
printf("Spanning tree exists\n");
printf("The spanning tree is as follows:\n");
for(i=0;i<n-1;i++)
{
printf("%d-->%d\n",t[i][0],t[i][1]);
}
printf("\nThe cost of the spanning tree is %d\n",sum);
}
else
printf("\n spanning tree does not exist");
}
int main()
{
int n,m,a,b,i,cost;
edge e[20];
printf("Enter the number of vertices:");
scanf("%d",&n);
printf("Enter the number of edges:\n");
scanf("%d",&m);
printf("Enter the edge list( u v cost)\n");
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&cost);
e[i].u=a;
e[i].v=b;
e[i].cost=cost;
}
double starttime=clock();
kruskal(n,e,m);
double endtime=clock();
printf("The time taken is %f\n",(endtime-starttime)/CLOCKS_PER_SEC);
return 0;
}

Output:
p

You might also like