<?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>Minimum spanning tree &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/minimum-spanning-tree/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Prim&#8217;s Minimum Spanning Tree</title>
		<link>/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/</link>
		<comments>/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/#comments</comments>
		<pubDate>Mon, 19 Nov 2012 13:08:18 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Distributed minimum spanning tree]]></category>
		<category><![CDATA[Environment]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[mathematician]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Minimum spanning tree]]></category>
		<category><![CDATA[minimum spanning tree algorithm]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Prim-Jarnik algorithm]]></category>
		<category><![CDATA[Prim's algorithm]]></category>
		<category><![CDATA[Reverse-delete algorithm]]></category>
		<category><![CDATA[Robert Prim]]></category>
		<category><![CDATA[Shortest path problem]]></category>
		<category><![CDATA[Spanning tree]]></category>
		<category><![CDATA[Theoretical computer science]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[Vojtech Jarnik]]></category>

		<guid isPermaLink="false">/?p=3452</guid>
		<description><![CDATA[Introduction Along with the Kruskal’s minimum spanning tree algorithm, there’s another general algorithm that solves the problem. The algorithm of Prim. As we already know the algorithm of Kruskal works in a pretty natural and logical way. Since we’re trying to build a MST, which is naturally build by the minimal edges of the graph &#8230; <a href="/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Prim&#8217;s Minimum Spanning Tree</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree">Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/11/05/computer-algorithms-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Minimum Spanning Tree">Computer Algorithms: Minimum Spanning Tree </a></li>
<li><a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Dijkstra Shortest Path in a Graph">Computer Algorithms: Dijkstra Shortest Path in a Graph </a></li>
<li><a href="/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Bellman-Ford Shortest Path in a Graph">Computer Algorithms: Bellman-Ford Shortest Path in a Graph </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Along with the <a href="/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/" title="Computer Algorithms: Kruskal’s Minimum Spanning Tree">Kruskal’s minimum spanning tree algorithm</a>, there’s another general algorithm that solves the problem. The algorithm of Prim.</p>
<p>As we already know the algorithm of Kruskal works in a pretty natural and logical way. Since we’re trying to build a MST, which is naturally build by the minimal edges of the graph (G), we sort them in a non-descending order and we start building the tree. </p>
<figure id="attachment_3470" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/1.-The-algorithm-of-Kruskal.png"><img src="/wp-content/uploads/2012/11/1.-The-algorithm-of-Kruskal.png" alt="The algorithm of Kruskal" title="The algorithm of Kruskal" width="620" height="399" class="size-full wp-image-3470" srcset="/wp-content/uploads/2012/11/1.-The-algorithm-of-Kruskal.png 620w, /wp-content/uploads/2012/11/1.-The-algorithm-of-Kruskal-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>During the whole process of building the final minimum spanning tree Kruskal’s algorithm keeps a forest of trees. The number of trees in that forest decreases on each step and finally we get the minimum weight spanning tree. </p>
<p>A key point in the Kruskal’s approach is the way we get the “next” edge from G that should be added to one of the trees of the forest (or to connect two trees from the forest). The only thing we should be aware of is to choose an edge that’s connecting two vertices – u and v and these two shouldn’t be in the same tree. That’s all.</p>
<figure id="attachment_3469" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/2.-The-Kruskals-Tricky-Part.png"><img src="/wp-content/uploads/2012/11/2.-The-Kruskals-Tricky-Part.png" alt="The Kruskal&#039;s Tricky Part" title="The Kruskal&#039;s Tricky Part" width="620" height="399" class="size-full wp-image-3469" srcset="/wp-content/uploads/2012/11/2.-The-Kruskals-Tricky-Part.png 620w, /wp-content/uploads/2012/11/2.-The-Kruskals-Tricky-Part-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>An important feature of the Kruskal’s algorithm is that it builds the MST just by sorting the edges by their weight and doesn’t care about a particular starting vertex.</p>
<p>In the same time there’s another algorithm that builds a MST – the algorithm of Prim designed by <a href="http://en.wikipedia.org/wiki/Robert_C._Prim" title="Robert C. Prim" target="_blank">Robert Prim</a> in 1957.<span id="more-3452"></span></p>
<h2>Overview</h2>
<p>The idea behind the Prim’s algorithm is rather different from Kruskal’s approach. During the process of building the MST this algorithm keeps a single tree, which is finally sub-tree of the final minimum weight spanning tree.</p>
<figure id="attachment_3468" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/3.-Prims-approach.png"><img src="/wp-content/uploads/2012/11/3.-Prims-approach.png" alt="Prim&#039;s approach" title="Prim&#039;s approach" width="620" height="399" class="size-full wp-image-3468" srcset="/wp-content/uploads/2012/11/3.-Prims-approach.png 620w, /wp-content/uploads/2012/11/3.-Prims-approach-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>On each step we chose an edge which we add to the growing tree that finally forms the MST. </p>
<p>It is somehow unnatural approach! We start from a given vertex and initially we don’t choose the lightest edge. Thus during the whole process the tree grows, but outside the tree (T) there might be edges that are lighter than those in the tree (i.e. the edge (5, 1) from the tree above is lighter than (2, 5) but (2, 5) is added to the growing tree before the edge (5, 1)).</p>
<p>Compared to the Kruskal’s algorithm this time everything seems to be really unnatural. How we should be sure the final tree (T) will be a minimum spanning tree since we don’t get the lightest edge on each step? </p>
<p>Actually we are sure that the final tree is a MST because of another obvious feature of the minimum spanning trees. They should “connect” all the vertices of G, thus somehow at least one edge reaching each vertex will appear in the MST. Thus we shouldn’t care where do we start, the only important thing is to choose the lightest edge that’s visible so far. </p>
<p>This algorithm looks much like <a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" title="Computer Algorithms: Dijkstra Shortest Path in a Graph" target="_blank">Dijkstra’s shortest path in a graph</a>, because we start from a vertex, we push all the edges starting from this node to a priority queue and we chose the lightest edge. Going to the next node connected by this edge we append to the queue all the edges that aren’t in the queue. </p>
<figure id="attachment_3467" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/4.-Prims-Priority-Queue.png"><img src="/wp-content/uploads/2012/11/4.-Prims-Priority-Queue.png" alt="Prim&#039;s Priority Queue" title="Prim&#039;s Priority Queue" width="620" height="399" class="size-full wp-image-3467" srcset="/wp-content/uploads/2012/11/4.-Prims-Priority-Queue.png 620w, /wp-content/uploads/2012/11/4.-Prims-Priority-Queue-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>That way the queue grows and we get always the lightest edge – thus forming a priority queue. </p>
<p>Now let’s summarize the algorithm of Prim</p>
<h2>Pseudo Code</h2>
<p>As an initial input we have the graph (G) and a starting vertex (s).</p>
<pre>
1.  Make a queue (Q) with all the vertices of G (V);
2.  For each member of Q set the priority to INFINITY;
3.  Only for the starting vertex (s) set the priority to 0;
4.  The parent of (s) should be NULL;
5.  While Q isn’t empty
6.     Get the minimum from Q – let’s say (u); (priority queue);
7.     For each adjacent vertex to (v) to (u)
8.        If (v) is in Q and weight of (u, v) < priority of (v) then
9.           The parent of (v) is set to be (u)
10.          The priority of (v) is the weight of (u, v)
</pre>
<p>Indeed it looks much like the Dijkstra’s algorithm.</p>
<h2>Code</h2>
<p>Here’s a <a href="/category/php/" title="PHP on stoimen.com">PHP</a> implementation of the algorithm of Prim, which directly follows the pseudo code. </p>
<pre lang="PHP">
// Prim's algorithm

define('INFINITY', 100000000);

// the graph
$G = array(
    0 => array( 0,  4,  0,  0,  0,  0,  0,  0,  8),
    1 => array( 4,  0,  8,  0,  0,  0,  0,  0,  11),
    2 => array( 0,  8,  0,  7,  0,  4,  2,  0,  0),
    3 => array( 0,  0,  7,  0,  9,  14,  0,  0,  0),
    4 => array( 0,  0,  0,  9,  0,  10,  0,  0,  0),
    5 => array( 0,  0,  4,  14,  10,  0,  0,  2,  0),
    6 => array( 0,  0,  2,  0,  0,  0,  0,  6,  7),
    7 => array( 0,  0,  0,  0,  0,  2,  6,  0,  1),
    8 => array( 8,  11,  0,  0,  0,  0,  7,  1,  0),
);

function prim(&$graph, $start)
{
    $q = array(); // queue
    $p = array(); // parent
    
    foreach (array_keys($graph) as $k) {
        $q[$k] = INFINITY;
    }
    
    $q[$start] = 0;
    $p[$start] = NULL;
    
    asort($q);
    
    while ($q) {
        // get the minimum value
        $keys = array_keys($q);
        $u = $keys[0];
        
        foreach ($graph[$u] as $v => $weight) {
            if ($weight > 0 && in_array($v, $keys) && $weight < $q[$v]) {
                $p[$v] = $u;
                $q[$v] = $weight;
            }
        }
        
        unset($q[$u]);
        asort($q);
    }
    
    return $p;
}

prim($G, 5);
</pre>
<h2>History</h2>
<p>It’s curious to say that the algorithm developed by Robert Prim isn’t developed by him. It’s considered that a Czech mathematician <a href="http://www-history.mcs.st-andrews.ac.uk/Biographies/Jarnik.html" title="Vojtech Jarnik" target="_blank">Vojtech Jarnik</a> discovered back in 1930. However now we know this algorithm as the algorithm of Prim, which independently discovered it in 1957 as I said above, and finally <a href="http://en.wikipedia.org/wiki/Edsger_W._Dijkstra" title="Edsger Dijkstra" target="_blank">Edsger Dijkstra</a> described it in 1959. That’s why his algorithm on finding the single-source shortest paths in a graph looks so much to this algorithm. Perhaps by finding this algorithm on minimum spanning tree Dijkstra discovered how we can find the shortest paths to all vertices using a priority queue. Indeed the paths to all other vertices use the edges of the minimum spanning tree. </p>
<p>Just because Jarnik found and described this algorithm 27 years earlier than Robert Prim, today it’s more convenient to call this algorithm the Prim-Jarnik algorithm.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree">Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/11/05/computer-algorithms-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Minimum Spanning Tree">Computer Algorithms: Minimum Spanning Tree </a></li>
<li><a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Dijkstra Shortest Path in a Graph">Computer Algorithms: Dijkstra Shortest Path in a Graph </a></li>
<li><a href="/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Bellman-Ford Shortest Path in a Graph">Computer Algorithms: Bellman-Ford Shortest Path in a Graph </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree</title>
		<link>/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/</link>
		<comments>/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/#comments</comments>
		<pubDate>Mon, 12 Nov 2012 12:01:47 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Bridge]]></category>
		<category><![CDATA[Distributed minimum spanning tree]]></category>
		<category><![CDATA[Environment]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[Joseph Kruskal]]></category>
		<category><![CDATA[Kruskal's algorithm]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Minimum spanning tree]]></category>
		<category><![CDATA[minimum spanning tree algorithms]]></category>
		<category><![CDATA[Prim's algorithm]]></category>
		<category><![CDATA[Reverse-delete algorithm]]></category>
		<category><![CDATA[Spanning tree]]></category>
		<category><![CDATA[statistician]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[The algorithm]]></category>
		<category><![CDATA[Theoretical computer science]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[two main algorithms]]></category>

		<guid isPermaLink="false">/?p=3439</guid>
		<description><![CDATA[Introduction One of the two main algorithms in finding the minimum spanning tree algorithms is the algorithm of Kruskal. Before getting into the details, let’s get back to the principles of the minimum spanning tree. We have a weighted graph and of all spanning trees we’d like to find the one with minimal weight. As &#8230; <a href="/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Prim&#8217;s Minimum Spanning Tree">Computer Algorithms: Prim&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/11/05/computer-algorithms-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Minimum Spanning Tree">Computer Algorithms: Minimum Spanning Tree </a></li>
<li><a href="/2012/06/22/computer-algorithms-binary-search-tree-data-structure/" rel="bookmark" title="Computer Algorithms: Binary Search Tree">Computer Algorithms: Binary Search Tree </a></li>
<li><a href="/2012/07/03/computer-algorithms-balancing-a-binary-search-tree/" rel="bookmark" title="Computer Algorithms: Balancing a Binary Search Tree">Computer Algorithms: Balancing a Binary Search Tree </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>One of the two main algorithms in <a href="/2012/11/05/computer-algorithms-minimum-spanning-tree/" title="Computer Algorithms: Minimum Spanning Tree">finding the minimum spanning tree</a> algorithms is the algorithm of Kruskal. Before getting into the details, let’s get back to the principles of the minimum spanning tree. </p>
<p>We have a weighted graph and of all spanning trees we’d like to find the one with minimal weight. As an example on the picture above you see a spanning tree (T) on the graph (G), but that isn&#8217;t the minimum weight spanning tree!</p>
<p><figure id="attachment_3459" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/1.-A-graph-and-a-possible-spanning-tree.png"><img src="/wp-content/uploads/2012/11/1.-A-graph-and-a-possible-spanning-tree.png" alt="A graph and a possible spanning tree" title="A graph and a possible spanning tree" width="620" height="399" class="size-full wp-image-3459" srcset="/wp-content/uploads/2012/11/1.-A-graph-and-a-possible-spanning-tree.png 620w, /wp-content/uploads/2012/11/1.-A-graph-and-a-possible-spanning-tree-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure><span id="more-3439"></span></p>
<p>We can think of a group of islands and the possible connections of bridges connecting them. Of course building bridges is expensive and time consuming, so we must be aware of what kind of bridges we want to build. Nevertheless there is an important question, what’s the minimum price we’d like to pay to build such set of bridges connecting all the islands. </p>
<figure id="attachment_3457" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/2.-Islands-and-bridges.png"><img src="/wp-content/uploads/2012/11/2.-Islands-and-bridges.png" alt="Islands and bridges" title="Islands and bridges" width="620" height="399" class="size-full wp-image-3457" srcset="/wp-content/uploads/2012/11/2.-Islands-and-bridges.png 620w, /wp-content/uploads/2012/11/2.-Islands-and-bridges-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Thus we practically need to build a minimum spanning tree, where the vertices will be the islands, while the edges will be the possible bridges between them. Every possible bridge has a weight (the price or the time we need to build it, etc.).</p>
<p>This scenario is only one of possible use cases of where minimum spanning trees can be used in practice.  </p>
<p>The two main approaches – the Kruskal’s and the Prim’s algorithms however differ. </p>
<h2>Overview</h2>
<p>The algorithm of Kruskal starts by initializing a set of |V| trees. </p>
<figure id="attachment_3458" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/3.-A-set-of-V-trees.png"><img src="/wp-content/uploads/2012/11/3.-A-set-of-V-trees.png" alt="A set of V trees" title="A set of V trees" width="620" height="399" class="size-full wp-image-3458" srcset="/wp-content/uploads/2012/11/3.-A-set-of-V-trees.png 620w, /wp-content/uploads/2012/11/3.-A-set-of-V-trees-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>During the process of building the final spanning tree we keep a forest. Obviously we start with a forest with |V| trees, where each tree is a single node tree.</p>
<figure id="attachment_3456" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/4.-A-single-node-tree.png"><img src="/wp-content/uploads/2012/11/4.-A-single-node-tree.png" alt="A single node tree" title="A single node tree" width="620" height="399" class="size-full wp-image-3456" srcset="/wp-content/uploads/2012/11/4.-A-single-node-tree.png 620w, /wp-content/uploads/2012/11/4.-A-single-node-tree-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>On some point we have a forest of “k” trees which are all a sub-trees of the minimum spanning tree. </p>
<figure id="attachment_3455" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/5.-A-forest-out-of-K-sub-trees.png"><img src="/wp-content/uploads/2012/11/5.-A-forest-out-of-K-sub-trees.png" alt="Growing forest" title="Growing forest" width="620" height="399" class="size-full wp-image-3455" srcset="/wp-content/uploads/2012/11/5.-A-forest-out-of-K-sub-trees.png 620w, /wp-content/uploads/2012/11/5.-A-forest-out-of-K-sub-trees-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Finally one step before building the final MST we have two trees and we connect them with the less weighted edge left that connects them.</p>
<p>It’s important to note that during the process of building the tree we sort the edges in ascending order by their weight.</p>
<figure id="attachment_3454" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/6.-Sorted-Edges.png"><img src="/wp-content/uploads/2012/11/6.-Sorted-Edges.png" alt="Sorted edges" title="Sorted edges" width="620" height="399" class="size-full wp-image-3454" srcset="/wp-content/uploads/2012/11/6.-Sorted-Edges.png 620w, /wp-content/uploads/2012/11/6.-Sorted-Edges-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Than we start getting edges and check whether their ends (the two vertices making the edge) belong to a different sub-trees.</p>
<figure id="attachment_3453" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/7.-Check-edges.png"><img src="/wp-content/uploads/2012/11/7.-Check-edges.png" alt="Check edges" title="Check edges" width="620" height="399" class="size-full wp-image-3453" srcset="/wp-content/uploads/2012/11/7.-Check-edges.png 620w, /wp-content/uploads/2012/11/7.-Check-edges-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<h2>Pseudo Code</h2>
<pre>
1. T (the final spanning tree) is defined to be the empty set;
2. For each vertex v of G, make the empty set out of v;
3. Sort the edges of G in ascending (non-decreasing) order;
4. For each edge (u, v) from the sored list of step 3.
      If u and v belong to different sets
         Add (u,v) to T;
         Get together u and v in one single set;
5. Return T
</pre>
<p>A great feature about the Kruskal&#8217;s algorithm is that it also work on disconnected graphs.</p>
<h2>History</h2>
<p>Kruskal’s algorithm is named after <a href="http://en.wikipedia.org/wiki/Joseph_Kruskal" title="Joseph Kruskal" target="_blank">Joseph Kruskal</a>, who wasn’t only computer scientist, but also prominent mathematician and statistician. Although he is best known for its algorithm for computing the minimum spanning tree, described in this post, he’s also known with his work as a statistician and his contribution to the formulation of multidimensional scaling. </p>
<p>Kruskal also explored the Indo-European languages contributing the studies of the linguistics along with other scientists. His “Indo-European Lexicographical List” (http://www.wordgumbo.com/ie/cmp/) is still widely used.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Prim&#8217;s Minimum Spanning Tree">Computer Algorithms: Prim&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/11/05/computer-algorithms-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Minimum Spanning Tree">Computer Algorithms: Minimum Spanning Tree </a></li>
<li><a href="/2012/06/22/computer-algorithms-binary-search-tree-data-structure/" rel="bookmark" title="Computer Algorithms: Binary Search Tree">Computer Algorithms: Binary Search Tree </a></li>
<li><a href="/2012/07/03/computer-algorithms-balancing-a-binary-search-tree/" rel="bookmark" title="Computer Algorithms: Balancing a Binary Search Tree">Computer Algorithms: Balancing a Binary Search Tree </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Computer Algorithms: Minimum Spanning Tree</title>
		<link>/2012/11/05/computer-algorithms-minimum-spanning-tree/</link>
		<comments>/2012/11/05/computer-algorithms-minimum-spanning-tree/#respond</comments>
		<pubDate>Mon, 05 Nov 2012 12:23:42 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[electricity]]></category>
		<category><![CDATA[final solution]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[greedy algorithms]]></category>
		<category><![CDATA[However greedy algorithms]]></category>
		<category><![CDATA[Kruskal algorithm]]></category>
		<category><![CDATA[Kruskal's algorithm]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Minimum spanning tree]]></category>
		<category><![CDATA[minimum weight solution]]></category>
		<category><![CDATA[Prim algorithm]]></category>
		<category><![CDATA[Prim's algorithm]]></category>
		<category><![CDATA[Spanning tree]]></category>
		<category><![CDATA[Steiner tree problem]]></category>
		<category><![CDATA[Theoretical computer science]]></category>
		<category><![CDATA[two main algorithms]]></category>
		<category><![CDATA[Widest path problem]]></category>

		<guid isPermaLink="false">/?p=3428</guid>
		<description><![CDATA[Introduction Here’s a classical task on graphs. We have a group of cities and we must wire them to provide them all with electricity. Out of all possible connections we can make, which one is using minimum amount of wire. To wire N cities, it’s clear that, you need to use at least N-1 wires &#8230; <a href="/2012/11/05/computer-algorithms-minimum-spanning-tree/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Minimum Spanning Tree</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree">Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Prim&#8217;s Minimum Spanning Tree">Computer Algorithms: Prim&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/10/08/computer-algorithms-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Graph">Computer Algorithms: Shortest Path in a Graph </a></li>
<li><a href="/2012/07/03/computer-algorithms-balancing-a-binary-search-tree/" rel="bookmark" title="Computer Algorithms: Balancing a Binary Search Tree">Computer Algorithms: Balancing a Binary Search Tree </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Here’s a classical task on graphs. We have a group of cities and we must wire them to provide them all with electricity. Out of all possible connections we can make, which one is using minimum amount of wire. </p>
<p>To wire N cities, it’s clear that, you need to use at least N-1 wires connecting a pair of cities. The problem is that sometimes you have more than one choice to do it. Even for small number of cities there must be more than one solution as shown on the image bellow. </p>
<figure id="attachment_3440" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/1.-General-Wiring-Problem.png"><img src="/wp-content/uploads/2012/11/1.-General-Wiring-Problem.png" alt="General Wiring Problem" title="General Wiring Problem" width="620" height="399" class="size-full wp-image-3440" srcset="/wp-content/uploads/2012/11/1.-General-Wiring-Problem.png 620w, /wp-content/uploads/2012/11/1.-General-Wiring-Problem-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Here we can wire these four nodes in several ways, but the question is, which one is the best one. By the way defining the term “best one” is also tricky. Most often this means which uses least wire, but it can be anything else depending on the circumstances.</p>
<p>As we talk on weighted graphs we can generally speak of a minimum weight solution through all the vertices of the graph. </p>
<p>By the way there might be more the one equally optimal (minimal) solutions. <span id="more-3428"></span></p>
<h2>Overview</h2>
<p>Obviously we must choose those edges that are enough to connect all the vertices of the graph and whose sum of weights is minimal. Since we can’t have cycles in our final solution it must form a tree. Thus we’re speaking on a minimum weight spanning tree, as the tree spans over the whole graph.</p>
<p>Does each connected and weighted graph have a minimum spanning tree? The answer is yes! By removing the cycles from the graph G we get a spanning tree, since it’s connected. From all possible spanning trees one or more are minimal. </p>
<figure id="attachment_3445" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/2.-General-Wiring-Problem.png"><img src="/wp-content/uploads/2012/11/2.-General-Wiring-Problem.png" alt="MST on General Wiring Problem" title="MST on General Wiring Problem" width="620" height="399" class="size-full wp-image-3445" srcset="/wp-content/uploads/2012/11/2.-General-Wiring-Problem.png 620w, /wp-content/uploads/2012/11/2.-General-Wiring-Problem-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>If w(u, v) is the weight of the edge (u, v),  we can speak of weight of any spanning tree T – w(T) which is the sum of all the edges forming that tree. </p>
<p>Thus the weight of the minimum spanning tree is less than the weight of whatever other spanning tree of G.</p>
<p>After we’re sure that there is at least one minimum spanning tree for all connected and weighted graphs we only need to find it somehow.</p>
<p>We can go with an incremental approach. At the end we’ll have the minimum spanning tree (MST), but before that on each step of our algorithm we’ll have a sub-set of this final tree, which will grow and grow until it becomes the real MST. This subset of edges we’ll keep in one additional set A.</p>
<figure id="attachment_3444" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/3.-Growing-the-MST.png"><img src="/wp-content/uploads/2012/11/3.-Growing-the-MST.png" alt="Growing the MST" title="Growing the MST" width="620" height="399" class="size-full wp-image-3444" srcset="/wp-content/uploads/2012/11/3.-Growing-the-MST.png 620w, /wp-content/uploads/2012/11/3.-Growing-the-MST-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>So far we know that on each step we have a subset of the final MST, but first we need to answer a couple of questions. </p>
<h3>How do we start?</h3>
<p>Well, we’ll start with the empty set of edges. Clearly the empty set is a subset of any other set, thus it will be also a subset of the MST.</p>
<figure id="attachment_3443" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/4.-Start-with-the-empty-set.png"><img src="/wp-content/uploads/2012/11/4.-Start-with-the-empty-set.png" alt="Start with the empty set" title="Start with the empty set" width="620" height="399" class="size-full wp-image-3443" srcset="/wp-content/uploads/2012/11/4.-Start-with-the-empty-set.png 620w, /wp-content/uploads/2012/11/4.-Start-with-the-empty-set-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<h3>How do we grow the tree?</h3>
<p>Another question we must answer is how to grow the tree. Since we have a MST sub-set (A) on each step how do we add an edge to this set in order to get another (bigger than the previous one) subset of edges, which will be again a subset of the minimum spanning tree?</p>
<p>Clearly we must make a decision which edge to add to the growing subset and this is the tricky part of this algorithm. </p>
<h3>Chose the lowest weight edge!</h3>
<p>To find the minimum spanning tree on each step we must get the lowest weighted edge that connects our subset (A) with the rest of the vertices.</p>
<figure id="attachment_3442" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/5.-Chose-the-lowest-weighted-edge.png"><img src="/wp-content/uploads/2012/11/5.-Chose-the-lowest-weighted-edge.png" alt="Chose the lowest weighted edge" title="Chose the lowest weighted edge" width="620" height="399" class="size-full wp-image-3442" srcset="/wp-content/uploads/2012/11/5.-Chose-the-lowest-weighted-edge.png 620w, /wp-content/uploads/2012/11/5.-Chose-the-lowest-weighted-edge-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>However can we be sure that by choosing the less weighted edge we’ll get the MST? Well, let’s assume that isn’t right in order to prove that wrong!</p>
<p>OK so on some step of our growing sub-tree we don’t get the lightest edge (u, v), because we somehow doubt this rule, and we get another edge – let’s say (x, y). Mind that w(x, y) >= w(u, v). </p>
<figure id="attachment_3441" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/6.-Weights-of-edges.png"><img src="/wp-content/uploads/2012/11/6.-Weights-of-edges.png" alt="Weights of edges" title="Weights of edges" width="620" height="399" class="size-full wp-image-3441" srcset="/wp-content/uploads/2012/11/6.-Weights-of-edges.png 620w, /wp-content/uploads/2012/11/6.-Weights-of-edges-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Thus our final MST will contain somewhere in its set of edges the edge (x, y), but the weight of MST w(T) is minimal, and if we get another spanning tree that contains the exact same edges as T but instead of (x, y) contains (u, v) we’ll get a smaller weight!</p>
<p>That isn’t possible! Thus we proved that on each step we must get the less weighted edge. </p>
<p>This particular approach is called “greedy”, because on each step we get the best possible choice. However greedy algorithms don’t always get the right or optimal solution. Fortunately for MST this isn’t true so we can be greedy as much as we can!</p>
<p>OK let’s make a summary of our algorithm in the following pseudo code.</p>
<h2>Pseudo Code</h2>
<pre lang="PHP">
1. We start with an  empty set (A) subset of the final MST;
2. Until A does not form T:
      a. Get the less weighted edge u from G;	
      b. Add u to A;
3. Return A
</pre>
<h2>Application</h2>
<p>Actually this algorithm is used firstly by Borůvka which started to wire Moravia in 1926. Even without knowing that the “greedy” approach will lead him to the right solution he optimally covered Moravia with electricity. </p>
<p>However this algorithm is too general and there are two main algorithms – the Prim&#8217;s algorithm and the Kruskal&#8217;s algorithm that we shall see in future posts. </p>
<p>The thing is that on each step we must get the less weighted edge and both algorithms use different approaches to do that.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree">Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Prim&#8217;s Minimum Spanning Tree">Computer Algorithms: Prim&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/10/08/computer-algorithms-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Graph">Computer Algorithms: Shortest Path in a Graph </a></li>
<li><a href="/2012/07/03/computer-algorithms-balancing-a-binary-search-tree/" rel="bookmark" title="Computer Algorithms: Balancing a Binary Search Tree">Computer Algorithms: Balancing a Binary Search Tree </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/11/05/computer-algorithms-minimum-spanning-tree/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
