-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update igraph use; interrupt depth-first search in n.comp.nb, add igr…
…aph argument to n.comp.nb
- Loading branch information
Showing
5 changed files
with
51 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,33 @@ | ||
# Copyright 2001 by Nicholas Lewin-Koh | ||
# Copyright 2001 by Nicholas Lewin-Koh, igraph added RSB 2024 | ||
# | ||
|
||
|
||
n.comp.nb <- function(nb.obj){ | ||
n.comp.nb <- function(nb.obj, igraph=FALSE){ | ||
if(!inherits(nb.obj,"nb"))stop("not a neighbours list") | ||
nb.obj <- make.sym.nb(nb.obj) | ||
comp <- rep(0,length(nb.obj)) | ||
comp <- .Call("g_components", nb.obj, as.integer(comp), PACKAGE="spdep") | ||
answ <- list(nc=length(unique(comp)), comp.id=comp) | ||
stopifnot(is.logical(igraph)) | ||
stopifnot(length(igraph) == 1L) | ||
nb.sym <- is.symmetric.nb(nb.obj) | ||
if (igraph) { | ||
if (!requireNamespace("igraph", quietly=TRUE)) { | ||
igraph <- !igraph | ||
warning("igraph not available, set FALSE") | ||
} | ||
} | ||
if (!igraph) { | ||
if (!nb.sym) nb.obj <- make.sym.nb(nb.obj) | ||
comp <- rep(0,length(nb.obj)) | ||
comp <- .Call("g_components", nb.obj, as.integer(comp), PACKAGE="spdep") | ||
answ <- list(nc=length(unique(comp)), comp.id=comp) | ||
} else { | ||
stopifnot(requireNamespace("igraph", quietly=TRUE)) | ||
stopifnot(requireNamespace("spatialreg", quietly=TRUE)) | ||
B <- as(nb2listw(nb.obj, style="B", zero.policy=TRUE), "CsparseMatrix") | ||
|
||
g1 <- igraph::graph_from_adjacency_matrix(B, | ||
mode=ifelse(nb.sym, "undirected", "directed")) | ||
c1 <- igraph::components(g1, mode="weak") | ||
answ <- list(nc=c1$no, comp.id=c1$membership) | ||
} | ||
answ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters