{"id":238,"date":"2026-11-13T12:35:33","date_gmt":"2026-11-13T04:35:33","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-prim-grows-a-minimum-spanning-tree-from-a-cut\/"},"modified":"2026-11-13T21:53:26","modified_gmt":"2026-11-13T13:53:26","slug":"how-prim-grows-a-minimum-spanning-tree-from-a-cut","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-prim-grows-a-minimum-spanning-tree-from-a-cut\/","title":{"rendered":"How Prim grows a minimum spanning tree from a cut"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Prim&#8217;s algorithm<\/strong> grows one tree. The lightest edge with one end in the tree and one end outside it is safe to add, by the <strong>cut property<\/strong>. A binary heap implements the fringe in <strong>O((V+E) log V)<\/strong>. A Fibonacci heap improves the proof to <strong>O(E + V log V)<\/strong>. The graph is undirected and connected, and the weights may be negative. The result is a minimum spanning tree, not a shortest-path tree.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A spanning tree connects every vertex with |V|\u22121 edges and no cycle. It is minimum when the total weight is as small as possible. The cut property says that for any partition of the vertices into a nonempty proper subset and its complement, a lightest edge across the cut belongs to some minimum spanning tree. Prim maintains one side of the cut as the grown tree and always takes that edge.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The same growth rule is not Dijkstra. Dijkstra minimizes a path length from a source. Prim minimizes the weight of the next attachment edge. A cheap edge into a far vertex is good for Prim and may be a terrible path.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">On a connected undirected graph the algorithm returns a minimum spanning tree. If several edges share the lightest weight across the cut, any of them is safe, and the tree is not unique. Time is O((V+E) log V) with a binary heap and O(E + V log V) with a Fibonacci heap. Extra memory is \u0398(V). A disconnected graph yields a minimum spanning forest, one tree per component, if the outer loop restarts on an unreached vertex.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Negative weights need no special case. The cut property never assumes a sign. Self-loops are useless. Parallel edges keep only the lighter copy.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Start the tree as a single vertex. Give every other vertex a key of infinity, meaning the cheapest known edge into the tree.<\/li>\n<li>Put the vertices in a min-heap keyed by that attachment cost.<\/li>\n<li>Pop the vertex u of smallest key. The stored parent edge is the cut edge that added u. Include it in the tree, except for the start vertex.<\/li>\n<li>For each neighbor v not yet in the tree, if the edge u\u2013v is lighter than v&#8217;s key, decrease the key and remember u as the parent.<\/li>\n<li>Repeat until the heap has offered every reachable vertex. A leftover infinite key is another component.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The output minimizes the sum of edge weights in a connector, not the path length between a pair. Shortest paths belong to Dijkstra, which looks similar in code because both decrease a key on the fringe. The key in Prim is an edge weight. The key in Dijkstra is a path sum.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the grown set is not the cut<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Add the globally lightest remaining edge, even if both ends are already in the tree<\/td>\n<td>That edge closes a cycle. Kruskal will skip it with a union-find test. Prim never generates it, because both ends are inside.<\/td>\n<\/tr>\n<tr>\n<td>Use path distance as the heap key<\/td>\n<td>That is Dijkstra. The tree can omit a light edge that forms a short cut and include a heavy edge that continues a short path.<\/td>\n<\/tr>\n<tr>\n<td>Assume negative weights require Bellman-Ford<\/td>\n<td>Spanning trees do not walk paths repeatedly. A negative edge is simply a very attractive cut edge, and it is safe.<\/td>\n<\/tr>\n<tr>\n<td>Quote the Fibonacci bound for a binary-heap implementation<\/td>\n<td>The binary heap pays O((V+E) log V). The linear-in-E term is the Fibonacci heap&#8217;s decrease-key.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A global sort of the edges, plus a disjoint-set test, is Kruskal. A component-wise minimum edge, merged in rounds, is Bor\u016fvka. All three return a minimum spanning tree.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment grows one component. The cut is the set of edges with one end inside the tree, and the lightest of those is safe. It prints the same total as Kruskal, <code>6<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import heapq\nadj = {0: [(1, 1), (2, 4)], 1: [(0, 1), (2, 2), (3, 5)], 2: [(0, 4), (1, 2), (3, 3)], 3: [(1, 5), (2, 3)]}\ninside = {0}\npq = [(w, 0, v) for v, w in adj[0]]\nheapq.heapify(pq)\ntotal = 0\nwhile len(inside) &lt; 4:\n    w, u, v = heapq.heappop(pq)\n    if v in inside:\n        continue\n    inside.add(v)\n    total += w\n    for nxt, nw in adj[v]:\n        if nxt not in inside:\n            heapq.heappush(pq, (nw, v, nxt))\nassert total == 6\nprint(total)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Prim&#8217;s algorithm grows a minimum spanning tree from a start vertex, the way a cable layout grows from a substation. A binary heap makes it the twin of Dijkstra. Kruskal sorts the edges first and is the better fit when the edge list is already nearly sorted.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop Prim when the graph is so sparse that sorting edges for Kruskal is simpler and fast enough, which is common. Keep Prim when the graph is dense, the heap stays small, or an adjacency matrix makes the O(V\u00b2) scan without a heap the tight implementation.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Prim&#8217;s algorithm returns the cut property and the difference between an attachment weight and a path distance. <strong>Prim&#8217;s algorithm adds the lightest edge across the cut around the grown tree, which builds a minimum spanning tree in O((V+E) log V) time with a binary heap.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Prim&#8217;s algorithm grows one tree. The lightest edge with one end in the tree and one end outside it is safe to add, by the cut property. A binary heap implements the fringe in O((V+E) log V). A Fibonacci heap improves the proof to O(E + V&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,32],"tags":[],"class_list":["post-238","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-graphs"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":637,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/238\/revisions\/637"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}