Skip to content

Commit 18cd5a6

Browse files
committed
Add group_color
1 parent 51e2391 commit 18cd5a6

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export(graph_size)
221221
export(graph_unconn_count)
222222
export(group_biconnected_component)
223223
export(group_by)
224+
export(group_color)
224225
export(group_components)
225226
export(group_data)
226227
export(group_edge_betweenness)
@@ -519,6 +520,7 @@ importFrom(igraph,graph_from_adjacency_matrix)
519520
importFrom(igraph,graph_from_data_frame)
520521
importFrom(igraph,graph_from_edgelist)
521522
importFrom(igraph,graph_from_incidence_matrix)
523+
importFrom(igraph,greedy_vertex_coloring)
522524
importFrom(igraph,gsize)
523525
importFrom(igraph,harmonic_centrality)
524526
importFrom(igraph,has_eulerian_cycle)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Added `centrality_harmonic()` + deprecated `centrality_closeness_harmonic()`.
3232
The latter is an interface to netrankr while the former is a more efficient
3333
and flexible igraph implementation.
34+
* Added `group_color()` as an interface to `greedy_vertex_coloring()` in igraph
3435

3536
# tidygraph 1.2.3
3637

R/group.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ group_biconnected_component <- function() {
203203
group <- rep(seq_along(ind), lengths(ind))[order(unlist(ind))][focus_ind(.G(), 'edges')]
204204
desc_enumeration(group)
205205
}
206+
#' @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
207+
#' @importFrom igraph greedy_vertex_coloring
208+
#' @export
209+
group_color <- function() {
210+
expect_nodes()
211+
graph <- .G()
212+
group <- greedy_vertex_coloring(graph)
213+
214+
group <- as.integer(group[focus_ind(.G(), 'nodes')])
215+
desc_enumeration(group)
216+
}
206217

207218

208219
# HELPERS -----------------------------------------------------------------

man/centrality.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/group_graph.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-group.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test_that("grouping returns integer vector", {
2020
#expect_type(get_group(gr, group_optimal()), 'integer')
2121
expect_type(get_group(gr, group_spinglass()), 'integer')
2222
expect_type(get_group(gr, group_walktrap()), 'integer')
23+
expect_type(get_group(gr, group_color()), 'integer')
2324
gr1 <- activate(gr, edges)
2425
expect_type(get_group(gr1, group_biconnected_component()), 'integer')
2526

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

@@ -56,6 +58,7 @@ test_that("grouping requires correct activation", {
5658
#expect_error(get_group(gr1, group_optimal()))
5759
expect_error(get_group(gr1, group_spinglass()))
5860
expect_error(get_group(gr1, group_walktrap()))
61+
expect_error(get_group(gr1, group_color()))
5962

6063
skip_on_os('windows')
6164
expect_error(get_group(gr1, group_leading_eigen()))

0 commit comments

Comments
 (0)