Skip to content

Commit

Permalink
Redesign GenericNamedGraph type (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Apr 26, 2024
1 parent 3cca2b1 commit b8fd114
Show file tree
Hide file tree
Showing 35 changed files with 885 additions and 355 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.5.1"
version = "0.6.0"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ julia> using NamedGraphs.GraphsExtensions: ⊔, disjoint_union, subgraph, rename

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

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

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

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

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

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

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

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

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

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

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

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

julia> rename_vertices(first, subgraph(v -> v[2] == 2, g₁ g₂))
NamedGraphs.NamedGraph{Tuple{Int64, Int64}} with 4 vertices:
4-element Dictionaries.Indices{Tuple{Int64, Int64}}
4-element NamedGraphs.OrderedDictionaries.OrderedIndices{Tuple{Int64, Int64}}
(1, 1)
(2, 1)
(1, 2)
Expand Down
3 changes: 2 additions & 1 deletion examples/mincut.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Graphs: path_graph
using GraphsFlows: GraphsFlows
using NamedGraphs: NamedGraph
using NamedGraphs.GraphsExtensions: mincut_partitions

Expand All @@ -8,6 +7,8 @@ g = NamedGraph(path_graph(4), ["A", "B", "C", "D"])
part1, part2 = mincut_partitions(g)
@show part1, part2

# Requires `GraphsFlows` to be loaded.
using GraphsFlows: GraphsFlows
part1, part2 = mincut_partitions(g, "A", "D")
@show part1, part2

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

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

@show has_vertex(g, "A")
@show !has_vertex(g, "E")
Expand Down
10 changes: 5 additions & 5 deletions examples/multidimgraph_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ using Graphs: grid, has_edge, has_vertex, nv
using NamedGraphs: NamedGraph
using NamedGraphs.GraphsExtensions: , subgraph

parent_graph = grid((2, 2))
position_graph = grid((2, 2))
vs = [("X", 1), ("X", 2), ("Y", 1), ("Y", 2)]

g = NamedGraph(parent_graph, vs)
g = NamedGraph(position_graph, vs)

@show has_vertex(g, ("X", 1))
@show has_edge(g, ("X", 1) => ("X", 2))
Expand Down Expand Up @@ -42,9 +42,9 @@ g_sub = subgraph(v -> v[2] == 2, g)
@show !has_vertex(g_sub, ("Y", 1))
@show has_vertex(g_sub, ("Y", 2))

parent_graph = grid((2, 2))
g1 = NamedGraph(parent_graph, Tuple.(CartesianIndices((2, 2))))
g2 = NamedGraph(parent_graph, Tuple.(CartesianIndices((2, 2))))
position_graph = grid((2, 2))
g1 = NamedGraph(position_graph, Tuple.(CartesianIndices((2, 2))))
g2 = NamedGraph(position_graph, Tuple.(CartesianIndices((2, 2))))

g_disjoint_union = g1 g2

Expand Down
25 changes: 13 additions & 12 deletions ext/NamedGraphsGraphsFlowsExt/NamedGraphsGraphsFlowsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ using NamedGraphs:
AbstractNamedGraph,
DefaultNamedCapacity,
_symmetrize,
dist_matrix_to_parent_dist_matrix,
parent_graph,
parent_vertices_to_vertices,
vertex_to_parent_vertex
dist_matrix_to_position_dist_matrix,
ordered_vertices,
position_graph,
vertex_positions
using NamedGraphs.GraphsExtensions: GraphsExtensions, directed_graph
using SimpleTraits: SimpleTraits, @traitfn

@traitfn function NamedGraphs.dist_matrix_to_parent_dist_matrix(
@traitfn function NamedGraphs.dist_matrix_to_position_dist_matrix(
graph::AbstractNamedGraph::IsDirected, dist_matrix::DefaultNamedCapacity
)
return GraphsFlows.DefaultCapacity(graph)
Expand All @@ -26,15 +26,16 @@ end
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),
position_part1, position_part2, flow = GraphsFlows.mincut(
directed_graph(position_graph(graph)),
vertex_positions(graph)[source],
vertex_positions(graph)[target],
dist_matrix_to_position_dist_matrix(graph, capacity_matrix),
algorithm,
)
part1 = parent_vertices_to_vertices(graph, parent_part1)
part2 = parent_vertices_to_vertices(graph, parent_part2)
(part1, part2) = map((position_part1, position_part2)) do position_part
return map(v -> ordered_vertices(graph)[v], position_part)
end
return (part1, part2, flow)
end

Expand Down
2 changes: 2 additions & 0 deletions src/NamedGraphs.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module NamedGraphs
include("lib/SimilarType/src/SimilarType.jl")
include("lib/Keys/src/Keys.jl")
include("lib/OrdinalIndexing/src/OrdinalIndexing.jl")
include("lib/OrderedDictionaries/src/OrderedDictionaries.jl")
include("lib/GraphGenerators/src/GraphGenerators.jl")
include("lib/GraphsExtensions/src/GraphsExtensions.jl")
include("utils.jl")
Expand Down
Loading

0 comments on commit b8fd114

Please sign in to comment.