Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NamedGraphs"
uuid = "678767b0-92e7-4007-89e4-4527a8725b19"
authors = ["Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors"]
version = "0.8.1"
version = "0.8.2"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
16 changes: 1 addition & 15 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,7 @@ NamedGraphs.jl is supported by the Flatiron Institute, a division of the Simons

## Installation instructions

This package resides in the `ITensor/ITensorRegistry` local registry.
In order to install, simply add that registry through your package manager.
This step is only required once.
```julia
julia> using Pkg: Pkg

julia> Pkg.Registry.add(url="https://github.com/ITensor/ITensorRegistry")
```
or:
```julia
julia> Pkg.Registry.add(url="[email protected]:ITensor/ITensorRegistry.git")
```
if you want to use SSH credentials, which can make it so you don't have to enter your Github ursername and password when registering packages.

Then, the package can be added as usual through the package manager:
The package can be added as usual through the package manager:

```julia
julia> Pkg.add("NamedGraphs")
Expand Down
2 changes: 1 addition & 1 deletion docs/src/reference.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Reference

```@autodocs
Modules = [NamedGraphs, NamedGraphs.GraphsExtensions, NamedGraphs.NamedGraphGenerators, NamedGraphs.Keys]
Modules = [NamedGraphs, NamedGraphs.GraphsExtensions, NamedGraphs.NamedGraphGenerators, NamedGraphs.PartitionedGraphs, NamedGraphs.Keys]
```
6 changes: 5 additions & 1 deletion src/abstractnamedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,11 @@ function Graphs.rem_edge!(graph::AbstractNamedGraph, edge)
end

function Graphs.has_edge(graph::AbstractNamedGraph, edge::AbstractNamedEdge)
return has_edge(position_graph(graph), edge_to_position_edge(graph, edge))
src_position = get(vertex_positions(graph), src(edge), nothing)
isnothing(src_position) && return false
dst_position = get(vertex_positions(graph), dst(edge), nothing)
isnothing(dst_position) && return false
return has_edge(position_graph(graph), src_position, dst_position)
end

# handles two-argument edge constructors like src,dst
Expand Down
22 changes: 11 additions & 11 deletions src/lib/PartitionedGraphs/src/PartitionedGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ A library for partitioned graphs and their quotients.

This module provides data structures and functionalities to work with partitioned graphs,
including quotient vertices and edges, as well as views of partitioned graphs.
It defines an abstract supertype [`AbstractPartitionedGraph`](@ref) for graphs that have
It defines an abstract supertype `AbstractPartitionedGraph` for graphs that have
some notion of a non-trivial partitioning of their vertices. It also provides
a interface of functions that can be overloaded on any subtype of `Graphs.AbstractGraph` to
make this subtype behave like a partitioned graph, without itself subtyping [`AbstractPartitionedGraph`](@ref).
make this subtype behave like a partitioned graph, without itself subtyping `AbstractPartitionedGraph`.

It defines the following concrete types:
- [`QuotientVertex`](@ref): Represents a vertex in the quotient graph.
- [`QuotientEdge`](@ref): Represents an edge in the quotient graph.
- [`PartitionedView`](@ref): A lightweight view of a partitioned graph.
- [`PartitionedGraph`](@ref): An implementation of a partitioned graph with extra caching
not provided by [`PartitionedView`](@ref).
- [`QuotientView`](@ref): A view of the quotient graph derived from a partitioned graph.
- `QuotientVertex`: Represents a vertex in the quotient graph.
- `QuotientEdge`: Represents an edge in the quotient graph.
- `PartitionedView`: A lightweight view of a partitioned graph.
- `PartitionedGraph`: An implementation of a partitioned graph with extra caching
not provided by `PartitionedView`.
- `QuotientView`: A view of the quotient graph derived from a partitioned graph.
It provides the following functions:
- [`partitionedgraph`](@ref): Partitions an `AbstractGraph`.
- [`departition`](@ref): Removes a single layer of partitioning from a partitioned graph.
- [`unpartition`](@ref): Recursively removes all layers of partitioning from a partitioned graph.
- `partitionedgraph`: Partitions an `AbstractGraph`.
- `departition`: Removes a single layer of partitioning from a partitioned graph.
- `unpartition`: Recursively removes all layers of partitioning from a partitioned graph.

## Interfaces

Expand Down
6 changes: 4 additions & 2 deletions src/lib/PartitionedGraphs/src/abstractpartitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ function Base.:(==)(pg1::AbstractPartitionedGraph, pg2::AbstractPartitionedGraph
return true
end

function NamedGraphs._induced_subgraph(pg::AbstractPartitionedGraph, vlist)
return NamedGraphs._induced_subgraph(unpartitioned_graph(pg), vlist)
function NamedGraphs.induced_subgraph_from_vertices(
pg::AbstractPartitionedGraph, subvertices
)
return NamedGraphs.induced_subgraph_from_vertices(unpartitioned_graph(pg), subvertices)
end
14 changes: 8 additions & 6 deletions src/lib/PartitionedGraphs/src/partitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ function Graphs.rem_edge!(pg::PartitionedGraph, edge::AbstractEdge)
end

### PartitionedGraph Specific Functions
function partitionedgraph_induced_subgraph(pg::PartitionedGraph, vlist)
sub_pg_graph, _ = induced_subgraph(pg.graph, vlist)
function NamedGraphs.induced_subgraph_from_vertices(pg::PartitionedGraph, subvertices)
sub_pg_graph, _ = induced_subgraph(pg.graph, subvertices)
sub_partitioned_vertices = copy(pg.partitioned_vertices)
for qv in quotientvertices(pg)
pv = parent(qv)

vs = intersect(vlist, sub_partitioned_vertices[pv])
vs = intersect(subvertices, sub_partitioned_vertices[pv])
if !isempty(vs)
sub_partitioned_vertices[pv] = vs
else
Expand All @@ -170,7 +170,9 @@ function partitionedgraph_induced_subgraph(pg::PartitionedGraph, vlist)

return PartitionedGraph(sub_pg_graph, sub_partitioned_vertices), nothing
end

function NamedGraphs._induced_subgraph(pg::PartitionedGraph, vlist)
return partitionedgraph_induced_subgraph(pg, vlist)
function NamedGraphs.induced_subgraph_from_vertices(
pg::PartitionedGraph, subvertices::SubVertices{<:QuotientVertex}
)
sg, vs = NamedGraphs.induced_subgraph_from_vertices(pg, subvertices.vertices)
return unpartitioned_graph(sg), vs
end
14 changes: 10 additions & 4 deletions src/lib/PartitionedGraphs/src/quotientedge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ using Graphs: AbstractGraph, Graphs, AbstractEdge, dst, src, ne, has_edge
using ..NamedGraphs: AbstractNamedEdge
using ..NamedGraphs.GraphsExtensions: GraphsExtensions, not_implemented, rem_edges!, rem_edge

"""
QuotientEdge(e)

Represents a super-edge in a partitioned graph corresponding to the set of edges
in between partitions `src(e)` and `dst(e)`.
"""
struct QuotientEdge{V, E <: AbstractEdge{V}} <: AbstractNamedEdge{V}
edge::E
end
Expand All @@ -20,10 +26,10 @@ Base.reverse(se::QuotientEdge) = QuotientEdge(reverse(parent(se)))
Return the the quotient edge corresponding to `edge` of the graph `g`. Note,
the returned quotient edge may be a self-loop.

See also: [`quotientedges`](@ref), [`quotienttvertex`](@ref).
See also: `quotientedges`, `quotienttvertex`.
"""
quotientedge(pg::AbstractGraph, p::Pair) = quotientedge(pg, edgetype(pg)(p))
function quotientedge(g::AbstractGraph, edge)
quotientedge(g::AbstractGraph, edge::Pair) = quotientedge(g, edgetype(g)(edge))
function quotientedge(g::AbstractGraph, edge::AbstractEdge)
if !has_edge(g, edge)
throw(ArgumentError("Graph does not have an edge $edge"))
end
Expand Down Expand Up @@ -79,7 +85,7 @@ has_quotientedge(g::AbstractGraph, se::QuotientEdge) = has_edge(quotient_graph(g

Returns the number of edges in `g` that correspond to the quotient edge `qe`.

See also: [`nv`](@ref).
See also: `nv`.
"""
Graphs.ne(g::AbstractGraph, se::QuotientEdge) = length(edges(g, se))

Expand Down
29 changes: 27 additions & 2 deletions src/lib/PartitionedGraphs/src/quotientvertex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ using ..NamedGraphs.GraphsExtensions: GraphsExtensions, rem_vertices!, subgraph
using ..NamedGraphs.OrderedDictionaries: OrderedIndices


"""
QuotientVertex(v)

Represents a super-vertex in a partitioned graph corresponding to the set of vertices
in partition `v`.
"""
struct QuotientVertex{V}
vertex::V
end
Expand Down Expand Up @@ -59,6 +65,25 @@ function GraphsExtensions.rem_vertices!(g::AbstractGraph, sv::QuotientVertex)
end
rem_quotientvertex!(pg::AbstractGraph, sv::QuotientVertex) = rem_vertices!(pg, sv)

function NamedGraphs.to_vertices(g::AbstractGraph, sv::Union{SV, Vector{SV}}) where {SV <: QuotientVertex}
return vertices(g, sv)
# Represents a set of subvertices corresponding to a set of quotient vertices.
struct SubVertices{QV, V, Vs <: AbstractVector{V}} <: AbstractVector{V}
quotientvertices::QV
vertices::Vs
end
Base.size(v::SubVertices) = size(v.vertices)
Base.getindex(v::SubVertices, I...) = v.vertices[I...]

function NamedGraphs.to_vertices(g::AbstractGraph, qv::QuotientVertex)
return SubVertices(qv, vertices(g, qv))
end
function NamedGraphs.to_vertices(g::AbstractGraph, qv::AbstractVector{<:QuotientVertex})
return SubVertices(qv, vertices(g, qv))
end

# Special case so that `subgraph(g, QuotientVertex(v))` returns an unpartitioned graph.
function NamedGraphs.induced_subgraph_from_vertices(
g::AbstractGraph, subvertices::SubVertices{<:QuotientVertex}
)
sg, vs = NamedGraphs.induced_subgraph_from_vertices(g, subvertices.vertices)
return unpartitioned_graph(sg), vs
end
14 changes: 5 additions & 9 deletions src/lib/PartitionedGraphs/src/quotientview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ Base.parent(qg::QuotientView) = qg.graph
parent_graph_type(g::AbstractGraph) = parent_graph_type(typeof(g))
parent_graph_type(::Type{<:QuotientView{V, G}}) where {V, G} = G

function Base.convert(GT::Type{<:AbstractGraph}, qv::QuotientView)
function Base.copy(qv::QuotientView)
qg = quotient_graph_type(parent_graph_type(qv))(vertices(qv))
add_edges!(qg, edges(qv))
return convert(GT, qg)
return qg
end

NamedGraphs.edgetype(Q::Type{<:QuotientView}) = quotient_graph_edgetype(parent_graph_type(Q))

Graphs.vertices(qg::QuotientView) = parent.(quotientvertices(parent(qg)))
Graphs.edges(qg::QuotientView) = parent.(quotientedges(parent(qg)))

Base.copy(g::QuotientView) = QuotientView(copy(parent(g)))

function NamedGraphs.position_graph_type(type::Type{<:QuotientView})
return position_graph_type(quotient_graph_type(parent_graph_type(type)))
end
Expand All @@ -38,16 +36,14 @@ function Graphs.rem_edge!(qg::QuotientView, v)
end

for f in [
:(NamedGraphs.namedgraph_induced_subgraph),
:(NamedGraphs.induced_subgraph_from_vertices),
:(NamedGraphs.ordered_vertices),
:(NamedGraphs.vertex_positions),
:(NamedGraphs.position_graph),
]
@eval begin
function $f(
g::QuotientView{V, G}, args...; kwargs...
) where {V, G}
return $f(convert(AbstractGraph, g), args...; kwargs...)
function $f(g::QuotientView, args...; kwargs...)
return $f(copy(g), args...; kwargs...)
end
end
end
17 changes: 8 additions & 9 deletions src/namedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ function Graphs.is_directed(graph_type::Type{<:GenericNamedGraph})
return is_directed(position_graph_type(graph_type))
end

# Assumes the subvertices were already processed by `to_vertices`.
# TODO: Implement an edgelist version
function namedgraph_induced_subgraph(graph::AbstractGraph, subvertices)
function induced_subgraph_from_vertices(graph::AbstractGraph, subvertices)
subgraph = typeof(graph)(subvertices)
subvertices_set = Set(subvertices)
for src in subvertices
Expand All @@ -229,16 +230,14 @@ function namedgraph_induced_subgraph(graph::AbstractGraph, subvertices)
return subgraph, nothing
end

function Graphs.induced_subgraph(graph::AbstractNamedGraph, vlist)
return _induced_subgraph(graph, to_vertices(graph, vlist))
function Graphs.induced_subgraph(graph::AbstractNamedGraph, subvertices)
return induced_subgraph_from_vertices(graph, to_vertices(graph, subvertices))
end
# For method ambiguity resolution with Graphs.jl
function Graphs.induced_subgraph(graph::AbstractNamedGraph, vlist::AbstractVector{<:Integer})
return _induced_subgraph(graph, to_vertices(graph, vlist))
end

function _induced_subgraph(graph::AbstractNamedGraph, subvertices)
return namedgraph_induced_subgraph(graph, subvertices)
function Graphs.induced_subgraph(
graph::AbstractNamedGraph, subvertices::AbstractVector{<:Integer}
)
return induced_subgraph_from_vertices(graph, to_vertices(graph, subvertices))
end

#
Expand Down
Loading