@@ -32,30 +32,30 @@ function is_cyclic end
3232end
3333# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
3434@traitfn function is_cyclic (g:: AG :: IsDirected ) where {T,AG<: AbstractGraph{T} }
35- # 0 if not visited, 1 if visited, 2 if in the current dfs path, 3 if fully explored
35+ # 0 if not visited, 1 if in the current dfs path, 2 if fully explored
3636 vcolor = zeros (UInt8, nv (g))
3737 vertex_stack = Vector {T} ()
3838 for v in vertices (g)
3939 vcolor[v] != 0 && continue
4040 push! (vertex_stack, v)
41- vcolor[v] = 1
4241 while ! isempty (vertex_stack)
4342 u = vertex_stack[end ]
44- if vcolor[u] == 1
45- vcolor[u] = 2
43+ if vcolor[u] == 0
44+ vcolor[u] = 1
4645 for n in outneighbors (g, u)
4746 # we hit a loop when reaching back a vertex of the main path
48- if vcolor[n] == 2
47+ if vcolor[n] == 1
4948 return true
5049 elseif vcolor[n] == 0
5150 # we store neighbors, but these are not yet on the path
52- vcolor[n] = 1
5351 push! (vertex_stack, n)
5452 end
5553 end
5654 else
57- vcolor[u] = 3
5855 pop! (vertex_stack)
56+ if vcolor[u] == 1
57+ vcolor[u] = 2
58+ end
5959 end
6060 end
6161 end
@@ -87,32 +87,33 @@ graph `g` as a vector of vertices in topological order.
8787function topological_sort_by_dfs end
8888# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
8989@traitfn function topological_sort_by_dfs (g:: AG :: IsDirected ) where {T,AG<: AbstractGraph{T} }
90- # 0 if not visited, 1 if visited, 2 if in the current dfs path, 3 if fully explored
90+ # 0 if not visited, 1 if in the current dfs path, 2 if fully explored
9191 vcolor = zeros (UInt8, nv (g))
9292 verts = Vector {T} ()
9393 vertex_stack = Vector {T} ()
9494 for v in vertices (g)
9595 vcolor[v] != 0 && continue
9696 push! (vertex_stack, v)
97- vcolor[v] = 1
9897 while ! isempty (vertex_stack)
9998 u = vertex_stack[end ]
100- if vcolor[u] == 1
101- vcolor[u] = 2
99+ if vcolor[u] == 0
100+ vcolor[u] = 1
102101 for n in outneighbors (g, u)
103102 # we hit a loop when reaching back a vertex of the main path
104- if vcolor[n] == 2
103+ if vcolor[n] == 1
105104 error (" The input graph contains at least one loop." ) # TODO 0.7 should we use a different error?
106105 elseif vcolor[n] == 0
107106 # we store neighbors, but these are not yet on the path
108- vcolor[n] = 1
109107 push! (vertex_stack, n)
110108 end
111109 end
112- else
113- vcolor[u] = 3
110+ else
114111 pop! (vertex_stack)
115- pushfirst! (verts, u)
112+ # if vcolor[u] == 2, the vertex was already explored and added to verts
113+ if vcolor[u] == 1
114+ vcolor[u] = 2
115+ pushfirst! (verts, u)
116+ end
116117 end
117118 end
118119 end
0 commit comments