Skip to content

Conversation

@jack-dunham
Copy link
Contributor

This PR includes the following changes to [PartitionedGraphs]

  • Added some additional NamedGraphs interface functions to various data types.
  • Constructing a graph from a QuotientView now directly calls quotient_graph.
  • Calling edges and vertices on QuotientView now returns according to the interface priority.
  • Constructing graphs from QuotientViews of AbstractSimpleGraphs will now return a graph of similar type.

Jack Dunham added 24 commits November 24, 2025 16:44
- `vertices(::QuotientView)` now directly returns the keys
`paritioned_vertices` return value.
- `edges(::QuotientView)` now directly returns the edges of the
`quotient_graph` return value (to coincide with interface overloading
priority)
- Converting a `QuotientView` to graph now calls `quotient_graph`
directly
- Adding methods for return directed/undirected graph types.

Fix imports

Fix rebase
This behaves similarly to `partitionedgraph` function.
…ype by default

This function can now be used `SimpleGraph` etc without promoting to
`NamedGraph`.
This function constructs a graph with no edges, but with vertices of the
quotient graph.
…ype as a parameter

Allows for more generic quotient graph types.
- argument `vertices` must be of type `Base.OneTo{Int}`.
…e, and triple argument methods.

This interface is to overload `similar_graph(graph)`, i.e. the single
argument method.
This function now acts similarly to `similar_graph`.
Comment on lines +31 to +36
function add_vertices!(graph::AbstractSimpleGraph, vertices::Base.OneTo{Int})
for _ in vertices
add_vertex!(graph)
end
return graph
end
Copy link
Member

@mtfishman mtfishman Dec 9, 2025

Choose a reason for hiding this comment

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

Where is this used in practice?

There's an awkward design issue when trying to write code that is generic between SimpleGraphs and NamedGraphs, since as you know SimpleGraphs are one-based. For NamedGraphs, add_vertices!(g, vs) has the property that the resulting graph has vertices union(vertices(g), vs), which isn't the case for how you've defined this function above.

I wonder if we can get away with just not defining this function for now, Graphs.jl already has an add_vertices! function for SimpleGraphs: https://juliagraphs.org/Graphs.jl/dev/core_functions/core/#Graphs.add_vertices!-Tuple%7BAbstractGraph,%20Integer%7D. I think my goal with GraphsExtensions was to make code that is really generic between SimpleGraphs and NamedGraphs, in the sense that if I input a SimpleGraph or a NamedGraph with the same integer vertices and other arguments, the behavior is the same.

Copy link
Member

@mtfishman mtfishman Dec 9, 2025

Choose a reason for hiding this comment

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

Another idea could be to define this as:

function add_vertices!(g::AbstractSimpleGraph, vs::AbstractUnitRange)
    first(vs) >= 1 || throw(ArgumentError("Vertices must be greater than or equal to 1 for AbstractSimpleGraph"))
    nvs = length(setdiff(vs, vertices(g)))
    Graphs.add_vertices!(g, nvs)
    return g
end

which would have the property that the resulting graph has vertices union(vertices(g), vs). But that may be a little bit tricky so maybe we should only define that if we have a compelling use case.

Copy link
Contributor Author

@jack-dunham jack-dunham Dec 9, 2025

Choose a reason for hiding this comment

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

I'll check if this method is even needed.

Comment on lines +17 to +20
# So partitions of `AbstractSimpleGraph` make `AbstractSimpleGraph`s
function _quotient_graph_from_vertices(graph::PartitionedView, vertices)
return _quotient_graph_from_vertices(unpartitioned_graph(graph), vertices)
end
Copy link
Member

Choose a reason for hiding this comment

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

Should this be similar_quotient_graph?

Comment on lines +440 to +446

edge_list = edges(g)

for edge in edge_list
rem_edge!(g, edge)
add_edge!(g, reverse(edge))
end
Copy link
Member

@mtfishman mtfishman Dec 9, 2025

Choose a reason for hiding this comment

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

This was something that I only just appreciated when reading this function, but in general the edge list can change if the graph is changed:

julia> using Graphs

julia> g = path_graph(5)
{5, 4} undirected simple Int64 graph

julia> e = edges(g)
SimpleEdgeIter 4

julia> collect(e)
4-element Vector{Graphs.SimpleGraphs.SimpleEdge{Int64}}:
 Edge 1 => 2
 Edge 2 => 3
 Edge 3 => 4
 Edge 4 => 5

julia> rem_edge!(g, 2, 3)
true

julia> e
SimpleEdgeIter 3

julia> collect(e)
3-element Vector{Graphs.SimpleGraphs.SimpleEdge{Int64}}:
 Edge 1 => 2
 Edge 3 => 4
 Edge 4 => 5

Which is documented here: https://juliagraphs.org/Graphs.jl/dev/core_functions/interface/#Graphs.edges-Tuple%7BAny%7D. I'm not sure if that causes problems for this implementation, maybe to be safe we should collect the edges. I think maybe for NamedGraph, edges(::NamedGraph) isn't a view of the edges, but probably it should be both for efficiency and to match the design of Graphs.jl.

Copy link
Member

@mtfishman mtfishman Dec 9, 2025

Choose a reason for hiding this comment

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

Note that the same thing is true of vertices(g), i.e. it is supposed to be a view into the vertices of g (https://juliagraphs.org/Graphs.jl/dev/core_functions/interface/#Graphs.vertices-Tuple{AbstractGraph}). Again, I think maybe it isn't implemented that way right now for NamedGraph but probably it should be implemented that way.

Co-authored-by: Matt Fishman <mtfishman@users.noreply.github.com>
…textype`; add explict `similar_graph` method for vertex conversion
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.

2 participants