<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>O(ElogV) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/oelogv/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/oelogv/</link>
	<description></description>
	<lastBuildDate>Sun, 26 Jan 2020 18:04:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>O(ElogV) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/oelogv/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 Minimum Spanning Tree (MST) 最小生成树 SP18</title>
		<link>https://zxi.mytechroad.com/blog/sp/minimum-spanning-tree-sp18/</link>
					<comments>https://zxi.mytechroad.com/blog/sp/minimum-spanning-tree-sp18/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 24 Jan 2020 15:59:23 +0000</pubDate>
				<category><![CDATA[SP]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[MST]]></category>
		<category><![CDATA[O(ElogV)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6124</guid>

					<description><![CDATA[<p>Prim&#8217;s Algorithm Time complexity: O(ElogV)Space complexity: O(V+E) [crayon-663cb661d34ad649413911/] [crayon-663cb661d34b2544045944/] Kruskal&#8217;s Algorithm Time complexity: O(ElogV)Space complexity: O(V+E) [crayon-663cb661d34b4987391068/] [crayon-663cb661d34b6483401057/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sp/minimum-spanning-tree-sp18/">花花酱 Minimum Spanning Tree (MST) 最小生成树 SP18</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode - 最小生成树 (Minimum Spanning Tree) 刷题找工作 SP18" width="500" height="281" src="https://www.youtube.com/embed/wmW8G8SrXDs?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2><strong>Prim&#8217;s Algorithm</strong></h2>



<p>Time complexity: O(ElogV)<br>Space complexity: O(V+E)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
#include &lt;iostream&gt;
#include &lt;queue&gt;
#include &lt;vector&gt;
using namespace std;

int main(int argc, char** argv) {
  const int n = 4;
  vector&lt;vector&lt;int&gt;&gt; edges{{0,1,1},{0,3,3},{0,2,6},{2,3,2},{1,2,4},{1,3,5}};
  vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt; g(n);
  for (const auto&amp; e : edges) {
    g[e[0]].emplace_back(e[1], e[2]);
    g[e[1]].emplace_back(e[0], e[2]);
  }

  priority_queue&lt;pair&lt;int, int&gt;&gt; q; // (-w, v)
  vector&lt;int&gt; seen(n);
  q.emplace(0, 0); // (-w, v)

  int cost = 0;
  for (int i = 0; i &lt; n; ++i) {
    while (true) {
      const int w = -q.top().first;
      const int v = q.top().second;
      q.pop();
      if (seen[v]++) continue;
      cost += w;
      for (const auto&amp; p : g[v]) {
        if (seen[p.first]) continue;
        q.emplace(-p.second, p.first);
      }
      break;
    }
  }
  cout &lt;&lt; cost &lt;&lt; endl;
  return 0;
}</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">Python</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
from collections import defaultdict
from heapq import *

n = 4
edges = [[0,1,1],[0,3,3],[0,2,6],[2,3,2],[1,2,4],[1,3,5]]
g = defaultdict(list)
for e in edges:
  g[e[0]].append((e[1], e[2]))
  g[e[1]].append((e[0], e[2]))

q = []
cost = 0
seen = set()
heappush(q, (0, 0))
for _ in range(n):
  while True:
    w, u = heappop(q)
    if u in seen: continue  
    cost += w
    seen.add(u)
    for v, w in g[u]:
      if v in seen: continue
      heappush(q, (w, v))
    break

print(cost)</pre>
</div></div>



<h2><strong>Kruskal&#8217;s Algorithm</strong></h2>



<p>Time complexity: O(ElogV)<br>Space complexity: O(V+E)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
#include &lt;iostream&gt;
#include &lt;queue&gt;
#include &lt;vector&gt;
#include &lt;functional&gt;
#include &lt;numeric&gt;
using namespace std;

int main(int argc, char** argv) {
  const int n = 4;
  vector&lt;vector&lt;int&gt;&gt; edges{{0,1,1},{0,3,3},{0,2,6},{2,3,2},{1,2,4},{1,3,5}};    
  vector&lt;vector&lt;int&gt;&gt; q; // (w, u, v)  
  for (const auto&amp; e : edges)    
    q.push_back({e[2], e[0], e[1]});
  sort(begin(q), end(q));

  vector&lt;int&gt; p(n);
  iota(begin(p), end(p), 0);

  function&lt;int(int)&gt; find = [&amp;](int x) {
    return x == p[x] ? x : p[x] = find(p[p[x]]);
  };

  int cost = 0;
  for (const auto&amp; t : q) {    
    int w = t[0], u = t[1], v = t[2];      
    int ru = find(u), rv = find(v);      
    if (ru == rv) continue;
    p[ru] = rv; // merge (u, v)      
    cost += w;
  }  
  cout &lt;&lt; cost &lt;&lt; endl;

  return 0;
}</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">Python</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">from collections import defaultdict
from heapq import *

n = 4
edges = [[0,1,1],[0,3,3],[0,2,6],[2,3,2],[1,2,4],[1,3,5]]
p = list(range(n))


def find(x):
  if x != p[x]: p[x] = find(p[p[x]])
  return p[x]

cost = 0
for u, v, w in sorted(edges, key=lambda x: x[2]):
  ru, rv = find(u), find(v)
  if ru == rv: continue
  p[ru] = rv
  cost += w

print(cost)</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sp/minimum-spanning-tree-sp18/">花花酱 Minimum Spanning Tree (MST) 最小生成树 SP18</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/sp/minimum-spanning-tree-sp18/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
