Skip to content

Commit

Permalink
fix: distances(algorithm='johnson') now throws an error if mode != 'o…
Browse files Browse the repository at this point in the history
…ut' for directed graphs
  • Loading branch information
ntamas committed Sep 12, 2022
1 parent ee764fa commit 7a34b43
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 9 additions & 1 deletion R/structural.properties.R
Expand Up @@ -396,6 +396,14 @@ distances <- function(graph, v=V(graph), to=V(graph),
if (!is_igraph(graph)) {
stop("Not a graph object")
}

# make sure that the lower-level function in C gets mode == "out"
# unconditionally when the graph is undirected; this is used for
# the selection of Johnson's algorithm in automatic mode
if (!is_directed(graph)) {
mode <- "out"
}

v <- as.igraph.vs(graph, v)
to <- as.igraph.vs(graph, to)
mode <- igraph.match.arg(mode)
Expand All @@ -420,7 +428,7 @@ distances <- function(graph, v=V(graph), to=V(graph),
weights <- NULL
warning("Unweighted algorithm chosen, weights ignored")
}

on.exit( .Call(C_R_igraph_finalizer) )
res <- .Call(C_R_igraph_shortest_paths, graph, v-1, to-1,
as.numeric(mode), weights, as.numeric(algorithm))
Expand Down
9 changes: 8 additions & 1 deletion tools/stimulus/rinterface.c.in
Expand Up @@ -4191,7 +4191,7 @@ SEXP R_igraph_shortest_paths(SEXP graph, SEXP pvids, SEXP pto,
igraph_matrix_init(&res, 0, 0);
switch (algo) {
case 0: /* automatic */
if (negw && mode==IGRAPH_OUT && GET_LENGTH(pvids)>100) {
if (negw && mode == IGRAPH_OUT && GET_LENGTH(pvids)>100) {
igraph_shortest_paths_johnson(&g, &res, vs, to, pw);
} else if (negw) {
igraph_shortest_paths_bellman_ford(&g, &res, vs, to, pw,
Expand All @@ -4214,6 +4214,13 @@ SEXP R_igraph_shortest_paths(SEXP graph, SEXP pvids, SEXP pto,
(igraph_neimode_t) mode);
break;
case 4: /* johnson */
if (mode != IGRAPH_OUT) {
if (igraph_is_directed(&g)) {
Rf_error("Johnson's algorithm works with mode=\"out\" only for directed graphs");
} else {
Rf_error("Johnson's algorithm works with mode=\"all\" or mode=\"out\" only for undirected graphs");
}
}
igraph_shortest_paths_johnson(&g, &res, vs, to, pw);
break;
}
Expand Down

0 comments on commit 7a34b43

Please sign in to comment.