誰かが隣接行列を実装した MST を探しているなら、私の Java のサンプル コードがあります。ジャンクボットの回答に詳細が欠けているため、投稿します。これは O(n^2) で実行されるため、MST を見つけるための高密度/完全なグラフには Prim のアルゴリズムが最適です。
public void MST-Prim()
{
int[] source = new int[numberOfVertices]; // i-th element contains number of source vertex for the edge with the lowest cost from tree T to vertex i
double[] dist = new double[numberOfVertices]; //i-th element contains weight of minimal edge connecting i with source[i]
indicators = new boolean[numberOfVertices]; //if true, vertex i is in tree T
// Mark all vertices as NOT being in the minimum spanning tree
for (int i = 0; i < numberOfVertices; i++)
{
indicators[i] = false;
dist[i] = Double.POSITIVE_INFINITY;
}
//we start with vertex number 0
indicators[0] = true;
dist[0] = 0;
int bestNeighbour = 0;// lastly added vertex to the tree T
double minDist;
for (int i = 0; i < numberOfVertices - 1; i++)
{
minDist = Double.POSITIVE_INFINITY;
for (int j = 0; j < numberOfVertices; j++) // fill dist[] based on distance to bestNeighbour vertex
{
if (!indicators[j])
{
double weight = fullGraph.getEdgeWeight(bestNeighbour, j);
if (weight < dist[j])
{
source[j] = bestNeighbour;
dist[j] = weight;
}
}
}
for (int j = 0; j < numberOfVertices; j++) // find index of min in dist[]
{
if (!indicators[j])
{
if (dist[j] < minDist)
{
bestNeighbour = j;
minDist = dist[j];
}
}
}
if (bestNeighbour != 0)
{//add the edge (bestNeighbour, dist[bestNeighbour]) to tree T
addEdgeToTree(new GraphEdge(fullGraph.getNode(source[bestNeighbour]), fullGraph.getNode(bestNeighbour),
dist[bestNeighbour]));
indicators[bestNeighbour] = true;
}
}
}