Last active
August 19, 2025 03:04
-
-
Save siavashk/3bafa26a6df6325b27f140e301b29923 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def recursive_eulerian_path(adj, start_node): | |
def dfs(start): | |
stack = [start] | |
path = [] | |
while stack: | |
while temp_adj[stack[-1]]: | |
next_node = adj[stack[-1]].pop() | |
stack.append(next_node) | |
path.append(stack.pop()) | |
return path[::-1] | |
return dfs(start_node) | |
def iterative_eulerian_path(start, adj): | |
stack = [start] | |
path = [] | |
while stack: | |
u = stack[-1] | |
if adj[u]: # while current node still has edges | |
stack.append(adj[u].pop()) # follow one edge | |
else: | |
path.append(stack.pop()) # backtrack when no edges left | |
return path[::-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment