<?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>Ford Motor Company &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/ford-motor-company/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: 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>
	</channel>
</rss>
