0% found this document useful (0 votes)
32 views7 pages

Belmanfordt

The document describes the Floyd-Warshall algorithm for finding shortest paths in a weighted graph. It explains how the algorithm works in iterations to build up the shortest paths between all pairs of vertices. It also discusses handling negative cycles and reconstructing the actual shortest path.

Uploaded by

Razvan888
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

Belmanfordt

The document describes the Floyd-Warshall algorithm for finding shortest paths in a weighted graph. It explains how the algorithm works in iterations to build up the shortest paths between all pairs of vertices. It also discusses handling negative cycles and reconstructing the actual shortest path.

Uploaded by

Razvan888
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

Prior to the first recursion of the outer loop, labeled k = 0 above, the only known paths correspond to

the single edges in the graph. At k = 1, paths that go through the vertex 1 are found: in particular, the
path [2,1,3] is found, replacing the path [2,3] which has fewer edges but is longer (in terms of weight). At
k = 2, paths going through the vertices {1,2} are found. The red and blue boxes show how the path
[4,2,1,3] is assembled from the two known paths [4,2] and [2,1,3] encountered in previous iterations,
with 2 in the intersection. The path [4,2,3] is not considered, because [2,1,3] is the shortest path
encountered so far from 2 to 3. At k = 3, paths going through the vertices {1,2,3} are found. Finally, at k =
4, all shortest paths are found.

The distance matrix at each iteration of k, with the updated distances in bold, will be:

k=0 j

1 2 3 4

i 1 0 ∞ −2 ∞

2 4 0 3 ∞

3 ∞ ∞ 0 2

4 ∞ −1 ∞ 0

k=1 j

1 2 3 4

i 1 0 ∞ −2 ∞

2 4 0 2 ∞

3 ∞ ∞ 0 2

4 ∞ −1 ∞ 0

k=2 j

1 2 3 4

i 1 0 ∞ −2 ∞

2 4 0 2 ∞

3 ∞ ∞ 0 2
4 3 −1 1 0

k=3 j

1 2 3 4

i 1 0 ∞ −2 0

2 4 0 2 4

3 ∞ ∞ 0 2

4 3 −1 1 0

k=4 j

1 2 3 4

i 1 0 −1 −2 0

2 4 0 2 4

3 5 1 0 2

4 3 −1 1 0

Behavior with negative cycles

A negative cycle is a cycle whose edges sum to a negative value. There is no shortest path between any
pair of vertices

{\displaystyle i},

{\displaystyle j} which form part of a negative cycle, because path-lengths from

{\displaystyle i} to

{\displaystyle j} can be arbitrarily small (negative). For numerically meaningful output, the Floyd–
Warshall algorithm assumes that there are no negative cycles. Nevertheless, if there are negative cycles,
the Floyd–Warshall algorithm can be used to detect them. The intuition is as follows:
The Floyd–Warshall algorithm iteratively revises path lengths between all pairs of vertices

{\displaystyle (i,j)}, including where

{\displaystyle i=j};

Initially, the length of the path

{\displaystyle (i,i)} is zero;

A path

,
𝑖

{\displaystyle [i,k,\ldots ,i]} can only improve upon this if it has length less than zero, i.e. denotes a
negative cycle;

Thus, after the algorithm,

{\displaystyle (i,i)} will be negative if there exists a negative-length path from

{\displaystyle i} back to

{\displaystyle i}.

Hence, to detect negative cycles using the Floyd–Warshall algorithm, one can inspect the diagonal of the
path matrix, and the presence of a negative number indicates that the graph contains at least one
negative cycle.[9] During the execution of the algorithm, if there is a negative cycle, exponentially large
numbers can appear, as large as

𝑤
𝑚

{\displaystyle \Omega (\cdot 6^{n-1}w_{max})}, where

{\displaystyle w_{max}} is the largest absolute value of a negative edge in the graph. To avoid
overflow/underflow problems one should check for negative numbers on the diagonal of the path matrix
within the inner for loop of the algorithm.[10] Obviously, in an undirected graph a negative edge creates
a negative cycle (i.e., a closed walk) involving its incident vertices. Considering all edges of the above
example graph as undirected, e.g. the vertex sequence 4 – 2 – 4 is a cycle with weight sum −2.

Path reconstruction

The Floyd–Warshall algorithm typically only provides the lengths of the paths between all pairs of
vertices. With simple modifications, it is possible to create a method to reconstruct the actual path
between any two endpoint vertices. While one may be inclined to store the actual path from each vertex
to each other vertex, this is not necessary, and in fact, is very costly in terms of memory. Instead, the
shortest-path tree can be calculated for each node in

{\displaystyle \Theta (|E|)} time using

Θ
(

{\displaystyle \Theta (|V|)} memory to store each tree which allows us to efficiently reconstruct a path
from any two connected vertices.

Pseudocode

Source:[11]

let dist be a

{\displaystyle |V|\times |V|} array of minimum distances initialized to

∞{\displaystyle \infty } (infinity)

let prev be a

×
|

{\displaystyle |V|\times |V|} array of vertex indices initialized to null

procedure FloydWarshallWithPathReconstruction() is

for each edge (u, v) do

dist[u][v] ← w(u, v) // The weight of the edge (u, v)

prev[u][v] ← u

for each vertex v do

dist[v][v] ← 0

prev[v][v] ← v

for k from 1 to |V| do // standard Floyd-Warshall implementation

for i from 1 to |V|

for j from 1 to |V|

if dist[i][j] > dist[i][k] + dist[k][j] then

dist[i][j] ← dist[i][k] + dist[k][j]

prev[i][j] ← prev[k][j]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy