首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在Prim的MST算法中得到最小顶点?

为什么在Prim的MST算法中得到最小顶点?
EN

Stack Overflow用户
提问于 2016-08-14 05:39:25
回答 1查看 880关注 0票数 2

据我所知,Prim的MST算法将遍历图中的所有顶点,选择一个最佳的边去每个顶点。因此,每一次迭代都会为每个相邻顶点选择一个最优代价。因此,无论首先使用哪个顶点,最终结果都应该是相同的,因为在选择下一个顶点之前就已经选择了最优代价。

因此,我不明白为什么算法必须选择在每次迭代中代价最小的顶点。为了使我的描述更加清晰,我包含了来自geeksforgeeks.org的示例代码和图表:

代码语言:javascript
复制
// A C / C++ program for Prim's Minimum Spanning Tree (MST) algorithm. 
// The program is for adjacency matrix representation of the graph

#include <stdio.h>
#include <limits.h>

// Number of vertices in the graph
#define V 5

// A utility function to find the vertex with minimum key value, from
// the set of vertices not yet included in MST
int minKey(int key[], bool mstSet[])
{
   // Initialize min value
   int min = INT_MAX, min_index;

   for (int v = 0; v < V; v++)
     if (mstSet[v] == false && key[v] < min)
         min = key[v], min_index = v;

   return min_index;
}

// A utility function to print the constructed MST stored in parent[]
int printMST(int parent[], int n, int graph[V][V])
{
   printf("Edge   Weight\n");
   for (int i = 1; i < V; i++)
      printf("%d - %d    %d \n", parent[i], i, graph[i][parent[i]]);
}

// Function to construct and print MST for a graph represented using adjacency
// matrix representation
void primMST(int graph[V][V])
{
     int parent[V]; // Array to store constructed MST
     int key[V];   // Key values used to pick minimum weight edge in cut
     bool mstSet[V];  // To represent set of vertices not yet included in MST

     // Initialize all keys as INFINITE
     for (int i = 0; i < V; i++)
        key[i] = INT_MAX, mstSet[i] = false;

     // Always include first 1st vertex in MST.
     key[0] = 0;     // Make key 0 so that this vertex is picked as first vertex
     parent[0] = -1; // First node is always root of MST 

     // The MST will have V vertices
     for (int count = 0; count < V-1; count++)
     {
        // Pick thd minimum key vertex from the set of vertices
        // not yet included in MST
        int u = minKey(key, mstSet);

        // Add the picked vertex to the MST Set
        mstSet[u] = true;

        // Update key value and parent index of the adjacent vertices of
        // the picked vertex. Consider only those vertices which are not yet
        // included in MST
        for (int v = 0; v < V; v++)

           // graph[u][v] is non zero only for adjacent vertices of m
           // mstSet[v] is false for vertices not yet included in MST
           // Update the key only if graph[u][v] is smaller than key[v]
          if (graph[u][v] && mstSet[v] == false && graph[u][v] <  key[v])
             parent[v]  = u, key[v] = graph[u][v];
     }

     // print the constructed MST
     printMST(parent, V, graph);
}


// driver program to test above function
int main()
{
   /* Let us create the following graph
          2    3
      (0)--(1)--(2)
       |   / \   |
      6| 8/   \5 |7
       | /     \ |
      (3)-------(4)
            9          */
   int graph[V][V] = {{0, 2, 0, 6, 0},
                      {2, 0, 3, 8, 5},
                      {0, 3, 0, 0, 7},
                      {6, 8, 0, 0, 9},
                      {0, 5, 7, 9, 0},
                     };

    // Print the solution
    primMST(graph);

    return 0;
}

从下面的代码块中可以看出,每一次迭代都将为每个邻域选择最佳权重(在本例中,任何两个顶点只有一个边连接):

代码语言:javascript
复制
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
    parent[v] = u, key[v] = graph[u][v];

让我们以顶点7为例,它有三个边。最终将从边缘0-7、8-7和6-7中选择边缘6-7作为其重量最小,而不管我们是首先评估顶点0-7、8-7还是6-7,因为最优权重=min(所有相邻边缘的权重)。因此,每次迭代选择最小权顶点似乎是多余的。

请有人向我解释一下,为每一次迭代选择最小权顶点的目的是什么,如下面的代码块所示?

代码语言:javascript
复制
// Pick thd minimum key vertex from the set of vertices
// not yet included in MST
int u = minKey(key, mstSet);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-14 07:04:37

让我们以顶点7为例,它有三个边。最终将从边缘0-7、8-7和6-7中选择边缘6-7作为其重量最小,而不管我们是首先评估顶点0-7、8-7还是6-7,因为最优权重=min(所有相邻边缘的权重)。因此,每次迭代选择最小权顶点似乎是多余的。

看起来你混淆了这两个循环的目的。内环不选择任何内容。它只是计算重量。是外部循环完成了选择。

从这边看。想象一下,只要一个循环就开始了,我们已经得到了一个随机的开始顶点。现在这个循环需要取一个边缘。当然,它会越过相邻的边缘,得到最小值。它是如何做的并不重要:是将权重分配给顶点,还是只在边上循环并挑选出最好的。

然而,当有越来越多的顶点时,事情变得复杂起来。一旦添加了另一个顶点,您可能已经找到了一个更好的方法来获得一个新的顶点。假设你从顶点2开始。你添加顶点8。现在你可以从2(重量8),3从2(重量7),6从8(重量6),7从8(重量7)到1。然而,一旦你从8到6,你现在有一个更好的方法去7从6与重量只有1,而不是重量7(边8-7)。因此,您需要更新最佳路径的概念。

一种方法是简单地对每个迭代中已经包含在MST集合中的每个顶点相邻的所有边进行迭代,并选择最佳的一个。这引入了两个内环来寻找最小值:一个在MST集中的顶点上,另一个在每个这样的顶点附近的边缘上。对于一个具有n顶点和关于n^2边的几乎完全图,您将得到总体上的O(n^3)

所以这个算法所做的是,它只遍历顶点而不是边。每个顶点保持从当前MST集到那里的最简单方法的权重。这使得我们可以将内部循环分成两部分。通过对所有顶点进行迭代,可以找到最佳的下一个顶点。它选择最佳相邻的一个,因为不相邻有无穷大的权重。这正是令人困惑的线条所做的。我是O(n)

另一个内循环更新权重。但是,由于更新可能仅由新顶点的添加引起,因此只需要考虑与该特定顶点相邻的边!这又是O(n) (假设是一个几乎完全的图)。因此,将所有三个循环的复杂度降到O(n^2)

事实上,如果使用邻接矩阵,甚至不重要图是否完成。要摆脱这个minKey部分,您需要做三个嵌套循环:第一个内部循环将遍历MST集中的所有顶点,最内部的一个将迭代其相邻的边缘,包括不存在的边。minKey技巧只允许这样做:

代码语言:javascript
复制
    // Update key value and parent index of the adjacent vertices of
    // the picked vertex. Consider only those vertices which are not yet
    // included in MST
    for (int v = 0; v < V; v++) // note there is no loop over `u`!
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38939497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档