Skip to content
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

Fix sanity checks for Eulerian trail #397

Merged
merged 1 commit into from
Sep 5, 2024
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
2 changes: 1 addition & 1 deletion src/traversals/eulerian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function _check_eulerian_input(g, u)
)
end
else # isodd(du) # trail: start (u) != stop (v) - all nodes, except u and v, must have even degree
if count(x -> iseven(degree(g, x)), vertices(g)) != 2
if count(x -> isodd(degree(g, x)), vertices(g)) != 2
error(
"starting vertex has odd degree but the total number of vertices of odd degree is not equal to 2: a eulerian trail does not exist",
)
Expand Down
15 changes: 12 additions & 3 deletions test/traversals/eulerian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
@test last(eulerian(g0, 1)) == 1 # a cycle

# a tour (different start/end)
g1 = GenericGraph(SimpleGraph([Edge(1, 2), Edge(2, 3), Edge(3, 4)]))
@test eulerian(g1, 1) == [1, 2, 3, 4]
g1_3 = GenericGraph(SimpleGraph([Edge(1, 2), Edge(2, 3)]))
@test eulerian(g1_3, 3) == [3, 2, 1]
g1_4 = GenericGraph(SimpleGraph([Edge(1, 2), Edge(2, 3), Edge(3, 4)]))
@test eulerian(g1_4, 1) == [1, 2, 3, 4]
# in a graph with an Eulerian trail but not cycle, you have to start at an odd degree vertex
@test_throws ErrorException(
"starting vertex has even degree but there are other vertices with odd degree: a eulerian cycle does not exist",
) eulerian(g1, 2)
) eulerian(g1_3, 2)
# too many odd degree vertices; Eulerian cycle does not exist
# 3-pointed star with vertex 1 at the center
g_star = GenericGraph(SimpleGraph([Edge(1, 2), Edge(1, 3), Edge(1, 4)]))
@test_throws ErrorException(
"starting vertex has odd degree but the total number of vertices of odd degree is not equal to 2: a eulerian trail does not exist",
) eulerian(g_star, 1)

# a cycle with a node (vertex 2) with multiple neighbors
g2 = GenericGraph(
Expand Down
Loading