Skip to content

Commit

Permalink
Add group_color
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Nov 29, 2023
1 parent 51e2391 commit 18cd5a6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export(graph_size)
export(graph_unconn_count)
export(group_biconnected_component)
export(group_by)
export(group_color)
export(group_components)
export(group_data)
export(group_edge_betweenness)
Expand Down Expand Up @@ -519,6 +520,7 @@ importFrom(igraph,graph_from_adjacency_matrix)
importFrom(igraph,graph_from_data_frame)
importFrom(igraph,graph_from_edgelist)
importFrom(igraph,graph_from_incidence_matrix)
importFrom(igraph,greedy_vertex_coloring)
importFrom(igraph,gsize)
importFrom(igraph,harmonic_centrality)
importFrom(igraph,has_eulerian_cycle)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* Added `centrality_harmonic()` + deprecated `centrality_closeness_harmonic()`.
The latter is an interface to netrankr while the former is a more efficient
and flexible igraph implementation.
* Added `group_color()` as an interface to `greedy_vertex_coloring()` in igraph

# tidygraph 1.2.3

Expand Down
11 changes: 11 additions & 0 deletions R/group.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ group_biconnected_component <- function() {
group <- rep(seq_along(ind), lengths(ind))[order(unlist(ind))][focus_ind(.G(), 'edges')]
desc_enumeration(group)
}
#' @describeIn group_graph Groups nodes by their color using [igraph::greedy_vertex_coloring()]. Be aware that this is not a clustering algorithm as coloring specifically provide a color to each node so that no neighbors have the same color
#' @importFrom igraph greedy_vertex_coloring
#' @export
group_color <- function() {
expect_nodes()
graph <- .G()
group <- greedy_vertex_coloring(graph)

group <- as.integer(group[focus_ind(.G(), 'nodes')])
desc_enumeration(group)
}


# HELPERS -----------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion man/centrality.Rd

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

5 changes: 5 additions & 0 deletions man/group_graph.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/test-group.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test_that("grouping returns integer vector", {
#expect_type(get_group(gr, group_optimal()), 'integer')
expect_type(get_group(gr, group_spinglass()), 'integer')
expect_type(get_group(gr, group_walktrap()), 'integer')
expect_type(get_group(gr, group_color()), 'integer')
gr1 <- activate(gr, edges)
expect_type(get_group(gr1, group_biconnected_component()), 'integer')

Expand All @@ -37,6 +38,7 @@ test_that("grouping returns integer of correct length", {
#expect_length(get_group(gr, group_optimal()), igraph::gorder(gr))
expect_length(get_group(gr, group_spinglass()), igraph::gorder(gr))
expect_length(get_group(gr, group_walktrap()), igraph::gorder(gr))
expect_length(get_group(gr, group_color()), igraph::gorder(gr))
gr1 <- activate(gr, edges)
expect_length(get_group(gr1, group_biconnected_component()), igraph::gsize(gr1))

Expand All @@ -56,6 +58,7 @@ test_that("grouping requires correct activation", {
#expect_error(get_group(gr1, group_optimal()))
expect_error(get_group(gr1, group_spinglass()))
expect_error(get_group(gr1, group_walktrap()))
expect_error(get_group(gr1, group_color()))

skip_on_os('windows')
expect_error(get_group(gr1, group_leading_eigen()))
Expand Down

0 comments on commit 18cd5a6

Please sign in to comment.