Skip to content

Commit

Permalink
feat: distances() now supports the Floyd-Warshall algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
szhorvat committed Feb 1, 2024
1 parent 1c4a301 commit 31bfea4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
11 changes: 7 additions & 4 deletions R/structural.properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,10 @@ degree_distribution <- function(graph, cumulative = FALSE, ...) {
#' are breadth-first search (\sQuote{`unweighted`}), this only works for
#' unweighted graphs; the Dijkstra algorithm (\sQuote{`dijkstra`}), this
#' works for graphs with non-negative edge weights; the Bellman-Ford algorithm
#' (\sQuote{`bellman-ford`}), and Johnson's algorithm
#' (\sQuote{`johnson`}). The latter two algorithms work with arbitrary
#' (\sQuote{`bellman-ford`}); Johnson's algorithm
#' (\sQuote{`johnson`}); and a faster version of the Floyd-Warshall algorithm
#' with expected quadratic running time (\sQuote{`floyd-warshall`}). The latter
#' three algorithms work with arbitrary
#' edge weights, but (naturally) only for graphs that don't have a negative
#' cycle. Note that a negative-weight edge in an undirected graph implies
#' such a cycle. Johnson's algorithm performs better than the Bellman-Ford
Expand Down Expand Up @@ -833,7 +835,7 @@ distances <- function(graph, v = V(graph), to = V(graph),
weights = NULL,
algorithm = c(
"automatic", "unweighted", "dijkstra",
"bellman-ford", "johnson"
"bellman-ford", "johnson", "floyd-warshall"
)) {
ensure_igraph(graph)

Expand All @@ -858,7 +860,8 @@ distances <- function(graph, v = V(graph), to = V(graph),
"unweighted" = 1,
"dijkstra" = 2,
"bellman-ford" = 3,
"johnson" = 4
"johnson" = 4,
"floyd-warshall" = 5
)

if (is.null(weights)) {
Expand Down
9 changes: 6 additions & 3 deletions man/distances.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/rinterface_extra.c
Original file line number Diff line number Diff line change
Expand Up @@ -4264,6 +4264,9 @@ SEXP R_igraph_shortest_paths(SEXP graph, SEXP pvids, SEXP pto,
case 4: /* johnson */
IGRAPH_R_CHECK(distances_johnson(&g, &res, vs, to, pw, mode, negw));
break;
case 5: /* floyd-warshall */
IGRAPH_R_CHECK(igraph_distances_floyd_warshall(&g, &res, vs, to, pw, mode, IGRAPH_FLOYD_WARSHALL_AUTOMATIC));
break;
}
PROTECT(result=R_igraph_matrix_to_SEXP(&res));
igraph_matrix_destroy(&res);
Expand Down

0 comments on commit 31bfea4

Please sign in to comment.