PRIM’S ALGORITHM - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Wednesday, October 27, 2010

PRIM’S ALGORITHM

/* PRIM’S ALGORITHM */

#include
#define INF 1000
int allpair(int a[],int n);
class prims
{
public:
int perm[10],path[10],re_path[10];
int s,k,n,l,dis,newdis;
prims()
{
dis=0;
}
int wh[10][10];
void getdetails();
void calculate();
};



void prims::getdetails()
{
cout<<"\nEnter the no of nodes: "; cin>>n;
cout<<"\nEnter the weights: "; for(int i=0;i>wh[i][j];
if(wh[i][j]==0)
wh[i][j]=INF;
}
}
}


void prims::calculate()
{
s=0;
perm[s]=1;
cout<<"\nThe path is: \n"; while(allpair(perm,n)) { newdis=INF-1; do { for(int i=0;iwh[s][i]&&perm[i]==0)
{
newdis=wh[s][i];
k=i;
l=s;
}
s++;
while(s"< }
cout<<"\nThe minimum cost is: "< }


int allpair(int a[],int n)
{
int i;
for(i=0;i if(a[i]==0)
break;
if(i return 1;
else
return 0;
}



main()
{
cout<<"\nPRIM'S ALGORITHM TO CONSTRUCT SPANNING TREE AND TO CALCULATE
MINIMUM COST\t\n";
prims p;
p.getdetails();
p.calculate();
}

No comments:

Post a Comment