Prim 的 O(|V|^2) 中的 MST 算法

发布于 2024-09-13 00:00:13 字数 777 浏览 10 评论 0原文

如果使用邻接矩阵表示,Prim 的 MST 算法的时间复杂度为 O(|V|^2)

我正在尝试使用邻接矩阵来实现 Prim 算法。我正在使用这个 作为参考。

V = {1,2...,n}
U = {1}
T = NULL
while V != U:

     /* 
         Now this implementation means that 
         I find lowest cost edge in O(n).
         How do I do that using adjacency list? 
     */

     let (u, v) be the lowest cost edge 
                such that u is in U and v is in V - U;

     T = T + {(u,v)}
     U = U + {v}

编辑:

  1. 我非常了解Prim的算法。
  2. 我知道如何使用堆和优先级队列有效地实现它。
  3. 我也知道更好的算法。
  4. 我想使用图的邻接矩阵表示并获得 O(|V|^2) 实现。

我想要低效的实施

Time complexity of Prim's MST algorithm is O(|V|^2) if you use adjacency matrix representation.

I am trying to implement Prim's algorithm using adjacency matrix. I am using this
as a reference.

V = {1,2...,n}
U = {1}
T = NULL
while V != U:

     /* 
         Now this implementation means that 
         I find lowest cost edge in O(n).
         How do I do that using adjacency list? 
     */

     let (u, v) be the lowest cost edge 
                such that u is in U and v is in V - U;

     T = T + {(u,v)}
     U = U + {v}

EDIT:

  1. I understand Prim's algorithm very well.
  2. I know how to implement it efficiently using heaps and priority queues.
  3. I also know about better algorithms.
  4. I want to use adjacency matrix representation of graph and get O(|V|^2) implementation.

I WANT THE INEFFICIENT IMPLEMENTATION

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还不是爱你 2024-09-20 00:00:13

找到最低成本边 (u,v),使得 u 在 U 中,v 在 VU 中,是通过优先级队列完成的。更准确地说,优先级队列包含来自 VU 的每个节点 v 以及从 v 到当前树 U 的最低成本边。换句话说,队列恰好包含 |VU |元素。

将新节点u添加到U后,您必须通过检查现在是否可以通过比以前成本更低的边到达u的邻居节点来更新优先级队列。

优先级队列的选择决定了时间复杂度。通过将优先级队列实现为简单数组 cheapest_edges[1..|V|],您将获得 O(|V|^2)。这是因为在这个队列中找到最小值需要 O(|V|) 时间,并且您重复 |V|次。

在伪代码中:

V = {2...,n}
U = {1}
T = NULL
P = array, for each v set P[v] = (1,v)

while V != U

    (u,v) = P[v] with v such that  length P[v]  is minimal

    T = T + {(u,v)}
    U = U + {v}

    for each w adjacent to v
        if length (v,w) < length P[w] then
            P[w] = (v,w)

Finding the lowest cost edge (u,v), such that u is in U and v is in V-U, is done with a priority queue. More precisely, the priority queue contains each node v from V-U together with the lowest cost edge from v into the current tree U. In other words, the queue contains exactly |V-U| elements.

After adding a new node u to U, you have to update the priority queue by checking whether the neighboring nodes of u can now be reached by an edge of lower cost than previously.

The choice of priority queue determines the time complexity. You will get O(|V|^2) by implementing the priority queue as a simply array cheapest_edges[1..|V|]. That's because finding minimum in this queue takes O(|V|) time, and you repeat that |V| times.

In pseudo-code:

V = {2...,n}
U = {1}
T = NULL
P = array, for each v set P[v] = (1,v)

while V != U

    (u,v) = P[v] with v such that  length P[v]  is minimal

    T = T + {(u,v)}
    U = U + {v}

    for each w adjacent to v
        if length (v,w) < length P[w] then
            P[w] = (v,w)
窗影残 2024-09-20 00:00:13

您可以像 Dijkstra 算法 一样,通过选择连接到当前的节点具有最小成本边的部分树(不生成循环)。我认为 wikipedia 比你的伪代码更好地解释了 Prim。看一下,如果您还有其他问题,请告诉我。

You do it like in Dijkstra's algorithm, by selecting the node that is connected to your current partial tree with the minimum cost edge (that doesn't generate a cycle). I think wikipedia explains Prim better than that pseudocode you have. Give it a look and let me know if you have more questions.

海未深 2024-09-20 00:00:13

您可以按成本对边进行排序,然后按照成本的顺序迭代边,如果该边连接两个不同的子图,则使用该边。

我在此处实现了。它按顺序读取顶点数(N)、边数(M)和边(A,B,Cost),然后输出边。这就是克鲁斯卡尔算法。

使用相同输入的 Prim 算法的堆实现可以在此处找到。

You can sort the edges by the cost and then iterate the edges in the order of the cost, and if that edge joins two distinct subgraphs use that edge.

I have a implementation here. It reads the number of verticles (N), the number of edges (M) and the edges in order (A, B, Cost) and then outputs the edges. This is the Kruskal algorithm.

A implementation of the Prim's algorithm with a heap, using the same input can be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文