Learn R Programming

fastmatrix (version 0.6-2)

floyd: Find the shortest paths in a directed graph

Description

The Floyd-Warshall algorithm finds all shortest paths (if exist) in a directed graph.

Usage

floyd(x)

Value

Returns a list with final costs and the shortest path between two nodes.

Arguments

x

adjacency matrix of a directed graph. \((i,j)\)-entry represents the weight (cost) of the edge from \(i\) to \(j\) if one exists and \(\infty\) otherwise.

References

Floyd, R.W. (1962). Algorithm 97: Shortest Path. Communications of the ACM 5 (6), 345.

Examples

Run this code
x <- matrix(c(0,3,Inf,5,2,0,Inf,4,Inf,1,0,Inf,Inf,Inf,2,0), nrow = 4, 
            ncol = 4, byrow = TRUE)
z <- floyd(x)
z

Run the code above in your browser using DataLab