我试图优化以下确定最小生成树的算法:
public void Prim(int sVertex)
{
source = sVertex;
boolean[] mstv = new boolean[maxSize];
for (int j = 0; j < gSize; j++)
{
mstv[j] = false;
edges[j] = source;
edgeWeights[j] = weights[source][j];
}
mstv[source] = true;
edgeWeights[source] = 0;
for (int i = 0; i < gSize - 1; i++)
{
double minWeight = Integer.MAX_VALUE;
int startVertex = 0;
int endVertex = 0;
for (int j = 0; j < gSize; j++)
if (mstv[j])
for (int k = 0; k < gSize; k++)
if (!mstv[k] && weights[j][k] < minWeight)
{
endVertex = k;
startVertex = j;
minWeight = weights[j][k];
}
mstv[endVertex] = true;
edges[endVertex] = startVertex;
edgeWeights[endVertex] = minWeight;
} //end for
} //end Prim
我的朋友说我可以加快速度,但我不知道怎么做。有什么想法吗?