Java中邻接矩阵的最小生成树
请帮助我理解如何从图的邻接矩阵中得到最小生成树! 我用java写了关于它的课程,截止日期是2010年12月16日,但我觉得它会失败。 现在我的程序可以:Java中邻接矩阵的最小生成树,java,minimum-spanning-tree,Java,Minimum Spanning Tree,请帮助我理解如何从图的邻接矩阵中得到最小生成树! 我用java写了关于它的课程,截止日期是2010年12月16日,但我觉得它会失败。 现在我的程序可以: 绘制节点 画边 使用边的权重在图形的基础上生成图形的邻接矩阵 查找连接到节点的最小边 并具有一些其他测试/测试功能 但我不知道如何在Java中实现Prim/Kruskal算法节奏。我试图找到一些解决办法 在谷歌,但只找到Java小程序,这需要工作。obj文件,我也不能运行它 我编写了一些简单的控制台java,现在可以生成和打印图形的邻接矩阵。任
public static int[][] mst(int[][] graph, int n) {
...
}
其中:
- 图-是在n中生成的图
- 顶点(节点)的数量
提前谢谢 鉴于您的程序目前无法处理不相交的集合数据结构,您可能需要使用Prim 考虑到您已经可以完成Prim所需的大部分工作,我将以伪代码的形式提供给您
int bestDist[N]
int mst[N][N]
int cameHere[N]
bool done[N]
FOR i = 0..N-1:
bestDist[i] = INFINITY
done[i] = false
FOR j=0..N-1:
mst[i][j] = INFINITY
// start at any node
bestDist[0] = 0;
FOR i = 0..N-1:
bestNode = INFINITY
bestNodeDist = INFINITY
IF bestNode != 0:
mst[cameHere[bestNode]][bestNode] = graph[cameHere[bestNode]][bestNode]
// find closest node
FOR j= 0..N-1:
IF !done[j] AND bestDist[j] < bestNodeDist:
bestNode = j
bestNodeDist = bestNodeDist[j]
// update surrounding nodes
FOR j=0..N-1:
IF !done[j] AND bestNodeDist + graph[bestNode][j] < bestDist[j]:
bestDist[j] = bestNodeDist + graph[bestNode][j]
cameHere[j] = bestNode
return mst
int最佳距离[N]
int mst[N][N]
int在这里[N]
bool done[N]
对于i=0..N-1:
最佳距离[i]=无穷大
完成[完成]=错误
对于j=0..N-1:
mst[i][j]=无穷大
//从任何节点开始
最佳距离[0]=0;
对于i=0..N-1:
最佳节点=无穷大
bestNodeDist=无穷大
如果是bestNode!=0:
mst[cameHere[bestNode]][bestNode]=图形[cameHere[bestNode]][bestNode]
//查找最近的节点
对于j=0..N-1:
如果!完成[j]和最佳Dist[j]
这在O(N^2)中运行,但您可以让它在O(E log E)中运行,如果您使用堆,则E=num edges。如果有人正在寻找具有邻接矩阵实现的MST,则有我的Java示例代码。我发布它是因为Junkbot的答案缺少一些细节。它在O(n^2)中运行,因此Prim算法是寻找MST的稠密/完整图的最佳选择
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;
}
}
}
public void MST-Prim()
{
int[]source=new int[numberoftexts];//第i个元素包含从树T到顶点i的成本最低的边的源顶点数
double[]dist=新的double[numberOfVertices];//第i个元素包含连接i和源[i]的最小边的权重
指示符=新布尔[numberOfVertices];//如果为true,则顶点i位于树T中
//将所有顶点标记为不在最小生成树中
对于(int i=0;i
家庭作业标签警察注意事项-OP已声明这是家庭作业。之前有人是如何做家庭作业的?您给出的示例中的GetEdgeWight是什么?我们是否需要为此编写一个单独的方法?和addEdgetotree概念?