Skip to content

Commit c0f59ff

Browse files
authored
Improve subgraph from QuotientVertex and other small improvements (#113)
1 parent d8ba480 commit c0f59ff

File tree

11 files changed

+81
-61
lines changed

11 files changed

+81
-61
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NamedGraphs"
22
uuid = "678767b0-92e7-4007-89e4-4527a8725b19"
33
authors = ["Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors"]
4-
version = "0.8.1"
4+
version = "0.8.2"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

docs/src/index.md

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,7 @@ NamedGraphs.jl is supported by the Flatiron Institute, a division of the Simons
2323

2424
## Installation instructions
2525

26-
This package resides in the `ITensor/ITensorRegistry` local registry.
27-
In order to install, simply add that registry through your package manager.
28-
This step is only required once.
29-
```julia
30-
julia> using Pkg: Pkg
31-
32-
julia> Pkg.Registry.add(url="https://github.com/ITensor/ITensorRegistry")
33-
```
34-
or:
35-
```julia
36-
julia> Pkg.Registry.add(url="[email protected]:ITensor/ITensorRegistry.git")
37-
```
38-
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.
39-
40-
Then, the package can be added as usual through the package manager:
26+
The package can be added as usual through the package manager:
4127

4228
```julia
4329
julia> Pkg.add("NamedGraphs")

docs/src/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Reference
22

33
```@autodocs
4-
Modules = [NamedGraphs, NamedGraphs.GraphsExtensions, NamedGraphs.NamedGraphGenerators, NamedGraphs.Keys]
4+
Modules = [NamedGraphs, NamedGraphs.GraphsExtensions, NamedGraphs.NamedGraphGenerators, NamedGraphs.PartitionedGraphs, NamedGraphs.Keys]
55
```

src/abstractnamedgraph.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,11 @@ function Graphs.rem_edge!(graph::AbstractNamedGraph, edge)
379379
end
380380

381381
function Graphs.has_edge(graph::AbstractNamedGraph, edge::AbstractNamedEdge)
382-
return has_edge(position_graph(graph), edge_to_position_edge(graph, edge))
382+
src_position = get(vertex_positions(graph), src(edge), nothing)
383+
isnothing(src_position) && return false
384+
dst_position = get(vertex_positions(graph), dst(edge), nothing)
385+
isnothing(dst_position) && return false
386+
return has_edge(position_graph(graph), src_position, dst_position)
383387
end
384388

385389
# handles two-argument edge constructors like src,dst

src/lib/PartitionedGraphs/src/PartitionedGraphs.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ A library for partitioned graphs and their quotients.
55
66
This module provides data structures and functionalities to work with partitioned graphs,
77
including quotient vertices and edges, as well as views of partitioned graphs.
8-
It defines an abstract supertype [`AbstractPartitionedGraph`](@ref) for graphs that have
8+
It defines an abstract supertype `AbstractPartitionedGraph` for graphs that have
99
some notion of a non-trivial partitioning of their vertices. It also provides
1010
a interface of functions that can be overloaded on any subtype of `Graphs.AbstractGraph` to
11-
make this subtype behave like a partitioned graph, without itself subtyping [`AbstractPartitionedGraph`](@ref).
11+
make this subtype behave like a partitioned graph, without itself subtyping `AbstractPartitionedGraph`.
1212
1313
It defines the following concrete types:
14-
- [`QuotientVertex`](@ref): Represents a vertex in the quotient graph.
15-
- [`QuotientEdge`](@ref): Represents an edge in the quotient graph.
16-
- [`PartitionedView`](@ref): A lightweight view of a partitioned graph.
17-
- [`PartitionedGraph`](@ref): An implementation of a partitioned graph with extra caching
18-
not provided by [`PartitionedView`](@ref).
19-
- [`QuotientView`](@ref): A view of the quotient graph derived from a partitioned graph.
14+
- `QuotientVertex`: Represents a vertex in the quotient graph.
15+
- `QuotientEdge`: Represents an edge in the quotient graph.
16+
- `PartitionedView`: A lightweight view of a partitioned graph.
17+
- `PartitionedGraph`: An implementation of a partitioned graph with extra caching
18+
not provided by `PartitionedView`.
19+
- `QuotientView`: A view of the quotient graph derived from a partitioned graph.
2020
It provides the following functions:
21-
- [`partitionedgraph`](@ref): Partitions an `AbstractGraph`.
22-
- [`departition`](@ref): Removes a single layer of partitioning from a partitioned graph.
23-
- [`unpartition`](@ref): Recursively removes all layers of partitioning from a partitioned graph.
21+
- `partitionedgraph`: Partitions an `AbstractGraph`.
22+
- `departition`: Removes a single layer of partitioning from a partitioned graph.
23+
- `unpartition`: Recursively removes all layers of partitioning from a partitioned graph.
2424
2525
## Interfaces
2626

src/lib/PartitionedGraphs/src/abstractpartitionedgraph.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ function Base.:(==)(pg1::AbstractPartitionedGraph, pg2::AbstractPartitionedGraph
161161
return true
162162
end
163163

164-
function NamedGraphs._induced_subgraph(pg::AbstractPartitionedGraph, vlist)
165-
return NamedGraphs._induced_subgraph(unpartitioned_graph(pg), vlist)
164+
function NamedGraphs.induced_subgraph_from_vertices(
165+
pg::AbstractPartitionedGraph, subvertices
166+
)
167+
return NamedGraphs.induced_subgraph_from_vertices(unpartitioned_graph(pg), subvertices)
166168
end

src/lib/PartitionedGraphs/src/partitionedgraph.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ function Graphs.rem_edge!(pg::PartitionedGraph, edge::AbstractEdge)
154154
end
155155

156156
### PartitionedGraph Specific Functions
157-
function partitionedgraph_induced_subgraph(pg::PartitionedGraph, vlist)
158-
sub_pg_graph, _ = induced_subgraph(pg.graph, vlist)
157+
function NamedGraphs.induced_subgraph_from_vertices(pg::PartitionedGraph, subvertices)
158+
sub_pg_graph, _ = induced_subgraph(pg.graph, subvertices)
159159
sub_partitioned_vertices = copy(pg.partitioned_vertices)
160160
for qv in quotientvertices(pg)
161161
pv = parent(qv)
162162

163-
vs = intersect(vlist, sub_partitioned_vertices[pv])
163+
vs = intersect(subvertices, sub_partitioned_vertices[pv])
164164
if !isempty(vs)
165165
sub_partitioned_vertices[pv] = vs
166166
else
@@ -170,7 +170,9 @@ function partitionedgraph_induced_subgraph(pg::PartitionedGraph, vlist)
170170

171171
return PartitionedGraph(sub_pg_graph, sub_partitioned_vertices), nothing
172172
end
173-
174-
function NamedGraphs._induced_subgraph(pg::PartitionedGraph, vlist)
175-
return partitionedgraph_induced_subgraph(pg, vlist)
173+
function NamedGraphs.induced_subgraph_from_vertices(
174+
pg::PartitionedGraph, subvertices::SubVertices{<:QuotientVertex}
175+
)
176+
sg, vs = NamedGraphs.induced_subgraph_from_vertices(pg, subvertices.vertices)
177+
return unpartitioned_graph(sg), vs
176178
end

src/lib/PartitionedGraphs/src/quotientedge.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ using Graphs: AbstractGraph, Graphs, AbstractEdge, dst, src, ne, has_edge
22
using ..NamedGraphs: AbstractNamedEdge
33
using ..NamedGraphs.GraphsExtensions: GraphsExtensions, not_implemented, rem_edges!, rem_edge
44

5+
"""
6+
QuotientEdge(e)
7+
8+
Represents a super-edge in a partitioned graph corresponding to the set of edges
9+
in between partitions `src(e)` and `dst(e)`.
10+
"""
511
struct QuotientEdge{V, E <: AbstractEdge{V}} <: AbstractNamedEdge{V}
612
edge::E
713
end
@@ -20,10 +26,10 @@ Base.reverse(se::QuotientEdge) = QuotientEdge(reverse(parent(se)))
2026
Return the the quotient edge corresponding to `edge` of the graph `g`. Note,
2127
the returned quotient edge may be a self-loop.
2228
23-
See also: [`quotientedges`](@ref), [`quotienttvertex`](@ref).
29+
See also: `quotientedges`, `quotienttvertex`.
2430
"""
25-
quotientedge(pg::AbstractGraph, p::Pair) = quotientedge(pg, edgetype(pg)(p))
26-
function quotientedge(g::AbstractGraph, edge)
31+
quotientedge(g::AbstractGraph, edge::Pair) = quotientedge(g, edgetype(g)(edge))
32+
function quotientedge(g::AbstractGraph, edge::AbstractEdge)
2733
if !has_edge(g, edge)
2834
throw(ArgumentError("Graph does not have an edge $edge"))
2935
end
@@ -79,7 +85,7 @@ has_quotientedge(g::AbstractGraph, se::QuotientEdge) = has_edge(quotient_graph(g
7985
8086
Returns the number of edges in `g` that correspond to the quotient edge `qe`.
8187
82-
See also: [`nv`](@ref).
88+
See also: `nv`.
8389
"""
8490
Graphs.ne(g::AbstractGraph, se::QuotientEdge) = length(edges(g, se))
8591

src/lib/PartitionedGraphs/src/quotientvertex.jl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ using ..NamedGraphs.GraphsExtensions: GraphsExtensions, rem_vertices!, subgraph
44
using ..NamedGraphs.OrderedDictionaries: OrderedIndices
55

66

7+
"""
8+
QuotientVertex(v)
9+
10+
Represents a super-vertex in a partitioned graph corresponding to the set of vertices
11+
in partition `v`.
12+
"""
713
struct QuotientVertex{V}
814
vertex::V
915
end
@@ -59,6 +65,25 @@ function GraphsExtensions.rem_vertices!(g::AbstractGraph, sv::QuotientVertex)
5965
end
6066
rem_quotientvertex!(pg::AbstractGraph, sv::QuotientVertex) = rem_vertices!(pg, sv)
6167

62-
function NamedGraphs.to_vertices(g::AbstractGraph, sv::Union{SV, Vector{SV}}) where {SV <: QuotientVertex}
63-
return vertices(g, sv)
68+
# Represents a set of subvertices corresponding to a set of quotient vertices.
69+
struct SubVertices{QV, V, Vs <: AbstractVector{V}} <: AbstractVector{V}
70+
quotientvertices::QV
71+
vertices::Vs
72+
end
73+
Base.size(v::SubVertices) = size(v.vertices)
74+
Base.getindex(v::SubVertices, I...) = v.vertices[I...]
75+
76+
function NamedGraphs.to_vertices(g::AbstractGraph, qv::QuotientVertex)
77+
return SubVertices(qv, vertices(g, qv))
78+
end
79+
function NamedGraphs.to_vertices(g::AbstractGraph, qv::AbstractVector{<:QuotientVertex})
80+
return SubVertices(qv, vertices(g, qv))
81+
end
82+
83+
# Special case so that `subgraph(g, QuotientVertex(v))` returns an unpartitioned graph.
84+
function NamedGraphs.induced_subgraph_from_vertices(
85+
g::AbstractGraph, subvertices::SubVertices{<:QuotientVertex}
86+
)
87+
sg, vs = NamedGraphs.induced_subgraph_from_vertices(g, subvertices.vertices)
88+
return unpartitioned_graph(sg), vs
6489
end

src/lib/PartitionedGraphs/src/quotientview.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ Base.parent(qg::QuotientView) = qg.graph
1111
parent_graph_type(g::AbstractGraph) = parent_graph_type(typeof(g))
1212
parent_graph_type(::Type{<:QuotientView{V, G}}) where {V, G} = G
1313

14-
function Base.convert(GT::Type{<:AbstractGraph}, qv::QuotientView)
14+
function Base.copy(qv::QuotientView)
1515
qg = quotient_graph_type(parent_graph_type(qv))(vertices(qv))
1616
add_edges!(qg, edges(qv))
17-
return convert(GT, qg)
17+
return qg
1818
end
1919

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

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

25-
Base.copy(g::QuotientView) = QuotientView(copy(parent(g)))
26-
2725
function NamedGraphs.position_graph_type(type::Type{<:QuotientView})
2826
return position_graph_type(quotient_graph_type(parent_graph_type(type)))
2927
end
@@ -38,16 +36,14 @@ function Graphs.rem_edge!(qg::QuotientView, v)
3836
end
3937

4038
for f in [
41-
:(NamedGraphs.namedgraph_induced_subgraph),
39+
:(NamedGraphs.induced_subgraph_from_vertices),
4240
:(NamedGraphs.ordered_vertices),
4341
:(NamedGraphs.vertex_positions),
4442
:(NamedGraphs.position_graph),
4543
]
4644
@eval begin
47-
function $f(
48-
g::QuotientView{V, G}, args...; kwargs...
49-
) where {V, G}
50-
return $f(convert(AbstractGraph, g), args...; kwargs...)
45+
function $f(g::QuotientView, args...; kwargs...)
46+
return $f(copy(g), args...; kwargs...)
5147
end
5248
end
5349
end

0 commit comments

Comments
 (0)