floyd: Find the shortest paths in a directed graph

View source: R/floyd.R

floydR Documentation

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)

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.

Value

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

References

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

Examples

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

fastmatrix documentation built on Nov. 5, 2025, 6:21 p.m.