根据我的理解,Prim的MST算法将遍历图中的所有顶点,选择一个最佳边缘以移动到每个顶点。因此,每次迭代将为每个相邻顶点选择最佳成本。因此,无论首先使用哪个顶点,最终结果都应该相同,因为即使在选择下一个顶点之前也会选择最优成本。
因此,我不明白为什么算法必须选择每次迭代中成本最低的顶点。为了使我的描述更清楚,我已经包含了geeksforgeeks.org的示例代码和图表:
// 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;
}
我们可以从下面的代码块中看到,每次迭代都会为每个邻居选择最佳权重(在这种情况下,只有一条边连接任意两个顶点):
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
让我们以顶点7为例,它有3条边。边缘6-7最终将从边缘0-7,8-7和6-7中选择,因为它的权重最小,无论我们是否首先评估顶点0-7,8-7或6-7,因为最佳权重= min(所有相邻边缘的重量)。因此,每次迭代选择最小权重顶点似乎是多余的。
有人可以向我解释为每次迭代选择最小权重顶点的目的是什么,如下面的代码块?
// Pick thd minimum key vertex from the set of vertices
// not yet included in MST
int u = minKey(key, mstSet);
答案 0 :(得分:1)
让我们以顶点7为例,它有3条边。边缘6-7会 最终从边缘0-7,8-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,你现在有一个更好的方法从6开始到7只重量只有1,而不是重量7(边缘8-7)。因此,您需要更新最佳路径的概念。
一种方法是在每次迭代时简单地迭代附近的所有边到MST集中已经包含的每个顶点,然后选择最好的一个。这引入了两个内部循环来找到最小值:一个在MST集合中的顶点上,另一个在与每个这样的顶点相邻的边缘上。对于包含n
个顶点和大约n^2
个边缘的近乎完整的图表,您总共会获得O(n^3)
。
那么这个算法的作用是什么,它只是在顶点而不是边缘上循环。每个顶点保持从当前MST集中最简单的方式去那里的权重。这允许我们将内循环分成两部分。通过迭代所有顶点,可以找到最佳的下一个顶点。它选择最好的相邻一个因为非相邻具有无限权重。这正是令人困惑的线所做的。这是O(n)
。
另一个内循环更新权重。但是,由于更新可能仅由添加新顶点引起,因此只需要考虑与该特定顶点相邻的边缘!再次O(n)
(假设几乎完整的图表)。因此,对于所有三个循环,您将复杂性降低到O(n^2)
。
事实上,如果你使用邻接矩阵,那么图表是否完整并不重要。要摆脱那个minKey
部分,你需要制作三个嵌套循环:第一个内部循环遍历MST集中的所有顶点,最里面的一个将迭代通过其相邻的边缘,包括不存在的边缘。 minKey
技巧只允许这样:
// 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`!