Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ export(isomorphisms)
export(ivs)
export(ivs_size)
export(k.regular.game)
export(k_shortest_paths)
export(kautz_graph)
export(keeping_degseq)
export(knn)
Expand Down Expand Up @@ -914,6 +915,7 @@ importFrom(pkgconfig,set_config)
importFrom(pkgconfig,set_config_in)
importFrom(rlang,.data)
importFrom(rlang,.env)
importFrom(rlang,check_dots_empty)
importFrom(rlang,check_installed)
importFrom(rlang,inject)
importFrom(rlang,warn)
Expand Down
9 changes: 5 additions & 4 deletions R/aaa-auto.R
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,9 @@ voronoi_impl <- function(graph, generators, weights=NULL, mode=c("out", "in", "a
res
}

get_k_shortest_paths_impl <- function(graph, weights=NULL, k, from, to, mode=c("out", "in", "all", "total")) {
get_k_shortest_paths_impl <- function(graph, from, to, ..., k, weights=NULL, mode=c("out", "in", "all", "total")) {
# Argument checks
check_dots_empty()
ensure_igraph(graph)
if (is.null(weights) && "weight" %in% edge_attr_names(graph)) {
weights <- E(graph)$weight
Expand All @@ -822,10 +823,10 @@ get_k_shortest_paths_impl <- function(graph, weights=NULL, k, from, to, mode=c("
# Function call
res <- .Call(R_igraph_get_k_shortest_paths, graph, weights, k, from-1, to-1, mode)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we have -1 here but no inverse correction for the output. Is this expected?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the code in front of me right now, but I remember that there aren't any clear guidelines on where to do the +1/-1 adjustments for vertex/edge indices and sometimes it's done in the output conversion of the result. For instance, there is R_igraph_vector_int_to_SEXP to convert an igraph_vector_int_t back to R without adjustment and R_igraph_vector_int_to_SEXPp1 to convert it back with adjustments. (The p1 suffix stands for "plus one").

Ideally, there should be guidelines on where the adjustment is done. If it is easier to do in R and there are no performance implications (like an unnecessary copying of the vector), it should probably be done in R and we could get rid of the p1 functions in rinterface_extra.c.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are performance implications, ideally we'd convert everything in C.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dreaming big: If we could get views that cast to int and perform the "minus one" on the fly, in the C core...

if (igraph_opt("return.vs.es")) {
res$vertex.paths <- lapply(res$vertex.paths, unsafe_create_vs, graph = graph, verts = V(graph))
res$vpaths <- lapply(res$vpaths, unsafe_create_vs, graph = graph, verts = V(graph))
}
if (igraph_opt("return.vs.es")) {
res$edge.paths <- lapply(res$edge.paths, unsafe_create_es, graph = graph, es = E(graph))
res$epaths <- lapply(res$epaths, unsafe_create_es, graph = graph, es = E(graph))
}
res
}
Expand Down Expand Up @@ -1196,7 +1197,7 @@ ecc_impl <- function(graph, eids=E(graph), k=3, offset=FALSE, normalize=TRUE) {
res
}

reciprocity_impl <- function(graph, ignore.loops=TRUE, mode=c('default', 'ratio')) {
reciprocity_impl <- function(graph, ignore.loops=TRUE, mode=c("default", "ratio")) {
# Argument checks
ensure_igraph(graph)
ignore.loops <- as.logical(ignore.loops)
Expand Down
1 change: 1 addition & 0 deletions R/igraph-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @importFrom lifecycle deprecated
#' @importFrom magrittr %>%
#' @importFrom rlang .data .env
#' @importFrom rlang check_dots_empty
#' @importFrom rlang check_installed
#' @importFrom rlang inject
#' @importFrom rlang warn
Expand Down
28 changes: 26 additions & 2 deletions R/structural.properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,32 @@ all_shortest_paths <- function(graph, from,
res
}

#' Find the \eqn{k} shortest paths between two vertices
#'
#' Finds the \eqn{k} shortest paths between the given source and target
#' vertex in order of increasing length. Currently this function uses
#' Yen's algorithm.
#'
#' @param graph The input graph.
#' @param from The source vertex of the shortest paths.
#' @param to The target vertex of the shortest paths.
#' @param k The number of paths to find. They will be returned in order of
#' increasing length.
#' @inheritParams shortest_paths
#' @return A named list with two components is returned:
#' \item{vpaths}{The list of \eqn{k} shortest paths in terms of vertices}
#' \item{epaths}{The list of \eqn{k} shortest paths in terms of edges}
#' @references Yen, Jin Y.:
#' An algorithm for finding shortest routes from all source nodes to a given
#' destination in general networks.
#' Quarterly of Applied Mathematics. 27 (4): 526–530. (1970)
#' <https://doi.org/10.1090/qam/253822>
#' @export
#' @family structural.properties
#' @seealso [shortest_paths()], [all_shortest_paths()]
#' @keywords graphs
k_shortest_paths <- get_k_shortest_paths_impl

#' In- or out- component of a vertex
#'
#' Finds all vertices reachable from a given vertex, or the opposite: all
Expand Down Expand Up @@ -1103,8 +1129,6 @@ subcomponent <- function(graph, v, mode = c("all", "out", "in")) {
res
}



#' Subgraph of a graph
#'
#' `subgraph()` creates a subgraph of a graph, containing only the specified
Expand Down
1 change: 1 addition & 0 deletions man/bfs.Rd

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

1 change: 1 addition & 0 deletions man/components.Rd

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

1 change: 1 addition & 0 deletions man/constraint.Rd

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

1 change: 1 addition & 0 deletions man/coreness.Rd

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

1 change: 1 addition & 0 deletions man/degree.Rd

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

1 change: 1 addition & 0 deletions man/dfs.Rd

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

1 change: 1 addition & 0 deletions man/distances.Rd

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

1 change: 1 addition & 0 deletions man/edge_density.Rd

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

1 change: 1 addition & 0 deletions man/ego.Rd

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

1 change: 1 addition & 0 deletions man/feedback_arc_set.Rd

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

1 change: 1 addition & 0 deletions man/girth.Rd

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

1 change: 1 addition & 0 deletions man/is_acyclic.Rd

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

1 change: 1 addition & 0 deletions man/is_dag.Rd

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

86 changes: 86 additions & 0 deletions man/k_shortest_paths.Rd

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

1 change: 1 addition & 0 deletions man/knn.Rd

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

1 change: 1 addition & 0 deletions man/laplacian_matrix.Rd

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

1 change: 1 addition & 0 deletions man/matching.Rd

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

1 change: 1 addition & 0 deletions man/reciprocity.Rd

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

1 change: 1 addition & 0 deletions man/subcomponent.Rd

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

1 change: 1 addition & 0 deletions man/subgraph.Rd

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

1 change: 1 addition & 0 deletions man/topo_sort.Rd

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

1 change: 1 addition & 0 deletions man/transitivity.Rd

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

1 change: 1 addition & 0 deletions man/unfold_tree.Rd

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

1 change: 1 addition & 0 deletions man/which_multiple.Rd

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

1 change: 1 addition & 0 deletions man/which_mutual.Rd

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

4 changes: 2 additions & 2 deletions src/rinterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -2492,8 +2492,8 @@ SEXP R_igraph_get_k_shortest_paths(SEXP graph, SEXP weights, SEXP k, SEXP from,
IGRAPH_FINALLY_CLEAN(1);
SET_VECTOR_ELT(r_result, 0, vertex_paths);
SET_VECTOR_ELT(r_result, 1, edge_paths);
SET_STRING_ELT(r_names, 0, Rf_mkChar("vertex_paths"));
SET_STRING_ELT(r_names, 1, Rf_mkChar("edge_paths"));
SET_STRING_ELT(r_names, 0, Rf_mkChar("vpaths"));
SET_STRING_ELT(r_names, 1, Rf_mkChar("epaths"));
SET_NAMES(r_result, r_names);
UNPROTECT(3);

Expand Down
Loading