Dijkstras Algorithm for the SSSP Problem

J.E.D.I

6.7 Shortest Path Problems for Directed Graphs

Another classic set of problems in graphs is finding the shortest path given a weighted graph. In finding the shortest path, there is a need to get the length which, in this case, is the sum of the nonnegative cost of each edge in the path. There are two path problems on weighted graphs: • Single Source Shortest Paths SSSP Problem that determines the cost of the shortest path from a source vertex u to a destination vertex v, where u and v are elements of V. • All-Pairs Shortest Paths APSP Problem that determines the cost of the shortest path from each vertex to every other vertex in V. We will discuss the algorithm created by Dijkstra to solve the SSSP problem, and for the APSP, we will use the algorithm developed by Floyd.

6.7.1 Dijkstras Algorithm for the SSSP Problem

Just like Prims and Kruskals, Dijkstras algorithm uses the greedy approach. In this algorithm, each vertex is assigned a class and a value, where: • Class 1 vertex is a vertex whose shortest distance from the source vertex, say k, has already been found; Its value is equal to its distance from vertex k along the shortest path. • Class 2 vertex is a vertex whose shortest distance from k has yet to be found. Its value is its shortest distance from vertex k found thus far. Let vertex u be the source vertex and vertex v be the destination vertex. Let pivot be the vertex that is most recently considered to be a part of the path. Let path of a vertex be its direct source in the shortest path. Now the algorithm: 1. Place vertex u in class 1 and all other vertices in class 2. 2. Set the value of vertex u to zero and the value of all other vertices to infinity. 3. Do the following until vertex v is placed in class 1: a. Define the pivot vertex as the vertex most recently placed in class 1. b. Adjust all class 2 nodes in the following way: i. If a vertex is not connected to the pivot vertex, its value remains the same. ii. If a vertex is connected to the pivot vertex, replace its value by the minimum of its current value or the value of the pivot vertex plus the distance from the pivot vertex to the vertex in class 2. Set its path to pivot. c. Choose a class 2 vertex with minimal value and place it in class 1. Data Structures 116 J.E.D.I For example, given the following weighted, directed graph, find the shortest path from vertex 1 to vertex 7. Figure 1.98 A Weighted Directed Graph for SSSP Example Figure 1.99 Figure 1.100 The following shows the execution of the algorithm: Vertex class value path Description 1 2 3 4 5 6 7 1 2 2 2 2 2 2 ∞ ∞ ∞ ∞ ∞ ∞ The source vertex is 1. Its class is set to 1. All others are set to 2. The value of vertex 1 is 0 while the rest are ∞. Path of all vertices are set to 0 since the paths to the vertex from the source have not yet been found. pivot 1 2 3 4 5 6 7 1 2 2 2 2 2 2 4 3 ∞ ∞ ∞ ∞ The source vertex is the first pivot. It is connected to vertices 2 and 3. Hence, the values of the vertices are set to 4 and 3 respectively. The class 2 vertex with the minimum value is 3, so it is chosen as the next pivot. pivot 1 2 3 4 5 6 7 1 2 1 2 2 2 2 4 3 7 ∞ ∞ ∞ 1 1 3 Class of vertex 3 is set to 1. It is adjacent to vertices 1, 2 and 4 but the value of 1 and 2 are smaller than the value if the path that includes vertex 3 is considered so there will be no change in their values. For vertex 4, the value is set to value of 3 + cost3, 4 = 3 + 4 = 7. Path4 is set to 3. pivot 1 2 3 1 1 1 4 3 1 1 Next pivot is 2. It is adjacent to 4, 5 and 6. Adding the pivot to the current shortest path to 4 will increase its cost, but not with 5 and 6, Data Structures 117 J.E.D.I Vertex class value path Description 4 5 6 7 2 2 2 2 7 5 16 ∞ 3 2 2 where the values are changed to 5 and 16 respectively. pivot 1 2 3 4 5 6 7 1 1 1 2 1 2 2 4 3 7 5 7 11 1 1 3 2 5 5 Next pivot is 5. It is connected to vertices 6 and 7. Adding vertex 5 to the path will change the value of vertex 6 to 7 and vertex 7 to 11. pivot 1 2 3 4 5 6 7 1 1 1 1 1 2 2 4 3 7 5 7 11 1 1 3 2 5 5 Next pivot is 4. Although it is adjacent to vertex 6, the value of vertex 6 will not change if the pivot will be added to its path. pivot 1 2 3 4 5 6 7 1 1 1 1 1 1 2 4 3 7 5 7 8 1 1 3 2 5 6 Having 6 as the pivot vertex entails changing the value of vertex 7 from 11 to 8, and adding vertex 6 into the path of the latter. pivot 1 2 3 4 5 6 7 1 1 1 1 1 1 1 4 3 7 5 7 8 1 1 3 2 5 6 Now, destination vertex v is placed in class 1. Algorithm ends. The path from the source vertex 1 to destination vertex 7 can be obtained by retrieving the value of path7 in reverse order, that is, path7 = 6 path6 = 5 path5 = 2 path2 = 1 Hence, the shortest path is 1 -- 2 -- 5 -- 6 -- 7, and the cost is value7 = 8. Data Structures 118 J.E.D.I

6.7.2 Floyds Algorithm for the APSP Problem