<?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>final solution &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/final-solution/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: Strassen&#8217;s Matrix Multiplication</title>
		<link>/2012/11/26/computer-algorithms-strassens-matrix-multiplication/</link>
		<comments>/2012/11/26/computer-algorithms-strassens-matrix-multiplication/#comments</comments>
		<pubDate>Mon, 26 Nov 2012 14:16:51 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Binary operations]]></category>
		<category><![CDATA[Coppersmith–Winograd algorithm]]></category>
		<category><![CDATA[Divide and conquer algorithm]]></category>
		<category><![CDATA[faster solution]]></category>
		<category><![CDATA[final solution]]></category>
		<category><![CDATA[given solution]]></category>
		<category><![CDATA[graph algorithms]]></category>
		<category><![CDATA[Linear algebra]]></category>
		<category><![CDATA[mathematician]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Matrix]]></category>
		<category><![CDATA[matrix multiplication algorithm]]></category>
		<category><![CDATA[Matrix theory]]></category>
		<category><![CDATA[Multiplication]]></category>
		<category><![CDATA[Multiplication algorithm]]></category>
		<category><![CDATA[n^3 algorithm]]></category>
		<category><![CDATA[n^3 matrix multiplication algorithm]]></category>
		<category><![CDATA[Numerical linear algebra]]></category>
		<category><![CDATA[NxN]]></category>
		<category><![CDATA[Operations research]]></category>
		<category><![CDATA[purpose algorithm]]></category>
		<category><![CDATA[sort algorithm]]></category>
		<category><![CDATA[sub-solutions]]></category>
		<category><![CDATA[Volker Strassen]]></category>

		<guid isPermaLink="false">/?p=3466</guid>
		<description><![CDATA[Introduction The Strassen’s method of matrix multiplication is a typical divide and conquer algorithm. We’ve seen so far some divide and conquer algorithms like merge sort and the Karatsuba’s fast multiplication of large numbers. However let’s get again on what’s behind the divide and conquer approach. Unlike the dynamic programming where we “expand” the solutions &#8230; <a href="/2012/11/26/computer-algorithms-strassens-matrix-multiplication/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Strassen&#8217;s Matrix Multiplication</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2013/01/14/computer-algorithms-multiplication/" rel="bookmark" title="Computer Algorithms: Multiplication">Computer Algorithms: Multiplication </a></li>
<li><a href="/2012/05/15/computer-algorithms-karatsuba-fast-multiplication/" rel="bookmark" title="Computer Algorithms: Karatsuba Fast Multiplication">Computer Algorithms: Karatsuba Fast Multiplication </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>
<li><a href="/2012/08/31/computer-algorithms-graphs-and-their-representation/" rel="bookmark" title="Computer Algorithms: Graphs and their Representation">Computer Algorithms: Graphs and their Representation </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>The Strassen’s method of matrix multiplication is a typical divide and conquer algorithm. We’ve seen so far some divide and conquer algorithms like <a href="/2012/03/05/computer-algorithms-merge-sort/" title="Computer Algorithms: Merge Sort">merge sort</a> and the <a href="/2012/05/15/computer-algorithms-karatsuba-fast-multiplication/" title="Computer Algorithms: Karatsuba Fast Multiplication">Karatsuba’s fast multiplication</a> of large numbers. However let’s get again on what’s behind the divide and conquer approach.</p>
<p>Unlike the dynamic programming where we “expand” the solutions of sub-problems in order to get the final solution, here we are talking more on joining sub-solutions together. These solutions of some sub-problems of the general problem are equal and their merge is somehow well defined.</p>
<p>A typical example is the merge sort algorithm. In merge sort we have two sorted arrays and all we want is to get the array representing their union again sorted. Of course, the tricky part in merge sort is the merging itself. That’s because we’ve to pass through the two arrays, A and B, and we’ve to compare each “pair” of items representing an item from A and from B. A bit off topic, but this is the weak point of merge sort and although its worst-case time complexity is O(n.log(n)), quicksort is often preferred in practice because there’s no “merge”. <a href="/2012/03/13/computer-algorithms-quicksort/" title="Computer Algorithms: Quicksort">Quicksort</a> just concatenates the two sub-arrays. Note that in quicksort the sub-arrays aren’t with an equal length in general and although its worst-case time complexity is O(n^2) it often outperforms merge sort.</p>
<p>This simple example from the paragraph above shows us how sometimes merging the solutions of two sub-problems actually isn’t a trivial task to do. Thus we must be careful when applying any divide and conquer approach.</p>
<h2>History</h2>
<p><a href="http://en.wikipedia.org/wiki/Volker_Strassen" title="Volker Strassen" target="_blank">Volker Strassen</a> is a German mathematician born in 1936. He is well known for his works on probability, but in the computer science and algorithms he’s mostly recognized because of his algorithm for matrix multiplication that’s still one of the main methods that outperforms the general matrix multiplication algorithm.</p>
<p>Strassen firstly published this algorithm in 1969 and proved that the n^3 algorithm isn’t the optimal one. Actually the given solution by Strassen is slightly better, but his contribution is enormous because this resulted in many more researches about matrix multiplication that led to some faster approaches, i.e. <a href="http://en.wikipedia.org/wiki/Coppersmith%E2%80%93Winograd_algorithm" title="Coppersmith-Winograd algorithm" target="_blank">the Coppersmith-Winograd algorithm</a> with O(n^2,3737).<span id="more-3466"></span></p>
<h2>Overview</h2>
<p>The general algorithm on multiplying two matrices A[NxN] and B[NxN] is fairly simple. Although it’s more difficult than multiplying two numbers and also it is not commutative it’s still very simple – but slow.</p>
<p>Let’s first define what’s a matrix A[NxN]. As we speak about matrices NxN we usually think of a square grid with N rows and N columns. In each row and column A[i][j] we’ve a value. </p>
<figure id="attachment_3489" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/1.-Square-matrix.png"><img src="/wp-content/uploads/2012/11/1.-Square-matrix.png" alt="Square matrix" title="Square matrix" width="620" height="399" class="size-full wp-image-3489" srcset="/wp-content/uploads/2012/11/1.-Square-matrix.png 620w, /wp-content/uploads/2012/11/1.-Square-matrix-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Of course, as developers, we can think of a matrix as a two-dimensional array. </p>
<pre lang="PHP">
// PHP two-dimensional array
$a = array(
    0 => array($v1, $v2, $v3, $v4),
    1 => array($v5, $v6, $v7, $v8),
    2 => array($v9, $v10, $v11, $v12),
); 
</pre>
<p>Don’t forget that a NxN matrix is just a private case for a matrix. We can equally likely have any other size of a matrix NxM (N <> M). </p>
<p>However the size of a matrix is crucial in order to multiply it with another matrix. Why is that? </p>
<p>As I said above multiplying matrices isn’t the same as multiplying numbers. First of all this operation isn’t commutative.</p>
<figure id="attachment_3488" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/2.-Commutative-problem.png"><img src="/wp-content/uploads/2012/11/2.-Commutative-problem.png" alt="Commutative problem" title="Commutative problem" width="620" height="399" class="size-full wp-image-3488" srcset="/wp-content/uploads/2012/11/2.-Commutative-problem.png 620w, /wp-content/uploads/2012/11/2.-Commutative-problem-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>And the second problem is the way you multiply two matrices A with B.</p>
<figure id="attachment_3487" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/3.-Matrix-Multiplication.png"><img src="/wp-content/uploads/2012/11/3.-Matrix-Multiplication.png" alt="Matrix Multiplication" title="Matrix Multiplication" width="620" height="399" class="size-full wp-image-3487" srcset="/wp-content/uploads/2012/11/3.-Matrix-Multiplication.png 620w, /wp-content/uploads/2012/11/3.-Matrix-Multiplication-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Just because this works with NxN matrices we can see the problem with multiplying rectangular matrices. Indeed, this wouldn’t be possible unless the second dimension of A isn’t exactly equal to the first dimension of B. </p>
<figure id="attachment_3486" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/4.-Rect-Matrix-Multiplication.png"><img src="/wp-content/uploads/2012/11/4.-Rect-Matrix-Multiplication.png" alt="Rectangular Matrix Multiplication" title="Rectangular Matrix Multiplication" width="620" height="399" class="size-full wp-image-3486" srcset="/wp-content/uploads/2012/11/4.-Rect-Matrix-Multiplication.png 620w, /wp-content/uploads/2012/11/4.-Rect-Matrix-Multiplication-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Hopefully we are now talking about square matrices with exactly the same dimensions.</p>
<p>OK, so now we know how to multiply two square matrices (with the same dimensions NxN) and now let’s evaluate the time complexity for the general purpose algorithm.</p>
<p>As we know A.B = C only when:</p>
<pre>
C[i][j] = sum(A[i][k] * B[k][j]) for k = 0 .. n
</pre>
<p>Thus we have n^3 operations. Let’s try to find out a divide and conquer approach.</p>
<p>Indeed this isn’t difficult in case of matrices because as we know we can divide in matrix in smaller sub-matrices.</p>
<figure id="attachment_3485" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/5.-Divide-and-Conquer.png"><img src="/wp-content/uploads/2012/11/5.-Divide-and-Conquer.png" alt="Divide and Conquer" title="Divide and Conquer" width="620" height="399" class="size-full wp-image-3485" srcset="/wp-content/uploads/2012/11/5.-Divide-and-Conquer.png 620w, /wp-content/uploads/2012/11/5.-Divide-and-Conquer-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Now what do we have?</p>
<figure id="attachment_3484" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/6.-Divide-and-Conquer-Result.png"><img src="/wp-content/uploads/2012/11/6.-Divide-and-Conquer-Result.png" alt="Divide and Conquer Result" title="Divide and Conquer Result" width="620" height="399" class="size-full wp-image-3484" srcset="/wp-content/uploads/2012/11/6.-Divide-and-Conquer-Result.png 620w, /wp-content/uploads/2012/11/6.-Divide-and-Conquer-Result-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Again &#8211; the same complexity – we have 8 products and 4 sums. Where’s the catch? </p>
<p>Of course in order to get faster solution we’ve to be looking as Strassen did in 1969. He defined P1, P2, P3, P4, P5, P6 and P7 as defined on the image below.</p>
<figure id="attachment_3483" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/7.-Strassens-Algorithm.png"><img src="/wp-content/uploads/2012/11/7.-Strassens-Algorithm.png" alt="Strassen&#039;s Algorithm" title="Strassen&#039;s Algorithm" width="620" height="399" class="size-full wp-image-3483" srcset="/wp-content/uploads/2012/11/7.-Strassens-Algorithm.png 620w, /wp-content/uploads/2012/11/7.-Strassens-Algorithm-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<h2>Complexity</h2>
<p>As I mentioned above the Strassen’s algorithm is slightly faster than the general matrix multiplication algorithm. The general algorithm’s time complexity is O(n^3), while the Strassen’s algorithm is O(n^2.80).</p>
<p>You can see on the chart below how slightly faster is this even for large n.</p>
<figure id="attachment_3482" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/11/Strassens-Complexity.png"><img src="/wp-content/uploads/2012/11/Strassens-Complexity.png" alt="Strassen&#039;s Complexity" title="Strassen&#039;s Complexity" width="600" height="371" class="size-full wp-image-3482" srcset="/wp-content/uploads/2012/11/Strassens-Complexity.png 600w, /wp-content/uploads/2012/11/Strassens-Complexity-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<h2>Application</h2>
<p>Although this algorithm seems to be more close to pure mathematics than to computer practically everywhere we use NxN arrays we can benefit from matrix multiplication.</p>
<p>In the other hand the algorithm of Strassen is not much faster than the general n^3 matrix multiplication algorithm. That’s very important because for small n (usually n < 45) the general algorithm is practically a better choice. However as you can see from the chart above for n > 100 the difference can be very big.</p>
<p>In the same time typically NxN arrays are used always when we talk about adjacency matrix of graphs |V| = n and some graph algorithms practically depend on matrix multiplication. </p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2013/01/14/computer-algorithms-multiplication/" rel="bookmark" title="Computer Algorithms: Multiplication">Computer Algorithms: Multiplication </a></li>
<li><a href="/2012/05/15/computer-algorithms-karatsuba-fast-multiplication/" rel="bookmark" title="Computer Algorithms: Karatsuba Fast Multiplication">Computer Algorithms: Karatsuba Fast Multiplication </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>
<li><a href="/2012/08/31/computer-algorithms-graphs-and-their-representation/" rel="bookmark" title="Computer Algorithms: Graphs and their Representation">Computer Algorithms: Graphs and their Representation </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/11/26/computer-algorithms-strassens-matrix-multiplication/feed/</wfw:commentRss>
		<slash:comments>25</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>
