Skip to content

fix is_cyclic #168

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

Merged
merged 18 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 21 additions & 2 deletions src/traversals/dfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@
Return `true` if graph `g` contains a cycle.

### Implementation Notes
Uses DFS.
The algorithm uses a DFS. Self-loops are counted as cycles.
"""
function is_cyclic end
@traitfn is_cyclic(g::::(!IsDirected)) = ne(g) > 0
@enum Vertex_state unvisited visited
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@enum Vertex_state unvisited visited

Since this enum is binary, I believe it's clearer to replace it by a vector of booleans. It's actually also slightly more memory-friendly since those can be nicely packed into a BitVector

@traitfn function is_cyclic(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
visited = falses(nv(g))
for v in vertices(g)
visited[v] && continue
visited[v] = true
S = [(v,v)]
while !isempty(S)
parent, w = pop!(S)
for u in neighbors(g, w)
u == w && return true # self-loop
u == parent && continue
visited[u] && return true
visited[u] = true
push!(S, (w, u))
end
end
end
return false
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct to me, but there is a small edge that we also have to consider: If an undirected graph has a self-loop (i.e. a vertex that is connected to itself), then this should also count as a cycle in my opinion. (or if not, at least we have to document this).

# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function is_cyclic(g::AG::IsDirected) where {T, AG<:AbstractGraph{T}}
vcolor = zeros(UInt8, nv(g))
Expand Down
33 changes: 30 additions & 3 deletions test/traversals/dfs.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
@testset "DFS" begin

gnodes_directed=SimpleDiGraph(4)
gnodes_undirected=SimpleGraph(4)
gloop_directed=SimpleDiGraph(1)
add_edge!(gloop_directed, 1, 1);
gloop_undirected=SimpleGraph(1)
add_edge!(gloop_undirected, 1, 1);
g5 = SimpleDiGraph(4)
add_edge!(g5, 1, 2); add_edge!(g5, 2, 3); add_edge!(g5, 1, 3); add_edge!(g5, 3, 4)
gx = cycle_digraph(3)

gcyclic = SimpleGraph([0 1 1 0 0; 1 0 1 0 0; 1 1 0 0 0; 0 0 0 0 1; 0 0 0 1 0])
gtree = SimpleGraph([0 1 1 1 0 0 0; 1 0 0 0 0 0 0; 1 0 0 0 0 0 0; 1 0 0 0 1 1 1; 0 0 0 1 0 0 0; 0 0 0 1 0 0 0; 0 0 0 1 0 0 0])
@testset "dfs_tree" begin
for g in testdigraphs(g5)
z = @inferred(dfs_tree(g, 1))
Expand All @@ -26,8 +32,29 @@

@testset "is_cyclic" begin
for g in testgraphs(path_graph(2))
@test !@inferred(is_cyclic(g))
@test !@inferred(is_cyclic(zero(g)))
end
for g in testgraphs(gcyclic)
@test @inferred(is_cyclic(g))
end
for g in testgraphs(gtree)
@test !@inferred(is_cyclic(g))
end
for g in testgraphs(g5)
@test !@inferred(is_cyclic(g))
end
for g in testgraphs(gnodes_directed)
@test !@inferred(is_cyclic(g))
end
for g in testgraphs(gnodes_undirected)
@test !@inferred(is_cyclic(g))
end
for g in testgraphs(gloop_directed)
@test @inferred(is_cyclic(g))
end
for g in testgraphs(gloop_undirected)
@test @inferred(is_cyclic(g))
@test @inferred(!is_cyclic(zero(g)))
end
end

Expand Down