Shortest Path Problem: Difference between revisions
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 54: | Line 54: | ||
can detect it by running an extra time, since if there is a negative | can detect it by running an extra time, since if there is a negative | ||
cycle, the run time will improve. | cycle, the run time will improve. | ||
[[Category:Algorithms]] | |||
Revision as of 03:43, 5 March 2024
Definitions
A path is a sequence of nodes Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x_1, x_2, x_3, \ldots, x_i } such that for all consecutive nodes, there exist an edge
Let there be a weight assigned to each edge.
Single Source Shortest Path (SSSP)
Given a graph Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle G(V,E), w(e) } , source node Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle S } , outupt the shortest path from the source
Variants
- Single destination problem: shortest path from all nodes to a single destination
- Single pair problem: Shortest path between input pair
Implementation: Bellman Ford
All shortest path must have Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \leq n - 1 } edges. If this condition is not satisfied, there is a cycle in in the path, and therefore it is not the shortest.
OPT(a, n-1) = min(w(u,a) + OPT(n-1, u) for all (u, a) in E)
Time complexity: Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O(V^2 + VE) }
- Sparse: Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O(V^2) }
- Dense: Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O(V^3) }
// G: Graph with vertices (V) and edges (E)
// w[e]: weight of edge e
// S: starting node
Bellman-Ford(G, w, S)
V, E = G
pi[v] = null for all v // traceback
// initialize all shortest path algo
d[v] = infty for all v
d[s] = 0
for i from 1 to |V| - 1:
for all (u,v) in E
if d[v] > d[u] + w(u, v):
d[v] = d[u] + w(u,v)
pi[v] = u;
for all (u, v) in E:
if d[v] > d[u] + w(u, v):
// has negative cycle
return false
This accounts for negative edges but not negative cycles. Bellman Ford can detect it by running an extra time, since if there is a negative cycle, the run time will improve.
