Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 10, 2025

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original issue description:

See discussion in #1908 and #1932 (also related: #60)

What should be the strategy when a named and unnamed graph are combined by whatever means
(e.g. disjoint_union(), a graph product, "+",...)

I see two options:

  1. throw an error (consistent with throwing an error when duplicated names are found)
  2. create generic vertex names. An example implementation is at the bottom.

Currently, names are set to NA, which is in my opinion not desirable.

library(igraph)
library(purrr)

consolidate_names <- function(graphs, prefix = "V") {
  have_names <- map_lgl(graphs, function(g) "name" %in% vertex_attr_names(g))
  if (all(have_names)) {
    all_names <- list_c(map(graphs, function(g) V(g)$name))

    if (any(duplicated(all_names))) {
      cli::cli_abort("Duplicated vertex names found across graphs.")
    }
    return(graphs)
  }

  existing_names <- list_c(map(graphs, function(g) V(g)$name))

  # Create a counter for generating new names (check if named graph already has generic names)
  name_counter <- if (any(grepl(paste0(prefix, "[0-9]+"), existing_names))) {
    max(as.integer(gsub(paste0(prefix, "([0-9]+)"), "\\1", existing_names)), na.rm = TRUE) + 1
  } else {
    1
  }

  for (i in seq_along(graphs)) {
    if (!have_names[i]) {
      n <- vcount(graphs[[i]])
      num_id <- seq(name_counter, length.out = n)
      V(graphs[[i]])$name <- paste0(prefix, num_id)
      name_counter <- name_counter + n + 1
    }
  }

  return(graphs)
}

g1 <- g2 <- make_ring(4)
V(g1)$name <- c("A", "B", "C", "D")
graphs <- list(g1, g2)
consolidate_names(graphs)
#> [[1]]
#> IGRAPH 467efa4 UN-- 4 4 -- Ring graph
#> + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c)
#> + edges from 467efa4 (vertex names):
#> [1] A--B B--C C--D A--D
#> 
#> [[2]]
#> IGRAPH 467efa4 UN-- 4 4 -- Ring graph
#> + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c)
#> + edges from 467efa4 (vertex names):
#> [1] V1--V2 V2--V3 V3--V4 V1--V4

V(g1)$name <- c("V1", "V2", "V3", "V4")
graphs <- list(g1, g2)
consolidate_names(graphs)
#> [[1]]
#> IGRAPH 467efa4 UN-- 4 4 -- Ring graph
#> + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c)
#> + edges from 467efa4 (vertex names):
#> [1] V1--V2 V2--V3 V3--V4 V1--V4
#> 
#> [[2]]
#> IGRAPH 467efa4 UN-- 4 4 -- Ring graph
#> + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c)
#> + edges from 467efa4 (vertex names):
#> [1] V5--V6 V6--V7 V7--V8 V5--V8

Created on 2025-07-04 with reprex v2.1.1

Fixes #1946.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing changed here, redo please.

- Add logic to detect when some graphs have vertex names and others don't
- Generate generic vertex names (V1, V2, etc.) for unnamed graphs
- Respect existing V-pattern names by continuing the sequence
- Add comprehensive tests for the new functionality
- Fixes issue #1946

Co-authored-by: krlmlr <[email protected]>
Copy link
Author

Copilot AI commented Jul 10, 2025

Nothing changed here, redo please.

I've implemented a solution for handling mixed named/unnamed graphs in disjoint_union(). The implementation adds logic to detect when some graphs have vertex names and others don't, then generates generic vertex names (V1, V2, etc.) for the unnamed graphs.

Key changes:

  • Modified disjoint_union() in R/operators.R to consolidate names before graph combination
  • Added comprehensive tests covering various scenarios
  • Handles V-pattern name continuation properly

This addresses the issue described in #1946 by implementing option 2 (create generic vertex names) rather than throwing an error.

Commit: 19f69ed

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cloud.r-project.org
    • Triggering command: /usr/lib/R/bin/exec/R -e .libPaths(c(&#39;~/R/library&#39;,~&#43;~.libPaths()));~&#43;~install.packages(c(&#39;cli&#39;,~&#43;~&#39;vctrs&#39;,~&#43;~&#39;rlang&#39;,~&#43;~&#39;lifecycle&#39;,~&#43;~&#39;withr&#39;),~&#43;~repos=&#39;REDACTED&#39;,~&#43;~lib=&#39;~/R/library&#39;) (dns block)
  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@krlmlr krlmlr requested a review from schochastics July 10, 2025 13:41
@Copilot Copilot AI requested a review from krlmlr July 10, 2025 13:41
Copilot finished work on behalf of krlmlr July 10, 2025 13:41
@krlmlr
Copy link
Contributor

krlmlr commented Jul 10, 2025

I think we want instructions about coding conventions for Copilot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding vertex names when a named and unnamed graph are combined?

2 participants