Skip to content

Commit

Permalink
Add Simple(Di)Graph constructors from AbstractGraph (JuliaGraphs#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Sep 14, 2023
1 parent fd9f90a commit 6e47549
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/SimpleGraphs/simpledigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function SimpleDiGraph(
return SimpleDiGraph{T}(ne, fadjlist, badjlist)
end


# DiGraph{UInt8}(6), DiGraph{Int16}(7), DiGraph{Int8}()
"""
SimpleDiGraph{T}(n=0)
Expand Down Expand Up @@ -281,6 +280,29 @@ function SimpleDiGraph(edge_list::Vector{SimpleDiGraphEdge{T}}) where {T<:Intege
return g
end

"""
SimpleDiGraph{T}(g::AbstractGraph)
SimpleDiGraph(g::AbstractGraph)
Construct a `SimpleDiGraph` from any `AbstractGraph` by enumerating edges.
If `g` is undirected, both directed edges `(u, v)` and `(v, u)` are added if undirected edge `{u, v}` exists.
"""
function SimpleDiGraph{T}(g::AbstractGraph) where {T}
eds = edges(g)
srcs = src.(eds)
dsts = dst.(eds)
if !is_directed(g)
append!(srcs, dst.(eds))
append!(dsts, src.(eds))
end
newg = SimpleDiGraph(Edge{T}.(srcs, dsts))
add_vertices!(newg, nv(g) - nv(newg))
return newg
end

SimpleDiGraph(g::AbstractGraph{T}) where {T} = SimpleDiGraph{T}(g)

@inbounds function add_to_lists!(
fadjlist::Vector{Vector{T}}, badjlist::Vector{Vector{T}}, s::T, d::T
) where {T<:Integer}
Expand Down
19 changes: 19 additions & 0 deletions src/SimpleGraphs/simplegraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ function SimpleGraph(edge_list::Vector{SimpleGraphEdge{T}}) where {T<:Integer}
return g
end

"""
SimpleGraph{T}(g::AbstractGraph)
SimpleGraph(g::AbstractGraph)
Construct a `SimpleGraph` from any `AbstractGraph` by enumerating edges.
If `g` is directed, a directed edge `{u, v}` is added if either directed edge `(u, v)` or `(v, u)` exists.
"""
function SimpleGraph{T}(g::AbstractGraph) where {T}
eds = edges(g)
srcs = src.(eds)
dsts = dst.(eds)
newg = SimpleGraph(Edge{T}.(srcs, dsts))
add_vertices!(newg, nv(g) - nv(newg))
return newg
end

SimpleGraph(g::AbstractGraph{T}) where {T} = SimpleGraph{T}(g)

@inbounds function add_to_fadjlist!(
fadjlist::Vector{Vector{T}}, s::T, d::T
) where {T<:Integer}
Expand Down
38 changes: 38 additions & 0 deletions test/simplegraphs/simplegraphs.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Random: Random
using Graphs.Test

@testset "SimpleGraphs" begin
rng = StableRNG(1)
Expand Down Expand Up @@ -509,4 +510,41 @@ using Random: Random
SimpleDiGraphFromIterator(Edge.([(1, 2), (3, 2)]))
@test SimpleDiGraphFromIterator(Edge.([(1, 2), (2, 3)])) !=
SimpleDiGraphFromIterator(Edge.([(1, 2), (1, 3)]))

# Tests for constructors from AbstractGraph

g = path_graph(4)
add_vertex!(g)
for h in [g, GenericGraph(g)]
hu = SimpleGraph(h)
hd = SimpleDiGraph(h)
@test nv(hu) == 5
@test ne(hu) == 3
@test nv(hd) == 5
@test ne(hd) == 6
@test eltype(SimpleGraph{Int32}(h)) == Int32
@test eltype(SimpleDiGraph{Int32}(h)) == Int32
end

g = path_digraph(4)
add_vertex!(g)
for h in [g, GenericDiGraph(g)]
hu = SimpleGraph(h)
hd = SimpleDiGraph(h)
@test nv(hu) == 5
@test ne(hu) == 3
@test nv(hd) == 5
@test ne(hd) == 3
end

g = union(path_digraph(4), reverse(path_digraph(4)))
add_vertex!(g)
for h in [g, GenericDiGraph(g)]
hu = SimpleGraph(h)
hd = SimpleDiGraph(h)
@test nv(hu) == 5
@test ne(hu) == 3
@test nv(hd) == 5
@test ne(hd) == 6
end
end

0 comments on commit 6e47549

Please sign in to comment.