Skip to content

Combinatorics: added copy(::Graph)#5930

Merged
benlorenz merged 4 commits into
oscar-system:masterfrom
YueRen:yr/copyGraph
Jul 7, 2026
Merged

Combinatorics: added copy(::Graph)#5930
benlorenz merged 4 commits into
oscar-system:masterfrom
YueRen:yr/copyGraph

Conversation

@YueRen

@YueRen YueRen commented Apr 10, 2026

Copy link
Copy Markdown
Member

Many operations for graphs work inplace, so I think it would be nice if there was a copy command for graphs:

julia> G = graph_from_edges([[1,2]])
Undirected graph with 2 nodes and the following edges:
(2, 1)

julia> H = G
Undirected graph with 2 nodes and the following edges:
(2, 1)

julia> collect(edges(H))
1-element Vector{Edge}:
 Edge(2, 1)

julia> rem_edge!(G,1,2)
true

julia> collect(edges(H))
Edge[]

@YueRen YueRen added topic: combinatorics release notes: not needed PRs introducing changes that are wholly irrelevant to the release notes labels Apr 10, 2026

@benlorenz benlorenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems reasonable, can you add a small test?

Maybe this should copy labelings and other graph maps as well, @antonydellavecchia ?

@YueRen

YueRen commented Apr 13, 2026

Copy link
Copy Markdown
Member Author

Tests are added. Not sure how copying of attributes are done. For example, the following works if G has attributes:

julia> G = graph_from_edges([[1,2]])
Undirected graph with 2 nodes and the following edges:
(2, 1)

julia> set_attribute!(G, :asdf, "qwer")

julia> G.__attrs
Dict{Symbol, Any} with 1 entry:
  :asdf => "qwer"

julia> H = graph_from_edges([[1,2]])
Undirected graph with 2 nodes and the following edges:
(2, 1)

julia> H.__attrs = copy(G.__attrs)
Dict{Symbol, Any} with 1 entry:
  :asdf => "qwer"

But it will not work if G has no attributes:

julia> G = graph_from_edges([[1,2]])
Undirected graph with 2 nodes and the following edges:
(2, 1)

julia> copy(G.__attrs)
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getproperty(G::Graph{Undirected}, p::Symbol)
   @ Oscar ~/.julia/dev/Oscar/src/Combinatorics/Graphs/functions.jl:998
 [2] top-level scope
   @ REPL[18]:1

Is there a way to check whether an object has attributes? Something like:

julia> isdefined(G.__attrs)
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getproperty(G::Graph{Undirected}, p::Symbol)
   @ Oscar ~/.julia/dev/Oscar/src/Combinatorics/Graphs/functions.jl:998
 [2] top-level scope
   @ REPL[20]:1

@antonydellavecchia

Copy link
Copy Markdown
Collaborator

So if the graph G has a labeling it will appear as a symbol in labelings(G)
You can then loop through this list and copy?
Not sure if copying a GraphMap will work though?

but you can do [getproperty(G, l) for l in labelings(G)] to get all the GraphMaps

@antonydellavecchia

antonydellavecchia commented Apr 13, 2026

Copy link
Copy Markdown
Collaborator

Ok you essentially would need to turn each GraphMap into an dict representing the EdgeMap and The NodeMap and then relabel.

something like

copy_g = copy(g)
for l in labelings(g)
        gm = get_attribute(g, l)
        if !isnothing(gm.edge_map)
          em = Dict((src(e), dst(e)) => gm.edge_map[e] for e in edges(g))
        else
          em = nothing
        end
        # QQ has nothing to do with copying, just how the pm functions was implemented
        vm = _pmdata_for_oscar(gm.vertex_map, QQ)
        
       label!(copy_g, em, vm)
      end
end

@fingolfin

Copy link
Copy Markdown
Member

What's the status of this PR?

@fingolfin

Copy link
Copy Markdown
Member

@fieker asks whether this should perhaps be deepcopy instead (which should produce a new graph that is "independent" in every way from the original one; while copy is for shallow copies).

Based on discussion in triage, it sounds to me that deepcopy is indeed more fitting? (But we can also offer both names.)

Beware: you actually need to implement deepcopy_internal, not deepcopy.


(Of course we have not yet really discussed what deepcopy does with attributes... ugh...)

@YueRen

YueRen commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@antonydellavecchia and I discussed this PR briefly, and we believe that functionally copy should "deepcopy" the list of vertices and edges, while the dictionary of attributes should be copied normally. This should also be consistent with Graphs.jl.

We will work on that at some point this week.

Comment thread src/Combinatorics/Graphs/functions.jl Outdated
Comment thread src/TropicalGeometry/TropicalPolyhedron/constructors.jl Outdated
@benlorenz benlorenz closed this Jul 7, 2026
@benlorenz benlorenz reopened this Jul 7, 2026
@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.24%. Comparing base (cd3d581) to head (7f2e326).
⚠️ Report is 10 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5930      +/-   ##
==========================================
+ Coverage   84.21%   84.24%   +0.02%     
==========================================
  Files         787      787              
  Lines      107376   107508     +132     
==========================================
+ Hits        90432    90569     +137     
+ Misses      16944    16939       -5     
Files with missing lines Coverage Δ
src/Combinatorics/Graphs/functions.jl 92.93% <100.00%> (+0.17%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@benlorenz benlorenz merged commit e3f6bc1 into oscar-system:master Jul 7, 2026
92 of 103 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes: not needed PRs introducing changes that are wholly irrelevant to the release notes topic: combinatorics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants