2.4 Shortest Path Algorithms
2.4.1 Dijkstra’s Algorithm
Given a weighted graph, G = (V, E), where V is a set of vertices and E is a set of edges, and s is a source vertex in V , find the path of shortest length connecting s to all vertices in V . This is the SSSP. The problem is trying to get the minimum cost between vertex s to all other vertices, v ∈ V . If the minimum cost is found, then d(v) denotes the minimum edge cost from s to v. Initially, d(s) is assumed to be 0, d(s) = 0 to denote that s is the source or the start vertex.
The length of the directed edge connecting vertex u to vertex v is represented as cost(u, v). The eponymous Dijkstra’s algorithm is used to solve the single source shortest path problem (SSSP) for a non-negative edge lengths graph. The single source shortest path algorithm is described in the following. Let G = (V, E) be a directed graph where V is the set of vertices and E ⊆ V x V is the set of edges. OU T (v) is defined as a set of vertices w such that there is a directed edge from vertex v to vertex w. The non-negative cost of edge (v, w) is denoted by cost(v, w). It is assumed that cost(v, v) = 0 and cost(v, w) = ∞ if there is no edge from v to w. A vertex s is denoted as the source vertex. The shortest path from s to vertex v is the path such that the sum of edge costs of the path is minimum among all paths from s to v. The minimum cost is also called the shortest distance. In Dijkstra’s algorithm, two set of vertices, S and F , are maintained. The set S, called the solution set, is the set of vertices to which the shortest distances have been finalized and the set F , called the frontier, is the set of vertices which can be reached from S by a single edge. Vertices that remain outside S and F are considered unexplored vertices that need to be explored.
In solving a single-shortest path problem, Dijkstra’s algorithm maintains a distance value d[v] for each vertex v in the graph. The value of d[v] indicates the shortest distance from the source vertex to vertex v. If v is in F , d[v] is the distance of the shortest path that lies in S except for the end point v.
Initially, the source vertex s, with d[s] = 0 is put in S. Vertices in OU T (s) are put in F with their keys values. The key value of v ∈ OU T (s) is computed as d[v] = d[u]+ cost(u, v). The algorithm works as the following:
2. If v is outside S, move v from F to S and the following steps are taken. Otherwise, the first step is repeated.
3. The shortest distance from s to v, d[v] is now known and finalised.
4. For every vertex w ∈ OU T (v), a new distance key, key, is calculated by adding the shortest distance of v and the edge length from v to w, key = d[v] + cost(v, w).
5. If w is already in F , the new key is compared with the existing d[w] and the minimum distance is assigned to d[w].
6. If w is not in F , it will added into F with d(w) = key.
This process continues from the first step until there is no vertex in F . If F is empty, the shortest distance from s to all vertices has been finalised and all vertices are now known as labelled vertices. Algorithm 1 shows Dijkstra’s algorithm to solve the shortest path problem. Algorithm 1 Dijkstra’s Algorithm
1: ∀v ∈ V : d[v] = ∞; 2: S = ∅; d[s] = 0; F = {s}; 3: while |S| < n do
4: find v in F with d[v] = min{d[i] : i ∈ F }; / ∗ delete − min ∗ /
5: S = S + {v}; F = F − {v};
6: for each vertex w ∈ OU T (v) do
7: if w /∈ S then
8: if w ∈ F then
9: if d[v] + cost(v, w) < d[w] then
10: d[w] = d[v] + cost(v, w); / ∗ decrease − key ∗ /
11: else
12: d[w] = d[v] + cost(v, w); / ∗ insert ∗ /
13: F = F + {w};
There are a few operations involved in running the Dijkstra’s algorithm. When a vertex with the minimum key value is removed from F , a delete-min operation is called. When a vertex with certain key value needs to be updated in F , a decrease-key operation is used. Inserting a new vertex in F requires an insert operation. Therefore, a good data structure that supports theses operations is needed when executing Dijkstra’s algorithm. The data structure used to support the operations should have a reasonable time complexity. In a graph that has n vertices and m edges, delete-min and insert operations are executed n times. The decrease-key operation is done in m times in the worst case. As the decrease-key operation is done very frequently,
2.4 Shortest Path Algorithms
especially when m ≈ n2, many kinds of data structures, such as binary (28), Fibonacci (3),
2-3 heaps (4) and trinomial (2) heaps try to reduce the decrease-key complexity by adding complexity on the delete-min function.
Analysis of Dijkstra’s algorithm depends on the data structure used. If the data structure is a linear array, the total running time of the algorithm is O(m + n2), that is ≈ O(n2) time,
where n = |V | and m = |E|. This is because the delete-min takes O(n) time and it has to be performed n times. Therefore a total time for the delete-min is O(n2). Each operation of
decrease-key takes O(1) time and there are m operations. Hence, the running time of decrease- key operation is O(m) time. To run Dijkstra’s algorithm using a linear array data structure will take O(m + n2) ≈ O(n2) time.
If the Dijkstra algorithm is run using a binary heap data structure, the running time is different from the above. In the binary heap, delete-min takes O(log n) time. The insert and the decrease-key operations take O(log n) time. Hence, the total running time of the algorithm with the binary heap is O(m log n + n log n) = O((m + n) log n) time. If all nodes are reachable from the source, s, then the running time becomes O(m log n) time. For higher graph densities, the number of edges, m is almost n2; this will give a time of complexity O(n2log n) time if
Dijkstra’s algorithm is running with the binary heap.
Table 2.3 shows how Dijkstra’s algorithm works when a graph in Figure 1.2 is used. The solution set is labelled as S and d[i] shows distance of the shortest path.
Iteration S d[1] d[2] d[3] d[4] d[5] 1 {0} 3 5 ∞ ∞ ∞ 2 {0, 1} 3 5 4 ∞ ∞ 3 {0,1, 3} 3 5 4 8 6 4 {0,1 , 3, 2} 3 5 4 7 6 5 {0, 1, 3, 2, 5} 3 5 4 7 6 6 {0, 1, 3, 2, 5, 4} 3 5 4 7 6
Table 2.3: Performing Dijkstra’s algorithm on Figure 1.2
When n× SSSP problem is solved, the APSP problem is catered. Many APSP algorithms that have been developed employ this method to solve the problem. The common APSP algorithm is Floyd’s algorithm that will be discussed in the following section. This algorithm, however use matrix operation rather than using the common technique, n× SSSP algorithm in solving the APSP problem.