Skip to content

Commit d561823

Browse files
authored
Simplify NamedGraph constructors (#61)
1 parent a3372a1 commit d561823

File tree

11 files changed

+50
-79
lines changed

11 files changed

+50
-79
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]> and contributors"]
4-
version = "0.2.0"
4+
version = "0.3.0"
55

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

README.md

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,7 @@ julia> using NamedGraphs
4747

4848
julia> g = NamedGraph(grid((4,)), ["A", "B", "C", "D"])
4949
NamedGraph{String} with 4 vertices:
50-
4-element Dictionaries.Indices{String}
51-
"A"
52-
"B"
53-
"C"
54-
"D"
55-
56-
and 3 edge(s):
57-
"A" => "B"
58-
"B" => "C"
59-
"C" => "D"
60-
61-
62-
julia> g = NamedGraph(grid((4,)); vertices=["A", "B", "C", "D"]) # Same as above
63-
NamedGraph{String} with 4 vertices:
64-
4-element Dictionaries.Indices{String}
50+
4-element Indices{String}
6551
"A"
6652
"B"
6753
"C"
@@ -94,7 +80,7 @@ julia> neighbors(g, "B")
9480

9581
julia> subgraph(g, ["A", "B"])
9682
NamedGraph{String} with 2 vertices:
97-
2-element Dictionaries.Indices{String}
83+
2-element Indices{String}
9884
"A"
9985
"B"
10086

@@ -116,9 +102,12 @@ It is natural to use tuples of integers as the names for the vertices of graphs
116102
For example:
117103

118104
```julia
119-
julia> g = NamedGraph(grid((2, 2)); vertices=Tuple.(CartesianIndices((2, 2))))
105+
julia> dims = (2, 2)
106+
(2, 2)
107+
108+
julia> g = NamedGraph(grid(dims), Tuple.(CartesianIndices(dims)))
120109
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
121-
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
110+
4-element Indices{Tuple{Int64, Int64}}
122111
(1, 1)
123112
(2, 1)
124113
(1, 2)
@@ -162,7 +151,7 @@ You can use vertex names to get [induced subgraphs](https://juliagraphs.org/Grap
162151
```julia
163152
julia> subgraph(v -> v[1] == 1, g)
164153
NamedGraph{Tuple{Int64, Int64}} with 2 vertices:
165-
2-element Dictionaries.Indices{Tuple{Int64, Int64}}
154+
2-element Indices{Tuple{Int64, Int64}}
166155
(1, 1)
167156
(1, 2)
168157

@@ -172,7 +161,7 @@ and 1 edge(s):
172161

173162
julia> subgraph(v -> v[2] == 2, g)
174163
NamedGraph{Tuple{Int64, Int64}} with 2 vertices:
175-
2-element Dictionaries.Indices{Tuple{Int64, Int64}}
164+
2-element Indices{Tuple{Int64, Int64}}
176165
(1, 2)
177166
(2, 2)
178167

@@ -182,7 +171,7 @@ and 1 edge(s):
182171

183172
julia> subgraph(g, [(1, 1), (2, 2)])
184173
NamedGraph{Tuple{Int64, Int64}} with 2 vertices:
185-
2-element Dictionaries.Indices{Tuple{Int64, Int64}}
174+
2-element Indices{Tuple{Int64, Int64}}
186175
(1, 1)
187176
(2, 2)
188177

@@ -196,7 +185,7 @@ You can also take [disjoint unions](https://en.wikipedia.org/wiki/Disjoint_union
196185
```julia
197186
julia> g₁ = g
198187
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
199-
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
188+
4-element Indices{Tuple{Int64, Int64}}
200189
(1, 1)
201190
(2, 1)
202191
(1, 2)
@@ -211,7 +200,7 @@ and 4 edge(s):
211200

212201
julia> g₂ = g
213202
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
214-
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
203+
4-element Indices{Tuple{Int64, Int64}}
215204
(1, 1)
216205
(2, 1)
217206
(1, 2)
@@ -226,7 +215,7 @@ and 4 edge(s):
226215

227216
julia> disjoint_union(g₁, g₂)
228217
NamedGraph{Tuple{Tuple{Int64, Int64}, Int64}} with 8 vertices:
229-
8-element Dictionaries.Indices{Tuple{Tuple{Int64, Int64}, Int64}}
218+
8-element Indices{Tuple{Tuple{Int64, Int64}, Int64}}
230219
((1, 1), 1)
231220
((2, 1), 1)
232221
((1, 2), 1)
@@ -249,7 +238,7 @@ and 8 edge(s):
249238

250239
julia> g₁ g₂ # Same as above
251240
NamedGraph{Tuple{Tuple{Int64, Int64}, Int64}} with 8 vertices:
252-
8-element Dictionaries.Indices{Tuple{Tuple{Int64, Int64}, Int64}}
241+
8-element Indices{Tuple{Tuple{Int64, Int64}, Int64}}
253242
((1, 1), 1)
254243
((2, 1), 1)
255244
((1, 2), 1)
@@ -287,9 +276,9 @@ be added manually.
287276
The original graphs can be obtained from subgraphs:
288277

289278
```julia
290-
julia> rename_vertices(v -> v[1], subgraph(v -> v[2] == 1, g₁ g₂))
279+
julia> rename_vertices(first, subgraph(v -> v[2] == 1, g₁ g₂))
291280
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
292-
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
281+
4-element Indices{Tuple{Int64, Int64}}
293282
(1, 1)
294283
(2, 1)
295284
(1, 2)
@@ -302,9 +291,9 @@ and 4 edge(s):
302291
(1, 2) => (2, 2)
303292

304293

305-
julia> rename_vertices(v -> v[1], subgraph(v -> v[2] == 2, g₁ g₂))
294+
julia> rename_vertices(first, subgraph(v -> v[2] == 2, g₁ g₂))
306295
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
307-
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
296+
4-element Indices{Tuple{Int64, Int64}}
308297
(1, 1)
309298
(2, 1)
310299
(1, 2)

examples/README.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
using Graphs
2929
using NamedGraphs
3030
g = NamedGraph(grid((4,)), ["A", "B", "C", "D"])
31-
g = NamedGraph(grid((4,)); vertices=["A", "B", "C", "D"]) # Same as above
3231

3332
#'Common operations are defined as you would expect:
3433
#+ term=true
@@ -47,7 +46,8 @@ subgraph(g, ["A", "B"])
4746
#' For example:
4847
#+ term=true
4948

50-
g = NamedGraph(grid((2, 2)); vertices=Tuple.(CartesianIndices((2, 2))))
49+
dims = (2, 2)
50+
g = NamedGraph(grid(dims), Tuple.(CartesianIndices(dims)))
5151

5252
#' In the future we will provide a shorthand notation for this, such as `cartesian_graph(grid((2, 2)), (2, 2))`.
5353
#' Internally the vertices are all stored as tuples with a label in each dimension.
@@ -86,8 +86,8 @@ g₁ ⊔ g₂ # Same as above
8686
#' The original graphs can be obtained from subgraphs:
8787
#+ term=true
8888

89-
rename_vertices(v -> v[1], subgraph(v -> v[2] == 1, g₁ g₂))
90-
rename_vertices(v -> v[1], subgraph(v -> v[2] == 2, g₁ g₂))
89+
rename_vertices(first, subgraph(v -> v[2] == 1, g₁ g₂))
90+
rename_vertices(first, subgraph(v -> v[2] == 2, g₁ g₂))
9191

9292
#' ## Generating this README
9393

examples/disjoint_union.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Graphs
22
using NamedGraphs
33

4-
g1 = NamedGraph(grid((2, 2)); vertices=(2, 2))
5-
g2 = NamedGraph(grid((2, 2)); vertices=(2, 2))
4+
g1 = NamedGraph(grid((2, 2)), Tuple.(CartesianIndices((2, 2))))
5+
g2 = NamedGraph(grid((2, 2)), Tuple.(CartesianIndices((2, 2))))
66
g = ("X" => g1, "Y" => g2)
77

88
@show g1

examples/multidimgraph_1d.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ g = NamedGraph(parent_graph, vs)
1010
@show has_edge(g, "A" => "B")
1111
@show !has_edge(g, "A" => "C")
1212

13-
g_sub = g[["A"]]
13+
g_sub = subgraph(g, ["A"])
1414

1515
@show has_vertex(g_sub, "A")
1616
@show !has_vertex(g_sub, "B")
1717
@show !has_vertex(g_sub, "C")
1818
@show !has_vertex(g_sub, "D")
1919

20-
g_sub = g[["A", "B"]]
20+
g_sub = subgraph(g, ["A", "B"])
2121

2222
@show has_vertex(g_sub, "A")
2323
@show has_vertex(g_sub, "B")

examples/multidimgraph_2d.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ g_sub = subgraph(v -> v[2] == 2, g)
4242
@show has_vertex(g_sub, ("Y", 2))
4343

4444
parent_graph = grid((2, 2))
45-
g1 = NamedGraph(parent_graph; vertices=(2, 2))
46-
g2 = NamedGraph(parent_graph; vertices=(2, 2))
45+
g1 = NamedGraph(parent_graph, Tuple.(CartesianIndices((2, 2))))
46+
g2 = NamedGraph(parent_graph, Tuple.(CartesianIndices((2, 2))))
4747

4848
g_disjoint_union = g1 g2
4949

examples/namedgraph.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ add_edge!(g, "A" => "C")
1616
@show issetequal(neighbors(g, "A"), ["B", "C"])
1717
@show issetequal(neighbors(g, "B"), ["A", "C"])
1818

19-
g_sub = g[["A", "B"]]
19+
g_sub = subgraph(g, ["A", "B"])
2020

2121
@show has_vertex(g_sub, "A")
2222
@show has_vertex(g_sub, "B")

src/generators/named_staticgraphs.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function named_bfs_tree(
4747
simple_graph::SimpleGraph, source::Integer=1; source_name=1, child_name=identity
4848
)
4949
named_vertices = named_bfs_tree_vertices(simple_graph, source; source_name, child_name)
50-
return NamedGraph(simple_graph; vertices=named_vertices)
50+
return NamedGraph(simple_graph, named_vertices)
5151
end
5252

5353
function named_binary_tree(
@@ -72,12 +72,12 @@ end
7272

7373
function named_grid(dims; kwargs...)
7474
simple_graph = grid(dims; kwargs...)
75-
return NamedGraph(simple_graph; vertices=dims)
75+
return NamedGraph(simple_graph, Tuple.(CartesianIndices(Tuple(dims))))
7676
end
7777

7878
function named_comb_tree(dims::Tuple)
7979
simple_graph = comb_tree(dims)
80-
return NamedGraph(simple_graph; vertices=dims)
80+
return NamedGraph(simple_graph, Tuple.(CartesianIndices(Tuple(dims))))
8181
end
8282

8383
function named_comb_tree(tooth_lengths::Vector{<:Integer})
@@ -88,5 +88,5 @@ function named_comb_tree(tooth_lengths::Vector{<:Integer})
8888
vertices = filter(Tuple.(CartesianIndices((nx, ny)))) do (jx, jy)
8989
jy <= tooth_lengths[jx]
9090
end
91-
return NamedGraph(simple_graph; vertices)
91+
return NamedGraph(simple_graph, vertices)
9292
end

src/namedgraph.jl

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ function GenericNamedGraph{<:Any,G}(parent_graph::AbstractSimpleGraph, vertices)
107107
return GenericNamedGraph{<:Any,G}(parent_graph, to_vertices(vertices))
108108
end
109109

110+
function GenericNamedGraph{<:Any,G}(parent_graph::AbstractSimpleGraph) where {G}
111+
return GenericNamedGraph{<:Any,G}(parent_graph, vertices(parent_graph))
112+
end
113+
110114
function GenericNamedGraph(parent_graph::AbstractSimpleGraph, vertices::Vector)
111115
return GenericNamedGraph{eltype(vertices)}(parent_graph, vertices)
112116
end
@@ -115,6 +119,10 @@ function GenericNamedGraph(parent_graph::AbstractSimpleGraph, vertices)
115119
return GenericNamedGraph(parent_graph, to_vertices(vertices))
116120
end
117121

122+
function GenericNamedGraph(parent_graph::AbstractSimpleGraph)
123+
return GenericNamedGraph(parent_graph, vertices(parent_graph))
124+
end
125+
118126
#
119127
# Tautological constructors
120128
#
@@ -157,34 +165,6 @@ GenericNamedGraph{<:Any,G}() where {G} = GenericNamedGraph{<:Any,G}(Any[])
157165

158166
GenericNamedGraph() = GenericNamedGraph(Any[])
159167

160-
#
161-
# Keyword argument constructor syntax
162-
#
163-
164-
function GenericNamedGraph{V,G}(
165-
parent_graph::AbstractSimpleGraph; vertices=vertices(parent_graph)
166-
) where {V,G}
167-
return GenericNamedGraph{V,G}(parent_graph, vertices)
168-
end
169-
170-
function GenericNamedGraph{V}(
171-
parent_graph::AbstractSimpleGraph; vertices=vertices(parent_graph)
172-
) where {V}
173-
return GenericNamedGraph{V}(parent_graph, vertices)
174-
end
175-
176-
function GenericNamedGraph{<:Any,G}(
177-
parent_graph::AbstractSimpleGraph; vertices=vertices(parent_graph)
178-
) where {G}
179-
return GenericNamedGraph{<:Any,G}(parent_graph, vertices)
180-
end
181-
182-
function GenericNamedGraph(
183-
parent_graph::AbstractSimpleGraph; vertices=vertices(parent_graph)
184-
)
185-
return GenericNamedGraph(parent_graph, vertices)
186-
end
187-
188168
# TODO: implement as:
189169
# graph = set_parent_graph(graph, copy(parent_graph(graph)))
190170
# graph = set_vertices(graph, copy(vertices(graph)))

test/test_multidimgraph.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ using Test
1818
show(io, "text/plain", g)
1919
@test String(take!(io)) isa String
2020

21-
g_sub = g[[("X", 1)]]
21+
g_sub = subgraph(g, [("X", 1)])
2222

2323
@test has_vertex(g_sub, ("X", 1))
2424
@test !has_vertex(g_sub, ("X", 2))
2525
@test !has_vertex(g_sub, ("Y", 1))
2626
@test !has_vertex(g_sub, ("Y", 2))
2727

28-
g_sub = g[[("X", 1), ("X", 2)]]
28+
g_sub = subgraph(g, [("X", 1), ("X", 2)])
2929

3030
@test has_vertex(g_sub, ("X", 1))
3131
@test has_vertex(g_sub, ("X", 2))
@@ -50,7 +50,7 @@ using Test
5050
@test !has_vertex(g_sub, ("Y", 1))
5151
@test has_edge(g_sub, ("X", 2) => ("Y", 2))
5252

53-
g1 = NamedGraph(grid((2, 2)); vertices=(2, 2))
53+
g1 = NamedGraph(grid((2, 2)), Tuple.(CartesianIndices((2, 2))))
5454

5555
@test nv(g1) == 4
5656
@test ne(g1) == 4
@@ -64,7 +64,7 @@ using Test
6464
@test has_edge(g1, (2, 1) => (2, 2))
6565
@test !has_edge(g1, (1, 1) => (2, 2))
6666

67-
g2 = NamedGraph(grid((2, 2)); vertices=(2, 2))
67+
g2 = NamedGraph(grid((2, 2)), Tuple.(CartesianIndices((2, 2))))
6868

6969
g = ("X" => g1) ("Y" => g2)
7070

@@ -73,7 +73,7 @@ using Test
7373
@test has_vertex(g, ((1, 1), "X"))
7474
@test has_vertex(g, ((1, 1), "Y"))
7575

76-
g3 = NamedGraph(grid((2, 2)); vertices=(2, 2))
76+
g3 = NamedGraph(grid((2, 2)), Tuple.(CartesianIndices((2, 2))))
7777
g = disjoint_union("X" => g1, "Y" => g2, "Z" => g3)
7878

7979
@test nv(g) == 12

0 commit comments

Comments
 (0)