{"id":632,"date":"2018-07-19T11:56:23","date_gmt":"2018-07-19T11:56:23","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=632"},"modified":"2018-12-21T10:48:39","modified_gmt":"2018-12-21T10:48:39","slug":"shortest-path-algorithm-i","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/shortest-path-algorithm-i\/","title":{"rendered":"Shortest Path Algorithm &#8211; I"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/y5fyMYT9i64\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we will discuss about Shortest Path Algorithm.<\/p>\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe learning objectives of the module are as follows:\r\n\r\n&nbsp;\r\n\r\n\u2022 To understand the Shortest Path problem and its variants\r\n\r\n\u2022 To discuss the properties of Shortest path\r\n\r\n\u2022 To explain the Dijkstra\u2019s Algorithm\r\n\r\n\u2022 To illustrate the Dijkstra\u2019s Algorithm with an example\r\n\r\n&nbsp;\r\n\r\n<strong>39.1 Recap<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Topological sort is a method of arranging the vertices in a directed acyclic graph (DAG), as a sequence, such that no vertex appear in the sequence before its predecessor. The properties of the Topological sort and the step -by-step description had been discussed with an example.<\/p>\r\n&nbsp;\r\n\r\n<strong>39.2 Shortest Path Problem<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this module we discuss the shortest path algorithm. S hortest Path Problem is usually associated with a weighted graph that has two vertices <em>u<\/em> and <em>v<\/em>, and we want to find a path of minimum total weight between <em>u<\/em> and <em>v.<\/em> Length of a path is the sum of the weights of its edges.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Example: Shortest path between Bangalore and Madurai. An example of a graph for shortest path is given in Figure 39.1. Applications of shortest path problem include Internet packet routing, Flight reservations and Driving directions.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-635 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-433.png\" alt=\"\" width=\"578\" height=\"202\" \/>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">The shortest path algorithm is associated with a directed weighted graph and the path length is sum of weights of the edges on the path. The source vertex is the place where the path begins and the destination vertex is the vertex where the path ends. Once the source and destination are finalized the vertices in the increasing order is reported from the source vertex. We construct the shortest path edge by edge; at each step adding one new edge, corresponding to construction of shortest path to the current new vertex.<\/p>\r\n&nbsp;\r\n\r\n<strong>39.2.1 Shortest Path Notations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For each vertex v \u00ce V where V is the set of vertices. \u03b4(s, v) is shortest-path weight and d[v] is the shortest-path weight estimate. Initially, d[v]=\u00a5 and d[v]\u00e0\u03b4(s,v) as algorithm progresses. p[v] = <strong>predecessor<\/strong> of v on a shortest path from s and if no predecessor exists, p[v] = NIL. p induces a tree called <strong>the shortest-path tree as shown in Figure<\/strong> <strong>39.2.<\/strong><\/p>\r\n<img class=\"size-full wp-image-636 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-434.png\" alt=\"\" width=\"361\" height=\"250\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>39.3 Shortest Path Algorithm<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The input to the algorithm is the Directed graph G = (V, E), with V being the set of vertices and E being the edges. A weight function w : E \u2192 R is associated with each edge of the graph. A path is denoted by a sequence of vertices. The weight of path p = \u00e1v0, v1, . . . , vk\u00f1, is the weight of the edges between the vertices in the path. The Shortest-path weight from u to v: d(u, v) = min w(p) : if there exists a path p or p from u to v \u00a5 :otherwise. Note that there might be multiple shortest paths from u to v. There may be many types of shortest path algorithms.<\/p>\r\n&nbsp;\r\n\r\n<strong>39.3.1 Initialization<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE. Initially for every vertex v in G, d[v] the shortest path estimate is set to \u00a5 and the p[v], the predecessor of every vertex v is set to Nil. d[s], the shortest path estimate of source node is set to 0.<\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-637\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-435.png\" alt=\"\" width=\"308\" height=\"102\" \/>\r\n<div>\r\n\r\n\u00a0 \u00a0d[s] \u2190 0\r\n\r\n&nbsp;\r\n\r\n<strong>39.2.2<\/strong>\u00a0 <strong>Shortest Path Tree<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For every node <em>v<\/em> <em>\u00ce<\/em> <em>V, \u03c0[v]<\/em> is the predecessor of <em>v<\/em> in shortest path from source <em>s<\/em> to <em>v.<\/em> This value is set to Nil if does not exist. All our algorithms will output a shortest-path tree whose root is the source s and the edges are <em>(\u03c0[v],v).<\/em> The shortest path between <em>s<\/em> and <em>v<\/em> is the unique tree path from root s to <em>v<\/em>. Consider Source node 1 and destination node 7, a direct path between the nodes will cost 14, however a shorter path through two other nodes costs only 11 (Figure 39.3).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-638 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-436.png\" alt=\"\" width=\"669\" height=\"230\" \/>\r\n\r\n<strong>39.3 Shortest Path Variants<\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are many variants of the shortest path algorithms. They are:\r\n\r\n&nbsp;\r\n\r\n<strong>Single-source single-destination (1-1): <\/strong>Find the shortest path from source <em>s<\/em> to destination <em>v<\/em>.\r\n\r\n&nbsp;\r\n\r\n<strong>Single-source all-destination(1-Many): <\/strong>Find the shortest path from <em>s<\/em> to each vertex <em>v<\/em>.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Single-destination shortest-paths (Many-1): <\/strong>Find a shortest path to a given <em>destination <\/em>vertex<em> t <\/em>from each vertex<em> v<\/em>.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>All-pairs shortest-paths problem (Many-Many): <\/strong>Find a shortest path from <em>u<\/em> to <em>v<\/em> for every pair of vertices <em>u<\/em> and <em>v<\/em>.<\/p>\r\n&nbsp;\r\n\r\n<strong>Single source\/All destinations <\/strong>-nonnegative edge cost\r\n\r\n&nbsp;\r\n\r\n<strong>Need to solve: <\/strong>determine a shortest path from <em>v<\/em> to each of the remaining vertices of <em>G<\/em>.\r\n\r\n&nbsp;\r\n\r\n<strong>39.3.1 Single-destination shortest paths<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Among the shortest path algorithms given above the single destination shortest paths is about finding a shortest path to a given destination vertex <strong>t<\/strong> from each vertex v. Here we reverse the direction of each edge and finding the distances between every pair of vertices in a weighted directed graph G. Figure 39.4 shows the example of the <strong>Single source\/All destinations <\/strong>-nonnegative edge cost where we need to determine a shortest path from <em>v<\/em> to each of the remaining vertices of <em>G<\/em>.<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-639 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-437.png\" alt=\"\" width=\"374\" height=\"393\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>39.3.2 Shortest Path Properties<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">If some path from s to v contains a negative cost cycle, there does not exist a shortest path. Otherwise, there exists a shortest s-v that is simple. A negative cycle can produce arbitrarily long negative paths by traversing cycle a number of times. The negative-weight edges may form negative-weight cycle. If such cycles are reachable from the source, then \u03b4(s, v) the shortest-path weight is not properly defined .<\/p>\r\n&nbsp;\r\n\r\n<strong>39.4 Relaxation Process<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For each vertex v, we maintain an upper bound d[v] on the weight of shortest path from s to v and d[v] initialized to infinity. <strong>Relaxing<\/strong> an edge (u, v) = testing whether we can improve the shortest path to v found so far by going through u. d[v] is the weight of the path to vertex v, d[u] is the weight of the path to verte x u, w[u,v] is weight of edge [u,v] and p[v] indicates the predecessor of v in the shortest path to it (Figure 39.5).<\/p>\r\n<p style=\"text-align: justify\">If d[v] &gt; d[u] + w(u, v) and we can improve the shortest path to v . The updated d[v] =&gt; d[v]=d[u]+w(u,v) and p[v] is updated as =&gt; p[v] \u2190 u<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-640 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-438.png\" alt=\"\" width=\"329\" height=\"268\" \/>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">Relaxing an edge (<\/span><em style=\"text-align: justify;font-size: 1em\">u<\/em><span style=\"text-align: justify;font-size: 1em\">, <\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">) consists of testing whether we can improve the shortest path to <\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\"> found so far by going through <\/span><em style=\"text-align: justify;font-size: 1em\">u<\/em><span style=\"text-align: justify;font-size: 1em\">, then updating <\/span><em style=\"text-align: justify;font-size: 1em\">d<\/em><span style=\"text-align: justify;font-size: 1em\">[<\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">] and p[<\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">] accordingly. A relaxation step <\/span><strong style=\"text-align: justify;font-size: 1em\">may <\/strong><span style=\"text-align: justify;font-size: 1em\">either<\/span><strong style=\"text-align: justify;font-size: 1em\"> decrease <\/strong><span style=\"text-align: justify;font-size: 1em\">the value of the shortest-path estimate <\/span><em style=\"text-align: justify;font-size: 1em\">d<\/em><span style=\"text-align: justify;font-size: 1em\">[<\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">] and update <\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">\u2019s predecessor p[<\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">], or cause <\/span><strong style=\"text-align: justify;font-size: 1em\">no change<\/strong><span style=\"text-align: justify;font-size: 1em\">.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>39.5 Shortest Path Properties<\/strong>\r\n\r\n&nbsp;\r\n\r\nNow let us define some properties of shortest paths.\r\n\r\n&nbsp;\r\n\r\n<strong>39.5.1 Triangle Inequality<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For any edge (u, v), we have (s; v) (s; u)+w(u; v). The weight of the shortest path from s to v is no greater than the weight of the shortest path from s to u plus the weight of the edge from u to v. For all (u, v) \u00ce E, we have:<\/p>\r\n&nbsp;\r\n\r\n\u03b4 (s, v) \u2264 \u03b4 (s, u) + w (u, v)\r\n\r\n&nbsp;\r\n\r\nIf u is on the shortest path to v we have the equality sign (Figure 39.6).\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-641 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-439.png\" alt=\"\" width=\"268\" height=\"199\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>39.5.2 Optimal substructure property<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">All sub-paths of shortest paths are also shortest paths. The shortest path problem has the following optimal substructure property. If a node x lies in the shortest path from a source node u to destination node v then the shortest path from u to v is combination of shortest path from u to x and shortest path from x to v.<\/p>\r\n&nbsp;\r\n\r\n<strong>39.5.3 Upper-bound property<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe always have d[v] \u2265 \u03b4 (s, v) for all v. The estimate never rises since the relaxation process is so defined that it only lowers the estimate (Figure 39.7).\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-642 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-440.png\" alt=\"\" width=\"626\" height=\"258\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0<\/strong><strong style=\"text-align: initial;font-size: 1em\">39.5.4 Convergence property<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\nIf s\u00a0 then u \u2192 v is a shortest path, and if d[u] = \u03b4(s, u) at any time prior to relaxing edge (u, v), d[v] = \u03b4(s, v) at all times after relaxing (u, v).\r\n\r\n<\/div>\r\n<div>\r\n\r\nAS shown in Figure 39.8, If d[v] &gt; \u03b4(s, v) <em>\u00de<\/em> after relaxation:\r\n\r\n<\/div>\r\n<div>\r\n\r\nd[v] = d[u] + w(u, v) and\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 d[v] = 5 + 2 = 7.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Otherwise, the value remains unchanged, because it must have already been the shortest path value.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-643 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-441.png\" alt=\"\" width=\"298\" height=\"247\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>39.5.5 Path relaxation property<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let p = \u00e1v0, v1, . . . , vk\u00f1 be a shortest path from s = v0 to vk. If we relax, in order, (v0, v1), (v1, v2), . . . , (vk-1, vk), even intermixed with other relaxations, then d[vk ] = \u03b4 (s, vk).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-644 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-442.png\" alt=\"\" width=\"526\" height=\"204\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong> 39.5.6 Predecessor-subgraph property<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-645\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-443.png\" alt=\"\" width=\"708\" height=\"191\" \/>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">39.6 Applications<\/strong>\r\n<div>\r\n<p style=\"text-align: justify\">Some of the general types of applications of shortest path algorithms include minimization of the total <em>distance<\/em> traveled, minimization of the total <em>cost<\/em> of a sequence of activities, minimization of the total <em>time<\/em> of a sequence of activities. Application areas include network optimization, packet routing, image segmentation, computer-assisted surgery, computer games, DNA analysis, injection molding, operator scheduling , production planning, re-allocation of resources, approximation of piecewise linear functions and VLSI physical design. As discussed the shortest path algorithms find applications in many diverse areas.<\/p>\r\n&nbsp;\r\n\r\n<strong>39.7\u00a0 Relaxation Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">All algorithms in this module call INITIALIZE-SINGLE-SOURCE and then repeatedly relax edges. Relaxation is the only means by which shortest-path estimates and predecessors change. The shortest path algorithms differ in <strong>how many times<\/strong> they relax each edge and <strong>the order in which they relax edges<\/strong>. Bellman-Ford algorithm relaxes each edge many times, while Dijkstra\u2019s algorithm for directed acyclic graphs relax each edge exactly once.<\/p>\r\n&nbsp;\r\n\r\n<strong>39.8\u00a0 Algorithms<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Dijkstra\u2019s algorithm and Bellman-Ford algorithm both use the common operations of Initialization and Relaxation. In Dijkstra\u2019s algorithm negative weights are not allowed. In the Bellman-Ford algorithm negative weights are allowed but negative cycles reachable from the source are not allowed.<\/p>\r\n&nbsp;\r\n\r\n<strong>39.9\u00a0 Dijkstra\u2019s Algorithm<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The steps and assumptions of the Dijkstra\u2019s algorithm are as follows:<\/p>\r\n&nbsp;\r\n<ol>\r\n \t<li>The distance of a vertex <strong><em>v<\/em><\/strong> from a vertex <strong><em>s<\/em><\/strong> is the length of a shortest path between <strong><em>s<\/em><\/strong> and <strong><em>v.<\/em><\/strong><\/li>\r\n \t<li>Dijkstra\u2019s algorithm computes the distances of all the vertices from a given start vertex <strong style=\"text-align: initial;font-size: 1em\"><em>s.<\/em><\/strong><\/li>\r\n \t<li>Assumptions are made that<\/li>\r\n<\/ol>\r\n<ul>\r\n \t<li>the graph is connected<\/li>\r\n \t<li>the edges are undirected.<\/li>\r\n \t<li>The edge weights are nonnegative.<\/li>\r\n<\/ul>\r\n4. Grow a \u201c<strong>cloud<\/strong>\u201d of vertices, beginning with <strong><em>s<\/em><\/strong> eventually covering all the vertices.\r\n\r\n5. We store with each vertex <strong><em>v<\/em><\/strong> a label <strong><em>d<\/em><\/strong>(<strong><em>v<\/em><\/strong>) representing the distance of <strong><em>v<\/em><\/strong> from <strong><em>s<\/em><\/strong> in the subgraph consisting of the cloud and its adjacent vertices.\r\n\r\n6.\u00a0 At each step,\r\n<ul>\r\n \t<li style=\"text-align: justify\">we add to the cloud the vertex <strong><em>u<\/em><\/strong> outside the cloud with the smallest distance label, <strong><em>d<\/em><\/strong>(<strong><em>u<\/em><\/strong>) and<\/li>\r\n \t<li>we update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>39.9.1 Edge Relaxation<\/strong>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Now let us discuss the edge relaxation procedure in detail. Consider an edge <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>e =<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> (<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>u,z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) such that, <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>u<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> is the vertex most recently added to the cloud and <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> is not in the cloud. The relaxation of edge <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>e<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> updates distance <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>d<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) as follows: <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>d<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) \u00ac min{<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>d<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">)<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>,d<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>u<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) +\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>weight<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>e<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">)}. In other words we consider the edge from u to z that is the minimum (Figure 39.11).<\/span><\/p>\r\n\r\n<div>\r\n\r\n<img class=\"size-full wp-image-646 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-444.png\" alt=\"\" width=\"311\" height=\"298\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us consider the example given in Figure 39.12 (i) and (ii) adopted from webhome.csc.uvic.ca\/~ruskey\/classes\/326\/slides\/Chpt7ShortestPath.ppt<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">We start with node A and d[A] at this point is 0 Figure 39.12 (i)-a. Now we add to the cloud the vertex <strong><em>u<\/em><\/strong> outside the cloud with the smallest distance label, <strong><em>d<\/em><\/strong>(<strong><em>u<\/em><\/strong>) which here is vertex C and d[C] =2 (A-C). We then update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>, here note that label of vertex D is updated from 4 to 3 (A-C-D), and label of vertex F (A-C-F) is updated to 11. Note that the path to B from A through C (C-B) and path to D from A (A-D) are not the shortest and are marked (in dotted lines) shown in Figure 39.12 (i)-b.<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-647 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-445.png\" alt=\"\" width=\"579\" height=\"432\" \/>\r\n\r\n&nbsp;\r\n<div>\r\n<p style=\"text-align: justify\">Now we add to the cloud the vertex <strong><em>D<\/em><\/strong> outside the cloud with the smallest distance label, d[D] =3 (A-C-D). We then update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>, here note that label of vertex F is updated from 11 to 8 (A-C-D-F), and the path that is not the shortest (C-F) is marked (in dotted lines) shown in Figure 39.12 (i)-c.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we add to the cloud the vertex <strong><em>E<\/em><\/strong> outside the cloud with the smallest distance label, d[E] =5 (A-C-E). We then update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>, here note that label of vertex B is updated from 8 to 7 (A-C-E-B), and the path that is not the shortest (C-F) is marked (in dotted lines) shown in Figure 39.12 (i)-d.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we add to the cloud the vertex <strong><em>B<\/em><\/strong> outside the cloud with the smallest distance label, d[B] =7(A-C-E-B). We then update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>, here no updation occurs (Figure 39.12 (i)-e).<\/p>\r\n&nbsp;\r\n\r\nFinally we add the node F to the cloud. Again no updation occurs (Figure 39.12 (i)-f).\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-648 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-446.png\" alt=\"\" width=\"497\" height=\"341\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>39.9.2 Dijkstra\u2019s Algorithm<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A priority queue stores the vertices outside the cloud where Key is distance for the element which is the vertex. Locator-based methods are used to insert - <strong><em>insert<\/em><\/strong>(<strong><em>k,e<\/em><\/strong>) which returns a locator and <strong><em>replaceKey<\/em><\/strong>(<strong><em>l,k<\/em><\/strong>) which changes the key of an item. We store two labels with each vertex, Distance (d(v) label) and locator in the priority queue.<\/p>\r\n&nbsp;\r\n\r\nDijkstra\u2019s algorithm assumes that <em>w(e)<\/em><em>\u00b3<\/em><em>0<\/em> for each e in the graph.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We maintain a set <em>S<\/em> of vertices such that, every vertex <em>v<\/em> <em>\u00ce<\/em><em>S, d[v]=<\/em><em>d<\/em><em>(s, v),<\/em> i.e., the shortest-path from s to v has been found. (Initial values: S=empty, d[s]=0 and d[v]=\u00b5)<\/p>\r\n&nbsp;\r\n\r\n(a)\u00a0\u00a0 select the vertex <em>u<\/em><em>\u00ce<\/em><em>V-S<\/em> such that\r\n\r\n<em>d[u]=min {d[x]|x <\/em><em>\u00ce<\/em><em>V-S}. <\/em>Set S=S\u00c8{u}\r\n\r\n&nbsp;\r\n\r\n(b)\u00a0 <strong><em>for <\/em><\/strong>each node <em>v <\/em>adjacent to <em>u<strong> do <\/strong><\/em>RELAX(u, v, w).\r\n\r\nRepeat step the above steps until S=V.\r\n\r\n&nbsp;\r\n\r\nThe pseudo code for the algorithm is shown below:\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 \u00a0dist[s] \u21900\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (distance to source vertex is zero)\r\n\r\nfor all v \u2208 V\u2013{s}\r\n\r\ndo dist[v] \u2190\u00a5\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0(set all other distances to infinity)\r\n\r\nS\u2190\u00d8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (S, the set of visited vertices is initially empty)\u00a0 \u00a0 \\ Q\u2190V\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0(Q, the queue initially contains all vertices)\r\n\r\nwhile Q \u2260\u00d8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (while the queue is not empty)\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">do\u00a0\u00a0 u \u2190 mindistance(Q,dist)\u00a0\u00a0\u00a0 (select the element of Q with the min. distance)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">S\u2190S\u222a{u}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (add u to list of visited vertices)<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0for all v \u2208 neighbors[u]\r\n\r\ndo\u00a0 if\u00a0\u00a0 dist[v] &gt; dist[u] + w(u, v)\u00a0\u00a0\u00a0 (if new shortest path found)\r\n\r\nthen\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 d[v] \u2190d[u] + w(u, v) (set new value of shortest path)\r\n\r\nreturn dist\r\n\r\n&nbsp;\r\n\r\n<strong>39.9.3 Features of Dijkstra\u2019s Algorithm<\/strong>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">The Dijkstra\u2019s algorithm is a <\/span><em style=\"text-align: justify;font-size: 1em\">greedy<\/em><span style=\"text-align: justify;font-size: 1em\"> algorithm. It \u201cVisits\u201d every vertex only once, when it becomes the vertex with minimal distance amongst those still in the priority queue. However distances may be revised <\/span><strong style=\"text-align: justify;font-size: 1em\">multiple times<\/strong><span style=\"text-align: justify;font-size: 1em\"> since the current values represent only the \u2018best guess\u2019 based on our observations so far. Once a vertex is visited we are guaranteed to have found the shortest path to that vertex<\/span>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>39.9.4 Analysis of the Algorithm<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Graph operation is the method incidentEdges which is called once for each vertex. The Label operations are the ones where we set\/get the distance and locator labels of vertex <strong><em>z<\/em><\/strong> that is of the order <strong><em>O<\/em><\/strong>(deg(<strong><em>z<\/em><\/strong>)) times and setting\/getting a label takes <strong><em>O<\/em><\/strong>(1) time. The Priority queue operations include insertion where each vertex is inserted once into the priority queue and removed once from the priority queue, where each insertion or removal takes <strong><em>O<\/em><\/strong>(log <strong><em>n<\/em><\/strong>) time. The key of a vertex in the priority queue is modified at most deg(<strong><em>w<\/em><\/strong>) times, where each key change takes <strong><em>O<\/em><\/strong>(log <strong><em>n<\/em><\/strong>) time .<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Thus the Dijkstra\u2019s algorithm runs in <strong><em>O<\/em><\/strong>((<strong><em>n<\/em><\/strong> + <strong><em>m<\/em><\/strong>) log <strong><em>n<\/em><\/strong>) time provided the graph is represented by the adjacency list structure - Recall that <strong>S<\/strong><strong><em>v<\/em><\/strong> deg(<strong><em>v<\/em><\/strong>) = 2<strong><em>m.<\/em><\/strong> The running time can also be expressed as <strong><em>O<\/em><\/strong>(<strong><em>m<\/em><\/strong> log <strong><em>n<\/em><\/strong>) since the graph is connected.<\/p>\r\n&nbsp;\r\n\r\n<strong>39.10 Dijkstra\u2019s Algorithm \u2013 Walkthrough<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us use the graph in Figure 39.13 (a) to find the shortest path using Dijkstra\u2019s algorithm. .<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-649 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-447.png\" alt=\"\" width=\"512\" height=\"215\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nExample has been adopted from\r\n\r\n&nbsp;\r\n\r\n<a href=\"https:\/\/courses.cs.washington.edu\/courses\/cse326\/03wi\/...\/lecture21b.ppt\">https:\/\/courses.cs.washington.edu\/courses\/cse326\/03wi\/...\/lecture21b.ppt.<\/a>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Initially the distance to all vertices except the source vertex is initialized to \u00a5. Let us assume that C is the source vertex d[C] =0 and C is added to the cloud . Now we update labels of neighbors of C \u2013 here A is updated to 9 and E to 8. Since distance to E is the shortest from C that will be the next node to be considered (Figure 39.13 (b)).<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-650 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-448.png\" alt=\"\" width=\"428\" height=\"326\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now vertex E is added to the cloud and we update labels of neighbors of E \u2013 here D is updated to 15 (C-E-D that is 8+7=15) and G to 9 (C-E-G that is 8+1=9). Since distance to A is the shortest from C that will be the next node to be considered (Figure 39.13 (c)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-651 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-449.png\" alt=\"\" width=\"556\" height=\"294\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now vertex A is added to the cloud and we update labels of neighbors of A \u2013 here B is updated to 11 (C-A-B that is 9+2=11) and D\u2019s label is revised from 15 to 13 through new path (C-A-D that is 9+4=13). Since distance to G is the shortest from C that will be the next node to be considered (Figure 39.13 (d)).<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-652 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-450.png\" alt=\"\" width=\"519\" height=\"317\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now vertex G is added to the cloud and we update labels of neighbors of G \u2013 here F is updated to 11 (C-E-G-F that is 8+1+2=11). Since distance to B is one of the shortest from C that will be the next node to be considered (Figure 39.13 (e)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-653 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-451.png\" alt=\"\" width=\"515\" height=\"383\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now vertex B is added to the cloud but no updating takes place. Since distance to F is the shortest from C that will be the next node to be considered (Figure 39.13 (f)).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now vertex F is added to the cloud and we update labels of neighbors of F \u2013 here H is updated to 14 (C-E-G-F-H that is 8+1+2+3=14). Since distance to D is the shortest from C that will be the next node to be considered (Figure 39.13 (g)).<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-654 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-452.png\" alt=\"\" width=\"548\" height=\"569\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now vertex D is added to the cloud but no updating takes place. Since distance to H is the shortest from C that will be the next node to be considered (Figure 39.13 (h)).<\/p>\r\n<img class=\"size-full wp-image-655 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-453.png\" alt=\"\" width=\"556\" height=\"290\" \/>\r\n<p style=\"text-align: justify\">Now vertex H is added to the cloud and since all the vertices of the graph have been visited the algorithm completes (Figure 39.13 (i)).<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-656 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-454.png\" alt=\"\" width=\"559\" height=\"380\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Explained the Shortest Path problem and its variants<\/li>\r\n \t<li>Discussed the properties of Shortest path<\/li>\r\n \t<li>Outlined the Dijkstra\u2019s Algorithm<\/li>\r\n \t<li>Illustrated the Dijkstra\u2019s Algorithm with an example<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Shortest Path Algorithm \u2013 I<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/y5fyMYT9i64\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<img class=\"size-full wp-image-657 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-455.png\" alt=\"\" width=\"648\" height=\"528\" \/>\r\n\r\n&nbsp;","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/y5fyMYT9i64\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we will discuss about Shortest Path Algorithm.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The learning objectives of the module are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 To understand the Shortest Path problem and its variants<\/p>\n<p>\u2022 To discuss the properties of Shortest path<\/p>\n<p>\u2022 To explain the Dijkstra\u2019s Algorithm<\/p>\n<p>\u2022 To illustrate the Dijkstra\u2019s Algorithm with an example<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.1 Recap<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Topological sort is a method of arranging the vertices in a directed acyclic graph (DAG), as a sequence, such that no vertex appear in the sequence before its predecessor. The properties of the Topological sort and the step -by-step description had been discussed with an example.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.2 Shortest Path Problem<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this module we discuss the shortest path algorithm. S hortest Path Problem is usually associated with a weighted graph that has two vertices <em>u<\/em> and <em>v<\/em>, and we want to find a path of minimum total weight between <em>u<\/em> and <em>v.<\/em> Length of a path is the sum of the weights of its edges.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Example: Shortest path between Bangalore and Madurai. An example of a graph for shortest path is given in Figure 39.1. Applications of shortest path problem include Internet packet routing, Flight reservations and Driving directions.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-635 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-433.png\" alt=\"\" width=\"578\" height=\"202\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-433.png 578w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-433-300x105.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-433-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-433-225x79.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-433-350x122.png 350w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">The shortest path algorithm is associated with a directed weighted graph and the path length is sum of weights of the edges on the path. The source vertex is the place where the path begins and the destination vertex is the vertex where the path ends. Once the source and destination are finalized the vertices in the increasing order is reported from the source vertex. We construct the shortest path edge by edge; at each step adding one new edge, corresponding to construction of shortest path to the current new vertex.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.2.1 Shortest Path Notations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For each vertex v \u00ce V where V is the set of vertices. \u03b4(s, v) is shortest-path weight and d[v] is the shortest-path weight estimate. Initially, d[v]=\u00a5 and d[v]\u00e0\u03b4(s,v) as algorithm progresses. p[v] = <strong>predecessor<\/strong> of v on a shortest path from s and if no predecessor exists, p[v] = NIL. p induces a tree called <strong>the shortest-path tree as shown in Figure<\/strong> <strong>39.2.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-636 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-434.png\" alt=\"\" width=\"361\" height=\"250\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-434.png 361w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-434-300x208.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-434-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-434-225x156.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-434-350x242.png 350w\" sizes=\"auto, (max-width: 361px) 100vw, 361px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.3 Shortest Path Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The input to the algorithm is the Directed graph G = (V, E), with V being the set of vertices and E being the edges. A weight function w : E \u2192 R is associated with each edge of the graph. A path is denoted by a sequence of vertices. The weight of path p = \u00e1v0, v1, . . . , vk\u00f1, is the weight of the edges between the vertices in the path. The Shortest-path weight from u to v: d(u, v) = min w(p) : if there exists a path p or p from u to v \u00a5 :otherwise. Note that there might be multiple shortest paths from u to v. There may be many types of shortest path algorithms.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.3.1 Initialization<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE. Initially for every vertex v in G, d[v] the shortest path estimate is set to \u00a5 and the p[v], the predecessor of every vertex v is set to Nil. d[s], the shortest path estimate of source node is set to 0.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-637\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-435.png\" alt=\"\" width=\"308\" height=\"102\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-435.png 308w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-435-300x99.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-435-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-435-225x75.png 225w\" sizes=\"auto, (max-width: 308px) 100vw, 308px\" \/><\/p>\n<div>\n<p>\u00a0 \u00a0d[s] \u2190 0<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.2.2<\/strong>\u00a0 <strong>Shortest Path Tree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For every node <em>v<\/em> <em>\u00ce<\/em> <em>V, \u03c0[v]<\/em> is the predecessor of <em>v<\/em> in shortest path from source <em>s<\/em> to <em>v.<\/em> This value is set to Nil if does not exist. All our algorithms will output a shortest-path tree whose root is the source s and the edges are <em>(\u03c0[v],v).<\/em> The shortest path between <em>s<\/em> and <em>v<\/em> is the unique tree path from root s to <em>v<\/em>. Consider Source node 1 and destination node 7, a direct path between the nodes will cost 14, however a shorter path through two other nodes costs only 11 (Figure 39.3).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-638 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-436.png\" alt=\"\" width=\"669\" height=\"230\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-436.png 669w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-436-300x103.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-436-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-436-225x77.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-436-350x120.png 350w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/p>\n<p><strong>39.3 Shortest Path Variants<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are many variants of the shortest path algorithms. They are:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Single-source single-destination (1-1): <\/strong>Find the shortest path from source <em>s<\/em> to destination <em>v<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Single-source all-destination(1-Many): <\/strong>Find the shortest path from <em>s<\/em> to each vertex <em>v<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Single-destination shortest-paths (Many-1): <\/strong>Find a shortest path to a given <em>destination <\/em>vertex<em> t <\/em>from each vertex<em> v<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>All-pairs shortest-paths problem (Many-Many): <\/strong>Find a shortest path from <em>u<\/em> to <em>v<\/em> for every pair of vertices <em>u<\/em> and <em>v<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Single source\/All destinations <\/strong>-nonnegative edge cost<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Need to solve: <\/strong>determine a shortest path from <em>v<\/em> to each of the remaining vertices of <em>G<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.3.1 Single-destination shortest paths<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Among the shortest path algorithms given above the single destination shortest paths is about finding a shortest path to a given destination vertex <strong>t<\/strong> from each vertex v. Here we reverse the direction of each edge and finding the distances between every pair of vertices in a weighted directed graph G. Figure 39.4 shows the example of the <strong>Single source\/All destinations <\/strong>-nonnegative edge cost where we need to determine a shortest path from <em>v<\/em> to each of the remaining vertices of <em>G<\/em>.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-639 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-437.png\" alt=\"\" width=\"374\" height=\"393\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-437.png 374w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-437-285x300.png 285w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-437-65x68.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-437-225x236.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-437-350x368.png 350w\" sizes=\"auto, (max-width: 374px) 100vw, 374px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>39.3.2 Shortest Path Properties<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If some path from s to v contains a negative cost cycle, there does not exist a shortest path. Otherwise, there exists a shortest s-v that is simple. A negative cycle can produce arbitrarily long negative paths by traversing cycle a number of times. The negative-weight edges may form negative-weight cycle. If such cycles are reachable from the source, then \u03b4(s, v) the shortest-path weight is not properly defined .<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.4 Relaxation Process<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For each vertex v, we maintain an upper bound d[v] on the weight of shortest path from s to v and d[v] initialized to infinity. <strong>Relaxing<\/strong> an edge (u, v) = testing whether we can improve the shortest path to v found so far by going through u. d[v] is the weight of the path to vertex v, d[u] is the weight of the path to verte x u, w[u,v] is weight of edge [u,v] and p[v] indicates the predecessor of v in the shortest path to it (Figure 39.5).<\/p>\n<p style=\"text-align: justify\">If d[v] &gt; d[u] + w(u, v) and we can improve the shortest path to v . The updated d[v] =&gt; d[v]=d[u]+w(u,v) and p[v] is updated as =&gt; p[v] \u2190 u<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-640 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-438.png\" alt=\"\" width=\"329\" height=\"268\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-438.png 329w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-438-300x244.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-438-65x53.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-438-225x183.png 225w\" sizes=\"auto, (max-width: 329px) 100vw, 329px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">Relaxing an edge (<\/span><em style=\"text-align: justify;font-size: 1em\">u<\/em><span style=\"text-align: justify;font-size: 1em\">, <\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">) consists of testing whether we can improve the shortest path to <\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\"> found so far by going through <\/span><em style=\"text-align: justify;font-size: 1em\">u<\/em><span style=\"text-align: justify;font-size: 1em\">, then updating <\/span><em style=\"text-align: justify;font-size: 1em\">d<\/em><span style=\"text-align: justify;font-size: 1em\">[<\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">] and p[<\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">] accordingly. A relaxation step <\/span><strong style=\"text-align: justify;font-size: 1em\">may <\/strong><span style=\"text-align: justify;font-size: 1em\">either<\/span><strong style=\"text-align: justify;font-size: 1em\"> decrease <\/strong><span style=\"text-align: justify;font-size: 1em\">the value of the shortest-path estimate <\/span><em style=\"text-align: justify;font-size: 1em\">d<\/em><span style=\"text-align: justify;font-size: 1em\">[<\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">] and update <\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">\u2019s predecessor p[<\/span><em style=\"text-align: justify;font-size: 1em\">v<\/em><span style=\"text-align: justify;font-size: 1em\">], or cause <\/span><strong style=\"text-align: justify;font-size: 1em\">no change<\/strong><span style=\"text-align: justify;font-size: 1em\">.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>39.5 Shortest Path Properties<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Now let us define some properties of shortest paths.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.5.1 Triangle Inequality<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For any edge (u, v), we have (s; v) (s; u)+w(u; v). The weight of the shortest path from s to v is no greater than the weight of the shortest path from s to u plus the weight of the edge from u to v. For all (u, v) \u00ce E, we have:<\/p>\n<p>&nbsp;<\/p>\n<p>\u03b4 (s, v) \u2264 \u03b4 (s, u) + w (u, v)<\/p>\n<p>&nbsp;<\/p>\n<p>If u is on the shortest path to v we have the equality sign (Figure 39.6).<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-641 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-439.png\" alt=\"\" width=\"268\" height=\"199\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-439.png 268w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-439-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-439-225x167.png 225w\" sizes=\"auto, (max-width: 268px) 100vw, 268px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.5.2 Optimal substructure property<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">All sub-paths of shortest paths are also shortest paths. The shortest path problem has the following optimal substructure property. If a node x lies in the shortest path from a source node u to destination node v then the shortest path from u to v is combination of shortest path from u to x and shortest path from x to v.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.5.3 Upper-bound property<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We always have d[v] \u2265 \u03b4 (s, v) for all v. The estimate never rises since the relaxation process is so defined that it only lowers the estimate (Figure 39.7).<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-642 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-440.png\" alt=\"\" width=\"626\" height=\"258\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-440.png 626w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-440-300x124.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-440-65x27.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-440-225x93.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-440-350x144.png 350w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0<\/strong><strong style=\"text-align: initial;font-size: 1em\">39.5.4 Convergence property<\/strong><\/p>\n<\/div>\n<div>\n<p>If s\u00a0 then u \u2192 v is a shortest path, and if d[u] = \u03b4(s, u) at any time prior to relaxing edge (u, v), d[v] = \u03b4(s, v) at all times after relaxing (u, v).<\/p>\n<\/div>\n<div>\n<p>AS shown in Figure 39.8, If d[v] &gt; \u03b4(s, v) <em>\u00de<\/em> after relaxation:<\/p>\n<\/div>\n<div>\n<p>d[v] = d[u] + w(u, v) and\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 d[v] = 5 + 2 = 7.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Otherwise, the value remains unchanged, because it must have already been the shortest path value.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-643 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-441.png\" alt=\"\" width=\"298\" height=\"247\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-441.png 298w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-441-65x54.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-441-225x186.png 225w\" sizes=\"auto, (max-width: 298px) 100vw, 298px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.5.5 Path relaxation property<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let p = \u00e1v0, v1, . . . , vk\u00f1 be a shortest path from s = v0 to vk. If we relax, in order, (v0, v1), (v1, v2), . . . , (vk-1, vk), even intermixed with other relaxations, then d[vk ] = \u03b4 (s, vk).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-644 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-442.png\" alt=\"\" width=\"526\" height=\"204\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-442.png 526w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-442-300x116.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-442-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-442-225x87.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-442-350x136.png 350w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong> 39.5.6 Predecessor-subgraph property<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-645\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-443.png\" alt=\"\" width=\"708\" height=\"191\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-443.png 708w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-443-300x81.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-443-65x18.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-443-225x61.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-443-350x94.png 350w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">39.6 Applications<\/strong><\/p>\n<div>\n<p style=\"text-align: justify\">Some of the general types of applications of shortest path algorithms include minimization of the total <em>distance<\/em> traveled, minimization of the total <em>cost<\/em> of a sequence of activities, minimization of the total <em>time<\/em> of a sequence of activities. Application areas include network optimization, packet routing, image segmentation, computer-assisted surgery, computer games, DNA analysis, injection molding, operator scheduling , production planning, re-allocation of resources, approximation of piecewise linear functions and VLSI physical design. As discussed the shortest path algorithms find applications in many diverse areas.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.7\u00a0 Relaxation Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">All algorithms in this module call INITIALIZE-SINGLE-SOURCE and then repeatedly relax edges. Relaxation is the only means by which shortest-path estimates and predecessors change. The shortest path algorithms differ in <strong>how many times<\/strong> they relax each edge and <strong>the order in which they relax edges<\/strong>. Bellman-Ford algorithm relaxes each edge many times, while Dijkstra\u2019s algorithm for directed acyclic graphs relax each edge exactly once.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.8\u00a0 Algorithms<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Dijkstra\u2019s algorithm and Bellman-Ford algorithm both use the common operations of Initialization and Relaxation. In Dijkstra\u2019s algorithm negative weights are not allowed. In the Bellman-Ford algorithm negative weights are allowed but negative cycles reachable from the source are not allowed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.9\u00a0 Dijkstra\u2019s Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The steps and assumptions of the Dijkstra\u2019s algorithm are as follows:<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li>The distance of a vertex <strong><em>v<\/em><\/strong> from a vertex <strong><em>s<\/em><\/strong> is the length of a shortest path between <strong><em>s<\/em><\/strong> and <strong><em>v.<\/em><\/strong><\/li>\n<li>Dijkstra\u2019s algorithm computes the distances of all the vertices from a given start vertex <strong style=\"text-align: initial;font-size: 1em\"><em>s.<\/em><\/strong><\/li>\n<li>Assumptions are made that<\/li>\n<\/ol>\n<ul>\n<li>the graph is connected<\/li>\n<li>the edges are undirected.<\/li>\n<li>The edge weights are nonnegative.<\/li>\n<\/ul>\n<p>4. Grow a \u201c<strong>cloud<\/strong>\u201d of vertices, beginning with <strong><em>s<\/em><\/strong> eventually covering all the vertices.<\/p>\n<p>5. We store with each vertex <strong><em>v<\/em><\/strong> a label <strong><em>d<\/em><\/strong>(<strong><em>v<\/em><\/strong>) representing the distance of <strong><em>v<\/em><\/strong> from <strong><em>s<\/em><\/strong> in the subgraph consisting of the cloud and its adjacent vertices.<\/p>\n<p>6.\u00a0 At each step,<\/p>\n<ul>\n<li style=\"text-align: justify\">we add to the cloud the vertex <strong><em>u<\/em><\/strong> outside the cloud with the smallest distance label, <strong><em>d<\/em><\/strong>(<strong><em>u<\/em><\/strong>) and<\/li>\n<li>we update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>39.9.1 Edge Relaxation<\/strong><\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Now let us discuss the edge relaxation procedure in detail. Consider an edge <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>e =<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> (<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>u,z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) such that, <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>u<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> is the vertex most recently added to the cloud and <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> is not in the cloud. The relaxation of edge <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>e<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> updates distance <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>d<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) as follows: <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>d<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) \u00ac min{<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>d<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>z<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">)<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>,d<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>u<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) +\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>weight<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>e<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">)}. In other words we consider the edge from u to z that is the minimum (Figure 39.11).<\/span><\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-646 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-444.png\" alt=\"\" width=\"311\" height=\"298\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-444.png 311w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-444-300x287.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-444-65x62.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-444-225x216.png 225w\" sizes=\"auto, (max-width: 311px) 100vw, 311px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us consider the example given in Figure 39.12 (i) and (ii) adopted from webhome.csc.uvic.ca\/~ruskey\/classes\/326\/slides\/Chpt7ShortestPath.ppt<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We start with node A and d[A] at this point is 0 Figure 39.12 (i)-a. Now we add to the cloud the vertex <strong><em>u<\/em><\/strong> outside the cloud with the smallest distance label, <strong><em>d<\/em><\/strong>(<strong><em>u<\/em><\/strong>) which here is vertex C and d[C] =2 (A-C). We then update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>, here note that label of vertex D is updated from 4 to 3 (A-C-D), and label of vertex F (A-C-F) is updated to 11. Note that the path to B from A through C (C-B) and path to D from A (A-D) are not the shortest and are marked (in dotted lines) shown in Figure 39.12 (i)-b.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-647 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-445.png\" alt=\"\" width=\"579\" height=\"432\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-445.png 579w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-445-300x224.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-445-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-445-225x168.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-445-350x261.png 350w\" sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p style=\"text-align: justify\">Now we add to the cloud the vertex <strong><em>D<\/em><\/strong> outside the cloud with the smallest distance label, d[D] =3 (A-C-D). We then update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>, here note that label of vertex F is updated from 11 to 8 (A-C-D-F), and the path that is not the shortest (C-F) is marked (in dotted lines) shown in Figure 39.12 (i)-c.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we add to the cloud the vertex <strong><em>E<\/em><\/strong> outside the cloud with the smallest distance label, d[E] =5 (A-C-E). We then update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>, here note that label of vertex B is updated from 8 to 7 (A-C-E-B), and the path that is not the shortest (C-F) is marked (in dotted lines) shown in Figure 39.12 (i)-d.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we add to the cloud the vertex <strong><em>B<\/em><\/strong> outside the cloud with the smallest distance label, d[B] =7(A-C-E-B). We then update the labels of the vertices adjacent to <strong><em>u<\/em><\/strong>, here no updation occurs (Figure 39.12 (i)-e).<\/p>\n<p>&nbsp;<\/p>\n<p>Finally we add the node F to the cloud. Again no updation occurs (Figure 39.12 (i)-f).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-648 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-446.png\" alt=\"\" width=\"497\" height=\"341\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-446.png 497w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-446-300x206.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-446-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-446-225x154.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-446-350x240.png 350w\" sizes=\"auto, (max-width: 497px) 100vw, 497px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>39.9.2 Dijkstra\u2019s Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A priority queue stores the vertices outside the cloud where Key is distance for the element which is the vertex. Locator-based methods are used to insert &#8211; <strong><em>insert<\/em><\/strong>(<strong><em>k,e<\/em><\/strong>) which returns a locator and <strong><em>replaceKey<\/em><\/strong>(<strong><em>l,k<\/em><\/strong>) which changes the key of an item. We store two labels with each vertex, Distance (d(v) label) and locator in the priority queue.<\/p>\n<p>&nbsp;<\/p>\n<p>Dijkstra\u2019s algorithm assumes that <em>w(e)<\/em><em>\u00b3<\/em><em>0<\/em> for each e in the graph.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We maintain a set <em>S<\/em> of vertices such that, every vertex <em>v<\/em> <em>\u00ce<\/em><em>S, d[v]=<\/em><em>d<\/em><em>(s, v),<\/em> i.e., the shortest-path from s to v has been found. (Initial values: S=empty, d[s]=0 and d[v]=\u00b5)<\/p>\n<p>&nbsp;<\/p>\n<p>(a)\u00a0\u00a0 select the vertex <em>u<\/em><em>\u00ce<\/em><em>V-S<\/em> such that<\/p>\n<p><em>d[u]=min {d[x]|x <\/em><em>\u00ce<\/em><em>V-S}. <\/em>Set S=S\u00c8{u}<\/p>\n<p>&nbsp;<\/p>\n<p>(b)\u00a0 <strong><em>for <\/em><\/strong>each node <em>v <\/em>adjacent to <em>u<strong> do <\/strong><\/em>RELAX(u, v, w).<\/p>\n<p>Repeat step the above steps until S=V.<\/p>\n<p>&nbsp;<\/p>\n<p>The pseudo code for the algorithm is shown below:<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 \u00a0dist[s] \u21900\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (distance to source vertex is zero)<\/p>\n<p>for all v \u2208 V\u2013{s}<\/p>\n<p>do dist[v] \u2190\u00a5\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0(set all other distances to infinity)<\/p>\n<p>S\u2190\u00d8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (S, the set of visited vertices is initially empty)\u00a0 \u00a0 \\ Q\u2190V<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0(Q, the queue initially contains all vertices)<\/p>\n<p>while Q \u2260\u00d8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (while the queue is not empty)<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">do\u00a0\u00a0 u \u2190 mindistance(Q,dist)\u00a0\u00a0\u00a0 (select the element of Q with the min. distance)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">S\u2190S\u222a{u}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (add u to list of visited vertices)<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0for all v \u2208 neighbors[u]<\/p>\n<p>do\u00a0 if\u00a0\u00a0 dist[v] &gt; dist[u] + w(u, v)\u00a0\u00a0\u00a0 (if new shortest path found)<\/p>\n<p>then\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 d[v] \u2190d[u] + w(u, v) (set new value of shortest path)<\/p>\n<p>return dist<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.9.3 Features of Dijkstra\u2019s Algorithm<\/strong><\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">The Dijkstra\u2019s algorithm is a <\/span><em style=\"text-align: justify;font-size: 1em\">greedy<\/em><span style=\"text-align: justify;font-size: 1em\"> algorithm. It \u201cVisits\u201d every vertex only once, when it becomes the vertex with minimal distance amongst those still in the priority queue. However distances may be revised <\/span><strong style=\"text-align: justify;font-size: 1em\">multiple times<\/strong><span style=\"text-align: justify;font-size: 1em\"> since the current values represent only the \u2018best guess\u2019 based on our observations so far. Once a vertex is visited we are guaranteed to have found the shortest path to that vertex<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>39.9.4 Analysis of the Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Graph operation is the method incidentEdges which is called once for each vertex. The Label operations are the ones where we set\/get the distance and locator labels of vertex <strong><em>z<\/em><\/strong> that is of the order <strong><em>O<\/em><\/strong>(deg(<strong><em>z<\/em><\/strong>)) times and setting\/getting a label takes <strong><em>O<\/em><\/strong>(1) time. The Priority queue operations include insertion where each vertex is inserted once into the priority queue and removed once from the priority queue, where each insertion or removal takes <strong><em>O<\/em><\/strong>(log <strong><em>n<\/em><\/strong>) time. The key of a vertex in the priority queue is modified at most deg(<strong><em>w<\/em><\/strong>) times, where each key change takes <strong><em>O<\/em><\/strong>(log <strong><em>n<\/em><\/strong>) time .<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Thus the Dijkstra\u2019s algorithm runs in <strong><em>O<\/em><\/strong>((<strong><em>n<\/em><\/strong> + <strong><em>m<\/em><\/strong>) log <strong><em>n<\/em><\/strong>) time provided the graph is represented by the adjacency list structure &#8211; Recall that <strong>S<\/strong><strong><em>v<\/em><\/strong> deg(<strong><em>v<\/em><\/strong>) = 2<strong><em>m.<\/em><\/strong> The running time can also be expressed as <strong><em>O<\/em><\/strong>(<strong><em>m<\/em><\/strong> log <strong><em>n<\/em><\/strong>) since the graph is connected.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>39.10 Dijkstra\u2019s Algorithm \u2013 Walkthrough<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us use the graph in Figure 39.13 (a) to find the shortest path using Dijkstra\u2019s algorithm. .<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-649 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-447.png\" alt=\"\" width=\"512\" height=\"215\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-447.png 512w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-447-300x126.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-447-65x27.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-447-225x94.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-447-350x147.png 350w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Example has been adopted from<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/courses.cs.washington.edu\/courses\/cse326\/03wi\/...\/lecture21b.ppt\">https:\/\/courses.cs.washington.edu\/courses\/cse326\/03wi\/&#8230;\/lecture21b.ppt.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Initially the distance to all vertices except the source vertex is initialized to \u00a5. Let us assume that C is the source vertex d[C] =0 and C is added to the cloud . Now we update labels of neighbors of C \u2013 here A is updated to 9 and E to 8. Since distance to E is the shortest from C that will be the next node to be considered (Figure 39.13 (b)).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-650 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-448.png\" alt=\"\" width=\"428\" height=\"326\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-448.png 428w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-448-300x229.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-448-65x50.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-448-225x171.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-448-350x267.png 350w\" sizes=\"auto, (max-width: 428px) 100vw, 428px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now vertex E is added to the cloud and we update labels of neighbors of E \u2013 here D is updated to 15 (C-E-D that is 8+7=15) and G to 9 (C-E-G that is 8+1=9). Since distance to A is the shortest from C that will be the next node to be considered (Figure 39.13 (c)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-651 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-449.png\" alt=\"\" width=\"556\" height=\"294\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-449.png 556w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-449-300x159.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-449-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-449-225x119.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-449-350x185.png 350w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now vertex A is added to the cloud and we update labels of neighbors of A \u2013 here B is updated to 11 (C-A-B that is 9+2=11) and D\u2019s label is revised from 15 to 13 through new path (C-A-D that is 9+4=13). Since distance to G is the shortest from C that will be the next node to be considered (Figure 39.13 (d)).<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-652 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-450.png\" alt=\"\" width=\"519\" height=\"317\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-450.png 519w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-450-300x183.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-450-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-450-225x137.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-450-350x214.png 350w\" sizes=\"auto, (max-width: 519px) 100vw, 519px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now vertex G is added to the cloud and we update labels of neighbors of G \u2013 here F is updated to 11 (C-E-G-F that is 8+1+2=11). Since distance to B is one of the shortest from C that will be the next node to be considered (Figure 39.13 (e)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-653 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-451.png\" alt=\"\" width=\"515\" height=\"383\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-451.png 515w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-451-300x223.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-451-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-451-225x167.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-451-350x260.png 350w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now vertex B is added to the cloud but no updating takes place. Since distance to F is the shortest from C that will be the next node to be considered (Figure 39.13 (f)).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now vertex F is added to the cloud and we update labels of neighbors of F \u2013 here H is updated to 14 (C-E-G-F-H that is 8+1+2+3=14). Since distance to D is the shortest from C that will be the next node to be considered (Figure 39.13 (g)).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-654 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-452.png\" alt=\"\" width=\"548\" height=\"569\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-452.png 548w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-452-289x300.png 289w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-452-65x67.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-452-225x234.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-452-350x363.png 350w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now vertex D is added to the cloud but no updating takes place. Since distance to H is the shortest from C that will be the next node to be considered (Figure 39.13 (h)).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-655 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-453.png\" alt=\"\" width=\"556\" height=\"290\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-453.png 556w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-453-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-453-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-453-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-453-350x183.png 350w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/p>\n<p style=\"text-align: justify\">Now vertex H is added to the cloud and since all the vertices of the graph have been visited the algorithm completes (Figure 39.13 (i)).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-656 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-454.png\" alt=\"\" width=\"559\" height=\"380\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-454.png 559w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-454-300x204.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-454-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-454-225x153.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-454-350x238.png 350w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Explained the Shortest Path problem and its variants<\/li>\n<li>Discussed the properties of Shortest path<\/li>\n<li>Outlined the Dijkstra\u2019s Algorithm<\/li>\n<li>Illustrated the Dijkstra\u2019s Algorithm with an example<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Shortest Path Algorithm \u2013 I<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/y5fyMYT9i64\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-657 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-455.png\" alt=\"\" width=\"648\" height=\"528\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-455.png 648w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-455-300x244.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-455-65x53.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-455-225x183.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-455-350x285.png 350w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":3,"menu_order":39,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-t-v-geetha"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-632","chapter","type-chapter","status-publish","hentry","contributor-dr-t-v-geetha"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/632\/revisions"}],"predecessor-version":[{"id":995,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/632\/revisions\/995"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/632\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=632"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=632"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=632"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}