Skip to content

Commit 941fe8a

Browse files
JoeyT1994mtfishman
andauthored
PartitionsGraphView (#104)
Co-authored-by: Matt Fishman <[email protected]> Co-authored-by: mtfishman <[email protected]>
1 parent 30172cb commit 941fe8a

File tree

9 files changed

+97
-49
lines changed

9 files changed

+97
-49
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.6.9"
4+
version = "0.7.0"
55

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

docs/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
44
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
55

66
[compat]
7-
Documenter = "1.10.0"
7+
Documenter = "1.10"
88
Literate = "2.20.1"
9-
NamedGraphs = "0.6.5"
9+
NamedGraphs = "0.7"

examples/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
44

55
[compat]
66
Graphs = "1.12.0"
7-
NamedGraphs = "0.6.6"
7+
NamedGraphs = "0.7.0"

src/lib/PartitionedGraphs/src/PartitionedGraphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ include("abstractpartitionedgraph.jl")
55
include("partitionvertex.jl")
66
include("partitionedge.jl")
77
include("partitionedgraph.jl")
8+
include("partitionsgraphview.jl")
89
end

src/lib/PartitionedGraphs/src/abstractpartitionedgraph.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ using ..NamedGraphs.GraphsExtensions:
1616
abstract type AbstractPartitionedGraph{V,PV} <: AbstractNamedGraph{V} end
1717

1818
#Needed for interface
19-
partitioned_graph(pg::AbstractPartitionedGraph) = not_implemented()
19+
partitions_graph(pg::AbstractPartitionedGraph) = not_implemented()
2020
unpartitioned_graph(pg::AbstractPartitionedGraph) = not_implemented()
2121
function unpartitioned_graph_type(pg::Type{<:AbstractPartitionedGraph})
2222
return not_implemented()
@@ -75,11 +75,11 @@ end
7575
function Graphs.has_vertex(
7676
pg::AbstractPartitionedGraph, partitionvertex::AbstractPartitionVertex
7777
)
78-
return has_vertex(partitioned_graph(pg), parent(partitionvertex))
78+
return has_vertex(partitions_graph(pg), parent(partitionvertex))
7979
end
8080

8181
function Graphs.has_edge(pg::AbstractPartitionedGraph, partitionedge::AbstractPartitionEdge)
82-
return has_edge(partitioned_graph(pg), parent(partitionedge))
82+
return has_edge(partitions_graph(pg), parent(partitionedge))
8383
end
8484

8585
function is_boundary_edge(pg::AbstractPartitionedGraph, edge::AbstractEdge)
@@ -91,17 +91,17 @@ function Graphs.add_edge!(pg::AbstractPartitionedGraph, edge::AbstractEdge)
9191
add_edge!(unpartitioned_graph(pg), edge)
9292
pg_edge = parent(partitionedge(pg, edge))
9393
if src(pg_edge) != dst(pg_edge)
94-
add_edge!(partitioned_graph(pg), pg_edge)
94+
add_edge!(partitions_graph(pg), pg_edge)
9595
end
9696
return pg
9797
end
9898

9999
function Graphs.rem_edge!(pg::AbstractPartitionedGraph, edge::AbstractEdge)
100100
pg_edge = partitionedge(pg, edge)
101-
if has_edge(partitioned_graph(pg), pg_edge)
101+
if has_edge(partitions_graph(pg), pg_edge)
102102
g_edges = edges(pg, pg_edge)
103103
if length(g_edges) == 1
104-
rem_edge!(partitioned_graph(pg), pg_edge)
104+
rem_edge!(partitions_graph(pg), pg_edge)
105105
end
106106
end
107107
return rem_edge!(unpartitioned_graph(pg), edge)
@@ -118,7 +118,7 @@ function Graphs.add_vertex!(
118118
pg::AbstractPartitionedGraph, vertex, partitionvertex::AbstractPartitionVertex
119119
)
120120
add_vertex!(unpartitioned_graph(pg), vertex)
121-
add_vertex!(partitioned_graph(pg), parent(partitionvertex))
121+
add_vertex!(partitions_graph(pg), parent(partitionvertex))
122122
insert_to_vertex_map!(pg, vertex, partitionvertex)
123123
return pg
124124
end
@@ -148,7 +148,7 @@ function Graphs.rem_vertex!(pg::AbstractPartitionedGraph, vertex)
148148
delete_from_vertex_map!(pg, pv, vertex)
149149
rem_vertex!(unpartitioned_graph(pg), vertex)
150150
if !haskey(partitioned_vertices(pg), parent(pv))
151-
rem_vertex!(partitioned_graph(pg), parent(pv))
151+
rem_vertex!(partitions_graph(pg), parent(pv))
152152
end
153153
return pg
154154
end
@@ -174,7 +174,7 @@ end
174174

175175
function Base.:(==)(pg1::AbstractPartitionedGraph, pg2::AbstractPartitionedGraph)
176176
if unpartitioned_graph(pg1) != unpartitioned_graph(pg2) ||
177-
partitioned_graph(pg1) != partitioned_graph(pg2)
177+
partitions_graph(pg1) != partitions_graph(pg2)
178178
return false
179179
end
180180
for v in vertices(pg1)

src/lib/PartitionedGraphs/src/partitionedgraph.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using ..NamedGraphs: NamedEdge, NamedGraph
1010
struct PartitionedGraph{V,PV,G<:AbstractGraph{V},PG<:AbstractGraph{PV}} <:
1111
AbstractPartitionedGraph{V,PV}
1212
graph::G
13-
partitioned_graph::PG
13+
partitions_graph::PG
1414
partitioned_vertices::Dictionary
1515
which_partition::Dictionary
1616
end
@@ -46,7 +46,7 @@ function PartitionedGraph(g::AbstractGraph; kwargs...)
4646
end
4747

4848
#Needed for interface
49-
partitioned_graph(pg::PartitionedGraph) = getfield(pg, :partitioned_graph)
49+
partitions_graph(pg::PartitionedGraph) = PartitionsGraphView(pg)
5050
unpartitioned_graph(pg::PartitionedGraph) = getfield(pg, :graph)
5151
function unpartitioned_graph_type(graph_type::Type{<:PartitionedGraph})
5252
return fieldtype(graph_type, :graph)
@@ -70,7 +70,7 @@ function partitionvertices(pg::PartitionedGraph, verts)
7070
end
7171

7272
function partitionvertices(pg::PartitionedGraph)
73-
return PartitionVertex.(vertices(partitioned_graph(pg)))
73+
return PartitionVertex.(vertices(pg.partitions_graph))
7474
end
7575

7676
function partitionedge(pg::PartitionedGraph, edge::AbstractEdge)
@@ -86,7 +86,7 @@ function partitionedges(pg::PartitionedGraph, edges::Vector)
8686
end
8787

8888
function partitionedges(pg::PartitionedGraph)
89-
return PartitionEdge.(edges(partitioned_graph(pg)))
89+
return PartitionEdge.(edges(pg.partitions_graph))
9090
end
9191

9292
function Graphs.edges(pg::PartitionedGraph, partitionedge::PartitionEdge)
@@ -105,7 +105,7 @@ end
105105

106106
function boundary_partitionedges(pg::PartitionedGraph, partitionvertices; kwargs...)
107107
return PartitionEdge.(
108-
boundary_edges(partitioned_graph(pg), parent.(partitionvertices); kwargs...)
108+
boundary_edges(pg.partitions_graph, parent.(partitionvertices); kwargs...)
109109
)
110110
end
111111

@@ -118,7 +118,7 @@ end
118118
function Base.copy(pg::PartitionedGraph)
119119
return PartitionedGraph(
120120
copy(unpartitioned_graph(pg)),
121-
copy(partitioned_graph(pg)),
121+
copy(pg.partitions_graph),
122122
copy(partitioned_vertices(pg)),
123123
copy(which_partition(pg)),
124124
)
@@ -160,7 +160,7 @@ end
160160
function partitionedgraph_induced_subgraph(pg::PartitionedGraph, vertices::Vector)
161161
sub_pg_graph, _ = induced_subgraph(unpartitioned_graph(pg), vertices)
162162
sub_partitioned_vertices = copy(partitioned_vertices(pg))
163-
for pv in NamedGraphs.vertices(partitioned_graph(pg))
163+
for pv in NamedGraphs.vertices(pg.partitions_graph)
164164
vs = intersect(vertices, sub_partitioned_vertices[pv])
165165
if !isempty(vs)
166166
sub_partitioned_vertices[pv] = vs
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
struct PartitionsGraphView{V,G<:AbstractGraph{V}} <: AbstractNamedGraph{V}
2+
graph::G
3+
end
4+
5+
Base.copy(g::PartitionsGraphView) = PartitionsGraphView(copy(g.graph))
6+
7+
using Graphs: AbstractGraph
8+
partitions_graph(g::AbstractGraph) = PartitionsGraphView(PartitionedGraph(g, [vertices(g)]))
9+
10+
# Graphs.jl and NamedGraphs.jl interface overloads for `PartitionsGraphView` wrapping
11+
# a `PartitionedGraph`.
12+
function NamedGraphs.position_graph_type(
13+
type::Type{<:PartitionsGraphView{V,G}}
14+
) where {V,G<:PartitionedGraph{V}}
15+
return fieldtype(fieldtype(fieldtype(type, :graph), :partitions_graph), :position_graph)
16+
end
17+
for f in [
18+
:(Graphs.add_vertex!),
19+
:(Graphs.edges),
20+
:(Graphs.vertices),
21+
:(Graphs.rem_vertex!),
22+
:(NamedGraphs.position_graph),
23+
:(NamedGraphs.vertex_positions),
24+
:(NamedGraphs.ordered_vertices),
25+
:(NamedGraphs.edgetype),
26+
:(NamedGraphs.vertextype),
27+
]
28+
@eval begin
29+
function $f(
30+
g::PartitionsGraphView{V,G}, args...; kwargs...
31+
) where {V,G<:PartitionedGraph{V}}
32+
return $f(g.graph.partitions_graph, args...; kwargs...)
33+
end
34+
end
35+
end

test/Project.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2020
AbstractTrees = "0.4.5"
2121
Aqua = "0.8.11"
2222
Dictionaries = "0.4.4"
23-
Graphs = "1.12.0"
23+
Graphs = "1.12"
2424
GraphsFlows = "0.1.1"
2525
KaHyPar = "0.3.1"
2626
Metis = "1.5.0"
27-
NamedGraphs = "0.6.6"
27+
NamedGraphs = "0.7.0"
2828
SafeTestsets = "0.1.0"
2929
SimpleGraphAlgorithms = "0.6.0"
3030
SimpleGraphConverter = "0.1.0"
3131
Suppressor = "0.2.8"
3232
SymRCM = "0.2.2"
33-
Pkg = "1.10.0"
34-
Random = "1.10.0"
35-
Test = "1.10.0"
33+
Pkg = "1.10"
34+
Random = "1.10"
35+
Test = "1.10"

test/test_partitionedgraph.jl

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
@eval module $(gensym())
22
using Graphs:
3+
a_star,
34
center,
5+
connected_components,
46
diameter,
57
edges,
68
has_vertex,
79
is_connected,
10+
is_directed,
811
is_tree,
912
ne,
13+
neighbors,
1014
nv,
1115
radius,
1216
random_regular_graph,
1317
rem_vertex!,
1418
vertices
1519
using Metis: Metis
16-
using NamedGraphs: NamedEdge, NamedGraph
20+
using NamedGraphs: NamedEdge, NamedGraph, NamedGraphs
1721
using NamedGraphs.GraphsExtensions:
1822
add_edges!,
1923
add_vertices!,
@@ -31,10 +35,11 @@ using NamedGraphs.NamedGraphGenerators:
3135
using NamedGraphs.OrderedDictionaries: OrderedDictionary
3236
using NamedGraphs.PartitionedGraphs:
3337
PartitionEdge,
34-
PartitionedGraph,
3538
PartitionVertex,
39+
PartitionedGraph,
40+
PartitionsGraphView,
3641
boundary_partitionedges,
37-
partitioned_graph,
42+
partitions_graph,
3843
partitionedge,
3944
partitionedges,
4045
partitionvertex,
@@ -51,20 +56,27 @@ using Test: @test, @testset
5156
#Partition it column-wise (into a 1D chain)
5257
partitions = [[(i, j) for j in 1:ny] for i in 1:nx]
5358
pg = PartitionedGraph(g, partitions)
54-
@test vertextype(partitioned_graph(pg)) == Int64
59+
@test vertextype(partitions_graph(pg)) == Int64
5560
@test vertextype(unpartitioned_graph(pg)) == vertextype(g)
5661
@test isa(partitionvertices(pg), OrderedDictionary{Int64,PartitionVertex{Int64}})
5762
@test isa(partitionedges(pg), Vector{PartitionEdge{Int64,NamedEdge{Int64}}})
58-
@test is_tree(partitioned_graph(pg))
63+
@test is_tree(partitions_graph(pg))
5964
@test nv(pg) == nx * ny
60-
@test nv(partitioned_graph(pg)) == nx
65+
@test nv(partitions_graph(pg)) == nx
6166
pg_c = copy(pg)
6267
@test pg_c == pg
6368

69+
#PartionsGraphView test
70+
pgv = PartitionsGraphView(pg)
71+
@test vertices(pgv) == parent.(partitionvertices(pg))
72+
@test edges(pgv) == parent.(partitionedges(pg))
73+
@test is_tree(pgv) == true
74+
@test neighbors(pgv, 1) == [2]
75+
6476
#Same partitioning but with a dictionary constructor
6577
partition_dict = Dictionary([first(partition) for partition in partitions], partitions)
6678
pg = PartitionedGraph(g, partition_dict)
67-
@test vertextype(partitioned_graph(pg)) == vertextype(g)
79+
@test vertextype(partitions_graph(pg)) == vertextype(g)
6880
@test vertextype(unpartitioned_graph(pg)) == vertextype(g)
6981
@test isa(
7082
partitionvertices(pg),
@@ -74,19 +86,19 @@ using Test: @test, @testset
7486
partitionedges(pg),
7587
Vector{PartitionEdge{Tuple{Int64,Int64},NamedEdge{Tuple{Int64,Int64}}}},
7688
)
77-
@test is_tree(partitioned_graph(pg))
89+
@test is_tree(partitions_graph(pg))
7890
@test nv(pg) == nx * ny
79-
@test nv(partitioned_graph(pg)) == nx
91+
@test nv(partitions_graph(pg)) == nx
8092
pg_c = copy(pg)
8193
@test pg_c == pg
8294

8395
#Partition the whole thing into just 1 vertex
8496
pg = PartitionedGraph([i for i in 1:nx])
85-
@test unpartitioned_graph(pg) == partitioned_graph(pg)
97+
@test unpartitioned_graph(pg) == partitions_graph(pg)
8698
@test nv(pg) == nx
87-
@test nv(partitioned_graph(pg)) == nx
99+
@test nv(partitions_graph(pg)) == nx
88100
@test ne(pg) == 0
89-
@test ne(partitioned_graph(pg)) == 0
101+
@test ne(partitions_graph(pg)) == 0
90102
pg_c = copy(pg)
91103
@test pg_c == pg
92104
end
@@ -132,22 +144,22 @@ end
132144

133145
#Strip the middle column from pg via the partitioned graph vertex, and make a new pg
134146
rem_vertex!(pg, pv)
135-
@test !is_connected(unpartitioned_graph(pg)) && !is_connected(partitioned_graph(pg))
136-
@test parent(pv) vertices(partitioned_graph(pg))
147+
@test !is_connected(unpartitioned_graph(pg)) && !is_connected(partitions_graph(pg))
148+
@test parent(pv) vertices(partitions_graph(pg))
137149
@test !has_vertex(pg, pv)
138150
@test nv(pg) == (nx - 1) * ny
139-
@test nv(partitioned_graph(pg)) == nx - 1
140-
@test !is_tree(partitioned_graph(pg))
151+
@test nv(partitions_graph(pg)) == nx - 1
152+
@test !is_tree(partitions_graph(pg))
141153

142154
#Add the column back to the in place graph
143155
add_vertices!(pg, v_set, pv)
144156
add_edges!(pg, edges_involving_v_set)
145-
@test is_connected(pg.graph) && is_path_graph(partitioned_graph(pg))
146-
@test parent(pv) vertices(partitioned_graph(pg))
157+
@test is_connected(pg.graph) && is_path_graph(partitions_graph(pg))
158+
@test parent(pv) vertices(partitions_graph(pg))
147159
@test has_vertex(pg, pv)
148-
@test is_tree(partitioned_graph(pg))
160+
@test is_tree(partitions_graph(pg))
149161
@test nv(pg) == nx * ny
150-
@test nv(partitioned_graph(pg)) == nx
162+
@test nv(partitions_graph(pg)) == nx
151163
end
152164

153165
@testset "Test Partitioned Graph Subgraph Functionality" begin
@@ -167,7 +179,7 @@ end
167179
pg_2 = subgraph(pg, subgraph_vertices)
168180
@test pg_1 == pg_2
169181
@test nv(pg_1) == length(subgraph_vertices)
170-
@test nv(partitioned_graph(pg_1)) == length(subgraph_partitioned_vertices)
182+
@test nv(partitions_graph(pg_1)) == length(subgraph_partitioned_vertices)
171183

172184
subgraph_partitioned_vertex = 3
173185
subgraph_vertices = partitions[subgraph_partitioned_vertex]
@@ -190,9 +202,9 @@ end
190202
pg = PartitionedGraph(g, [vertices(g)])
191203
@test f(pg) == f(unpartitioned_graph(pg))
192204
@test nv(pg) == nv(g)
193-
@test nv(partitioned_graph(pg)) == 1
205+
@test nv(partitions_graph(pg)) == 1
194206
@test ne(pg) == ne(g)
195-
@test ne(partitioned_graph(pg)) == 0
207+
@test ne(partitions_graph(pg)) == 0
196208
end
197209
end
198210
end
@@ -209,7 +221,7 @@ end
209221
for backend in backends
210222
pg = PartitionedGraph(g; npartitions, backend="metis")
211223
@test pg isa PartitionedGraph
212-
@test nv(partitioned_graph(pg)) == npartitions
224+
@test nv(partitions_graph(pg)) == npartitions
213225
end
214226
end
215227
end

0 commit comments

Comments
 (0)