|
| 1 | +# Shortest Path |
| 2 | + |
| 3 | +In a [Geo-spatial Network](https://en.wikipedia.org/wiki/Spatial_network) application, it is a common feature to compute shortest paths for various purposes, e.g. planning a route from current location to a destination on the geographic map; scheduling nearest [Uber](https://en.wikipedia.org/wiki/Uber_(company)) cars to users requesting ride service. |
| 4 | + |
| 5 | +Specifically, there are two types of tasks to compute shortest paths: [single-source](#single-source) and [multi-source](#multi-source), both of which are computed based on **weighted graph** structures. One of the most important algorithms in the 20th is [Dijkstra's Algorithm](#dijkstras-algorithm). |
| 6 | + |
| 7 | +## Generality |
| 8 | + |
| 9 | +a path from 𝓋<sub>0</sub> -> 𝓋<sub>k</sub> is defined by _p_ (𝓋<sub>0</sub> -> 𝓋<sub>0</sub> is 0), and the shortest path from source 𝓊 to 𝓋 is: |
| 10 | + |
| 11 | +<figure style="text-align:center"> |
| 12 | + <img src="../images/shortest-path.png" /> |
| 13 | + <figcaption>Figure 1. A Delta Function Denotes the Shortest Path Total Value</figcaption> |
| 14 | +</figure> |
| 15 | + |
| 16 | +## Single-Source |
| 17 | + |
| 18 | +Given weighted graph _G_ = (_V_, _E_), weight w(u, v) of two vertices (u, v) and a source _S_, compute δ(_S_, _v_<sub>i</sub>) wherein _v_<sub>i</sub> is a vertex from vertices set _V_. |
| 19 | + |
| 20 | +<figure style="text-align:center"> |
| 21 | + <img src="../images/shortest-path-2.png" /> |
| 22 | + <figcaption>Figure 2. Auxiliary Static Data Structure to Store Shortest Paths</figcaption> |
| 23 | +</figure> |
| 24 | + |
| 25 | +Mathematically, use the above structure to initialize to infinity and converge to the shortest paths values from a given source _S_ would yield the final solution at hand. |
| 26 | + |
| 27 | +In addition, define another array π<sub>i</sub> (i=1,2,...,n) to track the **predecessor** of all vertices, where π[s] = s in order to output the shortest paths. |
| 28 | + |
| 29 | +_Note: the convergence step is formally termed as **Relaxation**, which would be covered in Dijkstra's Algorithm discussion_. |
| 30 | + |
| 31 | +The classical [Dijkstra's Algorithm](#dijkstras-algorithm) and [Bellman-Ford algorithm](#bellman-ford-algorithm) are further discussed based on the above assumptions. |
| 32 | + |
| 33 | +### Dijkstra's Algorithm |
| 34 | + |
| 35 | +Invented by Turing Award winner [Edsger W. Dijkstra](https://en.wikipedia.org/wiki/Edsger_W._Dijkstra), this algorithm adopts a [greedy](../greedy-algorithms) approach to search for the shortest path among all the paths leading from a single source to the destination. |
| 36 | + |
| 37 | +#### Fundamental Ideas |
| 38 | + |
| 39 | +It is applicable for both directed and undirected graphs, in which the edges must have non-negative weights. In the original version of Dijkstra's algorithm, it only tries to find the minimal path between two nodes, but more commonly the modern variant can find the shortest paths from a single source |
| 40 | +to all other nodes, producing a shortest path tree. |
| 41 | + |
| 42 | +Specifically, for each edge (_u_, _v_) in a **non-negative** weighted graph _G_, maintain a set _S_ of vertices whose final shortest paths have been computed. Repeatedly select 𝓊 out of _V_ - _S_ with minimum shortest path estimate, add 𝓊 to _S_ |
| 43 | +and relax all edges adjacent and reachable from 𝓊. |
| 44 | + |
| 45 | +<pre> |
| 46 | +<code> |
| 47 | +DIJKSTRA(graph, source) |
| 48 | + Initialize d array to either Infinity or 0 (s = source) |
| 49 | + Initialize π array of vertices with their self |
| 50 | + set S := empty |
| 51 | + Q := a priority queue of reachable vertices from source |
| 52 | + while Q != empty |
| 53 | + u = EXTRACT_MIN(Q) // deletes a vertex with minimal distance to the source |
| 54 | + S.add(u) |
| 55 | + for vertex v adjacent to u |
| 56 | + RELAX(u, v, d, π) |
| 57 | + |
| 58 | + output array d and π |
| 59 | + |
| 60 | +RELAX(u, v, d, π) |
| 61 | + if d[v] > d[u] + w(u, v) |
| 62 | + d[v] = d[u] + w(u, v) |
| 63 | + π[v] = u; |
| 64 | +</code> |
| 65 | +</pre> |
| 66 | + |
| 67 | +<figure style="text-align:center"> |
| 68 | + <img src="../images/shortest-path-3.png" /> |
| 69 | + <figcaption>Figure 3. An Example of Dijkstra's Algorithm</figcaption> |
| 70 | +</figure> |
| 71 | + |
| 72 | +### Bellman-Ford Algorithm |
| 73 | + |
| 74 | +Though slower than peer shortest path algorithm like Dijkstra's, Bellman-Ford algorithm can compute the shortest paths from a single source that might bear negative edges in between. |
| 75 | + |
| 76 | +Specifically, given a graph _G_ = (_V_, _E_), this algorithm answers true if a shortest path exists from a single source to a destination without cycles, otherwise answers false; |
| 77 | + |
| 78 | +<pre> |
| 79 | +<code> |
| 80 | +BELLMAN_FORD(graph, source) |
| 81 | + Initialize d array to either Infinity or 0 (s = source) |
| 82 | + Initialize π array of vertices with their self |
| 83 | + for i := 1 to |V| - 1 |
| 84 | + for each edge (u, v) ∈ E |
| 85 | + RELAX(u, v, d, π) // same sub-routine as Dijkstra |
| 86 | + for edge (u, v) ∈ E |
| 87 | + if d[v] > d[u] + w(u, v) |
| 88 | + report a negative cycle exists, return false |
| 89 | + return true |
| 90 | +</code> |
| 91 | +</pre> |
| 92 | + |
| 93 | +_Note: why do we care about negative edges? reasons include computational tasks in a water pipeline or electric wires, or financial models with various cost and revenue gains in many vertices_. |
| 94 | + |
| 95 | +## Additional References |
| 96 | + |
| 97 | +1. Shortest Paths II, MIT OpenCourseware, https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec16.pdf |
| 98 | + |
0 commit comments