【问题标题】:Minimal Spanning Tree from adjacency matrix in JavaJava中邻接矩阵的最小生成树
【发布时间】:2010-12-14 14:35:55
【问题描述】:

请帮助我了解如何从图的邻接矩阵中获得最小生成树! 我用java写了关于它的课程,截止日期是16.12.2010,但我觉得它会失败。 现在我的程序可以:

  1. 绘制节点
  2. 画边
  3. 在绘图的地下室生成图形的邻接矩阵,并带有边的权重
  4. 找到连接到节点的最小边
  5. 并具有其他一些测试/测试功能

但我不知道如何在 Java 中实现 Prim / Kruskal 算法。我试图找到一些解决办法 在 google 中,但只找到需要工作 .obj 文件的 Java-applet,我也无法运行它。

我写了一些简单的控制台 java pattern 现在生成并打印图的邻接矩阵。任何人都可以添加返回图的最小生成树的邻接矩阵的函数,如下所示:

public static int[][] mst(int[][] graph, int n) {
    ...
}

在哪里:

  • graph - 在n中生成图
  • 顶点数量(节点)

提前致谢!

【问题讨论】:

  • 家庭作业标签警察注意 - OP 已声明这是家庭作业。
  • 在 SO 之前有人是怎么做作业的?

标签: java minimum-spanning-tree


【解决方案1】:

鉴于您的程序目前无法处理不相交集数据结构,您可能希望使用 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

这在 O(N^2) 中运行,但您可以使其在 O(E log E) 中运行,如果使用堆,则 E = num 个边。

【讨论】:

    【解决方案2】:

    如果有人正在寻找具有邻接矩阵实现的 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;
            }
    
        }
    
    }
    

    【讨论】:

    • 您给出的示例中的 getEdgeWeight 是什么?是不是我们需要为此编写一个单独的方法?和 addEdgetotree 概念?
    猜你喜欢
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多