VB icon

Prim's Algorithm

Email
Submitted on: 1/9/2015 11:30:00 PM
By: Adri Jovin (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C++ (general)
Views: 2403
 
     Implementataion of Prim's Algorithm
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Prim's Algorithm
// Description:Implementataion of Prim's Algorithm
// By: Adri Jovin (from psc cd)
//**************************************

#include<iostream.h>
#include<conio.h>
class prim
{
int a,b,u,v,i,j,n,noofedge;
int visited[10],min,minicost,cost[10][10];
public:
prim()
{
noofedge=1;
minicost=0;
}
void read();
void prims(int cost[][10],int n);
};
void prim::read()
{
cout<<"\nEnter the number of vertices:\n";
cin>>n;
cout<<"\nEnter the adjacency matrix:\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
}
prims(cost,n);
}
void prim::prims(int cost[][10],int n)
{
for(i=2;i<=n;i++)
visited[i]=0;
cout<<"\nEdges in the spanning tree are: \n";
visited[1]=1;
while(noofedge<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]==0)
continue;
else
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0||visited[v]==0)
{
noofedge++;
cout<<"\nEdge("<<a<<" , "<<b<<"):"<<min;
minicost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\nMinimum cost spanning tree is:"<<minicost;
}
void main()
{
clrscr();
cout<<"\nPrim's algorithm\n";
prim p;
p.read();
getch();
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.