Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: feedback_vertex_set() finds a minimum feedback vertex set in a graph #1560

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ export(farthest.nodes)
export(farthest_vertices)
export(fastgreedy.community)
export(feedback_arc_set)
export(feedback_vertex_set)
export(fit_hrg)
export(fit_power_law)
export(forest.fire.game)
Expand Down
22 changes: 22 additions & 0 deletions R/aaa-auto.R
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,28 @@ feedback_arc_set_impl <- function(graph, weights=NULL, algo=c("approx_eades", "e
res
}

feedback_vertex_set_impl <- function(graph, weights=NULL, algo=c("exact_ip")) {
# Argument checks
ensure_igraph(graph)
if (is.null(weights) && "weight" %in% vertex_attr_names(graph)) {
weights <- V(graph)$weight
}
if (!is.null(weights) && any(!is.na(weights))) {
weights <- as.numeric(weights)
} else {
weights <- NULL
}
algo <- switch(igraph.match.arg(algo), "exact_ip"=0L)

on.exit( .Call(R_igraph_finalizer) )
# Function call
res <- .Call(R_igraph_feedback_vertex_set, graph, weights, algo)
if (igraph_opt("return.vs.es")) {
res <- create_vs(graph, res)
}
res
}

is_loop_impl <- function(graph, eids=E(graph)) {
# Argument checks
ensure_igraph(graph)
Expand Down
30 changes: 30 additions & 0 deletions R/structural.properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,36 @@ topo_sort <- function(graph, mode = c("out", "all", "in")) {
#' @cdocs igraph_feedback_arc_set
feedback_arc_set <- feedback_arc_set_impl

#' Finding a feedback vertex set in a graph
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' A feedback vertex set of a graph is a subset of vertices whose removal breaks
#' all cycles in the graph. Finding a _minimum_ feedback vertex set is an
#' NP-complete problem, both on directed and undirected graphs.
#'
#' @param graph The input graph
#' @param weights Potential vertex weights. If the graph has a vertex
#' attribute called \sQuote{`weight`}, and this argument is
#' `NULL`, then the vertex attribute is used automatically. The goal of
#' the feedback vertex set problem is to find a feedback vertex set with
#' the smallest total weight.
#' @param algo Specifies the algorithm to use. Currently, \dQuote{`exact_ip`},
#' which solves the feedback vertex set problem with an exact integer
#' programming approach, is the only option.
#' @return A vertex sequence (by default, but see the `return.vs.es` option
#' of [igraph_options()]) containing the feedback vertex set.
#' @keywords graphs
#' @family structural.properties
#' @family cycles
#' @export
#' @examples
#'
#' g <- make_lattice(c(3,3))
#' feedback_vertex_set(g)
feedback_vertex_set <- feedback_vertex_set_impl

#' Girth of a graph
#'
#' The girth of a graph is the length of the shortest circle in it.
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.

2 changes: 2 additions & 0 deletions man/feedback_arc_set.Rd

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

74 changes: 74 additions & 0 deletions man/feedback_vertex_set.Rd

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

2 changes: 2 additions & 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/has_eulerian_path.Rd

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

2 changes: 2 additions & 0 deletions man/is_acyclic.Rd

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

2 changes: 2 additions & 0 deletions man/is_dag.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/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/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.

2 changes: 2 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ extern SEXP R_igraph_extended_chordal_ring(SEXP, SEXP, SEXP);
extern SEXP R_igraph_famous(SEXP);
extern SEXP R_igraph_farthest_points(SEXP, SEXP, SEXP, SEXP);
extern SEXP R_igraph_feedback_arc_set(SEXP, SEXP, SEXP);
extern SEXP R_igraph_feedback_vertex_set(SEXP, SEXP, SEXP);
extern SEXP R_igraph_finalizer(void);
extern SEXP R_igraph_find_cycle(SEXP, SEXP);
extern SEXP R_igraph_forest_fire_game(SEXP, SEXP, SEXP, SEXP, SEXP);
Expand Down Expand Up @@ -616,6 +617,7 @@ static const R_CallMethodDef CallEntries[] = {
{"R_igraph_famous", (DL_FUNC) &R_igraph_famous, 1},
{"R_igraph_farthest_points", (DL_FUNC) &R_igraph_farthest_points, 4},
{"R_igraph_feedback_arc_set", (DL_FUNC) &R_igraph_feedback_arc_set, 3},
{"R_igraph_feedback_vertex_set", (DL_FUNC) &R_igraph_feedback_vertex_set, 3},
{"R_igraph_finalizer", (DL_FUNC) &R_igraph_finalizer, 0},
{"R_igraph_find_cycle", (DL_FUNC) &R_igraph_find_cycle, 2},
{"R_igraph_forest_fire_game", (DL_FUNC) &R_igraph_forest_fire_game, 5},
Expand Down
Loading
Loading