Skip to content

Commit d53f29c

Browse files
Fix topological_sort (#214)
* Fix topological_sort Make `topological_sort` and alias for `Graphs.topological_sort_dfs` and do not export the broken function`Graphs.Experimental.Traversals.topological_sort` * Use @test_broken for Julia v1.6 compability * Fix call correct function in testset
1 parent c4360cf commit d53f29c

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

src/Experimental/Traversals/Traversals.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module Traversals
55
# to create new graph algorithms that rely on breadth-first or depth-first search traversals.
66

77
using Graphs
8-
import ..Graphs: topological_sort
98
using SimpleTraits
109
"""
1110
abstract type TraversalAlgorithm
@@ -204,6 +203,6 @@ function tree(
204203
return tree(p)
205204
end
206205

207-
export visited_vertices, parents, distances, topological_sort, tree
206+
export visited_vertices, parents, distances, tree
208207

209208
end # module

src/Experimental/Traversals/dfs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ end
7676
return true
7777
end
7878

79+
# TODO this function might return incorrect result (see tests) - use Graphs.topological_sort instead
7980
@traitfn function topological_sort(g::AG::IsDirected) where {T,AG<:AbstractGraph{T}}
8081
vcolor = zeros(UInt8, nv(g))
8182
verts = Vector{T}()

src/traversals/dfs.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ end
6060
return false
6161
end
6262

63+
"""
64+
topological_sort(g)
65+
66+
Return a [topological sort](https://en.wikipedia.org/wiki/Topological_sorting) of a directed
67+
graph `g` as a vector of vertices in topological order.
68+
69+
### Implementation Notes
70+
This is currently just an alias for `topological_sort_by_dfs`
71+
"""
72+
function topological_sort end
73+
74+
@traitfn function topological_sort(g::AG::IsDirected) where {AG<:AbstractGraph}
75+
return topological_sort_by_dfs(g)
76+
end
77+
6378
# Topological sort using DFS
6479
"""
6580
topological_sort_by_dfs(g)

test/experimental/traversals.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct DummyTraversalState <: LET.AbstractTraversalState end
112112

113113
ts1 = @inferred LET.topological_sort(dg)
114114
@test ts1 == [1, 3, 7, 6, 2, 5, 4]
115-
@test_throws LET.CycleError topological_sort(dg2)
115+
@test_throws LET.CycleError LET.topological_sort(dg2)
116116

117117
t1 = @inferred LET.tree(dg, 2, d)
118118
t2 = @inferred LET.tree(p1)
@@ -121,6 +121,9 @@ struct DummyTraversalState <: LET.AbstractTraversalState end
121121
@test !LET.is_cyclic(dg1)
122122
@test LET.is_cyclic(dg2)
123123
end
124+
125+
# Currently there is an error in Traverals.topological_sort, so this test fails
126+
@test_broken LET.topological_sort(SimpleDiGraph([Edge(2, 1)])) == [2, 1]
124127
end
125128
end
126129
end

test/traversals/dfs.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@
3232
end
3333
end
3434

35+
@testset "topological_sort" begin
36+
for g in testdigraphs(SimpleDiGraph([Edge(2, 1)]))
37+
@test @inferred(topological_sort(g)) == [2, 1]
38+
end
39+
40+
for g in testdigraphs(g5)
41+
@test @inferred(topological_sort(g)) == [1, 2, 3, 4]
42+
end
43+
44+
for g in testdigraphs(gx)
45+
@test @inferred(is_cyclic(g))
46+
@test_throws ErrorException topological_sort(g)
47+
end
48+
end
49+
3550
@testset "topological_sort_by_dfs" begin
3651
for g in testdigraphs(g5)
3752
@test @inferred(topological_sort_by_dfs(g)) == [1, 2, 3, 4]

0 commit comments

Comments
 (0)