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: New voronoi_cells() to compute the Voronoi partitioning of a graph #1173

Merged
merged 2 commits into from
Feb 20, 2024
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ export(vertex_attr_names)
export(vertex_connectivity)
export(vertex_disjoint_paths)
export(vertices)
export(voronoi_cells)
export(walktrap.community)
export(watts.strogatz.game)
export(weighted_clique_num)
Expand Down
3 changes: 2 additions & 1 deletion R/aaa-auto.R
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,9 @@ distances_floyd_warshall_impl <- function(graph, from=V(graph), to=V(graph), wei
res
}

voronoi_impl <- function(graph, generators, weights=NULL, mode=c("out", "in", "all", "total"), tiebreaker=c("random", "first", "last")) {
voronoi_impl <- function(graph, generators, ..., weights=NULL, mode=c("out", "in", "all", "total"), tiebreaker=c("random", "first", "last")) {
# Argument checks
check_dots_empty()
ensure_igraph(graph)
generators <- as_igraph_vs(graph, generators)
if (is.null(weights) && "weight" %in% edge_attr_names(graph)) {
Expand Down
41 changes: 41 additions & 0 deletions R/community.R
Original file line number Diff line number Diff line change
Expand Up @@ -2782,3 +2782,44 @@ communities <- groups.communities
#' @export
#' @family functions for manipulating graph structure
contract <- contract_vertices_impl


#' Voronoi partitioning of a graph
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' This function partitions the vertices of a graph based on a set of generator
#' vertices. Each vertex is assigned to the generator vertex from (or to) which
#' it is closest.
#'
#' [groups()] may be used on the output of this function.
#'
#' @param graph The graph to partition into Voronoi cells.
#' @param generators The generator vertices of the Voronoi cells.
#' @param mode Character string. In directed graphs, whether to compute
#' distances from generator vertices to other vertices (`"out"`), to
#' generator vertices from other vertices (`"in"`), or ignore edge
#' directions entirely (`"all"`). Ignored in undirected graphs.
#' @param tiebreaker Character string that specifies what to do when a vertex
#' is at the same distance from multiple generators. `"random"` assigns
#' a minimal-distance generator randomly, `"first"` takes the first one,
#' and `"last"` takes the last one.
#' @inheritParams distances
#' @inheritParams rlang::args_dots_empty
#' @return A named list with two components:
#' \item{membership}{numeric vector giving the cluster id to which each vertex
#' belongs.}
#' \item{distances}{numeric vector giving the distance of each vertex from its
#' generator}
#' @seealso [distances()]
#' @examples
#'
#' g <- make_lattice(c(10,10))
#' clu <- voronoi_cells(g, c(25, 43, 67))
#' groups(clu)
#' plot(g, vertex.color=clu$membership)
#'
#' @export
#' @family community
voronoi_cells <- voronoi_impl
3 changes: 2 additions & 1 deletion man/as_membership.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_edge_betweenness.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_fast_greedy.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_fluid_communities.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_infomap.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_label_prop.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_leading_eigen.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_leiden.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_louvain.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_optimal.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_spinglass.Rd

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

3 changes: 2 additions & 1 deletion man/cluster_walktrap.Rd

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

3 changes: 2 additions & 1 deletion man/communities.Rd

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

3 changes: 2 additions & 1 deletion man/compare.Rd

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

3 changes: 2 additions & 1 deletion man/groups.Rd

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

3 changes: 2 additions & 1 deletion man/make_clusters.Rd

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

3 changes: 2 additions & 1 deletion man/modularity.igraph.Rd

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

3 changes: 2 additions & 1 deletion man/plot_dendrogram.communities.Rd

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

3 changes: 2 additions & 1 deletion man/split_join_distance.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/voronoi_cells.Rd

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

11 changes: 11 additions & 0 deletions tests/testthat/test-voronoi.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("voronoi works", {
res <- voronoi_cells(make_ring(10), c(1, 6))
expect_equal(res$membership, c(0, 0, 0, 1, 1, 1, 1, 1, 0, 0))
expect_equal(res$distances, c(0, 1, 2, 2, 1, 0, 1, 2, 2, 1))
})

test_that("voronoi works with weights", {
res <- voronoi_cells(make_ring(10), c(1, 6), weights = 1:10)
expect_equal(res$membership, c(0, 0, 0, 0, 1, 1, 1, 1, 0, 0))
expect_equal(res$distances, c(0, 1, 3, 6, 5, 0, 6, 13, 19, 10))
})
1 change: 1 addition & 0 deletions tools/stimulus/functions-R.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ igraph_distances_johnson:
igraph_distances_floyd_warshall:

igraph_voronoi:
FIRST_KW_PARAM: weights

igraph_get_all_simple_paths:
IGNORE: RR
Expand Down
Loading