-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
#Given a graph, traverse it from start vertex to end vertex, covering each edge exactly once. | ||
#Complexity is O(length(edges(g))) | ||
function eulerian_path(g::AbstractGraph, start_vertex, end_vertex) | ||
#Conditions on g for the required path to exist | ||
if start_vertex != end_vertex | ||
@assert isodd(degree(g, start_vertex) % 2) | ||
@assert isodd(degree(g, end_vertex) % 2) | ||
@assert all( | ||
x -> iseven(x), degrees(g, setdiff(vertices(g), [start_vertex, end_vertex])) | ||
) | ||
else | ||
@assert all(x -> iseven(x), degrees(g, vertices(g))) | ||
end | ||
|
||
path = [] | ||
stack = [] | ||
current_vertex = end_vertex | ||
g_modified = copy(g) | ||
while !isempty(stack) || !iszero(degree(g_modified, current_vertex)) | ||
if iszero(degree(g_modified, current_vertex)) | ||
append!(path, current_vertex) | ||
last_vertex = pop!(stack) | ||
current_vertex = last_vertex | ||
else | ||
append!(stack, current_vertex) | ||
vn = first(neighbors(g_modified, current_vertex)) | ||
rem_edge!(g_modified, edgetype(g_modified)(current_vertex, vn)) | ||
current_vertex = vn | ||
end | ||
end | ||
|
||
append!(path, current_vertex) | ||
|
||
return edgetype(g_modified)[ | ||
edgetype(g_modified)(path[i], path[i + 1]) for i in 1:(length(path) - 1) | ||
] | ||
end | ||
|
||
function eulerian_cycle(g::AbstractGraph, start_vertex) | ||
return eulerian_path(g, start_vertex, start_vertex) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters