Skip to content

Commit 4bfea65

Browse files
Copilotkrlmlr
andauthored
feat: Add count_loops() to R interface (#2414)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: krlmlr <[email protected]> Co-authored-by: Kirill Müller <[email protected]>
1 parent 00bf05a commit 4bfea65

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ export(count.multiple)
273273
export(count_automorphisms)
274274
export(count_components)
275275
export(count_isomorphisms)
276+
export(count_loops)
276277
export(count_max_cliques)
277278
export(count_motifs)
278279
export(count_multiple)

R/structural-properties.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,6 +2475,8 @@ girth <- function(graph, circle = TRUE) {
24752475
#'
24762476
#' `which_loop()` decides whether the edges of the graph are loop edges.
24772477
#'
2478+
#' `count_loops()` counts the total number of loop edges in the graph.
2479+
#'
24782480
#' `any_multiple()` decides whether the graph has any multiple edges.
24792481
#'
24802482
#' `which_multiple()` decides whether the edges of the graph are multiple
@@ -2496,6 +2498,7 @@ girth <- function(graph, circle = TRUE) {
24962498
#' all edges in the graph.
24972499
#' @return `any_loop()` and `any_multiple()` return a logical scalar.
24982500
#' `which_loop()` and `which_multiple()` return a logical vector.
2501+
#' `count_loops()` returns a numeric scalar with the total number of loop edges.
24992502
#' `count_multiple()` returns a numeric vector.
25002503
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
25012504
#' @seealso [simplify()] to eliminate loop and multiple edges.
@@ -2508,6 +2511,7 @@ girth <- function(graph, circle = TRUE) {
25082511
#' g <- make_graph(c(1, 1, 2, 2, 3, 3, 4, 5))
25092512
#' any_loop(g)
25102513
#' which_loop(g)
2514+
#' count_loops(g)
25112515
#'
25122516
#' # Multiple edges
25132517
#' g <- sample_pa(10, m = 3, algorithm = "bag")
@@ -2569,6 +2573,14 @@ any_loop <- function(graph) {
25692573
graph = graph
25702574
)
25712575
}
2576+
#' @rdname which_multiple
2577+
#' @export
2578+
#' @cdocs igraph_count_loops
2579+
count_loops <- function(graph) {
2580+
count_loops_impl(
2581+
graph = graph
2582+
)
2583+
}
25722584

25732585

25742586
#' Breadth-first search

man/which_multiple.Rd

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

tests/testthat/test-structural-properties.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,31 @@ test_that("any_multiple(), count_multiple(), which_multiple() works", {
963963
)
964964
})
965965

966+
test_that("any_loop(), which_loop(), count_loops() works", {
967+
# Graph with loops
968+
g <- make_graph(c(1, 1, 2, 2, 3, 3, 4, 5))
969+
expect_true(any_loop(g))
970+
expect_equal(which_loop(g), c(TRUE, TRUE, TRUE, FALSE))
971+
expect_equal(count_loops(g), 3)
972+
973+
# Graph without loops
974+
g2 <- make_graph(c(1, 2, 2, 3, 3, 4))
975+
expect_false(any_loop(g2))
976+
expect_equal(which_loop(g2), c(FALSE, FALSE, FALSE))
977+
expect_equal(count_loops(g2), 0)
978+
979+
# Empty graph
980+
g3 <- make_empty_graph(n = 5)
981+
expect_false(any_loop(g3))
982+
expect_equal(count_loops(g3), 0)
983+
984+
# Graph with only loops
985+
g4 <- make_graph(c(1, 1, 2, 2, 3, 3))
986+
expect_true(any_loop(g4))
987+
expect_equal(which_loop(g4), c(TRUE, TRUE, TRUE))
988+
expect_equal(count_loops(g4), 3)
989+
})
990+
966991
test_that("edge_density works", {
967992
g <- sample_gnp(50, 4 / 50)
968993
gd <- edge_density(g)

0 commit comments

Comments
 (0)