Skip to content

Commit

Permalink
Return hash map/set for vertices (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Apr 15, 2024
1 parent ba2ff72 commit a3372a1
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 157 deletions.
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]> and contributors"]
version = "0.1.25"
version = "0.2.0"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
40 changes: 18 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ julia> using NamedGraphs

julia> g = NamedGraph(grid((4,)), ["A", "B", "C", "D"])
NamedGraph{String} with 4 vertices:
4-element Vector{String}:
4-element Dictionaries.Indices{String}
"A"
"B"
"C"
Expand All @@ -61,7 +61,7 @@ and 3 edge(s):

julia> g = NamedGraph(grid((4,)); vertices=["A", "B", "C", "D"]) # Same as above
NamedGraph{String} with 4 vertices:
4-element Vector{String}:
4-element Dictionaries.Indices{String}
"A"
"B"
"C"
Expand Down Expand Up @@ -92,9 +92,9 @@ julia> neighbors(g, "B")
"A"
"C"

julia> g[["A", "B"]]
julia> subgraph(g, ["A", "B"])
NamedGraph{String} with 2 vertices:
2-element Vector{String}:
2-element Dictionaries.Indices{String}
"A"
"B"

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


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

```julia
julia> g = NamedGraph(grid((2, 2)); vertices=(2, 2))
julia> g = NamedGraph(grid((2, 2)); vertices=Tuple.(CartesianIndices((2, 2))))
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Vector{Tuple{Int64, Int64}}:
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand All @@ -134,6 +133,7 @@ and 4 edge(s):
```


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


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

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

julia> subgraph(v -> v[2] == 2, g)
NamedGraph{Tuple{Int64, Int64}} with 2 vertices:
2-element Vector{Tuple{Int64, Int64}}:
2-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 2)
(2, 2)

and 1 edge(s):
(1, 2) => (2, 2)


julia> g[[(1, 1), (2, 2)]]
julia> subgraph(g, [(1, 1), (2, 2)])
NamedGraph{Tuple{Int64, Int64}} with 2 vertices:
2-element Vector{Tuple{Int64, Int64}}:
2-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 2)

Expand All @@ -191,16 +191,12 @@ and 0 edge(s):
```


Note that this is similar to multidimensional array slicing, and we may support syntax like `subgraph(v, 1, :)` in the future.



You can also take [disjoint unions](https://en.wikipedia.org/wiki/Disjoint_union) or concatenations of graphs:

```julia
julia> g₁ = g
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Vector{Tuple{Int64, Int64}}:
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand All @@ -215,7 +211,7 @@ and 4 edge(s):

julia> g₂ = g
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Vector{Tuple{Int64, Int64}}:
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand All @@ -230,7 +226,7 @@ and 4 edge(s):

julia> disjoint_union(g₁, g₂)
NamedGraph{Tuple{Tuple{Int64, Int64}, Int64}} with 8 vertices:
8-element Vector{Tuple{Tuple{Int64, Int64}, Int64}}:
8-element Dictionaries.Indices{Tuple{Tuple{Int64, Int64}, Int64}}
((1, 1), 1)
((2, 1), 1)
((1, 2), 1)
Expand All @@ -253,7 +249,7 @@ and 8 edge(s):

julia> g₁ g₂ # Same as above
NamedGraph{Tuple{Tuple{Int64, Int64}, Int64}} with 8 vertices:
8-element Vector{Tuple{Tuple{Int64, Int64}, Int64}}:
8-element Dictionaries.Indices{Tuple{Tuple{Int64, Int64}, Int64}}
((1, 1), 1)
((2, 1), 1)
((1, 2), 1)
Expand Down Expand Up @@ -293,7 +289,7 @@ The original graphs can be obtained from subgraphs:
```julia
julia> rename_vertices(v -> v[1], subgraph(v -> v[2] == 1, g₁ g₂))
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Vector{Tuple{Int64, Int64}}:
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand All @@ -308,7 +304,7 @@ and 4 edge(s):

julia> rename_vertices(v -> v[1], subgraph(v -> v[2] == 2, g₁ g₂))
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Vector{Tuple{Int64, Int64}}:
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand Down
1 change: 1 addition & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[deps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
Weave = "44d3d7a6-8a23-5bf8-98c5-b353f8df5ec9"
12 changes: 5 additions & 7 deletions examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ has_vertex(g, "A")
has_edge(g, "A" => "B")
has_edge(g, "A" => "C")
neighbors(g, "B")
g[["A", "B"]]
subgraph(g, ["A", "B"])

#' 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`.

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

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

g = NamedGraph(grid((2, 2)); vertices=(2, 2))
g = NamedGraph(grid((2, 2)); vertices=Tuple.(CartesianIndices((2, 2))))

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

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

subgraph(v -> v[1] == 1, g)
subgraph(v -> v[2] == 2, g)
g[[(1, 1), (2, 2)]]

#' Note that this is similar to multidimensional array slicing, and we may support syntax like `subgraph(v, 1, :)` in the future.
subgraph(g, [(1, 1), (2, 2)])

#' You can also take [disjoint unions](https://en.wikipedia.org/wiki/Disjoint_union) or concatenations of graphs:
#+ term=true
Expand Down
7 changes: 3 additions & 4 deletions src/Graphs/abstractgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ vertextype(graph::AbstractGraph) = vertextype(typeof(graph))

# Function `f` maps original vertices `vᵢ` of `g`
# to new vertices `f(vᵢ)` of the output graph.
function rename_vertices(f::Function, g::AbstractGraph)
return set_vertices(g, f.(vertices(g)))
end
rename_vertices(f::Function, g::AbstractGraph) = not_implemented()

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

# TODO: This isn't really a generic `AbstractGraph` function!
function permute_vertices(graph::AbstractGraph, permutation::Vector)
return subgraph(graph, vertices(graph)[permutation])
return subgraph(graph, parent_vertices_to_vertices(graph, permutation))
end

# Uniform interface for `outneighbors`, `inneighbors`, and `all_neighbors`
Expand Down
3 changes: 3 additions & 0 deletions src/Graphs/partitionedgraphs/abstractpartitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ parent_graph(pg::AbstractPartitionedGraph) = parent_graph(unpartitioned_graph(pg
function vertex_to_parent_vertex(pg::AbstractPartitionedGraph, vertex)
return vertex_to_parent_vertex(unpartitioned_graph(pg), vertex)
end
function parent_vertex_to_vertex(pg::AbstractPartitionedGraph, parent_vertex)
return parent_vertex_to_vertex(unpartitioned_graph(pg), parent_vertex)
end
edgetype(pg::AbstractPartitionedGraph) = edgetype(unpartitioned_graph(pg))
parent_graph_type(pg::AbstractPartitionedGraph) = parent_graph_type(unpartitioned_graph(pg))
nv(pg::AbstractPartitionedGraph, pv::AbstractPartitionVertex) = length(vertices(pg, pv))
Expand Down
23 changes: 15 additions & 8 deletions src/Graphs/partitionedgraphs/partitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function partitionvertex(pg::PartitionedGraph, vertex)
return PartitionVertex(which_partition(pg)[vertex])
end

function partitionvertices(pg::PartitionedGraph, verts::Vector)
function partitionvertices(pg::PartitionedGraph, verts)
return unique(partitionvertex(pg, v) for v in verts)
end

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

function boundary_partitionedges(
pg::PartitionedGraph, partitionvertices::Vector{<:PartitionVertex}; kwargs...
)
function boundary_partitionedges(pg::PartitionedGraph, partitionvertices; kwargs...)
return PartitionEdge.(
boundary_edges(partitioned_graph(pg), parent.(partitionvertices); kwargs...)
)
Expand Down Expand Up @@ -150,7 +148,7 @@ function delete_from_vertex_map!(
end

### PartitionedGraph Specific Functions
function induced_subgraph(pg::PartitionedGraph, vertices::Vector)
function partitionedgraph_induced_subgraph(pg::PartitionedGraph, vertices::Vector)
sub_pg_graph, _ = induced_subgraph(unpartitioned_graph(pg), vertices)
sub_partitioned_vertices = copy(partitioned_vertices(pg))
for pv in NamedGraphs.vertices(partitioned_graph(pg))
Expand All @@ -165,8 +163,17 @@ function induced_subgraph(pg::PartitionedGraph, vertices::Vector)
return PartitionedGraph(sub_pg_graph, sub_partitioned_vertices), nothing
end

function induced_subgraph(
pg::PartitionedGraph, partitionverts::Vector{V}
) where {V<:PartitionVertex}
function partitionedgraph_induced_subgraph(
pg::PartitionedGraph, partitionverts::Vector{<:PartitionVertex}
)
return induced_subgraph(pg, vertices(pg, partitionverts))
end

function Graphs.induced_subgraph(pg::PartitionedGraph, vertices)
return partitionedgraph_induced_subgraph(pg, vertices)
end

# Fixes ambiguity error with `Graphs.jl`.
function Graphs.induced_subgraph(pg::PartitionedGraph, vertices::Vector{<:Integer})
return partitionedgraph_induced_subgraph(pg, vertices)
end
Loading

2 comments on commit a3372a1

@mtfishman
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/104941

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" a3372a1f8c0c5659be5e5b639c9dad5480fd3fdf
git push origin v0.2.0

Please sign in to comment.