feat: Add is_same_graph() to compare graphs regardless of vertex and edge order - #2922
schochastics wants to merge 9 commits into
Conversation
…d edge order `is_same_graph()` checks whether two graphs have the same vertex and edge sets. Vertices are matched by name when both graphs are named. It uses the existing `is_same_graph_impl()` from the C core. Closes #349. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
This is how benchmark results would change (along with a 95% confidence interval in relative change) if a228258 is merged into main:
|
|
@schochastics With these AI-generated PRs, do you write the PR description yourself, or does the AI do it?
Is this a question from you or the AI? |
|
To me a crucial point is the naming. How would user know which one to use, how would they remember? Especially because "same" and "identical" are synonyms in everyday language. |
| .Call(Rx_igraph_identical_graphs, g1, g2, as.logical(attrs)) | ||
| } | ||
|
|
||
| #' Decide if two graphs are the same as labelled graphs |
There was a problem hiding this comment.
The terminology is standard in math and network science. I think this is a good title.
| #' Decide if two graphs are the same as labelled graphs | ||
| #' | ||
| #' @description | ||
| #' Two graphs are the same if they have the same directedness, |
There was a problem hiding this comment.
Possible, but my concern is that this would overexplain a standard concept and make it appear as if extra criteria could potentially be listed. In reality the function does something very simple: it compares labelled graphs for equality.
| #' the same vertex set and the same edge set. | ||
| #' Unlike [identical_graphs()], the order in which vertices and edges are | ||
| #' stored is ignored, | ||
| #' and unlike [isomorphic()], vertices are not relabelled. |
There was a problem hiding this comment.
maybe even have a sort of summary table?
There was a problem hiding this comment.
and adding an example case for each, when would you want to use one or the other.
There was a problem hiding this comment.
Example would be good. There's an example in the C docs.
| #' | ||
| #' @details | ||
| #' If `use_names` is `TRUE` and both graphs have a `name` vertex attribute, | ||
| #' vertices are matched by name, so the two graphs may store their vertices |
There was a problem hiding this comment.
I don't understand because this function already ignores the order in which vertices and edges are stored.
There was a problem hiding this comment.
Some more explanation here would be warranted.
If the vertices are not named, the ith vertex in the 1st graph is considered to be the same as the ith vertex in the second graph. If they are named, and use_names=T, then they are matched up by name.
The quirk is due to the fact that it is possible to have a graph with "unnamed" vertices at all in igraph. Coming from math, this would be a strange concept.
Perhaps a better way to think of it is: vertices may be identified by index, or by name. This flag controlled what to do.
| #' Vertex names must be unique in both graphs in this case. | ||
| #' Otherwise, vertices are matched by their IDs. | ||
| #' | ||
| #' Edges are compared as a multiset: |
There was a problem hiding this comment.
is "multiset" a common work in network analysis?
There was a problem hiding this comment.
No, that's a computer science term. But what is meant is explained below.
| #' @return A logical scalar, `TRUE` if the two graphs are the same. | ||
| #' @seealso [identical_graphs()] for comparing the internal representation, | ||
| #' [isomorphic()] for comparing graphs up to relabelling of the vertices. | ||
| #' @cdocs igraph_is_same_graph |
There was a problem hiding this comment.
we don't use the cdocs tag anymore, our roclet adds the link. And if it doesn't it's a roclet bug 😸
| ensure_igraph(g2) | ||
| check_bool(use_names) | ||
|
|
||
| if (use_names && is_named(g1) && is_named(g2)) { |
There was a problem hiding this comment.
should there be a warning when use_names but they are not both named?
There was a problem hiding this comment.
No yes or no from me, just mentioning that in some circumstances vertex indices are auto-converted to string vertex names. So some people may expect to be able to treat the vertex indices of unnamed graphs as vertex "names". Doing such things doesn't make me very comfortable though, so again no "yes" or "no" from me.
| - contents: | ||
| - graph_id | ||
| - identical_graphs | ||
| - is_same_graph |
There was a problem hiding this comment.
actually, should we document the three functions (identical, same, isomorphic) on the same page?
@szhorvat AI wrote the Description but the question comes from me |
I just don't want to feel silly answering AI questions assuming they came form you :-) I won't say a yes or no, but I will try to point out some use cases. The following would actually make a good example in the documentation of this function (CC @maelle): Take two graphs, canonincalize both (employ Now suppose we have vertex coloured graphs (which I hope this helps make the decision. |
…s named Clarify how `use_names` identifies vertices, drop the term "multiset", contrast `identical_graphs()`, `is_same_graph()` and `isomorphic()`, use the example from the C docs and remove the obsolete `@cdocs` tag. `is_same_graph()` now warns when `use_names = TRUE` but only one graph has vertex names. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
That's indeed rather unpleasant. Perhaps one way to phrase this is:
I hope some of the comments were useful but I don't want to barge in. All decisions are yours guys! |
`is_same_graph()` gains `vertex_attrs`, a character vector of vertex attributes that must also be identical after matching up the vertices. Attributes missing from either graph are an error. The docs add an example that uses `canonical_permutation()` to test for isomorphism, and state that identical, same and isomorphic are increasingly loose checks. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
(NO AI 🙂) One question: |
|
This is how benchmark results would change (along with a 95% confidence interval in relative change) if df44d42 is merged into main:
|
Closes #349.
identical_graphs()compares the internal representation, so it returnsFALSEwhen two graphs store the same vertices and edges in a different order.Both examples in #349 hit this:
graph_from_data_frame()with a reorderedverticesargument, and edges added/deleted vs.graph_from_adjacency_matrix().This PR adds
is_same_graph(g1, g2, ..., use_names = TRUE, vertex_attrs = NULL), which checks whether two graphs are the same as labelled graphs:use_names = TRUEand both graphs have anamevertex attribute (viapermute()), otherwise by index, as in the C function.use_names = FALSE.vertex_attrslists vertex attributes that must also be identical after matching up the vertices, e.g. vertex colours.identical()."name"can be included; this is only meaningful withuse_names = FALSE.The docs contrast
identical_graphs(),is_same_graph()andisomorphic()as increasingly loose checks, and include an example that canonicalizes both graphs withcanonical_permutation()so thatis_same_graph()tests for isomorphism.The C core already has
igraph_is_same_graph(), andis_same_graph_impl()was already generated, so there is no C or Stimulus change beyond updating the stale comment infunctions-R.yaml.identical_graphs()stays as it is and now links tois_same_graph()andisomorphic().Tests are in
test-iterators.Randtest-aaa-auto.R(foris_same_graph_impl()).🤖 Generated with Claude Code