//最小生成树,Kruskal
template<class T>
bool Graph<T>::Kruskal(PathData* E,int ne)
{
Heap<PathData> H;
int nv=ne+1;
int i,j,id,*DS=new int [nv];
for(i=0;i<nv;i++)
{
DS[i]=-1;
}
double cost;
PathData e;
for(i=0;i<nv;i++)
{
for(int j=i+1;j<nv;j++)
{
cost=GetCost(i,j);
if(cost!=0)
{
e.start=i;
e.dest=j;
e.cost=cost;
H.Insert(e);
}
}
}
id=0;
while(!H.Empty())
{
H.DeleteMin(e);
i=e.start;
while(DS[i]>=0)
i=DS[i];
j=e.dest;
while(DS[j]>=0)
j=DS[j];
if(i!=j)
{
if(i<j)
{
DS[i]+=DS[j];
DS[j]=i;
}
else
{
DS[j]+=DS[i];
DS[i]=j;
}
E[id++]=e;
}
}
delete[]DS;
return (id==ne?1:0);
}
最小生成树,Kruskal
最新推荐文章于 2021-11-19 15:37:15 发布