Dijkstra's algorithm with negative weights -


can use dijkstra's algorithm negative weights?

stop! before think "lol nub can endlessly hop between 2 points , infinitely cheap path", i'm more thinking of one-way paths.

an application mountainous terrain points on it. going high low doesn't take energy, in fact, generates energy (thus negative path weight)! going again wouldn't work way, unless chuck norris.

i thinking of incrementing weight of points until non-negative, i'm not sure whether work.

as long graph not contain negative cycle (a directed cycle edge weights have negative sum), have shortest path between 2 points, dijkstra's algorithm not designed find them. best-known algorithm finding single-source shortest paths in directed graph negative edge weights bellman-ford algorithm. comes @ cost, however: bellman-ford requires o(|v|·|e|) time, while dijkstra's requires o(|e| + |v|log|v|) time, asymptotically faster both sparse graphs (where e o(|v|)) , dense graphs (where e o(|v|^2)).

in example of mountainous terrain (necessarily directed graph, since going , down incline have different weights) there no possibility of negative cycle, since imply leaving point , returning net energy gain - used create perpetual motion machine.

increasing weights constant value non-negative not work. see this, consider graph there 2 paths b, 1 traversing single edge of length 2, , 1 traversing edges of length 1, 1, , -2. second path shorter, if increase edge weights 2, first path has length 4, , second path has length 6, reversing shortest paths. tactic work if possible paths between 2 points use same number of edges.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -