Skip to content
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
46 changes: 21 additions & 25 deletions R/cliques.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,11 @@ clique.number <- function(graph) {
#' vectors of vertex IDs. Each list element is a clique, i.e. a vertex sequence
#' of class [igraph.vs][V].
#'
#' `max_cliques()` returns `NULL`, invisibly, if its `file`
#' argument is not `NULL`. The output is written to the specified file in
#' `max_cliques()` returns:
#' - When `file` is `NULL` and `callback` is `NULL`, a list of vertex sequences.
#' - When `callback` is not `NULL`, the callback function is called for each click,

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.

clique?

#' then `max_cliques()` returns `NULL` invisibly.
#' - When `file` is not `NULL`, the output is written to the specified file in
#' this case.
#'
#' `clique_num()` and `count_max_cliques()` return an integer
Expand Down Expand Up @@ -286,16 +289,18 @@ largest_cliques <- function(graph) {
}

#' @rdname cliques
#' @param subset If not `NULL`, then it must be a vector of vertex IDs,
#' @param subset A vector of vertex IDs,
#' numeric or symbolic if the graph is named. The algorithm is run from these
#' vertices only, so only a subset of all maximal cliques is returned. See the
#' Eppstein paper for details. This argument makes it possible to easily
#' parallelize the finding of maximal cliques.
#' @param file If not `NULL`, then it must be a file name, i.e. a
#' character scalar. The output of the algorithm is written to this file. (If
#' it exists, then it will be overwritten.) Each clique will be a separate line
#' in the file, given with the numeric IDs of its vertices, separated by
#' whitespace.
#' parallelize the finding of maximal cliques. Default: `NULL`, the algorithm
#' is run from all vertices.
#' @param file A file name, i.e. a character scalar.
#' The output of the algorithm is written to this file.
#' (If it exists, then it will be overwritten.)
#' Each clique will be a separate line in the file,
#' given with the numeric IDs of its vertices, separated by whitespace.
#' Default: `NULL`, the function returns a list of vertex sequences.
#' @export
max_cliques <- function(
graph,
Expand All @@ -311,30 +316,21 @@ max_cliques <- function(

# Handle file and subset modes (original functionality)
if (!is.null(file)) {
if (
!is.character(file) ||
length(grep("://", file, fixed = TRUE)) > 0 ||
length(grep("~", file, fixed = TRUE)) > 0
) {
tmpfile <- TRUE

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this tmpfile was not returned!

origfile <- file
file <- tempfile()
} else {
tmpfile <- FALSE
if (!is.character(file)) {
cli::cli_abort("{.arg file} must be a file path, not a connection.")
}
if (grepl("://", file, fixed = TRUE)) {
cli::cli_abort("{.arg file} must be a file path, not a URL.")
}
on.exit(.Call(Rx_igraph_finalizer))
res <- .Call(
.Call(
Rx_igraph_maximal_cliques_file,
graph,
subset,
file,
path.expand(file),
as.numeric(min %||% 0),
as.numeric(max %||% 0)
)
if (tmpfile) {
buffer <- read.graph.toraw(file)
write.graph.fromraw(buffer, origfile)
}
return(invisible(NULL))
}

Expand Down
25 changes: 16 additions & 9 deletions man/cliques.Rd

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

16 changes: 9 additions & 7 deletions man/maximal.cliques.Rd

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

5 changes: 3 additions & 2 deletions man/maximal.cliques.count.Rd

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

31 changes: 7 additions & 24 deletions src/rinterface_extra.c
Original file line number Diff line number Diff line change
Expand Up @@ -5682,15 +5682,10 @@ SEXP Rx_igraph_maximal_cliques(SEXP graph, SEXP psubset,
SEXP Rx_igraph_maximal_cliques_file(SEXP graph, SEXP psubset, SEXP file,
SEXP pminsize, SEXP pmaxsize) {
igraph_t g;
igraph_integer_t minsize=(igraph_integer_t) REAL(pminsize)[0];
igraph_integer_t maxsize=(igraph_integer_t) REAL(pmaxsize)[0];
igraph_integer_t minsize = (igraph_integer_t) REAL(pminsize)[0];
igraph_integer_t maxsize = (igraph_integer_t) REAL(pmaxsize)[0];
igraph_vector_int_t subset;
SEXP result;
FILE *stream;
#if HAVE_OPEN_MEMSTREAM == 1
char *bp;
size_t size;
#endif

Rz_SEXP_to_igraph(graph, &g);
if (!Rf_isNull(psubset)) {
Expand All @@ -5699,29 +5694,17 @@ SEXP Rx_igraph_maximal_cliques_file(SEXP graph, SEXP psubset, SEXP file,
IGRAPH_R_CHECK(igraph_vector_int_init(&subset, 0));
}
IGRAPH_FINALLY_PV(igraph_vector_int_destroy, &subset);
#if HAVE_OPEN_MEMSTREAM == 1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

apparently not used

stream=open_memstream(&bp, &size);
#else
stream=fopen(CHAR(STRING_ELT(file, 0)), "w");
#endif
if (stream==0) { igraph_error("Cannot write cliques", __FILE__,
__LINE__, IGRAPH_EFILE); }
stream = fopen(CHAR(STRING_ELT(file, 0)), "w");
if (stream == 0) {
igraph_error("Cannot write cliques", __FILE__, __LINE__, IGRAPH_EFILE);
}
igraph_maximal_cliques_subset(&g, Rf_isNull(psubset) ? 0 : &subset,
/*ptr=*/ 0, /*no=*/ 0, /*file=*/ stream,
minsize, maxsize);
fclose(stream);
igraph_vector_int_destroy(&subset);
IGRAPH_FINALLY_CLEAN(1);
#if HAVE_OPEN_MEMSTREAM == 1
PROTECT(result=Rf_allocVector(RAWSXP, size));
memcpy(RAW(result), bp, sizeof(char)*size);
free(bp);
#else
PROTECT(result=NEW_NUMERIC(0));
#endif

UNPROTECT(1);
return result;
return R_NilValue;
}

/* TOP-LEVEL: called from R via .Call; must use IGRAPH_R_CHECK */
Expand Down
Loading