Skip to content

Commit 8b2cb94

Browse files
authored
Fix checks for Eulerian trail (#397)
1 parent aa3d197 commit 8b2cb94

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/traversals/eulerian.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function _check_eulerian_input(g, u)
9595
)
9696
end
9797
else # isodd(du) # trail: start (u) != stop (v) - all nodes, except u and v, must have even degree
98-
if count(x -> iseven(degree(g, x)), vertices(g)) != 2
98+
if count(x -> isodd(degree(g, x)), vertices(g)) != 2
9999
error(
100100
"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",
101101
)

test/traversals/eulerian.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55
@test last(eulerian(g0, 1)) == 1 # a cycle
66

77
# a tour (different start/end)
8-
g1 = GenericGraph(SimpleGraph([Edge(1, 2), Edge(2, 3), Edge(3, 4)]))
9-
@test eulerian(g1, 1) == [1, 2, 3, 4]
8+
g1_3 = GenericGraph(SimpleGraph([Edge(1, 2), Edge(2, 3)]))
9+
@test eulerian(g1_3, 3) == [3, 2, 1]
10+
g1_4 = GenericGraph(SimpleGraph([Edge(1, 2), Edge(2, 3), Edge(3, 4)]))
11+
@test eulerian(g1_4, 1) == [1, 2, 3, 4]
12+
# in a graph with an Eulerian trail but not cycle, you have to start at an odd degree vertex
1013
@test_throws ErrorException(
1114
"starting vertex has even degree but there are other vertices with odd degree: a eulerian cycle does not exist",
12-
) eulerian(g1, 2)
15+
) eulerian(g1_3, 2)
16+
# too many odd degree vertices; Eulerian cycle does not exist
17+
# 3-pointed star with vertex 1 at the center
18+
g_star = GenericGraph(SimpleGraph([Edge(1, 2), Edge(1, 3), Edge(1, 4)]))
19+
@test_throws ErrorException(
20+
"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",
21+
) eulerian(g_star, 1)
1322

1423
# a cycle with a node (vertex 2) with multiple neighbors
1524
g2 = GenericGraph(

0 commit comments

Comments
 (0)