Skip to content

New interface for GraphsExtensions.similar_graph; misc bug fixes.#147

Open
jack-dunham wants to merge 9 commits intoITensor:mainfrom
jack-dunham:main
Open

New interface for GraphsExtensions.similar_graph; misc bug fixes.#147
jack-dunham wants to merge 9 commits intoITensor:mainfrom
jack-dunham:main

Conversation

@jack-dunham
Copy link
Contributor

@jack-dunham jack-dunham commented Mar 13, 2026

This PR overhauls the way the GraphsExtensions.similar_graph function behaves, and includes some minor bug fixes and refactors.

New similar_graph interface

Essentially, the base case is now the three argument method, where one provides both vertices and edges. This defaults to constructing a concrete NamedGraph or NamedDiGraph depending on the IsDirected trait of the input graph.

similar_graph(::AbstractGraph::!(IsDirected), vertices, edges) # -> NamedGraph
similar_graph(::AbstractGraph::IsDirected, vertices, edges) # -> NamedDiGraph

If edges are not provided, then the function attempts to construct a similar graph to the input graph, but with the provided vertices and no edges. If no vertices or edges are provided, then the output graph will be a similar empty graph. For a given subtype of AbstractNamedGraph, one should overload the three argument method only (if a different return type is desired).

Type domain

If instead a graph type is provided as the first argument to similar_graph, then the constructor is called by default:

similar_graph(T::Type{<:AbstractGraph})  = T()
similar_graph(T::Type{<:AbstractGraph}, vertices)  = T(vertices)
similar_graph(T::Type{<:AbstractGraph}, vertices, edges)  = T(vertices, edges)

Note, unlike the case where a graph value is provided, the one and two argument methods are not defined in terms of the three argument method. This is to accommodate cases where a given AbstractGraph may have fixed edges, say (a notable example would be a tree graph). In that case, it is assumes there exists a constructor T(vertices). As the method

similar_graph(T::Type{<:AbstractGraph}, vertices)  = T(vertices)

may or may not return an edgeless graph, depending on the definition of the constructor, there exists the function

edgeless_graph(T::Type{<:AbstractGraph}, vertices)

that attempts to explicitly a construct an edgeless graph, thus throwing an error when this is not possible. Likewise, there exists a function

empty_graph(T::Type{<:AbstractGraph})

for explicitly constructing a graph of type T with no vertices or edges.

If your graph type MyGraph has freedom over vertices and edges, all three type-based method can be overloaded simultaneously using the followings signature:

similar_graph(T::Type{<:MyGraph}, vertices = [], edges = [])  = ...

@mtfishman
Copy link
Member

Likewise, there exists a function

empty_graph(T::Type{<:AbstractGraph}, vertices)

for explicitly constructing a graph of type T with no vertices or edges.

Is that supposed to be:

empty_graph(T::Type{<:AbstractGraph})

?

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