algorithm
TYChan6028
>Order of traverse:
The graph with and as the source can be obtained by using the same process.
Corollary 24.3
Let be a weighted, directed graph with source vertex and weight function , and assume that contains no negative-weight cycles that are reachable from s. Then, for each vertex , there is a path from to if and only ifBELLMAN-FORD
terminates with when it is run on . – textbook, p.653
We will prove this by contradiction.
Let graph be initialized with a call to INITIALIZE- SINGLE-SOURCE
, which sets and for .
At BELLMAN-FORD
's termination, assume there exists a on the path from , where and . Since for all at termination,
We know , so . The same can be said for to , until
This contradicts with , which proves there is no path from to if BELLMAN-FORD
terminates with .
DAG-SHORTEST-PATHS
on the directed graph of Figure 24.5, using vertex as the source.DAG-SHORTEST-PATHS
to read for the first |V| - 1 vertices, taken in topologically sorted order
. Show that the procedure would remain correct.Let topologically-sorted , then has no child nodes. This shows that there does not exist a for which could relax. Since G.Adj[u]
, the for
loop on line 4
would go through 0 iterations. Therefore, it is safe to only consider the first vertices.
while
loop.The graph with vertex as the source can be obtained by using the same process.
Below is an example for which Dijkstra's algorithm produces an incorrect answer. The negative-weight edge creates a negative cycle for which an even smaller weight can always be achieved by traversing the cycle indefinitely.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)
Dijkstra’s algorithm, run on a weighted, directed graph with non-negative weight function and source , terminates with for all vertices .
Theorem 24.6 proves correctness by showing, for each vertex , at the time when is added to set . Once the equality is shown, it relies on the upper-bound property to uphold the equality at all times thereafter. Therefore, the algorithm would terminate with for all vertices .
The proof of Theorem 24.6 illustrates a shortest path from , on which and . It makes a claim that is the first vertex for which when it is added to , then attempts to disprove this by contradiction.
By proving and because all edge weights are non-negative, it arrives at the relations
Since and is chosen over to be next included in set , . Combined with the relation shown above, , which contradicts its claim and completes the proof.
The reason why the proof of Theorem 24.6 does not go through when negative-weight edges are allowed is because we can no longer make the assumption that .
We know , but if due to a negative-weight edge, it is possible that