Skip to content

Commit 6e47549

Browse files
authored
Add Simple(Di)Graph constructors from AbstractGraph (#301)
1 parent fd9f90a commit 6e47549

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/SimpleGraphs/simpledigraph.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ function SimpleDiGraph(
2424
return SimpleDiGraph{T}(ne, fadjlist, badjlist)
2525
end
2626

27-
2827
# DiGraph{UInt8}(6), DiGraph{Int16}(7), DiGraph{Int8}()
2928
"""
3029
SimpleDiGraph{T}(n=0)
@@ -281,6 +280,29 @@ function SimpleDiGraph(edge_list::Vector{SimpleDiGraphEdge{T}}) where {T<:Intege
281280
return g
282281
end
283282

283+
"""
284+
SimpleDiGraph{T}(g::AbstractGraph)
285+
SimpleDiGraph(g::AbstractGraph)
286+
287+
Construct a `SimpleDiGraph` from any `AbstractGraph` by enumerating edges.
288+
289+
If `g` is undirected, both directed edges `(u, v)` and `(v, u)` are added if undirected edge `{u, v}` exists.
290+
"""
291+
function SimpleDiGraph{T}(g::AbstractGraph) where {T}
292+
eds = edges(g)
293+
srcs = src.(eds)
294+
dsts = dst.(eds)
295+
if !is_directed(g)
296+
append!(srcs, dst.(eds))
297+
append!(dsts, src.(eds))
298+
end
299+
newg = SimpleDiGraph(Edge{T}.(srcs, dsts))
300+
add_vertices!(newg, nv(g) - nv(newg))
301+
return newg
302+
end
303+
304+
SimpleDiGraph(g::AbstractGraph{T}) where {T} = SimpleDiGraph{T}(g)
305+
284306
@inbounds function add_to_lists!(
285307
fadjlist::Vector{Vector{T}}, badjlist::Vector{Vector{T}}, s::T, d::T
286308
) where {T<:Integer}

src/SimpleGraphs/simplegraph.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,25 @@ function SimpleGraph(edge_list::Vector{SimpleGraphEdge{T}}) where {T<:Integer}
265265
return g
266266
end
267267

268+
"""
269+
SimpleGraph{T}(g::AbstractGraph)
270+
SimpleGraph(g::AbstractGraph)
271+
272+
Construct a `SimpleGraph` from any `AbstractGraph` by enumerating edges.
273+
274+
If `g` is directed, a directed edge `{u, v}` is added if either directed edge `(u, v)` or `(v, u)` exists.
275+
"""
276+
function SimpleGraph{T}(g::AbstractGraph) where {T}
277+
eds = edges(g)
278+
srcs = src.(eds)
279+
dsts = dst.(eds)
280+
newg = SimpleGraph(Edge{T}.(srcs, dsts))
281+
add_vertices!(newg, nv(g) - nv(newg))
282+
return newg
283+
end
284+
285+
SimpleGraph(g::AbstractGraph{T}) where {T} = SimpleGraph{T}(g)
286+
268287
@inbounds function add_to_fadjlist!(
269288
fadjlist::Vector{Vector{T}}, s::T, d::T
270289
) where {T<:Integer}

test/simplegraphs/simplegraphs.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Random: Random
2+
using Graphs.Test
23

34
@testset "SimpleGraphs" begin
45
rng = StableRNG(1)
@@ -509,4 +510,41 @@ using Random: Random
509510
SimpleDiGraphFromIterator(Edge.([(1, 2), (3, 2)]))
510511
@test SimpleDiGraphFromIterator(Edge.([(1, 2), (2, 3)])) !=
511512
SimpleDiGraphFromIterator(Edge.([(1, 2), (1, 3)]))
513+
514+
# Tests for constructors from AbstractGraph
515+
516+
g = path_graph(4)
517+
add_vertex!(g)
518+
for h in [g, GenericGraph(g)]
519+
hu = SimpleGraph(h)
520+
hd = SimpleDiGraph(h)
521+
@test nv(hu) == 5
522+
@test ne(hu) == 3
523+
@test nv(hd) == 5
524+
@test ne(hd) == 6
525+
@test eltype(SimpleGraph{Int32}(h)) == Int32
526+
@test eltype(SimpleDiGraph{Int32}(h)) == Int32
527+
end
528+
529+
g = path_digraph(4)
530+
add_vertex!(g)
531+
for h in [g, GenericDiGraph(g)]
532+
hu = SimpleGraph(h)
533+
hd = SimpleDiGraph(h)
534+
@test nv(hu) == 5
535+
@test ne(hu) == 3
536+
@test nv(hd) == 5
537+
@test ne(hd) == 3
538+
end
539+
540+
g = union(path_digraph(4), reverse(path_digraph(4)))
541+
add_vertex!(g)
542+
for h in [g, GenericDiGraph(g)]
543+
hu = SimpleGraph(h)
544+
hd = SimpleDiGraph(h)
545+
@test nv(hu) == 5
546+
@test ne(hu) == 3
547+
@test nv(hd) == 5
548+
@test ne(hd) == 6
549+
end
512550
end

0 commit comments

Comments
 (0)