The shortest path algorithm is used mainly for

calculating the shortest path of a graph or network. The shortest path problem

is the problem of finding a path between two nodes in a graph or network such

that sum of the weights of its constituent edges is minimized. The algorithms which are used to find the

shortest path are shortest path algorithms 2. In this paper we have discussed

this algorithm only for single source.

2.2 Dijkstra’s Algorithm

Dijkstra’s algorithm1 is one of the

fastest algorithm to track the shortest path from any graph. It is almost

similar to Prim’s algorithm for minimum spanning tree. In Prim’s algorithm we

generate a shortest path tree with the given source. We maintain two sets where

one set is for vertices included in that tree and the other set contains

vertices not yet included in that shortest path tree. At each iteration of the

algorithm, we find a vertex which is in the other set and has minimum distance

from the source.

Steps to measure

the shortest for a single source vertex to all other vertices is given below:

1.

Need to create a set that keeps track of vertices

included in shortest path tree. Initiallay the set is empty.

2.

Need to assign distance value to all vertices in

the input graph. All the distance value must be initialized as INFINITE. Must

assign distance value as 0 for the source vertex so that it is picked first.

3.

While the set (which is mentioned in 1) does not

includes all the vertices

i) Need to pick

a vertex which is not there in the set and has minimum distance value.

ii) Then

include that vertex into the set.

ii) Update

distance value of all adjacent vertices of that vertex. To update the distance

values, iterate through all adjacent vertices. For any adjacent vertex, if the

sum of the distance value for that vertex and weight of edge from vertex to

adjacent vertex is less than the distance value of the adjacent vertex

then update the distance value of adjacent vertex.

2.2.1 Pseudocode

Algorithm 2.1 Pseudo code for

Dijkstra’s Algorithm

1: Create vertex set Q.

2: for each vertex v in

Graph do

3: distancev

??;

4: predecessorv

?

null;

5: add v to Q

6: end for

7: distancesource ? 0

8: while Q is not empty, do

9: u ?

vertex in Q with min distanceu

10: remove u

from Q

11: for each neighbor v of u: do

12: alt

?

distanceu + length(u,v)

13: if alt < distancev then 14: distancev ? alt 15: predecessorv ? u 16: end if 17: end for 18: end while 2.2.2 Advantages and Disadvantages Advantages: - • It is used in Google Maps • It is used in finding Shortest Path. • It is used in geographical Maps • To find locations of Map which refers to vertices of graph. • Distance between the location refers to edges. • It is used in IP routing to find Open Shortest Path First. • It is used in the telephone network. Disadvantages: - • It does blind search so wastes lot of time while processing. • It cannot handle negative edges. • This leads to acyclic graphs and most often cannot obtain the right shortest path. 2.2.3 Complexity It depends on the implementation of Dijkstra's Algorithm. Time Complexity: O(s*|E|log|E|+|V|) Space Complexity: O(V^2) Here, s: the number of sources n: the number of vertices to calculate V: the number of vertices in the graph E: the number of edges in the graph 2.3 Bellman Ford Algorithm Bellman Ford's algorithm does the same job which dijkstra's algorithm does but the difference is it can calculate the shortest path even if any edge of the graph has negative weight. Negative weight vertex may seem useless but it explain a lot of phenomena like cash flow, heat released or absorbed in any chemical reaction etc. Negative weight edges can create negative weight cycle which will reduce the total path distance. Shortest path algorithms like Dijkstra's algorithm that does not work in the presence of negative cycle because they can go through a negative weight cycle and reduce the path length. 2.3.1 Pseudo Code Algorithm 2.4 Pseudo code for Bellman Ford Algorithm 1: for each vertex v in vertices: do 2: distancev ??; 3: predecessorv ? null; 4: end for 5: distancesource ? 0 6: for i from 1 to size(vertices)-1: do 7: for each edge (u, v) with weight w in edges do 8: if distanceu + w < distancev then 9: distancev ? distanceu + w; 10: predecessorv ? u; 11: end if 12: end for 13: end for 14: for each edge (u, v) with weight w in edges do 15: if distanceu + w < distancev then 16: error "Graph contains a negative-weight cycle" 17: end if 18: end for 2.3.2 How Bellman Ford's Algorithm Works: Bellman Ford algorithm overestimates the distance of the path from source vertex to all other vertices. Then it iteratively relaxes those estimation by finding new shorter paths. By doing that for all vertices the final outcome will be the optimized one. All the steps for gaining the optimal cost and path given below: Start with a weighted graph Except the source vertex assign every path values as infinite to all other vertices. Need to go through each of the edges and relax the distance if that is inaccurate. Need to repeat it unless all the edges are visited. After all the vertices get their path distance , need to check if any negative cycle exists or not. 2.3.3 Cost of Bellman Ford using Sequences On the off chance that we accept the vertices are the whole numbers {0, 1, . . ., |V|?1} then we can utilize exhibit successions to execute a vtxTable. We can use nth requiring only O(1) work instead of using a formula which needs O(log n) work. This change in expenses can be connected for turning upward in the diagram to discover the neighbors of a vertex, and gazing upward out there table to locate the present separation.Using the improved costs, W = O (?v ? V (1 + |NG(v)| + ? u?NG(v) 1)) = O(m) S = O (max v?V (1 + log |NG(v)| + max u?N(v) 1)) = O (log n) Therefore the overall complexity for BellmanFord with array sequences is: W (n, m) = O(nm) S (n, m) = O (n log n) By using this sequences, we have decreased the work by a O (log n) factor. 2.3.4 Complexity Time Complexity: O(s*|E|*|V|) Space Complexity: O(V^2) Here, s: the number of sources n: the number of vertices to calculate V: the number of vertices in the graph E: the number of edges in the graph 2. 4 Floyd - Warshall Algorithm The Floyd – Warshall algorithm is an algorithm to have shortest paths with positive or negative edge weights. Lengths of the shortest paths between all vertices will be found by executing once. Although it does not give full details of the paths but we can recreate the paths by modifying the algorithm. The Floyd– Warshall calculation analyzes every conceivable way through the chart between each combine of vertices.It can work with ? (|V|3 ) comparisons in a graph. This is exceptional that there may be up to ? (|V|2 ) edges in the graph, and every combination of edges is examine. It does as such by incrementally enhancing a gauge on the most brief way between two vertices, until the point when the gauge is ideal. Suppose a graph G with vertices V numbering 1 through N, a function findshortPath(m,n,o) that gives the possible shortest path from m to n using the set {1,2,....,o} as medial points along the path.Our plan is to search the shortest path from each m to each n using only vertices in {1,2,...,N} For each of these pairs of vertices, the findshortPath(m,n,o) could be either 1) a path that doesn't go through o. 2) a path that goes through o. If w(i,j) is the weight of the edge between m and n , we can conclude findshortPath(m,n,o)) by recursive formula given below findshortPath(m,n,0) = w(m,n) and the recursive case is findshortPath(m,n,o) = min(findshortPath (m,n,o=1), findshortPath (m,o,o-1)+ findshortPath (o,n,o-1 ) 2.4.1 Pseudo Code Algorithm 2.5 Pseudo code for Floyd Warshall Algorithm 1: for i = 1 to N do 2: for j = 1 to N do 3: if there is an edge from i to j then 4: dist0ij = the length of the edge from i to j 5: else 6: dist0ij = INFINITY 7: end if 8: end for 9: end for 10: for k = 1 to N do 11: for i = 1 to N do 12: for j = 1 to N do 13: distkij = min(distk-1ij, distk-1ik + distk-1kj) 14: end for 15: end for 16: end for 2.4.2 Advantages and Disadvantages The Floyd Warshall algorithm is known for its plainness. Advantages: - · Easy to write. · Features all the most limited way combines in a chart. Disadvantages: - · Slower than all algorithms. 2.4.3 Complexity Time Complexity: O(n3) Space complexity: O(n2) 2. 5 Johnson's Algorithm Johnson's algorithm takes an input graph. This graph has a set of vertices that retail a set of edges. Each edge has a weight function. Directed, weighted graphs can be solved by using Johnson's algorithm. It allows edges to have negative weights, yet there can be no negative weight cycles. Because of its subroutines it can support negative weight. Johnson's algorithm has three main steps. i. Another vertex is added to the diagram, and it is associated by edges of zero weight to all different vertices in the chart. ii. Negative weights are being extracted by a process called reweighting. iii. The included vertex from stage 1 is expelled and on every node Dijkstra's algorithm is being runned. 2.5.1 Pseudo Code Algorithm 2.6 Pseudo code for Johnson's Algorithm 1: Compute G0, where VG0 = VG ?s ; 2: EG0 = EG ? (s,v) : v ? VG ; 3: w(s,v) = 0 FOR all v ? VG ; 4: if BELLMAN-FORD(G0, w, s) = FALSE then 5: Print the input graph contains a negative weight cycle 6: else 7: for each vertex v ? VG0 do 8: set h(v) to the value of ?(s,v)computed by the Bellman-Ford alg. 9: end for 10: for each edge (u,v) ? EG0 do 11: w0(u,v) ? w(u,v) + h(u) ?h(v) 12: end for 13: for each vertex u ? VG do 14: run DIJKSTRA(G,w0,u) to compute ?(s,v) for all v ? VG 15: for each vertexv ? VG do 16: d(u,v) ? ?(s,v) + h(v) ?h(u) 17: end for 18: end for 19: end if 2.5.2 Complexity Here V=total no. of vertices. and E= total no. of edges Time Complexity: O(V2log(V) +VE)