Skip to content

Commit

Permalink
Improve namespacing (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Apr 16, 2024
1 parent d561823 commit 254e4cd
Show file tree
Hide file tree
Showing 67 changed files with 1,291 additions and 1,025 deletions.
26 changes: 21 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
name = "NamedGraphs"
uuid = "678767b0-92e7-4007-89e4-4527a8725b19"
authors = ["Matthew Fishman <[email protected]> and contributors"]
version = "0.3.0"
version = "0.4.0"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"

[weakdeps]
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
KaHyPar = "2a6221f6-aa48-11e9-3542-2d9e0ef01880"
Metis = "2679e427-3c69-5b7f-982b-ece356f1e94b"
SymRCM = "286e6d88-80af-4590-acc9-0001b223b9bd"

[extensions]
NamedGraphsGraphsFlowsExt = "GraphsFlows"
NamedGraphsKaHyParExt = "KaHyPar"
NamedGraphsMetisExt = "Metis"
NamedGraphsSymRCMExt = "SymRCM"

[compat]
AbstractTrees = "0.3, 0.4"
Dictionaries = "0.4"
Graphs = "1.8"
GraphsFlows = "0.1.1"
KaHyPar = "0.3.1"
LinearAlgebra = "1.7"
Metis = "1.4"
PackageExtensionCompat = "1"
SimpleTraits = "0.9"
SparseArrays = "1.7"
SplitApplyCombine = "1.2.2"
SymRCM = "0.2.1"
Suppressor = "0.2"
Requires = "1.3"
SymRCM = "0.2.1"
julia = "1.7"

[extras]
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
KaHyPar = "2a6221f6-aa48-11e9-3542-2d9e0ef01880"
Metis = "2679e427-3c69-5b7f-982b-ece356f1e94b"
SymRCM = "286e6d88-80af-4590-acc9-0001b223b9bd"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
Expand Down
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ Graphs.jl operations, and two implementations: `NamedGraph` and `NamedDiGraph`.
`NamedGraph` simply takes a set of names for the vertices of the graph. For example:

```julia
julia> using Graphs
julia> using Graphs: grid, has_edge, has_vertex, neighbors

julia> using NamedGraphs
julia> using NamedGraphs: NamedGraph

julia> using NamedGraphs.GraphsExtensions: , disjoint_union, subgraph, rename_vertices

julia> g = NamedGraph(grid((4,)), ["A", "B", "C", "D"])
NamedGraph{String} with 4 vertices:
4-element Indices{String}
4-element Dictionaries.Indices{String}
"A"
"B"
"C"
Expand Down Expand Up @@ -80,7 +82,7 @@ julia> neighbors(g, "B")

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

Expand All @@ -107,7 +109,7 @@ julia> dims = (2, 2)

julia> g = NamedGraph(grid(dims), Tuple.(CartesianIndices(dims)))
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Indices{Tuple{Int64, Int64}}
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand Down Expand Up @@ -151,7 +153,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 Indices{Tuple{Int64, Int64}}
2-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(1, 2)

Expand All @@ -161,7 +163,7 @@ and 1 edge(s):

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

Expand All @@ -171,7 +173,7 @@ and 1 edge(s):

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

Expand All @@ -185,7 +187,7 @@ You can also take [disjoint unions](https://en.wikipedia.org/wiki/Disjoint_union
```julia
julia> g₁ = g
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Indices{Tuple{Int64, Int64}}
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand All @@ -200,7 +202,7 @@ and 4 edge(s):

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

julia> disjoint_union(g₁, g₂)
NamedGraph{Tuple{Tuple{Int64, Int64}, Int64}} with 8 vertices:
8-element Indices{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 @@ -238,7 +240,7 @@ and 8 edge(s):

julia> g₁ g₂ # Same as above
NamedGraph{Tuple{Tuple{Int64, Int64}, Int64}} with 8 vertices:
8-element Indices{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 @@ -278,7 +280,7 @@ The original graphs can be obtained from subgraphs:
```julia
julia> rename_vertices(first, subgraph(v -> v[2] == 1, g₁ g₂))
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Indices{Tuple{Int64, Int64}}
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand All @@ -293,7 +295,7 @@ and 4 edge(s):

julia> rename_vertices(first, subgraph(v -> v[2] == 2, g₁ g₂))
NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Indices{Tuple{Int64, Int64}}
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand All @@ -315,8 +317,9 @@ and 4 edge(s):
This file was generated with [Weave.jl](https://github.com/JunoLab/Weave.jl) with the following commands:

```julia
using NamedGraphs, Weave
weave(
using NamedGraphs: NamedGraphs
using Weave: Weave
Weave.weave(
joinpath(pkgdir(NamedGraphs), "examples", "README.jl");
doctype="github",
out_path=pkgdir(NamedGraphs),
Expand Down
10 changes: 6 additions & 4 deletions examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
#' `NamedGraph` simply takes a set of names for the vertices of the graph. For example:
#+ term=true

using Graphs
using NamedGraphs
using Graphs: grid, has_edge, has_vertex, neighbors
using NamedGraphs: NamedGraph
using NamedGraphs.GraphsExtensions: , disjoint_union, subgraph, rename_vertices
g = NamedGraph(grid((4,)), ["A", "B", "C", "D"])

#'Common operations are defined as you would expect:
Expand Down Expand Up @@ -94,8 +95,9 @@ rename_vertices(first, subgraph(v -> v[2] == 2, g₁ ⊔ g₂))
#' This file was generated with [Weave.jl](https://github.com/JunoLab/Weave.jl) with the following commands:
#+ eval=false

using NamedGraphs, Weave
weave(
using NamedGraphs: NamedGraphs
using Weave: Weave
Weave.weave(
joinpath(pkgdir(NamedGraphs), "examples", "README.jl");
doctype="github",
out_path=pkgdir(NamedGraphs),
Expand Down
5 changes: 3 additions & 2 deletions examples/boundary.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NamedGraphs
using Graphs
using NamedGraphs.GraphsExtensions:
boundary_edges, boundary_vertices, inner_boundary_vertices, outer_boundary_vertices
using NamedGraphs.NamedGraphGenerators: named_grid

g = named_grid((5, 5))
subgraph_vertices = [(2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4), (4, 2), (4, 3), (4, 4)]
Expand Down
5 changes: 3 additions & 2 deletions examples/disjoint_union.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Graphs
using NamedGraphs
using Graphs: grid
using NamedGraphs: NamedGraph
using NamedGraphs.GraphsExtensions: , subgraph

g1 = NamedGraph(grid((2, 2)), Tuple.(CartesianIndices((2, 2))))
g2 = NamedGraph(grid((2, 2)), Tuple.(CartesianIndices((2, 2))))
Expand Down
6 changes: 4 additions & 2 deletions examples/mincut.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using NamedGraphs
using GraphsFlows
using Graphs: path_graph
using GraphsFlows: GraphsFlows
using NamedGraphs: NamedGraph
using NamedGraphs.GraphsExtensions: mincut_partitions

g = NamedGraph(path_graph(4), ["A", "B", "C", "D"])

Expand Down
5 changes: 3 additions & 2 deletions examples/multidimgraph_1d.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Graphs
using NamedGraphs
using Graphs: grid, has_edge, has_vertex, ne, nv
using NamedGraphs: NamedGraph
using NamedGraphs.GraphsExtensions: , subgraph

parent_graph = grid((4,))
vs = ["A", "B", "C", "D"]
Expand Down
9 changes: 5 additions & 4 deletions examples/multidimgraph_2d.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Graphs
using NamedGraphs
using Graphs: grid, has_edge, has_vertex, nv
using NamedGraphs: NamedGraph
using NamedGraphs.GraphsExtensions: , subgraph

parent_graph = grid((2, 2))
vs = [("X", 1), ("X", 2), ("Y", 1), ("Y", 2)]
Expand All @@ -11,14 +12,14 @@ g = NamedGraph(parent_graph, vs)
@show !has_edge(g, ("X", 2) => ("Y", 1))
@show has_edge(g, ("X", 2) => ("Y", 2))

g_sub = g[[("X", 1)]]
g_sub = subgraph(g, [("X", 1)])

@show has_vertex(g_sub, ("X", 1))
@show !has_vertex(g_sub, ("X", 2))
@show !has_vertex(g_sub, ("Y", 1))
@show !has_vertex(g_sub, ("Y", 2))

g_sub = g[[("X", 1), ("X", 2)]]
g_sub = subgraph(g, [("X", 1), ("X", 2)])

@show has_vertex(g_sub, ("X", 1))
@show has_vertex(g_sub, ("X", 2))
Expand Down
5 changes: 3 additions & 2 deletions examples/namedgraph.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Graphs
using NamedGraphs
using Graphs: add_edge!, grid, has_edge, has_vertex, neighbors
using NamedGraphs: NamedGraph
using NamedGraphs.GraphsExtensions: subgraph

g = NamedGraph(grid((4,)), ["A", "B", "C", "D"])

Expand Down
16 changes: 0 additions & 16 deletions examples/partitioning.jl

This file was deleted.

63 changes: 63 additions & 0 deletions ext/NamedGraphsGraphsFlowsExt/NamedGraphsGraphsFlowsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module NamedGraphsGraphsFlowsExt
using Graphs: AbstractGraph, IsDirected
using GraphsFlows: GraphsFlows
using NamedGraphs:
NamedGraphs,
AbstractNamedGraph,
DefaultNamedCapacity,
_symmetrize,
dist_matrix_to_parent_dist_matrix,
parent_graph,
parent_vertices_to_vertices,
vertex_to_parent_vertex
using NamedGraphs.GraphsExtensions: GraphsExtensions, directed_graph
using SimpleTraits: SimpleTraits, @traitfn

@traitfn function NamedGraphs.dist_matrix_to_parent_dist_matrix(
graph::AbstractNamedGraph::IsDirected, dist_matrix::DefaultNamedCapacity
)
return GraphsFlows.DefaultCapacity(graph)
end

@traitfn function GraphsFlows.mincut(
graph::AbstractNamedGraph::IsDirected,
source,
target,
capacity_matrix=DefaultNamedCapacity(graph),
algorithm::GraphsFlows.AbstractFlowAlgorithm=GraphsFlows.PushRelabelAlgorithm(),
)
parent_part1, parent_part2, flow = GraphsFlows.mincut(
directed_graph(parent_graph(graph)),
vertex_to_parent_vertex(graph, source),
vertex_to_parent_vertex(graph, target),
dist_matrix_to_parent_dist_matrix(graph, capacity_matrix),
algorithm,
)
part1 = parent_vertices_to_vertices(graph, parent_part1)
part2 = parent_vertices_to_vertices(graph, parent_part2)
return (part1, part2, flow)
end

@traitfn function GraphsFlows.mincut(
graph::AbstractNamedGraph::(!IsDirected),
source,
target,
capacity_matrix=DefaultNamedCapacity(graph),
algorithm::GraphsFlows.AbstractFlowAlgorithm=GraphsFlows.PushRelabelAlgorithm(),
)
return GraphsFlows.mincut(
directed_graph(graph), source, target, _symmetrize(capacity_matrix), algorithm
)
end

function GraphsExtensions.mincut_partitions(
graph::AbstractGraph,
source,
target,
capacity_matrix=DefaultNamedCapacity(graph),
algorithm::GraphsFlows.AbstractFlowAlgorithm=GraphsFlows.PushRelabelAlgorithm(),
)
part1, part2, flow = GraphsFlows.mincut(graph, source, target, capacity_matrix, algorithm)
return part1, part2
end
end
Loading

2 comments on commit 254e4cd

@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/105035

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.4.0 -m "<description of version>" 254e4cd8c731417e955efef48524d4156ddeaad0
git push origin v0.4.0

Please sign in to comment.