<?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>Graphs &#8211; stoimen&#039;s web log</title>
	<atom:link href="/category/graphs/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: Topological Sort Revisited</title>
		<link>/2012/12/10/computer-algorithms-topological-sort-revisited/</link>
		<comments>/2012/12/10/computer-algorithms-topological-sort-revisited/#comments</comments>
		<pubDate>Mon, 10 Dec 2012 15:45:16 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Adjacency list]]></category>
		<category><![CDATA[Adjacency matrix]]></category>
		<category><![CDATA[Breadth-first search]]></category>
		<category><![CDATA[Dijkstra's algorithm]]></category>
		<category><![CDATA[Directed acyclic graph]]></category>
		<category><![CDATA[Graph]]></category>
		<category><![CDATA[graph algorithms]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[ineffective algorithm]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[time consuming algorithm]]></category>
		<category><![CDATA[Topological sorting]]></category>
		<category><![CDATA[Vertex]]></category>

		<guid isPermaLink="false">/?p=3494</guid>
		<description><![CDATA[Introduction We already know what’s topological sort of a directed acyclic graph. So why do we need a revision of this algorithm? First of all I never mentioned its complexity, thus to understand why we do need a revision let’s get again on the algorithm. We have a directed acyclic graph (DAG). There are no &#8230; <a href="/2012/12/10/computer-algorithms-topological-sort-revisited/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Topological Sort Revisited</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/10/01/computer-algorithms-topological-sort-of-a-graph/" rel="bookmark" title="Computer Algorithms: Topological Sort of a Graph">Computer Algorithms: Topological Sort of a Graph </a></li>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </a></li>
<li><a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </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>We already know what’s topological sort of a directed acyclic graph. So why do we need a revision of this algorithm? First of all I never mentioned its complexity, thus to understand why we do need a revision let’s get again on the algorithm.</p>
<p>We have a directed acyclic graph (DAG). There are no cycles so we must go for some kind of order putting all the vertices of the graph in such an order, that if there’s a directed edge (u, v), u must precede v in that order. </p>
<figure id="attachment_3506" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/1.-Topological-Sort.png"><img src="/wp-content/uploads/2012/12/1.-Topological-Sort.png" alt="Topological Sort" title="Topological Sort" width="620" height="399" class="size-full wp-image-3506" srcset="/wp-content/uploads/2012/12/1.-Topological-Sort.png 620w, /wp-content/uploads/2012/12/1.-Topological-Sort-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>The process of putting all the vertices of the DAG in such an order is called topological sorting. It’s commonly used in task scheduling or while finding the shortest paths in a DAG.</p>
<p>The algorithm itself is pretty simple to understand and code. We must start from the vertex (vertices) that don’t have predecessors. </p>
<p><figure id="attachment_3513" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/2.-Topological-Sort-step-1.png"><img src="/wp-content/uploads/2012/12/2.-Topological-Sort-step-1.png" alt="Topological Sort - step 1" title="Topological Sort - step 1" width="620" height="399" class="size-full wp-image-3513" srcset="/wp-content/uploads/2012/12/2.-Topological-Sort-step-1.png 620w, /wp-content/uploads/2012/12/2.-Topological-Sort-step-1-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure><span id="more-3494"></span></p>
<p>We put them in our sorted list in random order. Since they don’t depend on each other we can assume they are equally sorted already. Indeed thinking of a task schedule if there are tasks that don’t have predecessors (they don’t depend on other tasks before them) and that don’t depend on each other we can put them in random order (and execute them in random order).</p>
<figure id="attachment_3512" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/3.-Topological-Sort-Order.png"><img src="/wp-content/uploads/2012/12/3.-Topological-Sort-Order.png" alt="Topological Sort - Order" title="Topological Sort - Order" width="620" height="399" class="size-full wp-image-3512" srcset="/wp-content/uploads/2012/12/3.-Topological-Sort-Order.png 620w, /wp-content/uploads/2012/12/3.-Topological-Sort-Order-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Once we have the vertices with no predecessors we must remove the edges starting from them. Then – go again with the vertices with no predecessors. </p>
<figure id="attachment_3511" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/4.-Topological-Sort-step-2.png"><img src="/wp-content/uploads/2012/12/4.-Topological-Sort-step-2.png" alt="Topological Sort - step 2" title="Topological Sort - step 2" width="620" height="399" class="size-full wp-image-3511" srcset="/wp-content/uploads/2012/12/4.-Topological-Sort-step-2.png 620w, /wp-content/uploads/2012/12/4.-Topological-Sort-step-2-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>It’s as simple as that, so why do we need a revision of this algorithm? Well, basically because of its efficiency. </p>
<h2>Overview</h2>
<p>As we know most of the graph algorithms depend on the way the graph is represented in our application. We consider as the two main representations the adjacency matrix … </p>
<figure id="attachment_3510" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/5.-Adjacency-Matrix.png"><img src="/wp-content/uploads/2012/12/5.-Adjacency-Matrix.png" alt="Adjacency Matrix" title="Adjacency Matrix" width="620" height="399" class="size-full wp-image-3510" srcset="/wp-content/uploads/2012/12/5.-Adjacency-Matrix.png 620w, /wp-content/uploads/2012/12/5.-Adjacency-Matrix-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>… and adjacency lists.</p>
<figure id="attachment_3509" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/6.-Adjacency-Lists.png"><img src="/wp-content/uploads/2012/12/6.-Adjacency-Lists.png" alt="Adjacency Lists" title="Adjacency Lists" width="620" height="399" class="size-full wp-image-3509" srcset="/wp-content/uploads/2012/12/6.-Adjacency-Lists.png 620w, /wp-content/uploads/2012/12/6.-Adjacency-Lists-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Let’s first take a look of some of the main approaches to get the topologically sorted list at the end of the algorithm. </p>
<p>What can we do in order to find the vertices with no predecessors? We can only scan the entire list of vertices. </p>
<h3>Adjacency Matrix</h3>
<p>In case we’re using adjacency matrix we need|V|^2 space to store the graph. To find the vertices with no predecessors we have to scan the entire graph, which will cost us O(|V|^2) time.  And we’ll have to do that |V| times. This will be |V|^3 time consuming algorithm and for dense graphs this will be quite an ineffective algorithm.</p>
<h3>Adjacency Lists</h3>
<p>What about the adjacency list? There we need |E| space to store a directed graph. How fast can we find a node with no predecessor? Practically we’ll need O(|E|) time.  Thus in the worst case we have again O(|V|^2) time consuming programs.</p>
<p>So what can be done in order to optimize this algorithm?</p>
<p>Practically we can start by picking up a random vertex and “go back” until we get a node with no predecessors. This approach can be very effective yet also very ineffective. First of all if we have to scan all the way back to a node with no predecessors this will cost us |V| time, but if we stuck on a node that don’t have a preceding node then we’ll have a constant speed.</p>
<p>This means that we can modify the algorithm a bit in order to improve a lot the algorithm. We just need to store both incoming and outgoing edges and slightly modify the adjacency lists.</p>
<figure id="attachment_3508" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/7.-Modified-Adjacency-Lists.png"><img src="/wp-content/uploads/2012/12/7.-Modified-Adjacency-Lists.png" alt="Modified Adjacency Lists" title="Modified Adjacency Lists" width="620" height="399" class="size-full wp-image-3508" srcset="/wp-content/uploads/2012/12/7.-Modified-Adjacency-Lists.png 620w, /wp-content/uploads/2012/12/7.-Modified-Adjacency-Lists-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>What’s the algorithm now?</p>
<p>First we easily find the nodes with no predecessors. Then, using a queue, we can keep the nodes with no predecessors and on each dequeue we can remove the edges from the node to all other nodes.</p>
<h2>Pseudo Code</h2>
<pre>
1. Represent the graph with two lists on each vertex (incoming edges and outgoing edges)
2. Make an empty queue Q;
3. Make an empty topologically sorted list T;
4. Push all items with no predecessors in Q;
5. While Q is not empty
   a. Dequeue from Q into u;
   b. Push u in T;
   c. Remove all outgoing edges from u;
6. Return T;
</pre>
<p>This approach will give us a better performance than the “brute force” approach. The running time complexity is O(|V| + |E|). The problem is that we need additional space and an operational queue, but this approach is a perfect example of how by using additional space you can get a better performing algorithm.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/10/01/computer-algorithms-topological-sort-of-a-graph/" rel="bookmark" title="Computer Algorithms: Topological Sort of a Graph">Computer Algorithms: Topological Sort of a Graph </a></li>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </a></li>
<li><a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </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/12/10/computer-algorithms-topological-sort-revisited/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Computer Algorithms: Longest Increasing Subsequence</title>
		<link>/2012/12/03/computer-algorithms-longest-increasing-subsequence/</link>
		<comments>/2012/12/03/computer-algorithms-longest-increasing-subsequence/#comments</comments>
		<pubDate>Mon, 03 Dec 2012 13:22:08 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[dynamic programming]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Bellman–Ford algorithm]]></category>
		<category><![CDATA[Directed acyclic graph]]></category>
		<category><![CDATA[Dynamic programming]]></category>
		<category><![CDATA[equal sub-solutions]]></category>
		<category><![CDATA[Facebook Inc]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[graph algorithms]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[Longest increasing subsequence]]></category>
		<category><![CDATA[Mathematical optimization]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Network theory]]></category>
		<category><![CDATA[Operations research]]></category>
		<category><![CDATA[Richard Bellman]]></category>
		<category><![CDATA[Routing algorithms]]></category>
		<category><![CDATA[Shortest path problem]]></category>
		<category><![CDATA[sub-solution]]></category>
		<category><![CDATA[Theoretical computer science]]></category>
		<category><![CDATA[Yahoo! Inc.]]></category>

		<guid isPermaLink="false">/?p=3478</guid>
		<description><![CDATA[Introduction A very common problem in computer programming is finding the longest increasing (decreasing) subsequence in a sequence of numbers (usually integers). Actually this is a typical dynamic programming problem. Dynamic programming can be described as a huge area of computer science problems that can be categorized by the way they can be solved. Unlike &#8230; <a href="/2012/12/03/computer-algorithms-longest-increasing-subsequence/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Longest Increasing Subsequence</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </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>
<li><a href="/2012/12/10/computer-algorithms-topological-sort-revisited/" rel="bookmark" title="Computer Algorithms: Topological Sort Revisited">Computer Algorithms: Topological Sort Revisited </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>A very common problem in computer programming is finding the longest increasing (decreasing) subsequence in a sequence of numbers (usually integers). Actually this is a typical dynamic programming problem.</p>
<p>Dynamic programming can be described as a huge area of computer science problems that can be categorized by the way they can be solved. Unlike divide and conquer, where we were able to merge the fairly equal sub-solutions in order to receive one single solution of the problem, in dynamic programming we usually try to find an optimal sub-solution and then grow it.</p>
<p>Once we have an optimal sub-solution on each step we try to upgrade it in order to cover the whole problem. Thus a typical member of the dynamic programming class is finding the longest subsequence.</p>
<p>However this problem is interesting because it can be related to graph theory. Let’s find out how.<span id="more-3478"></span></p>
<h2>Overview</h2>
<p>We already know various ways to calculate the shortest paths in a graph. Indeed finding the single-source shortest path is a typical graph problem. To model such kind of solutions we definitely need a graph represented in our solution. </p>
<p>However the single-source shortest path isn’t a straight-forward problem. It depends on many factors. Thus for positive edges <a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" title="Computer Algorithms: Dijkstra Shortest Path in a Graph">the algorithm of Edsger Dijkstra</a> can be a perfect solution, but when the graph contains negative edges his algorithm is no longer useful. </p>
<p>In the presence of negative edges the Dijkstra’s algorithm doesn’t work and we’d better use the <a href="/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/" title="Computer Algorithms: Bellman-Ford Shortest Path in a Graph">Bellman-Ford algorithm</a>. It is interesting to note, that it was exactly <a href="http://en.wikipedia.org/wiki/Richard_E._Bellman" title="Richard E. Bellman" target="_blank">Richard Bellman</a> who first introduced the term “dynamic programming” in the 1940s.</p>
<p>In fact the Bellman-Ford algorithm was able to detect negative cycles. That’s too important, because in presence of negative cycles the shortest path problem is no longer well defined. </p>
<p>In the other hand when we’re talking about <a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">shortest paths in a DAG</a> (Directed Acyclic Graph) we can find a faster (linear) solution. That’s because we’re sure that there are no cycles (not even negative cycles)! </p>
<figure id="attachment_3498" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/1.-Toplogical-Sort.png"><img src="/wp-content/uploads/2012/12/1.-Toplogical-Sort.png" alt="Toplogical Sort" title="Toplogical Sort" width="620" height="399" class="size-full wp-image-3498" srcset="/wp-content/uploads/2012/12/1.-Toplogical-Sort.png 620w, /wp-content/uploads/2012/12/1.-Toplogical-Sort-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Finding the shortest paths in a DAG is closely related to the topological sorting of the DAG. This gives us a linear representation of the vertices of the DAG and we can clearly calculate the distances from the starting node to all other nodes. Note that in a DAG we have one or more nodes that can be considered as starting nodes – which means they don’t have predecessors (incoming edges).</p>
<figure id="attachment_3497" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/2.-Toplogical-Sort-2.png"><img src="/wp-content/uploads/2012/12/2.-Toplogical-Sort-2.png" alt="Toplogical Sort 2" title="Toplogical Sort 2" width="620" height="399" class="size-full wp-image-3497" srcset="/wp-content/uploads/2012/12/2.-Toplogical-Sort-2.png 620w, /wp-content/uploads/2012/12/2.-Toplogical-Sort-2-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Following the words above is pretty hard to find what all these graph algorithms have to do with finding the longest increasing (decreasing) subsequence. Actually this problem is very closely related to the toplogical sort of the DAG and the problem of finding shortest paths in a DAG.</p>
<p>That’s because we can represent our sequence as a DAG. The only thing we must care about is to “connect” with directed edges those elements that form an increasing (decreasing) pair. </p>
<figure id="attachment_3496" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/3.-Integer-Sequence.png"><img src="/wp-content/uploads/2012/12/3.-Integer-Sequence.png" alt="Integer Sequence as a DAG" title="Integer Sequence" width="620" height="399" class="size-full wp-image-3496" srcset="/wp-content/uploads/2012/12/3.-Integer-Sequence.png 620w, /wp-content/uploads/2012/12/3.-Integer-Sequence-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Thus the sequence from our example [1, 8, 2, 7, 3, 4, 1, 6] is going to look like this.</p>
<figure id="attachment_3495" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/4.-Longest-subsequence.png"><img src="/wp-content/uploads/2012/12/4.-Longest-subsequence.png" alt="Longest subsequence" title="Longest subsequence" width="620" height="399" class="size-full wp-image-3495" srcset="/wp-content/uploads/2012/12/4.-Longest-subsequence.png 620w, /wp-content/uploads/2012/12/4.-Longest-subsequence-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Another important thing to note is that we don’t search for shortest, but for longest path, since our task is to find the longest subsequence.</p>
<h2>Pseudo Code</h2>
<p>Our pseudo code for finding the shortest paths in a DAG was something like that.</p>
<pre>
1. Get the toplogically sorted list L of the DAG;
2. The starting node is s;
3. The distance to s equals to 0;
4. All other distances are initialized to &#8734;
5. For each node (v) in L\{s} do:
5.1. If dist(v) > dist(u) + w(u, v) then
5.1.1.  dist(v) := dist(u) + w(u, v)
</pre>
<p>In the above pseudo code &#8220;u&#8221; is every predecessor of &#8220;v&#8221;!</p>
<p>Now we must “reverse” the solution above in order to find the longest increasing subsequence. Note that we don&#8217;t care any more about the weight of the edges, thus we can simply substitute them with 1.</p>
<pre>
1. Get the sequence (L) as a toplogically sorted DAG;
2. For each (i) in L do:
2.1. S(i) := 1 + max(S(j), where (i, j) is an edge from the DAG);
</pre>
<h2>Application</h2>
<p>Finding the longest increasing subsequence can be very useful not only at the Google/Yahoo/Facebook interview, but also in various fields of statistics.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </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>
<li><a href="/2012/12/10/computer-algorithms-topological-sort-revisited/" rel="bookmark" title="Computer Algorithms: Topological Sort Revisited">Computer Algorithms: Topological Sort Revisited </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/12/03/computer-algorithms-longest-increasing-subsequence/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
		<item>
		<title>Computer Algorithms: Shortest Path in a Directed Acyclic Graph</title>
		<link>/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/</link>
		<comments>/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/#comments</comments>
		<pubDate>Sun, 28 Oct 2012 19:24:22 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Bellman–Ford algorithm]]></category>
		<category><![CDATA[Dijkstra's algorithm]]></category>
		<category><![CDATA[Directed acyclic graph]]></category>
		<category><![CDATA[Distance]]></category>
		<category><![CDATA[faster algorithm]]></category>
		<category><![CDATA[Ford Motor Company]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[Longest path problem]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Network theory]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Routing algorithms]]></category>
		<category><![CDATA[Shortest path problem]]></category>
		<category><![CDATA[Theoretical computer science]]></category>
		<category><![CDATA[Topological sorting]]></category>

		<guid isPermaLink="false">/?p=3419</guid>
		<description><![CDATA[Introduction We saw how to find the shortest path in a graph with positive edges using the Dijkstra’s algorithm. We also know how to find the shortest paths from a given source node to all other nodes even when there are negative edges using the Bellman-Ford algorithm. Now we’ll see that there’s a faster algorithm &#8230; <a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Shortest Path in a Directed Acyclic Graph</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<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/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/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/12/03/computer-algorithms-longest-increasing-subsequence/" rel="bookmark" title="Computer Algorithms: Longest Increasing Subsequence">Computer Algorithms: Longest Increasing Subsequence </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>We saw how to find the shortest path in a graph with positive edges using the <a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" title="Computer Algorithms: Dijkstra Shortest Path in a Graph">Dijkstra’s algorithm</a>. We also know how to find the shortest paths from a given source node to all other nodes even when there are negative edges using <a href="/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/" title="Computer Algorithms: Bellman-Ford Shortest Path in a Graph">the Bellman-Ford algorithm</a>. Now we’ll see that there’s a faster algorithm running in linear time that can find the shortest paths from a given source node to all other reachable vertices in a directed acyclic graph, also known as a DAG.</p>
<p>Because the DAG is acyclic we don’t have to worry about negative cycles. As we already know it’s pointless to speak about shortest path in the presence of negative cycles because we can “loop” over these cycles and practically our path will become shorter and shorter.</p>
<figure id="attachment_3431" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/1.-Negative-Cycles.png"><img src="/wp-content/uploads/2012/10/1.-Negative-Cycles.png" alt="Negative Cycles" title="Negative Cycles" width="620" height="399" class="size-full wp-image-3431" srcset="/wp-content/uploads/2012/10/1.-Negative-Cycles.png 620w, /wp-content/uploads/2012/10/1.-Negative-Cycles-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">The presence of a negative cycles make our atempt to find the shortest path pointless!</figcaption></figure>
<p>Thus we have two problems to overcome with Dijkstra and the Bellman-Ford algorithms. First of all we needed only positive weights and on the second place we didn’t want cycles. Well, we can handle both cases in this algorithm.<span id="more-3419"></span></p>
<h2>Overview</h2>
<p>The first thing we know about DAGs is that they can easily be topologically sorted. <a href="/2012/10/01/computer-algorithms-topological-sort-of-a-graph/" title="Computer Algorithms: Topological Sort of a Graph">Topological sort</a> can be used in many practical cases, but perhaps the mostly used one is when trying to schedule dependent tasks.</p>
<figure id="attachment_3429" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/2.-Topological-Sort.png"><img src="/wp-content/uploads/2012/10/2.-Topological-Sort.png" alt="Topological Sort" title="Topological Sort" width="620" height="399" class="size-full wp-image-3429" srcset="/wp-content/uploads/2012/10/2.-Topological-Sort.png 620w, /wp-content/uploads/2012/10/2.-Topological-Sort-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Topological sort is often used to &#8220;sort&#8221; dependent tasks!</figcaption></figure>
<p>After a topological sort we end with a list of vertices of the DAG and we’re sure that if there’s an edge (u, v), u will precede v in the topologically sorted list.</p>
<figure id="attachment_3430" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/3.-Topological-Sort-part-2.png"><img src="/wp-content/uploads/2012/10/3.-Topological-Sort-part-2.png" alt="Topological Sort (part 2)" title="Topological Sort (part 2)" width="620" height="399" class="size-full wp-image-3430" srcset="/wp-content/uploads/2012/10/3.-Topological-Sort-part-2.png 620w, /wp-content/uploads/2012/10/3.-Topological-Sort-part-2-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">If there&#8217;s an edge (u,v) then u must precede v. This results in the more general case from the image. There&#8217;s no edge between B and D, but B precedes D!</figcaption></figure>
<p>This information is precious and the only thing we need to do is to pass through this sorted list and to calculate distances for a shortest paths just like the algorithm of Dijkstra.</p>
<p>OK, so let’s summarize this algorithm:<br />
&#8211;	First we must topologically sort the DAG;<br />
&#8211;	As a second step we set the distance to the source to 0 and infinity to all other vertices;<br />
&#8211;	Then for each vertex from the list we pass through all its neighbors and we check for shortest path;</p>
<p>It’s pretty much like the Dijkstra’s algorithm with the main difference that we used a priority queue then, while this time we use the list from the topological sort.</p>
<h2>Code</h2>
<p>This time the code is actually a pseudocode. Altough all the examples so far was in PHP, perhaps pseudocode is easier to understand and doesn&#8217;t bind you in a specific language implementation. Also if you don&#8217;t feel comforatable with the given programming language it can be more difficult for you to understand the code than by reading pseudocode.</p>
<pre lang="PHP line="1">
1. Topologically sort G into L;
2. Set the distance to the source to 0;
3. Set the distances to all other vertices to infinity;
4. For each vertex u in L
5.    - Walk through all neighbors v of u;
6.    - If dist(v) > dist(u) + w(u, v) 
7.       - Set dist(v) <- dist(u) + w(u, v);
</pre>
<h2>Application</h2>
<p>It’s clear why and where we must use this algorithm. The only problem is that we must be sure that the graph doesn’t have cycles. However if we’re aware of how the graph is created we may have some additional information if there are cycles or not – then this linear time algorithm can be very applicable. </p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<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/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/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/12/03/computer-algorithms-longest-increasing-subsequence/" rel="bookmark" title="Computer Algorithms: Longest Increasing Subsequence">Computer Algorithms: Longest Increasing Subsequence </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Computer Algorithms: Bellman-Ford Shortest Path in a Graph</title>
		<link>/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/</link>
		<comments>/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/#comments</comments>
		<pubDate>Mon, 22 Oct 2012 13:55:28 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Adjacency matrix]]></category>
		<category><![CDATA[Algebraic graph theory]]></category>
		<category><![CDATA[Bellman–Ford algorithm]]></category>
		<category><![CDATA[Dijkstra's algorithm]]></category>
		<category><![CDATA[Floyd–Warshall algorithm]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[Lester Ford Jr.]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Matrix]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Richard E. Bellman]]></category>
		<category><![CDATA[Routing algorithms]]></category>
		<category><![CDATA[Shortest path problem]]></category>
		<category><![CDATA[The algorithm]]></category>
		<category><![CDATA[Theoretical computer science]]></category>

		<guid isPermaLink="false">/?p=3417</guid>
		<description><![CDATA[Introduction As we saw in the previous post, the algorithm of Dijkstra is very useful when it comes to find all the shortest paths in a weighted graph. However it has one major problem! Obviously it doesn’t work correctly when dealing with negative lengths of the edges. We know that the algorithm works perfectly when &#8230; <a href="/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Bellman-Ford Shortest Path in a Graph</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </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/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/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>As we saw in the previous post, <a title="Computer Algorithms: Dijkstra Shortest Path in a Graph" href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/">the algorithm of Dijkstra</a> is very useful when it comes to find all the shortest paths in a weighted graph. However it has one major problem! Obviously it doesn’t work correctly when dealing with negative lengths of the edges.</p>
<p>We know that the algorithm works perfectly when it comes to positive edges, and that is absolutely normal because we try to optimize the inequality of the triangle.</p>
<figure id="attachment_3420" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/1.-Dijkstras-Approach.png"><img class="size-full wp-image-3420" title="Dijkstra's Approach" src="/wp-content/uploads/2012/10/1.-Dijkstras-Approach.png" alt="Dijkstra's Approach" width="620" height="399" srcset="/wp-content/uploads/2012/10/1.-Dijkstras-Approach.png 620w, /wp-content/uploads/2012/10/1.-Dijkstras-Approach-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Since all the edges are positive we get the closest one!</figcaption></figure>
<p>Since Dijkstra’s algorithm make use of a priority queue normally we get first the shortest adjacent edge to the starting point. In our very basic example we’ll get first the edge with the length of 3 -&gt; (S, A).</p>
<p>However when it comes to negative edges we can&#8217;t use any more priority queues, so we need a different, yet working solution.<span id="more-3417"></span></p>
<h2>Overview</h2>
<p>The solution was published by <a title="Richard E. Bellman" href="http://en.wikipedia.org/wiki/Richard_Bellman" target="_blank">Richard E. Bellman</a> and <a title="Lester Ford, Jr." href="http://en.wikipedia.org/wiki/L._R._Ford,_Jr." target="_blank">Lester Ford, Jr.</a> in 1958 in their publication &#8220;On a Routing Problem&#8221; and it is quite simple to explain and understand. Since we can prioritize the edges by its lengths the only thing we should do is to calculate <span style="text-decoration: underline;">all</span> the paths. And to be sure that our algorithm will find all the paths correctly we repeat that N-1 times, where N is the number of vertices (|V| = N)!</p>
<figure id="attachment_3421" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/2.-Bellman-Ford-Approach.png"><img src="/wp-content/uploads/2012/10/2.-Bellman-Ford-Approach.png" alt="Bellman-Ford Approach" title="Bellman-Ford Approach" width="620" height="399" class="size-full wp-image-3421" srcset="/wp-content/uploads/2012/10/2.-Bellman-Ford-Approach.png 620w, /wp-content/uploads/2012/10/2.-Bellman-Ford-Approach-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">The algorithm of Bellman-Ford doesn&#8217;t use priority queues! Indeed they are useless since the closest node in the queue can have shorter path passing through another node!</figcaption></figure>
<p>In this very basic image we can see how Bellman-Ford solves the problem. First we get the distances from S to A and B, which are respectively 3 and 4, but there is a shorter path to A, which passes through B and it is (S, B) + (B, A) = 4 – 2 = 2.</p>
<h2>Code</h2>
<p>Here’s the code on <a href="/category/php/" title="PHP on Stoimen.com">PHP</a>. Note that this time we use an adjacency matrix and an additional array of distances. It’s important (for directed graphs, and our graph this time is directed) to put the positive value of A[j][i] if A[i][j] is negative. Note the case for A[1][2]!</p>
<pre lang="PHP">
define('INFINITY', 10000000);

$matrix = array(
    0 => array( 0,  3,  4),
    1 => array( 0,  0,  2),
    2 => array( 0,  -2, 0),
);

$len = count($matrix);

$dist = array();

function BellmanFord(&$matrix, &$dist, $start)
{
    global $len;
    
    foreach (array_keys($matrix) as $vertex) {
        $dist[$vertex] = INFINITY;
        if ($vertex == $start) {
            $dist[$vertex] = 0;
        }
    }
    
    for ($k = 0; $k < $len - 1; $k++) {
        for ($i = 0; $i < $len; $i++) {
            for ($j = 0; $j < $len; $j++) {
                if ($dist[$i] > $dist[$j] + $matrix[$j][$i]) {
                    $dist[$i] = $dist[$j] + $matrix[$j][$i];
                }
            }
        }
    }
}

BellmanFord($matrix, $dist, 0);

// [0, 2, 4]
print_r($dist);
</pre>
<h3>Complexity</h3>
<p>The complexity is clearly O(n<sup>3</sup>) which follows directly from the code above.</p>
<h2>Application</h2>
<p>Actually this algorithm is very useful and it not only works with negative weights, but also can help us find negative cycles in the graph.</p>
<figure id="attachment_3422" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/3.-Negative-Cycles.png"><img src="/wp-content/uploads/2012/10/3.-Negative-Cycles.png" alt="Negative Cycles" title="Negative Cycles" width="620" height="399" class="size-full wp-image-3422" srcset="/wp-content/uploads/2012/10/3.-Negative-Cycles.png 620w, /wp-content/uploads/2012/10/3.-Negative-Cycles-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">A negative cycle can be found with Bellman-Ford&#8217;s algorithm!</figcaption></figure>
<p>This is done with the simple check after the main loop.</p>
<pre lang="PHP">
    for ($i = 0; $i < $len; $i++) {
        for ($j = 0; $j < $len; $j++) {
            if ($dist[$i] > $dist[$j] + $matrix[$j][$i]) {
                echo 'The graph contains a negative cycle!';
            }
        }
    }
</pre>
<p>And here&#8217;s the full code.</p>
<pre lang="PHP">
$matrix = array(
    0 => array( 0,  3,  4),
    1 => array( 0,  0,  2),
    2 => array( 0,  -2, 0),
);

$len = count($matrix);

$dist = array();

function BellmanFord(&$matrix, &$dist, $start)
{
    global $len;
    
    foreach (array_keys($matrix) as $vertex) {
        $dist[$vertex] = INFINITY;
        if ($vertex == $start) {
            $dist[$vertex] = 0;
        }
    }
    
    for ($k = 0; $k < $len - 1; $k++) {
        for ($i = 0; $i < $len; $i++) {
            for ($j = 0; $j < $len; $j++) {
                if ($dist[$i] > $dist[$j] + $matrix[$j][$i]) {
                    $dist[$i] = $dist[$j] + $matrix[$j][$i];
                }
            }
        }
    }
    
    for ($i = 0; $i < $len; $i++) {
        for ($j = 0; $j < $len; $j++) {
            if ($dist[$i] > $dist[$j] + $matrix[$j][$i]) {
                echo 'The graph contains a negative cycle!';
            }
        }
    }
}

BellmanFord($matrix, $dist, 0);

// [0, 2, 4]
print_r($dist);
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </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/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/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Computer Algorithms: Dijkstra Shortest Path in a Graph</title>
		<link>/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/</link>
		<comments>/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/#comments</comments>
		<pubDate>Mon, 15 Oct 2012 14:12:50 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[BFS algorithm]]></category>
		<category><![CDATA[Breadth-first search]]></category>
		<category><![CDATA[Depth-first search]]></category>
		<category><![CDATA[Dijkstra algorithm]]></category>
		<category><![CDATA[Dijkstra's algorithm]]></category>
		<category><![CDATA[Distance]]></category>
		<category><![CDATA[Graph]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[library SPL]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Network theory]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Routing algorithms]]></category>
		<category><![CDATA[search algorithms]]></category>
		<category><![CDATA[Shortest path problem]]></category>
		<category><![CDATA[The algorithm]]></category>
		<category><![CDATA[Theoretical computer science]]></category>
		<category><![CDATA[USD]]></category>

		<guid isPermaLink="false">/?p=3381</guid>
		<description><![CDATA[Introduction We already know how we can find the shortest paths in a graph starting from a given vertex. Practically we modified breadth-first search in order to calculate the distances from s to all other nodes reachable from s. We know that this works because BFS walks through the graph level by level. Some sources &#8230; <a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Dijkstra Shortest Path in a Graph</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<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/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic 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>
<li><a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>We already know how we can find the shortest paths in a graph starting from a given vertex. Practically we modified breadth-first search in order to calculate the distances from s to all other nodes reachable from s. We know that this works because BFS walks through the graph level by level.</p>
<figure id="attachment_3397" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/1.-BFS-Shortest-Paths.png"><img src="/wp-content/uploads/2012/10/1.-BFS-Shortest-Paths.png" alt="BFS Shortest Paths" title="BFS Shortest Paths" width="620" height="399" class="size-full wp-image-3397" srcset="/wp-content/uploads/2012/10/1.-BFS-Shortest-Paths.png 620w, /wp-content/uploads/2012/10/1.-BFS-Shortest-Paths-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">BFS is often used to find shortest paths between a starting node (s) and all other reachable nodes in a graph!</figcaption></figure>
<p>Some sources give a very simple explanation of how BFS finds the shortest paths in a graph. We must just think of the graph as a set of balls connected through strings. </p>
<figure id="attachment_3398" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/2.-The-Graph-as-Balls-and-Strings.png"><img src="/wp-content/uploads/2012/10/2.-The-Graph-as-Balls-and-Strings.png" alt="The Graph as Balls and Strings" title="The Graph as Balls and Strings" width="620" height="399" class="size-full wp-image-3398" srcset="/wp-content/uploads/2012/10/2.-The-Graph-as-Balls-and-Strings.png 620w, /wp-content/uploads/2012/10/2.-The-Graph-as-Balls-and-Strings-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">We can think of a graph as a set of balls connected through strings!</figcaption></figure>
<p>As we can see by lifting the ball called “S” all other balls fall down. The closest balls are directly connected to “s” and this is the first level, while the outermost balls are those with longest paths.</p>
<figure id="attachment_3399" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/3.-The-Graph-as-Balls-and-Strings-Levels.png"><img src="/wp-content/uploads/2012/10/3.-The-Graph-as-Balls-and-Strings-Levels.png" alt="The Graph as Balls and Strings Levels" title="The Graph as Balls and Strings Levels" width="620" height="399" class="size-full wp-image-3399" srcset="/wp-content/uploads/2012/10/3.-The-Graph-as-Balls-and-Strings-Levels.png 620w, /wp-content/uploads/2012/10/3.-The-Graph-as-Balls-and-Strings-Levels-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Breadth-first search works much like the image above &#8211; it explores the graph level by level, thus we&#8217;re sure that all the paths are the shortest!</figcaption></figure>
<p>Clearly edges like those between A and B doesn’t matter for our BFS algorithm because they don’t make the path from S to C through B shorter. This is also known as the triangle inequality, where the sum of the lengths of two of the sides of the triangle is always greater than the length of the third side.</p>
<figure id="attachment_3400" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/4.-Triangle-inequality.png"><img src="/wp-content/uploads/2012/10/4.-Triangle-inequality.png" alt="Triangle inequality" title="Triangle inequality" width="620" height="399" class="size-full wp-image-3400" srcset="/wp-content/uploads/2012/10/4.-Triangle-inequality.png 620w, /wp-content/uploads/2012/10/4.-Triangle-inequality-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">What the triangle inequality says us is that if we have a direct edge between two nodes &#8211; that must be the shortest path between them!</figcaption></figure>
<p>We must only answer the question is BFS the best algorithm that finds the shortest path between any two nodes of the graph? This is a reasonable question because as we know by using BFS we don’t find only the shortest path between given vertices i and j, but we also get the shortest paths between i and all other vertices of G. This is an information that we actually don’t need, but can we find the shortest path between i and j without that info?<span id="more-3381"></span></p>
<p>The answer is simply “no”! Practically depth-first search can’t help us. Even worse &#8211; we can find paths that are far not the shortest ones.</p>
<figure id="attachment_3401" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/5.-DFS-and-shortest-path.png"><img src="/wp-content/uploads/2012/10/5.-DFS-and-shortest-path.png" alt="DFS and shortest path" title="DFS and shortest path" width="620" height="399" class="size-full wp-image-3401" srcset="/wp-content/uploads/2012/10/5.-DFS-and-shortest-path.png 620w, /wp-content/uploads/2012/10/5.-DFS-and-shortest-path-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">DFS actually can find the longest path in some cases and can&#8217;t be used for finding shortest path!</figcaption></figure>
<p>In the image above using DFS the distance between 1 and 7 is 7 while practically there is an edge between them.</p>
<p>So BFS is the optimal algorithm for finding shortest paths in a graph. But there’s a catch! This algorithm works fine when we assume that all the edges are the same length. In the examples so far each edge has the value of 1. So N edges between s and i made the distance between them of a length N.</p>
<h2>Overview</h2>
<p>As we know in practice different edges can have different values. Exactly that was the case in weighted graphs. Going back to the road map example the distances between different cities are commonly evaluated in miles or kilometers. Of course we can associate any other meaningful value to this edges. This can be either time in hours to travel between cities, money for fuel or anything else.</p>
<figure id="attachment_3403" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/6.-Weighted-Graphs-in-Practice.png"><img src="/wp-content/uploads/2012/10/6.-Weighted-Graphs-in-Practice.png" alt="Weighted Graphs in Practice" title="Weighted Graphs in Practice" width="620" height="399" class="size-full wp-image-3403" srcset="/wp-content/uploads/2012/10/6.-Weighted-Graphs-in-Practice.png 620w, /wp-content/uploads/2012/10/6.-Weighted-Graphs-in-Practice-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">In practice is more common to use weighted graphs than non-weighted graphs!</figcaption></figure>
<p>Now BFS can’t help us any more. Why? Because using non-equal values for the edges the triangle inequality is no longer true. Now the edge (the direct path) between A and B can be greater than the sum of the two edges (A, C) + (C, B)!</p>
<figure id="attachment_3404" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/7.-Triangle-Inequality-Problem.png"><img src="/wp-content/uploads/2012/10/7.-Triangle-Inequality-Problem.png" alt="Triangle Inequality Problem" title="Triangle Inequality Problem" width="620" height="399" class="size-full wp-image-3404" srcset="/wp-content/uploads/2012/10/7.-Triangle-Inequality-Problem.png 620w, /wp-content/uploads/2012/10/7.-Triangle-Inequality-Problem-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">In a weighted graph the edges aren&#8217;t equal for our BFS algorithm so we can&#8217;t use it!</figcaption></figure>
<p>In other words, assuming the same abstraction with balls and wires the hanging wires can’t be discarded so easily.</p>
<figure id="attachment_3405" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/8.-The-Graph-as-Balls-and-Strings.png"><img src="/wp-content/uploads/2012/10/8.-The-Graph-as-Balls-and-Strings.png" alt="Weighted Graph as Balls and Strings" title="Weighted Graph as Balls and Strings" width="620" height="399" class="size-full wp-image-3405" srcset="/wp-content/uploads/2012/10/8.-The-Graph-as-Balls-and-Strings.png 620w, /wp-content/uploads/2012/10/8.-The-Graph-as-Balls-and-Strings-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">On weighted graphs BFS is no longer useful!</figcaption></figure>
<p>So now how can we solve this problem? A very dummy approach is to break apart each edge with dummy vertices in order to make BFS work again.</p>
<figure id="attachment_3406" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/9.-Breaking-apart-edges.png"><img src="/wp-content/uploads/2012/10/9.-Breaking-apart-edges.png" alt="Breaking apart edges" title="Breaking apart edges" width="620" height="399" class="size-full wp-image-3406" srcset="/wp-content/uploads/2012/10/9.-Breaking-apart-edges.png 620w, /wp-content/uploads/2012/10/9.-Breaking-apart-edges-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Since the graph is weighted we can decompose its edges to more &#8220;dummy&#8221; edges!</figcaption></figure>
<p>However this approach has several weak points. The major one is that we’ll have to keep much more information, which means more memory usage, for even small graphs. This is done in case we break each edge on too many parts.</p>
<p>The solution of this problem was given by <a href="http://en.wikipedia.org/wiki/Edsger_W._Dijkstra" title="Edsger W. Dijkstra" target="_blank">Edsger Dijkstra</a> in 1956 and published in 1959. The only thing we should do now is to be sure that even discarding the triangle inequality we have the shortest paths. The first thing to do is to keep information for the distance from s to the parent (previous) node of i in the graph in order to calculate which distance is shorter.</p>
<p>In BFS we used a queue in order to walk through all the ancestors of a node. This was made consecutively. Thus for the graph G on the next image the order of enqueuing the ancestors of S was A, B, C.</p>
<figure id="attachment_3409" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/10.-Order-of-enqueuing.png"><img src="/wp-content/uploads/2012/10/10.-Order-of-enqueuing.png" alt="Order of enqueuing" title="Order of enqueuing" width="620" height="399" class="size-full wp-image-3409" srcset="/wp-content/uploads/2012/10/10.-Order-of-enqueuing.png 620w, /wp-content/uploads/2012/10/10.-Order-of-enqueuing-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">The order of enqueuing in BFS is consecutive &#8211; something that isn&#8217;t working for weighted graphs!</figcaption></figure>
<p>The Dijkstra’s algorithm make use of a priority queue, also know as a heap. This fact combined by the fact we keep info for the shortest path so far help us find shortest paths in a weighted graphs.</p>
<p>Why this works? To answer this question let’s see the next very basic example, assuming the graph G from the next image. As we can see the triangle inequality isn’t true.</p>
<figure id="attachment_3410" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/11.-Weighted-graph.png"><img src="/wp-content/uploads/2012/10/11.-Weighted-graph.png" alt="Weighted graph" title="Weighted graph" width="620" height="399" class="size-full wp-image-3410" srcset="/wp-content/uploads/2012/10/11.-Weighted-graph.png 620w, /wp-content/uploads/2012/10/11.-Weighted-graph-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">A weighted graph that doesn&#8217;t follow the triangle inequality!</figcaption></figure>
<p>OK, we see that the path [S, B, A] is shorter than [S, A] although the edge (S, A) exists. How the Dijkstra algorithm overcomes this problem.</p>
<p>First we have no information about the distances (S, A) and (S, B), the only thing we know is that S is the starting point, its distance is 0 and its path so far is the empty set. So first we enqueue in a priority the distances from S to A and B.</p>
<figure id="attachment_3411" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/12.-Dijkstra-Priority-Queue.png"><img src="/wp-content/uploads/2012/10/12.-Dijkstra-Priority-Queue.png" alt="Dijkstra Priority Queue" title="Dijkstra Priority Queue" width="620" height="399" class="size-full wp-image-3411" srcset="/wp-content/uploads/2012/10/12.-Dijkstra-Priority-Queue.png 620w, /wp-content/uploads/2012/10/12.-Dijkstra-Priority-Queue-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">The algorithm of Dijkstra make use of a priority queue!</figcaption></figure>
<p>Now we dequeue the minimum (first in the heap) element from the queue &#8211; the closest node to S, which is B. Then all the nodes adjacent to S in the queue are tested for adjacency to B, thus if we have already the distance between S and A now we can test if its longer than (S, B) + (B, A) &#8211; the triangle inequality!</p>
<p>So far we know that we must change a bit BFS to get the Dijkstra algorithm. The only thing to do is to keep info for each node for the path through its parent and to use a priority queue.</p>
<h2>Code</h2>
<p>Implementing this algorithms isn’t much more difficult than BFS, so here’s the code in <a href="/category/php/" title="PHP on Stoimen.com">PHP</a>. However this example make use of the standard php library SPL and the PriorityQueue data structure, but any developer can code <a href="/2012/08/07/computer-algorithms-heap-and-heapsort-data-structure/" title="Computer Algorithms: Heap and Heapsort">his own heap</a>.</p>
<p>Here&#8217;s the graph from the code:</p>
<figure id="attachment_3413" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/0.-Graph.png"><img src="/wp-content/uploads/2012/10/0.-Graph.png" alt="The Graph from the Code" title="The Graph from the Code" width="620" height="399" class="size-full wp-image-3413" srcset="/wp-content/uploads/2012/10/0.-Graph.png 620w, /wp-content/uploads/2012/10/0.-Graph-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">The graph!</figcaption></figure>
<pre lang="PHP">
class vertex
{
    public $key         = null;
    public $visited     = 0;
    public $distance    = 1000000;  // infinite
    public $parent      = null;
    public $path        = null;
    
    public function __construct($key) 
    {
        $this->key  = $key;
    }
}

class PriorityQueue extends SplPriorityQueue
{
    public function compare($a, $b)
    {
        if ($a === $b) return 0;
        return $a > $b ? -1 : 1;
    }
}

$v0 = new vertex(0);
$v1 = new vertex(1);
$v2 = new vertex(2);
$v3 = new vertex(3);
$v4 = new vertex(4);
$v5 = new vertex(5);

$list0 = new SplDoublyLinkedList();
$list0->push(array('vertex' => $v1, 'distance' => 3));
$list0->push(array('vertex' => $v3, 'distance' => 1));
$list0->rewind();

$list1 = new SplDoublyLinkedList();
$list1->push(array('vertex' => $v0, 'distance' => 3));
$list1->push(array('vertex' => $v2, 'distance' => 7));
$list1->rewind();

$list2 = new SplDoublyLinkedList();
$list2->push(array('vertex' => $v1, 'distance' => 7));
$list2->push(array('vertex' => $v3, 'distance' => 8));
$list2->push(array('vertex' => $v4, 'distance' => 12));
$list2->rewind();

$list3 = new SplDoublyLinkedList();
$list3->push(array('vertex' => $v0, 'distance' => 1));
$list3->push(array('vertex' => $v2, 'distance' => 8));
$list3->rewind();

$list4 = new SplDoublyLinkedList();
$list4->push(array('vertex' => $v2, 'distance' => 12));
$list4->push(array('vertex' => $v5, 'distance' => 3));
$list4->rewind();

$list5 = new SplDoublyLinkedList();
$list5->push(array('vertex' => $v4, 'distance' => 3));
$list5->rewind();

$adjacencyList = array(
    $list0,
    $list1,
    $list2,
    $list3,
    $list4,
    $list5,
);

function calcShortestPaths(vertex $start, &$adjLists)
{
    // define an empty queue
    $q = new PriorityQueue();
    
    // push the starting vertex into the queue
    $q->insert($start, 0);
    $q->rewind();
    
    // mark the distance to it 0
    $start->distance = 0;
    
    // the path to the starting vertex
    $start->path = array($start->key);
    
    while ($q->valid()) {
        $t = $q->extract();
        $t->visited = 1;
        
        $l = $adjLists[$t->key];
        while ($l->valid()) {
            $item = $l->current();
            
            if (!$item['vertex']->visited) {
                if ($item['vertex']->distance > $t->distance + $item['distance']) {
                    $item['vertex']->distance = $t->distance + $item['distance'];
                    $item['vertex']->parent = $t;
                }
                
                $item['vertex']->path = array_merge($t->path, array($item['vertex']->key));
                
                $q->insert($item["vertex"], $item["vertex"]->distance);
            }
            $l->next();
        }
        $q->recoverFromCorruption();
        $q->rewind();
    }
}

calcShortestPaths($v0, $adjacencyList);

// The path from node 0 to node 5
// [0, 1, 2, 4, 5]
echo '[' . implode(', ', $v5->path) . ']';
</pre>
<h2>Complexity</h2>
<p>The complexity of that code is based on the complexity of BFS with the main difference that we keep a priority queue. For BFS we knew that the complexity was O(|V| + |E|), while Dijkstra&#8217;s algorithm has running time of O((|V| + |E|).log(|V|)). That is quite natural since the heapsort&#8217;s complexity is O(n.log(n))!</p>
<h2>Application</h2>
<p>Since the basic BFS can&#8217;t help us for weighted graphs and there are plenty of problems designed with weighted graphs obviously Dijkstra&#8217;s algorithm can be very handy. The only thing we should be aware of is the positive values of the edges.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<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/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic 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>
<li><a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Computer Algorithms: Shortest Path in a Graph</title>
		<link>/2012/10/08/computer-algorithms-shortest-path-in-a-graph/</link>
		<comments>/2012/10/08/computer-algorithms-shortest-path-in-a-graph/#respond</comments>
		<pubDate>Mon, 08 Oct 2012 13:39:55 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Breadth-first search]]></category>
		<category><![CDATA[breadth-first search algorithm]]></category>
		<category><![CDATA[breadth-first search will]]></category>
		<category><![CDATA[Distance]]></category>
		<category><![CDATA[Edge disjoint shortest pair algorithm]]></category>
		<category><![CDATA[faster and more efficient algorithm]]></category>
		<category><![CDATA[graph algorithm]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[handy algorithm]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Network theory]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Routing algorithms]]></category>
		<category><![CDATA[search algorithm]]></category>
		<category><![CDATA[search algorithms]]></category>
		<category><![CDATA[shortest path algorithm]]></category>
		<category><![CDATA[Shortest path problem]]></category>
		<category><![CDATA[Theoretical computer science]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[USD]]></category>

		<guid isPermaLink="false">/?p=3369</guid>
		<description><![CDATA[Introduction Since with graphs we can represent real-life problems it’s almost clear why we would need an efficient algorithm that calculates the shortest path between two vertices. Getting back to our example of a road map we can use such an algorithm in order to find the shortest path between two cities. This example, of &#8230; <a href="/2012/10/08/computer-algorithms-shortest-path-in-a-graph/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Shortest Path in a Graph</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<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/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic 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>
<li><a href="/2012/09/24/computer-algorithms-graph-best-first-search/" rel="bookmark" title="Computer Algorithms: Graph Best-First Search">Computer Algorithms: Graph Best-First Search </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Since with graphs we can represent real-life problems it’s almost clear why we would need an efficient algorithm that calculates the shortest path between two vertices. Getting back to our example of a road map we can use such an algorithm in order to find the shortest path between two cities. This example, of course, is very basic indeed, but it can give us a clear example of where shortest path can be applied.</p>
<p>In the other hand, we can model an enormous field of real-life problems using graphs – not only road maps. As we already know, whenever we have relations between different abstract objects we can refer an efficient graph algorithm.</p>
<p>OK, so we need a shortest path algorithm, but before we proceed with the exact algorithm first we’ll need to answer some questions and give some definitions.</p>
<h2>Overview</h2>
<p>First we need a definition of the terms distance and path between two nodes. A path is considered to be the sequence of vertices (or edges if you wish) between two vertices i and j. Of course we assume that there might be no path between any to vertices in the graph! Also we assume that this definition relates both for directed and undirected graphs. After we have the definition of a path we can proceed by defining a “distance”, which is said to be the number of edges in the path between i and j.</p>
<p><figure id="attachment_3391" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/1.-Path-and-Distance.png"><img src="/wp-content/uploads/2012/10/1.-Path-and-Distance.png" alt="Path and Distance" title="Path and Distance" width="620" height="399" class="size-full wp-image-3391" srcset="/wp-content/uploads/2012/10/1.-Path-and-Distance.png 620w, /wp-content/uploads/2012/10/1.-Path-and-Distance-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">First we need to define what&#8217;s a path and a distance between two vertices in order to continue searching for the shortest path!</figcaption></figure><span id="more-3369"></span></p>
<p>Using this terms, if there’s an edge between i and j, the path between them is [i, j], while the distance is 1. Of course, for an undirected graph (i, j) equals to (j, i) and the path [i, j] equals the path [j, i], but that isn’t true for directed graphs where the path [i, j] differs in general from [j, i].</p>
<figure id="attachment_3390" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/2.-Rule-of-the-Triangle.png"><img src="/wp-content/uploads/2012/10/2.-Rule-of-the-Triangle.png" alt="Rule of the Triangle" title="Rule of the Triangle" width="620" height="399" class="size-full wp-image-3390" srcset="/wp-content/uploads/2012/10/2.-Rule-of-the-Triangle.png 620w, /wp-content/uploads/2012/10/2.-Rule-of-the-Triangle-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Although path (shortest path) is applicable for both directed and undirectd graphs, they depend in both cases of the graph type!</figcaption></figure>
<p>Here we talk about the path between two adjacent vertices, but we can go with the more general case of a path between two vertices that aren’t adjacent. </p>
<p>Now, getting back to the road map example, there might be many paths between city A and city B. </p>
<figure id="attachment_3389" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/3.-Paths-Between-Cities.png"><img src="/wp-content/uploads/2012/10/3.-Paths-Between-Cities.png" alt="Paths Between Cities" title="Paths Between Cities" width="620" height="399" class="size-full wp-image-3389" srcset="/wp-content/uploads/2012/10/3.-Paths-Between-Cities.png 620w, /wp-content/uploads/2012/10/3.-Paths-Between-Cities-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">If we talk about paths between cities its pretty natural to talk about more than one &#8220;valid&#8221; path!</figcaption></figure>
<p>What we actually need to find is the shortest one. This can be very important, because we often want to get from A to B as quickly as possible using the shortest path.</p>
<figure id="attachment_3388" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/4.-Shortest-Path-Between-Cities.png"><img src="/wp-content/uploads/2012/10/4.-Shortest-Path-Between-Cities.png" alt="Shortest Path Between Cities" title="Shortest Path Between Cities" width="620" height="399" class="size-full wp-image-3388" srcset="/wp-content/uploads/2012/10/4.-Shortest-Path-Between-Cities.png 620w, /wp-content/uploads/2012/10/4.-Shortest-Path-Between-Cities-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">The shortest path between two vertices is the path with lower distance compared to all other paths between the same points!</figcaption></figure>
<p>So first, what is a shortest path between i and j. Well, besides the strict definition, I’ll give a simplified one that might be clearer. The shortest path between i and j is such a path, which has the lowest distance compared to all other paths between i and j. </p>
<p>In our algorithm we will use breadth-first search. Why? That is because by using BFS by starting at a given point we expand our search consecutively starting with the closest vertices.</p>
<figure id="attachment_3387" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/5.-Shortest-Path-Canvas.png"><img src="/wp-content/uploads/2012/10/5.-Shortest-Path-Canvas.png" alt="BFS: Shortest Path Canvas" title="BFS: Shortest Path Canvas" width="620" height="399" class="size-full wp-image-3387" srcset="/wp-content/uploads/2012/10/5.-Shortest-Path-Canvas.png 620w, /wp-content/uploads/2012/10/5.-Shortest-Path-Canvas-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Breadth-first search can help us find the shortest paths between a given vertex (s) and all other reachable vertices!</figcaption></figure>
<p>Is breadth-first search enough and will it give us the correct answer – the shortest path between i and j. Actually breadth-first search will gives us even more – the shortest paths to each reachable vertex from a given starting point – the staring vertex.</p>
<p>Why this is correct? Well, because of the nature of the breadth-first search algorithm. As we already know BFS uses a queue in order to store the front of the expansion. Usually as an abstraction BFS colors the vertices in white, gray and black, where the white vertices are those that aren’t visited yet, the gray are in the queue and the black vertices are already visited.</p>
<figure id="attachment_3386" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/6.-White-Gray-Black.png"><img src="/wp-content/uploads/2012/10/6.-White-Gray-Black.png" alt="White, Gray, Black" title="White, Gray, Black" width="620" height="399" class="size-full wp-image-3386" srcset="/wp-content/uploads/2012/10/6.-White-Gray-Black.png 620w, /wp-content/uploads/2012/10/6.-White-Gray-Black-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">By putting a color to visited/unvisited and currently inspected vertices we can get a clearer impression on how breadth-first search works!</figcaption></figure>
<p>However how can be sure that BFS will give us the shortest paths to each vertex? To answer this question and to be sure that BFS will work for us we must take a closer look at the queue. Clearly by starting at a given point the algorithm is correct – the distance is 0.</p>
<p>Now the second step is to put into the queue all the vertices adjacent to s (where s is the starting point). Clearly this will give us the shortest paths to all adjacent vertices of s.</p>
<p>Continuing by induction we can assume that at level k we have all the shortest paths from s to all the vertices at the level k. It is clear the path between s and the vertices at level k is k, since we assume that each edge adds 1 to the path from s to i. Now by adding all the vertices adjacent (and not visited yet) to the paths of level k we get paths with length k+1 which is again the shortest paths from s to level k+1. </p>
<figure id="attachment_3385" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/7.-Shortest-Paths.png"><img src="/wp-content/uploads/2012/10/7.-Shortest-Paths.png" alt="Shortest Paths" title="Shortest Paths" width="620" height="399" class="size-full wp-image-3385" srcset="/wp-content/uploads/2012/10/7.-Shortest-Paths.png 620w, /wp-content/uploads/2012/10/7.-Shortest-Paths-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Finding the shortest paths using BFS can be proved by induction!</figcaption></figure>
<p>Actually we can talk about a tree built out of the graph by staring at s (which is the root of the tree).</p>
<figure id="attachment_3384" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/8.-Spanning-tree.png"><img src="/wp-content/uploads/2012/10/8.-Spanning-tree.png" alt="Spanning tree" title="Spanning tree" width="620" height="399" class="size-full wp-image-3384" srcset="/wp-content/uploads/2012/10/8.-Spanning-tree.png 620w, /wp-content/uploads/2012/10/8.-Spanning-tree-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">BFS walks through the graph by constructing a virtual tree!</figcaption></figure>
<h2>Code</h2>
<p>OK, now we know that BFS will find us the shortest paths from s to all the reachable vertices from s. Here’s a simple PHP implementation, that makes use of the Standard PHP Library data structures. Of course, everyone can code and use his own implementation of lists in order to keep the information of the adjacency lists.</p>
<p>The important thing to note is that we keep an additional information in each vertex – the distance between it and s, which is initially infinite. First we go with the modification of BFS in order to find all the distances between s and the other vertices.</p>
<p>Here’s our graph:</p>
<figure id="attachment_3392" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/9.-Graph.png"><img src="/wp-content/uploads/2012/10/9.-Graph.png" alt="Graph" title="Graph" width="620" height="399" class="size-full wp-image-3392" srcset="/wp-content/uploads/2012/10/9.-Graph.png 620w, /wp-content/uploads/2012/10/9.-Graph-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">The graph for the example!</figcaption></figure>
<pre lang="PHP">
class vertex
{
    public $key = null;
    public $color = 'white';
    public $distance = -1;  // infinite
    
    public function __construct($key) 
    {
        $this->key = $key;
    }
}

$v0 = new vertex(0);
$v1 = new vertex(1);
$v2 = new vertex(2);
$v3 = new vertex(3);
$v4 = new vertex(4);
$v5 = new vertex(5);

$list0 = new SplDoublyLinkedList();
$list0->push($v1);
$list0->push($v3);
$list0->rewind();

$list1 = new SplDoublyLinkedList();
$list1->push($v0);
$list1->push($v2);
$list1->rewind();

$list2 = new SplDoublyLinkedList();
$list2->push($v1);
$list2->push($v3);
$list2->push($v4);
$list2->rewind();

$list3 = new SplDoublyLinkedList();
$list3->push($v1);
$list3->push($v2);
$list3->rewind();

$list4 = new SplDoublyLinkedList();
$list4->push($v2);
$list4->push($v5);
$list4->rewind();

$list5 = new SplDoublyLinkedList();
$list5->push($v4);
$list5->rewind();

$adjacencyList = array(
    $list0,
    $list1,
    $list2,
    $list3,
    $list4,
    $list5,
);

function calcDistances(vertex $start, &$adjLists)
{
    // define an empty queue
    $q = array();
    
    // push the starting vertex into the queue
    array_push($q, $start);
    
    // color it gray
    $start->color = 'gray';
    
    // mark the distance to it 0
    $start->distance = 0;
    
    while ($q) {
        // 1. pop from the queue
        $t = array_pop($q);
        
        // 2. foreach poped item find it's adjacent white vertices
        $l = $adjLists[$t->key];
        while ($l->valid()) {
            // 3. mark them gray, increment their length with one from their parent
            if ($l->current()->color == 'white') {
                $l->current()->color = 'gray';
                $l->current()->distance = $t->distance + 1;
                // 4. push them to the queue
                array_push($q, $l->current());
            }
            
            $l->next();
        }
    }
}

calcDistances($v0, $adjacencyList);

print_r($adjacencyList);
</pre>
<p>Now we can modify the algorithm even more and we add the path property of each vertex. Now each vertex will keep the path from s.</p>
<pre lang="PHP">
class vertex
{
    public $key         = null;
    public $color       = 'white';
    public $distance    = -1;  // infinite
    public $path        = null;
    
    public function __construct($key) 
    {
        $this->key  = $key;
    }
}

$v0 = new vertex(0);
$v1 = new vertex(1);
$v2 = new vertex(2);
$v3 = new vertex(3);
$v4 = new vertex(4);
$v5 = new vertex(5);

$list0 = new SplDoublyLinkedList();
$list0->push($v1);
$list0->push($v3);
$list0->rewind();

$list1 = new SplDoublyLinkedList();
$list1->push($v0);
$list1->push($v2);
$list1->rewind();

$list2 = new SplDoublyLinkedList();
$list2->push($v1);
$list2->push($v3);
$list2->push($v4);
$list2->rewind();

$list3 = new SplDoublyLinkedList();
$list3->push($v1);
$list3->push($v2);
$list3->rewind();

$list4 = new SplDoublyLinkedList();
$list4->push($v2);
$list4->push($v5);
$list4->rewind();

$list5 = new SplDoublyLinkedList();
$list5->push($v4);
$list5->rewind();

$adjacencyList = array(
    $list0,
    $list1,
    $list2,
    $list3,
    $list4,
    $list5,
);

function calcShortestPaths(vertex $start, &$adjLists)
{
    // define an empty queue
    $q = array();
    
    // push the starting vertex into the queue
    array_push($q, $start);
    
    // color it gray
    $start->color = 'gray';
    
    // mark the distance to it 0
    $start->distance = 0;
    
    // the path to the starting vertex
    $start->path = new SplDoublyLinkedList();
    $start->path->push($start->key);
    
    while ($q) {
        // 1. pop from the queue
        $t = array_pop($q);
        
        // 2. foreach poped item find it's adjacent white vertices
        $l = $adjLists[$t->key];
        while ($l->valid()) {
            // 3. mark them gray, increment their length with one from their parent
            if ($l->current()->color == 'white') {
                $l->current()->color = 'gray';
                $l->current()->distance = $t->distance + 1;
                $l->current()->path = clone $t->path;
                $l->current()->path->push($l->current()->key);
                
                // 4. push them to the queue
                array_push($q, $l->current());
            }
            
            $l->next();
        }
    }
}

calcShortestPaths($v0, $adjacencyList);

print_r($adjacencyList);
</pre>
<h2>Complexity</h2>
<p>Clearly the complexity of enqueue and dequeue is O(V), while searching for adjacent vertices is O(E), thus the complexity of this algorithm is O(V + E)!</p>
<h2>Application</h2>
<p>Finding the shortest path between two nodes is obviousely a very handy algorithm. Applied almost everywhere graphs exists this algorithm is widely used. However there&#8217;s one very reasonable question. We&#8217;re searching for the shortest path between two vertices and we end with the shortest paths between a starting node an all other vertices? Why we need this &#8220;useless&#8221; information? Acutally the question should be: is there a faster and more efficient algorithm compared to this one. Well, we&#8217;ll see that!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<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/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic 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>
<li><a href="/2012/09/24/computer-algorithms-graph-best-first-search/" rel="bookmark" title="Computer Algorithms: Graph Best-First Search">Computer Algorithms: Graph Best-First Search </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/10/08/computer-algorithms-shortest-path-in-a-graph/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Computer Algorithms: Topological Sort of a Graph</title>
		<link>/2012/10/01/computer-algorithms-topological-sort-of-a-graph/</link>
		<comments>/2012/10/01/computer-algorithms-topological-sort-of-a-graph/#comments</comments>
		<pubDate>Mon, 01 Oct 2012 12:03:58 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[basic graph algorithms]]></category>
		<category><![CDATA[Directed acyclic graph]]></category>
		<category><![CDATA[Graph]]></category>
		<category><![CDATA[Graph coloring]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[L]]></category>
		<category><![CDATA[Longest path problem]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[NP-complete problems]]></category>
		<category><![CDATA[Path decomposition]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[rational solution]]></category>
		<category><![CDATA[Theoretical computer science]]></category>
		<category><![CDATA[Topological sorting]]></category>
		<category><![CDATA[Tree]]></category>

		<guid isPermaLink="false">/?p=3367</guid>
		<description><![CDATA[Introduction Let’s assume we have a list of tasks to accomplish. Some of the tasks depend on others, so we must be very careful with the order of their execution. If the relationship between these tasks were simple enough we could represent them as a linked list, which would be great, and we would know &#8230; <a href="/2012/10/01/computer-algorithms-topological-sort-of-a-graph/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Topological Sort of a Graph</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/12/10/computer-algorithms-topological-sort-revisited/" rel="bookmark" title="Computer Algorithms: Topological Sort Revisited">Computer Algorithms: Topological Sort Revisited </a></li>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </a></li>
<li><a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </a></li>
<li><a href="/2012/09/17/computer-algorithms-graph-depth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Depth-First Search">Computer Algorithms: Graph Depth-First Search </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Let’s assume we have a list of tasks to accomplish. Some of the tasks depend on others, so we must be very careful with the order of their execution. If the relationship between these tasks were simple enough we could represent them as a linked list, which would be great, and we would know the exact order of their execution. The problem is that sometimes the relations between the different tasks are more complex and some tasks depend on two or more other tasks, which in their turn depend on one or more tasks, etc.</p>
<p>Thus we can’t model this problem using linked lists or trees. The only rational solution is to model the problem using a graph. What kind of graph do we need? Well, we definitely need a directed graph, to desribe the relations, and this graph shouldn&#8217;t have cycles. So we need the so called directed acyclic graph (DAG).</p>
<figure id="attachment_3373" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/1.-TS-Directed-Graph.png"><img class="size-full wp-image-3373" title="Topological Sort. Directed Graph." src="/wp-content/uploads/2012/10/1.-TS-Directed-Graph.png" alt="Topological Sort. Directed Graph." width="620" height="399" srcset="/wp-content/uploads/2012/10/1.-TS-Directed-Graph.png 620w, /wp-content/uploads/2012/10/1.-TS-Directed-Graph-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">In order to sort a graph using topological sort we need this graph to be acyclic and directed!</figcaption></figure>
<p>Why we don’t what a cycle in the graph? The answer of this question is simple and obvious. In case of cyclic graph, we wouldn’t be able to determine the priority of task execution, thus we won’t be able to sort the tasks properly.</p>
<p>Now the solution we want is to sort the vertices of the graph in some order so for each edge (u, v) u will precede v. Then we&#8217;ll have a linear order of all tasks and by starting their execution we’ll know that everything will be OK.</p>
<figure id="attachment_3372" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/2.-TS-Sort-the-vertices.png"><img src="/wp-content/uploads/2012/10/2.-TS-Sort-the-vertices.png" alt="Topological Sort. Sort the vertices." title="Topological Sort. Sort the vertices." width="620" height="399" class="size-full wp-image-3372" srcset="/wp-content/uploads/2012/10/2.-TS-Sort-the-vertices.png 620w, /wp-content/uploads/2012/10/2.-TS-Sort-the-vertices-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">The output of topological sort should be a list of vertices!</figcaption></figure>
<p>This kind of sort is also known as “topological” sort (or topsort) and it is one of the very basic graph algorithms.<span id="more-3367"></span></p>
<h2>Overview</h2>
<p>OK, so we have an acyclic directed graph, how do we proceed to get a linked list with all the vertices sorted? Since it’s an acyclic graph we know that there is at least one vertex without predecessor. Thus at first place, we can put all the vertices without predecessors into our linked list.</p>
<figure id="attachment_3371" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/3.-TS-First-Step.png"><img src="/wp-content/uploads/2012/10/3.-TS-First-Step.png" alt="Topological Sort. First Step." title="Topological Sort. First Step." width="620" height="399" class="size-full wp-image-3371" srcset="/wp-content/uploads/2012/10/3.-TS-First-Step.png 620w, /wp-content/uploads/2012/10/3.-TS-First-Step-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Initially we get only the vertices without a predecessor!</figcaption></figure>
<p>This approach answers the question – is there a possibility to have more than one valid topological sort of a graph? Indeed, the only thing we’d like to do is to put all the vertices in the correct order, but since there might be vertices with no predecessors any combination of them will be a valid topological sort for a graph.</p>
<p>As we can see from the picture above even for vertices with predecessors our topological sort can vary. Thus [9, 6, 2, 7, 4, 1] is a valid topological sorted graph, but [6, 9, 2, 7, 4, 1] is also a valid topological sort out of the same graph!</p>
<p>Now we can generalize the algorithm in some basic steps.</p>
<p>1. Make an empty list L and an empty list S;<br />
2. Put all the vertices with no predecessors in L;<br />
3. While L has items in it;<br />
    3.1. Pop an item from L – n, and push it to S;<br />
    3.2. For each vertex m adjacent to n;<br />
         3.2.1. Remove (n, m);<br />
	 3.2.2. If m has no predecessors – push it to L;</p>
<figure id="attachment_3370" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/10/4.-TS-Second-Step.png"><img src="/wp-content/uploads/2012/10/4.-TS-Second-Step.png" alt="Topological Sort. Second Step." title="Topological Sort. Second Step." width="620" height="399" class="size-full wp-image-3370" srcset="/wp-content/uploads/2012/10/4.-TS-Second-Step.png 620w, /wp-content/uploads/2012/10/4.-TS-Second-Step-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">The image above explains step 3.2. from the algorithm!</figcaption></figure>
<h2>Code</h2>
<p>Here’s the very basic <a href="/category/php/" title="PHP on stoimen.com">PHP</a> implementation. As you can see the short implementation shows us how easy this algorithm is. However its importance to computer science and programming is enormous.</p>
<pre lang="PHP">
class G
{
    protected $_g = array(
        array(0, 1, 1, 0, 0, 0, 0),
        array(0, 0, 0, 1, 0, 0, 0),
        array(0, 0, 0, 0, 1, 0, 0),
        array(0, 0, 0, 0, 1, 0, 0),
        array(0, 0, 0, 0, 0, 0, 1),
        array(0, 0, 0, 0, 0, 0, 1),
        array(0, 0, 0, 0, 0, 0, 0),
    );
    protected $_list = array();
    protected $_ts   = array();
    protected $_len  = null;
    
    public function __construct()
    {
        $this->_len = count($this->_g);
        
        // finds the vertices with no predecessors
        $sum = 0;
        for ($i = 0; $i < $this->_len; $i++) {
            for ($j = 0; $j < $this->_len; $j++) {
                $sum += $this->_g[$j][$i];
            }
            
            if (!$sum) {
                // append to list
                array_push($this->_list, $i);
            }
            $sum = 0;
        }
    }
    
    public function topologicalSort() 
    {
        while ($this->_list) {
            $t = array_shift($this->_list);
            array_push($this->_ts, $t);
            
            foreach ($this->_g[$t] as $key => $vertex) {
                if ($vertex == 1) {
                    $this->_g[$t][$key] = 0;
                    
                    $sum = 0;
                    for ($i = 0; $i < $this->_len; $i++) {
                        $sum += $this->_g[$i][$key];
                    }
                    
                    if (!$sum) {
                        array_push($this->_list, $key);
                    }
                }
                $sum = 0;
            }
        }
        
        print_r($this->_ts);
    }
}

$g = new G();
/*
Array
(
    [0] => 0
    [1] => 5
    [2] => 1
    [3] => 2
    [4] => 3
    [5] => 4
    [6] => 6
)*/
$g->topologicalSort();
</pre>
<h2>Application</h2>
<p>As I already mentioned above this algorithm is practically used to sort the execution of different tasks that depend on each other. However this isn’t its only use. Actually any kind of objects that depend on each other can be modeled with a graph. Indeed sometimes these graphs may be a trees, but most of the cases that isn’t true.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/12/10/computer-algorithms-topological-sort-revisited/" rel="bookmark" title="Computer Algorithms: Topological Sort Revisited">Computer Algorithms: Topological Sort Revisited </a></li>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </a></li>
<li><a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </a></li>
<li><a href="/2012/09/17/computer-algorithms-graph-depth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Depth-First Search">Computer Algorithms: Graph Depth-First Search </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/10/01/computer-algorithms-topological-sort-of-a-graph/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
