Skip to content

Commit 1389df7

Browse files
Copilotkrlmlr
andauthored
feat: Add make_full_multipartite() and make_turan() graph constructors (#2406)
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 4bfea65 commit 1389df7

21 files changed

+389
-13
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ export(from_prufer)
369369
export(full_bipartite_graph)
370370
export(full_citation_graph)
371371
export(full_graph)
372+
export(full_multipartite)
372373
export(get.adjacency)
373374
export(get.adjedgelist)
374375
export(get.adjlist)
@@ -662,6 +663,7 @@ export(make_from_prufer)
662663
export(make_full_bipartite_graph)
663664
export(make_full_citation_graph)
664665
export(make_full_graph)
666+
export(make_full_multipartite)
665667
export(make_graph)
666668
export(make_kautz_graph)
667669
export(make_lattice)
@@ -670,6 +672,7 @@ export(make_neighborhood_graph)
670672
export(make_ring)
671673
export(make_star)
672674
export(make_tree)
675+
export(make_turan)
673676
export(make_undirected_graph)
674677
export(match_vertices)
675678
export(max_bipartite_match)
@@ -876,6 +879,7 @@ export(tree)
876879
export(triad.census)
877880
export(triad_census)
878881
export(triangles)
882+
export(turan)
879883
export(undirected_graph)
880884
export(unfold.tree)
881885
export(unfold_tree)

R/aaa-auto.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,12 @@ full_multipartite_impl <- function(
755755
mode
756756
)
757757

758+
if (igraph_opt("add.params")) {
759+
res$name <- 'Full multipartite graph'
760+
res$n <- n
761+
res$mode <- mode
762+
}
763+
758764
res
759765
}
760766

@@ -888,6 +894,12 @@ turan_impl <- function(
888894
r
889895
)
890896

897+
if (igraph_opt("add.params")) {
898+
res$name <- 'Turan graph'
899+
res$n <- n
900+
res$r <- r
901+
}
902+
891903
res
892904
}
893905

R/make.R

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,117 @@ bipartite_graph <- function(...) constructor_spec(make_bipartite_graph, ...)
26172617

26182618
## -----------------------------------------------------------------
26192619

2620+
#' Create a full multipartite graph
2621+
#'
2622+
#' A multipartite graph contains multiple types of vertices and connections
2623+
#' are only possible between vertices of different types. This function
2624+
#' creates a complete multipartite graph where all possible edges between
2625+
#' different partitions are present.
2626+
#'
2627+
#' @param n A numeric vector giving the number of vertices in each partition.
2628+
#' @param directed Logical scalar, whether to create a directed graph.
2629+
#' @param mode Character scalar, the type of connections for directed graphs.
2630+
#' If `"out"`, then edges point from vertices of partitions with lower
2631+
#' indices to partitions with higher indices; if `"in"`, then the opposite
2632+
#' direction is realized; `"all"` creates mutual edges. This parameter is
2633+
#' ignored for undirected graphs.
2634+
#' @return An igraph graph with a vertex attribute `type` storing the
2635+
#' partition index of each vertex. Partition indices start from 1.
2636+
#'
2637+
#' @family deterministic constructors
2638+
#' @export
2639+
#' @examples
2640+
#' # Create a multipartite graph with partitions of size 2, 3, and 4
2641+
#' g <- make_full_multipartite(c(2, 3, 4))
2642+
#' plot(g)
2643+
#'
2644+
#' # Create a directed multipartite graph
2645+
#' g2 <- make_full_multipartite(c(2, 2, 2), directed = TRUE, mode = "out")
2646+
#' plot(g2)
2647+
#' @cdocs igraph_full_multipartite
2648+
make_full_multipartite <- function(
2649+
n,
2650+
directed = FALSE,
2651+
mode = c("all", "out", "in")
2652+
) {
2653+
n <- as.numeric(n)
2654+
directed <- as.logical(directed)
2655+
mode <- igraph.match.arg(mode)
2656+
2657+
res <- full_multipartite_impl(n = n, directed = directed, mode = mode)
2658+
graph <- set_vertex_attr(res$graph, "type", value = res$types)
2659+
2660+
# Transfer graph attributes from res to graph if add.params is enabled
2661+
if (igraph_opt("add.params")) {
2662+
for (attr_name in setdiff(names(res), c("graph", "types"))) {
2663+
graph <- set_graph_attr(graph, attr_name, res[[attr_name]])
2664+
}
2665+
}
2666+
graph
2667+
}
2668+
2669+
#' @rdname make_full_multipartite
2670+
#' @param ... Passed to `make_full_multipartite()`.
2671+
#' @export
2672+
full_multipartite <- function(...) {
2673+
constructor_spec(make_full_multipartite, ...)
2674+
}
2675+
2676+
## -----------------------------------------------------------------
2677+
2678+
#' Create a Turán graph
2679+
#'
2680+
#' Turán graphs are complete multipartite graphs with the property that the
2681+
#' sizes of the partitions are as close to equal as possible.
2682+
#'
2683+
#' @details
2684+
#' The Turán graph with `n` vertices and `r` partitions is the densest
2685+
#' graph on `n` vertices that does not contain a clique of size `r+1`.
2686+
#'
2687+
#' This function generates undirected graphs. The null graph is
2688+
#' returned when the number of vertices is zero. A complete graph is
2689+
#' returned if the number of partitions is greater than the number of vertices.
2690+
#'
2691+
#' @param n Integer, the number of vertices in the graph.
2692+
#' @param r Integer, the number of partitions in the graph, must be positive.
2693+
#' @return An igraph graph with a vertex attribute `type` storing the
2694+
#' partition index of each vertex. Partition indices start from 1.
2695+
#'
2696+
#' @family deterministic constructors
2697+
#' @export
2698+
#' @examples
2699+
#' # Create a Turán graph with 10 vertices and 3 partitions
2700+
#' g <- make_turan(10, 3)
2701+
#' plot(g)
2702+
#'
2703+
#' # The sizes of the partitions are as balanced as possible
2704+
#' table(V(g)$type)
2705+
#' @cdocs igraph_turan
2706+
make_turan <- function(n, r) {
2707+
n <- as.numeric(n)
2708+
r <- as.numeric(r)
2709+
2710+
res <- turan_impl(n = n, r = r)
2711+
graph <- set_vertex_attr(res$graph, "type", value = res$types)
2712+
2713+
# Transfer graph attributes from res to graph if add.params is enabled
2714+
if (igraph_opt("add.params")) {
2715+
for (attr_name in setdiff(names(res), c("graph", "types"))) {
2716+
graph <- set_graph_attr(graph, attr_name, res[[attr_name]])
2717+
}
2718+
}
2719+
graph
2720+
}
2721+
2722+
#' @rdname make_turan
2723+
#' @param ... Passed to `make_turan()`.
2724+
#' @export
2725+
turan <- function(...) {
2726+
constructor_spec(make_turan, ...)
2727+
}
2728+
2729+
## -----------------------------------------------------------------
2730+
26202731
#' Create a complete (full) citation graph
26212732
#'
26222733
#' `make_full_citation_graph()` creates a full citation graph. This is a

man/graph_from_atlas.Rd

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

man/graph_from_edgelist.Rd

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

man/graph_from_literal.Rd

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

man/make_.Rd

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

man/make_chordal_ring.Rd

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

man/make_empty_graph.Rd

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

man/make_full_citation_graph.Rd

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

0 commit comments

Comments
 (0)