Skip to content

Commit a3372a1

Browse files
authored
Return hash map/set for vertices (#56)
1 parent ba2ff72 commit a3372a1

File tree

16 files changed

+206
-157
lines changed

16 files changed

+206
-157
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.1.25"
4+
version = "0.2.0"
55

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

README.md

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +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 Vector{String}:
50+
4-element Dictionaries.Indices{String}
5151
"A"
5252
"B"
5353
"C"
@@ -61,7 +61,7 @@ and 3 edge(s):
6161

6262
julia> g = NamedGraph(grid((4,)); vertices=["A", "B", "C", "D"]) # Same as above
6363
NamedGraph{String} with 4 vertices:
64-
4-element Vector{String}:
64+
4-element Dictionaries.Indices{String}
6565
"A"
6666
"B"
6767
"C"
@@ -92,9 +92,9 @@ julia> neighbors(g, "B")
9292
"A"
9393
"C"
9494

95-
julia> g[["A", "B"]]
95+
julia> subgraph(g, ["A", "B"])
9696
NamedGraph{String} with 2 vertices:
97-
2-element Vector{String}:
97+
2-element Dictionaries.Indices{String}
9898
"A"
9999
"B"
100100

@@ -113,13 +113,12 @@ Graph operations are implemented by mapping back and forth between the generaliz
113113

114114

115115
It is natural to use tuples of integers as the names for the vertices of graphs with grid connectivities.
116-
For this, we use the convention that if a tuple is input, it is interpreted as the grid size and
117-
the vertex names label cartesian coordinates:
116+
For example:
118117

119118
```julia
120-
julia> g = NamedGraph(grid((2, 2)); vertices=(2, 2))
119+
julia> g = NamedGraph(grid((2, 2)); vertices=Tuple.(CartesianIndices((2, 2))))
121120
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
122-
4-element Vector{Tuple{Int64, Int64}}:
121+
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
123122
(1, 1)
124123
(2, 1)
125124
(1, 2)
@@ -134,6 +133,7 @@ and 4 edge(s):
134133
```
135134

136135

136+
In the future we will provide a shorthand notation for this, such as `cartesian_graph(grid((2, 2)), (2, 2))`.
137137
Internally the vertices are all stored as tuples with a label in each dimension.
138138

139139

@@ -162,7 +162,7 @@ You can use vertex names to get [induced subgraphs](https://juliagraphs.org/Grap
162162
```julia
163163
julia> subgraph(v -> v[1] == 1, g)
164164
NamedGraph{Tuple{Int64, Int64}} with 2 vertices:
165-
2-element Vector{Tuple{Int64, Int64}}:
165+
2-element Dictionaries.Indices{Tuple{Int64, Int64}}
166166
(1, 1)
167167
(1, 2)
168168

@@ -172,17 +172,17 @@ and 1 edge(s):
172172

173173
julia> subgraph(v -> v[2] == 2, g)
174174
NamedGraph{Tuple{Int64, Int64}} with 2 vertices:
175-
2-element Vector{Tuple{Int64, Int64}}:
175+
2-element Dictionaries.Indices{Tuple{Int64, Int64}}
176176
(1, 2)
177177
(2, 2)
178178

179179
and 1 edge(s):
180180
(1, 2) => (2, 2)
181181

182182

183-
julia> g[[(1, 1), (2, 2)]]
183+
julia> subgraph(g, [(1, 1), (2, 2)])
184184
NamedGraph{Tuple{Int64, Int64}} with 2 vertices:
185-
2-element Vector{Tuple{Int64, Int64}}:
185+
2-element Dictionaries.Indices{Tuple{Int64, Int64}}
186186
(1, 1)
187187
(2, 2)
188188

@@ -191,16 +191,12 @@ and 0 edge(s):
191191
```
192192

193193

194-
Note that this is similar to multidimensional array slicing, and we may support syntax like `subgraph(v, 1, :)` in the future.
195-
196-
197-
198194
You can also take [disjoint unions](https://en.wikipedia.org/wiki/Disjoint_union) or concatenations of graphs:
199195

200196
```julia
201197
julia> g₁ = g
202198
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
203-
4-element Vector{Tuple{Int64, Int64}}:
199+
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
204200
(1, 1)
205201
(2, 1)
206202
(1, 2)
@@ -215,7 +211,7 @@ and 4 edge(s):
215211

216212
julia> g₂ = g
217213
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
218-
4-element Vector{Tuple{Int64, Int64}}:
214+
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
219215
(1, 1)
220216
(2, 1)
221217
(1, 2)
@@ -230,7 +226,7 @@ and 4 edge(s):
230226

231227
julia> disjoint_union(g₁, g₂)
232228
NamedGraph{Tuple{Tuple{Int64, Int64}, Int64}} with 8 vertices:
233-
8-element Vector{Tuple{Tuple{Int64, Int64}, Int64}}:
229+
8-element Dictionaries.Indices{Tuple{Tuple{Int64, Int64}, Int64}}
234230
((1, 1), 1)
235231
((2, 1), 1)
236232
((1, 2), 1)
@@ -253,7 +249,7 @@ and 8 edge(s):
253249

254250
julia> g₁ g₂ # Same as above
255251
NamedGraph{Tuple{Tuple{Int64, Int64}, Int64}} with 8 vertices:
256-
8-element Vector{Tuple{Tuple{Int64, Int64}, Int64}}:
252+
8-element Dictionaries.Indices{Tuple{Tuple{Int64, Int64}, Int64}}
257253
((1, 1), 1)
258254
((2, 1), 1)
259255
((1, 2), 1)
@@ -293,7 +289,7 @@ The original graphs can be obtained from subgraphs:
293289
```julia
294290
julia> rename_vertices(v -> v[1], subgraph(v -> v[2] == 1, g₁ g₂))
295291
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
296-
4-element Vector{Tuple{Int64, Int64}}:
292+
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
297293
(1, 1)
298294
(2, 1)
299295
(1, 2)
@@ -308,7 +304,7 @@ and 4 edge(s):
308304

309305
julia> rename_vertices(v -> v[1], subgraph(v -> v[2] == 2, g₁ g₂))
310306
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
311-
4-element Vector{Tuple{Int64, Int64}}:
307+
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
312308
(1, 1)
313309
(2, 1)
314310
(1, 2)

examples/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[deps]
22
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
33
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
4+
Weave = "44d3d7a6-8a23-5bf8-98c5-b353f8df5ec9"

examples/README.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ has_vertex(g, "A")
3737
has_edge(g, "A" => "B")
3838
has_edge(g, "A" => "C")
3939
neighbors(g, "B")
40-
g[["A", "B"]]
40+
subgraph(g, ["A", "B"])
4141

4242
#' Internally, this type wraps a `SimpleGraph`, and stores a `Dictionary` from the [Dictionaries.jl](https://github.com/andyferris/Dictionaries.jl) package that maps the vertex names to the linear indices of the underlying `SimpleGraph`.
4343

4444
#' Graph operations are implemented by mapping back and forth between the generalized named vertices and the linear index vertices of the `SimpleGraph`.
4545

4646
#' It is natural to use tuples of integers as the names for the vertices of graphs with grid connectivities.
47-
#' For this, we use the convention that if a tuple is input, it is interpreted as the grid size and
48-
#' the vertex names label cartesian coordinates:
47+
#' For example:
4948
#+ term=true
5049

51-
g = NamedGraph(grid((2, 2)); vertices=(2, 2))
50+
g = NamedGraph(grid((2, 2)); vertices=Tuple.(CartesianIndices((2, 2))))
5251

52+
#' 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.
5454

5555
#' Vertices can be referred to by their tuples:
@@ -65,9 +65,7 @@ neighbors(g, (2, 2))
6565

6666
subgraph(v -> v[1] == 1, g)
6767
subgraph(v -> v[2] == 2, g)
68-
g[[(1, 1), (2, 2)]]
69-
70-
#' Note that this is similar to multidimensional array slicing, and we may support syntax like `subgraph(v, 1, :)` in the future.
68+
subgraph(g, [(1, 1), (2, 2)])
7169

7270
#' You can also take [disjoint unions](https://en.wikipedia.org/wiki/Disjoint_union) or concatenations of graphs:
7371
#+ term=true

src/Graphs/abstractgraph.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,15 @@ vertextype(graph::AbstractGraph) = vertextype(typeof(graph))
5757

5858
# Function `f` maps original vertices `vᵢ` of `g`
5959
# to new vertices `f(vᵢ)` of the output graph.
60-
function rename_vertices(f::Function, g::AbstractGraph)
61-
return set_vertices(g, f.(vertices(g)))
62-
end
60+
rename_vertices(f::Function, g::AbstractGraph) = not_implemented()
6361

6462
function rename_vertices(g::AbstractGraph, name_map)
6563
return rename_vertices(v -> name_map[v], g)
6664
end
6765

66+
# TODO: This isn't really a generic `AbstractGraph` function!
6867
function permute_vertices(graph::AbstractGraph, permutation::Vector)
69-
return subgraph(graph, vertices(graph)[permutation])
68+
return subgraph(graph, parent_vertices_to_vertices(graph, permutation))
7069
end
7170

7271
# Uniform interface for `outneighbors`, `inneighbors`, and `all_neighbors`

src/Graphs/partitionedgraphs/abstractpartitionedgraph.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ parent_graph(pg::AbstractPartitionedGraph) = parent_graph(unpartitioned_graph(pg
3131
function vertex_to_parent_vertex(pg::AbstractPartitionedGraph, vertex)
3232
return vertex_to_parent_vertex(unpartitioned_graph(pg), vertex)
3333
end
34+
function parent_vertex_to_vertex(pg::AbstractPartitionedGraph, parent_vertex)
35+
return parent_vertex_to_vertex(unpartitioned_graph(pg), parent_vertex)
36+
end
3437
edgetype(pg::AbstractPartitionedGraph) = edgetype(unpartitioned_graph(pg))
3538
parent_graph_type(pg::AbstractPartitionedGraph) = parent_graph_type(unpartitioned_graph(pg))
3639
nv(pg::AbstractPartitionedGraph, pv::AbstractPartitionVertex) = length(vertices(pg, pv))

src/Graphs/partitionedgraphs/partitionedgraph.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function partitionvertex(pg::PartitionedGraph, vertex)
5353
return PartitionVertex(which_partition(pg)[vertex])
5454
end
5555

56-
function partitionvertices(pg::PartitionedGraph, verts::Vector)
56+
function partitionvertices(pg::PartitionedGraph, verts)
5757
return unique(partitionvertex(pg, v) for v in verts)
5858
end
5959

@@ -94,9 +94,7 @@ function edges(pg::PartitionedGraph, partitionedges::Vector{<:PartitionEdge})
9494
return unique(reduce(vcat, [edges(pg, pe) for pe in partitionedges]))
9595
end
9696

97-
function boundary_partitionedges(
98-
pg::PartitionedGraph, partitionvertices::Vector{<:PartitionVertex}; kwargs...
99-
)
97+
function boundary_partitionedges(pg::PartitionedGraph, partitionvertices; kwargs...)
10098
return PartitionEdge.(
10199
boundary_edges(partitioned_graph(pg), parent.(partitionvertices); kwargs...)
102100
)
@@ -150,7 +148,7 @@ function delete_from_vertex_map!(
150148
end
151149

152150
### PartitionedGraph Specific Functions
153-
function induced_subgraph(pg::PartitionedGraph, vertices::Vector)
151+
function partitionedgraph_induced_subgraph(pg::PartitionedGraph, vertices::Vector)
154152
sub_pg_graph, _ = induced_subgraph(unpartitioned_graph(pg), vertices)
155153
sub_partitioned_vertices = copy(partitioned_vertices(pg))
156154
for pv in NamedGraphs.vertices(partitioned_graph(pg))
@@ -165,8 +163,17 @@ function induced_subgraph(pg::PartitionedGraph, vertices::Vector)
165163
return PartitionedGraph(sub_pg_graph, sub_partitioned_vertices), nothing
166164
end
167165

168-
function induced_subgraph(
169-
pg::PartitionedGraph, partitionverts::Vector{V}
170-
) where {V<:PartitionVertex}
166+
function partitionedgraph_induced_subgraph(
167+
pg::PartitionedGraph, partitionverts::Vector{<:PartitionVertex}
168+
)
171169
return induced_subgraph(pg, vertices(pg, partitionverts))
172170
end
171+
172+
function Graphs.induced_subgraph(pg::PartitionedGraph, vertices)
173+
return partitionedgraph_induced_subgraph(pg, vertices)
174+
end
175+
176+
# Fixes ambiguity error with `Graphs.jl`.
177+
function Graphs.induced_subgraph(pg::PartitionedGraph, vertices::Vector{<:Integer})
178+
return partitionedgraph_induced_subgraph(pg, vertices)
179+
end

0 commit comments

Comments
 (0)