Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some AbstractNamedGraph functionalities #8

Merged
merged 4 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/NamedGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export NamedGraph,
named_comb_tree,
post_order_dfs_vertices,
post_order_dfs_edges,
rename_vertices,
# Operations for tree-like graphs
is_leaf,
is_tree,
Expand Down
6 changes: 6 additions & 0 deletions src/abstractnamededge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ function ==(e1::AbstractNamedEdge, e2::AbstractNamedEdge)
return (src(e1) == src(e2) && dst(e1) == dst(e2))
end
hash(e::AbstractNamedEdge, h::UInt) = hash(src(e), hash(dst(e), h))

function rename_vertices(e::ET, name_map::Dictionary) where {ET<:AbstractNamedEdge}
# strip type parameter to allow renaming to change the vertex type
base_edge_type = Base.typename(ET).wrapper
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
return base_edge_type(name_map[src(e)], name_map[dst(e)])
end
30 changes: 30 additions & 0 deletions src/abstractnamedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,33 @@ function show(io::IO, mime::MIME"text/plain", graph::AbstractNamedGraph)
end

show(io::IO, graph::AbstractNamedGraph) = show(io, MIME"text/plain"(), graph)

#
# Convenience functions
#

function Base.:(==)(g1::GT, g2::GT) where {GT<:AbstractNamedGraph}
issetequal(vertices(g1), vertices(g2)) || return false
for v in vertices(g1)
issetequal(inneighbors(g1, v), inneighbors(g2, v)) || return false
issetequal(outneighbors(g1, v), outneighbors(g2, v)) || return false
end
return true
end

function rename_vertices(g::GT, name_map::Dictionary) where {GT<:AbstractNamedGraph}
original_vertices = vertices(g)
new_vertices = getindices(name_map, original_vertices)
# strip type parameter to allow renaming to change the vertex type
base_graph_type = Base.typename(GT).wrapper
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
new_g = base_graph_type(new_vertices)
for e in edges(g)
add_edge!(new_g, rename_vertices(e, name_map))
end
return new_g
end

function rename_vertices(g::AbstractNamedGraph, name_map::Function)
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
original_vertices = vertices(g)
return rename_vertices(g, Dictionary(original_vertices, name_map.(original_vertices)))
end
4 changes: 4 additions & 0 deletions src/nameddimdigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ function NamedDimDiGraph(parent_graph::DiGraph; dims=nothing, vertices=nothing)
return NamedDimDiGraph(parent_graph, vertices)
end

function NamedDimDiGraph(vertices::Array)
return NamedDimDiGraph(DiGraph(length(vertices)); vertices)
end

NamedDimDiGraph() = NamedDimDiGraph(DiGraph())

# AbstractNamedGraph required interface.
Expand Down
4 changes: 4 additions & 0 deletions src/nameddimgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ function NamedDimGraph(parent_graph::Graph; dims=nothing, vertices=nothing)
return NamedDimGraph(parent_graph, vertices)
end

function NamedDimGraph(vertices::Array)
return NamedDimGraph(Graph(length(vertices)); vertices)
end

NamedDimGraph() = NamedDimGraph(Graph())

# AbstractNamedGraph required interface.
Expand Down
4 changes: 4 additions & 0 deletions src/namedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function NamedGraph(parent_graph::Graph, vertices::Vector{V}) where {V}
return NamedGraph{V}(parent_graph, vertices)
end

function NamedGraph(vertices::Vector)
return NamedGraph(Graph(length(vertices)), vertices)
end

# AbstractNamedGraph required interface.
parent_graph(graph::NamedGraph) = graph.parent_graph
vertices(graph::NamedGraph) = graph.vertices
Expand Down
142 changes: 142 additions & 0 deletions test/test_abstractnamedgraph.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using Test
using Graphs
using MultiDimDictionaries
using NamedGraphs

@testset "AbstractNamedGraph equality" begin
# NamedGraph
g = grid((2, 2))
vs = ["A", "B", "C", "D"]
ng1 = NamedGraph(g, vs)
# construct same NamedGraph with different underlying structure
ng2 = NamedGraph(Graph(4), vs[[1, 4, 3, 2]])
add_edge!(ng2, "A" => "B")
add_edge!(ng2, "A" => "C")
add_edge!(ng2, "B" => "D")
add_edge!(ng2, "C" => "D")
@test NamedGraphs.parent_graph(ng1) != NamedGraphs.parent_graph(ng2)
@test ng1 == ng2
rem_edge!(ng2, "B" => "A")
@test ng1 != ng2

# NamedDimGraph
dvs = [("X", 1), ("X", 2), ("Y", 1), ("Y", 2)]
ndg1 = NamedDimGraph(g, dvs)
# construct same NamedDimGraph from different underlying structure
ndg2 = NamedDimGraph(Graph(4), dvs[[1, 4, 3, 2]])
add_edge!(ndg2, ("X", 1) => ("X", 2))
add_edge!(ndg2, ("X", 1) => ("Y", 1))
add_edge!(ndg2, ("X", 2) => ("Y", 2))
add_edge!(ndg2, ("Y", 1) => ("Y", 2))
@test NamedGraphs.parent_graph(ndg1) != NamedGraphs.parent_graph(ndg2)
@test ndg1 == ndg2
rem_edge!(ndg2, ("Y", 1) => ("X", 1))
@test ndg1 != ndg2

# NamedDimDiGraph
nddg1 = NamedDimDiGraph(DiGraph(collect(edges(g))), dvs)
# construct same NamedDimDiGraph from different underlying structure
nddg2 = NamedDimDiGraph(DiGraph(4), dvs[[1, 4, 3, 2]])
add_edge!(nddg2, ("X", 1) => ("X", 2))
add_edge!(nddg2, ("X", 1) => ("Y", 1))
add_edge!(nddg2, ("X", 2) => ("Y", 2))
add_edge!(nddg2, ("Y", 1) => ("Y", 2))
@test NamedGraphs.parent_graph(nddg1) != NamedGraphs.parent_graph(nddg2)
@test nddg1 == nddg2
rem_edge!(nddg2, ("X", 1) => ("Y", 1))
add_edge!(nddg2, ("Y", 1) => ("X", 1))
@test nddg1 != nddg2
end

@testset "AbstractNamedGraph vertex renaming" begin
g = grid((2, 2))
integer_names = collect(1:4)
string_names = ["A", "B", "C", "D"]
tuple_names = [("X", 1), ("X", 2), ("Y", 1), ("Y", 2)]
function_name = x -> reverse(x)

# NamedGraph
ng = NamedGraph(g, string_names)
# rename to integers
vmap_int = Dictionary(Graphs.vertices(ng), integer_names)
ng_int = rename_vertices(ng, vmap_int)
@test isa(ng_int, NamedGraph{Int})
@test has_vertex(ng_int, 3)
@test has_edge(ng_int, 1 => 2)
@test has_edge(ng_int, 2 => 4)
# rename to tuples
vmap_tuple = Dictionary(Graphs.vertices(ng), tuple_names)
ng_tuple = rename_vertices(ng, vmap_tuple)
@test isa(ng_tuple, NamedGraph{Tuple{String,Int}})
@test has_vertex(ng_tuple, ("X", 1))
@test has_edge(ng_tuple, ("X", 1) => ("X", 2))
@test has_edge(ng_tuple, ("X", 2) => ("Y", 2))
# rename with name map function
ng_function = rename_vertices(ng_tuple, function_name)
@test isa(ng_function, NamedGraph{Tuple{Int,String}})
@test has_vertex(ng_function, (1, "X"))
@test has_edge(ng_function, (1, "X") => (2, "X"))
@test has_edge(ng_function, (2, "X") => (2, "Y"))

# NamedDimGraph
ndg = named_grid((2, 2))
# rename to integers
vmap_int = Dictionary(Graphs.vertices(ndg), integer_names)
ndg_int = rename_vertices(ndg, vmap_int)
@test isa(ndg_int, NamedDimGraph{Tuple})
@test has_vertex(ndg_int, (1,))
@test has_edge(ndg_int, (1,) => (2,))
@test has_edge(ndg_int, (2,) => (4,))
# rename to strings
vmap_string = Dictionary(Graphs.vertices(ndg), string_names)
ndg_string = rename_vertices(ndg, vmap_string)
@test isa(ndg_string, NamedDimGraph{Tuple})
@test has_vertex(ndg_string, ("A",))
@test has_edge(ndg_string, ("A",) => ("B",))
@test has_edge(ndg_string, ("B",) => ("D",))
# rename to strings
vmap_tuple = Dictionary(Graphs.vertices(ndg), tuple_names)
ndg_tuple = rename_vertices(ndg, vmap_tuple)
@test isa(ndg_tuple, NamedDimGraph{Tuple})
@test has_vertex(ndg_tuple, "X", 1)
@test has_edge(ndg_tuple, ("X", 1) => ("X", 2))
@test has_edge(ndg_tuple, ("X", 2) => ("Y", 2))
# rename with name map function
ndg_function = rename_vertices(ndg_tuple, function_name)
@test isa(ndg_function, NamedDimGraph{Tuple})
@test has_vertex(ndg_function, 1, "X")
@test has_edge(ndg_function, (1, "X") => (2, "X"))
@test has_edge(ndg_function, (2, "X") => (2, "Y"))

# NamedDimDiGraph
nddg = NamedDimDiGraph(DiGraph(collect(edges(g))), Graphs.vertices(ndg))
# rename to integers
vmap_int = Dictionary(Graphs.vertices(nddg), integer_names)
nddg_int = rename_vertices(nddg, vmap_int)
@test isa(nddg_int, NamedDimDiGraph{Tuple})
@test has_vertex(nddg_int, (1,))
@test has_edge(nddg_int, (1,) => (2,))
@test has_edge(nddg_int, (2,) => (4,))
# rename to strings
vmap_string = Dictionary(Graphs.vertices(nddg), string_names)
nddg_string = rename_vertices(nddg, vmap_string)
@test isa(nddg_string, NamedDimDiGraph{Tuple})
@test has_vertex(nddg_string, ("A",))
@test has_edge(nddg_string, ("A",) => ("B",))
@test has_edge(nddg_string, ("B",) => ("D",))
@test !has_edge(nddg_string, ("D",) => ("B",))
# rename to strings
vmap_tuple = Dictionary(Graphs.vertices(nddg), tuple_names)
nddg_tuple = rename_vertices(nddg, vmap_tuple)
@test isa(nddg_tuple, NamedDimDiGraph{Tuple})
@test has_vertex(nddg_tuple, "X", 1)
@test has_edge(nddg_tuple, ("X", 1) => ("X", 2))
@test !has_edge(nddg_tuple, ("Y", 2) => ("X", 2))
# rename with name map function
nddg_function = rename_vertices(nddg_tuple, function_name)
@test isa(nddg_function, NamedDimDiGraph{Tuple})
@test has_vertex(nddg_function, 1, "X")
@test has_edge(nddg_function, (1, "X") => (2, "X"))
@test has_edge(nddg_function, (2, "X") => (2, "Y"))
@test !has_edge(nddg_function, (2, "Y") => (2, "X"))
end