# Finding an Eulerian Path

In Udacity’s Introduction to Algorithms course, one of the challenge problems asks you to define a function that, given a graph that contains an Eulerian path, returns one such path. I’ve reproduced my solution below. The solutions posted on the Udacity forum where often in two camps: somewhat verbose imperative, and very concise functional ones that implemented Hierholzer’s algorithm for finding cycles in Eulerian paths. My solution is somewhat in the middle. It’s relatively concise, but it makes use of an additional data structure I found helpful as it allows to easily inspect the state during program execution: a list containing the number of unused edges per node. I think it’s quite easy to follow the flow of execution, but I’ll walk you through step by step afterwards.

Here is the program listing:

```def find_eulerian_tour(graph):

def freqencies():
my_list = [x for (x, y) in graph]
result = [0 for i in range(max(my_list) + 1)]
for i in my_list:
result[i] += 1
return result

def find_node(tour):
for i in tour:
if freq[i] != 0:
return i
return -1

def helper(tour, next):
find_path(tour, next)
u = find_node(tour)
while sum(freq) != 0:
sub = find_path([], u)
tour = tour[:tour.index(u)] + sub + tour[tour.index(u) + 1:]
u = find_node(tour)

def find_path(tour, next):
for (x, y) in graph:
if x == next:
current = graph.pop(graph.index((x,y)))
graph.pop(graph.index((current, current)))
tour.append(current)
freq[current] -= 1
freq[current] -= 1
return find_path(tour, current)
tour.append(next)

graph += [(y, x) for (x, y) in graph]
freq = freqencies()
return helper([], graph)
```

The graph is given as a list of tuples. Tuple (x, y) indicates that node x has an (undirected) edge to node y. In line 37 the list with nodes is enlarged by adding tuples (y, x) to it, which reflects the fact that the edges are undirected.

In addition, a list of the number of edges per node (“freq”) is created and updated as the list with the eventual Eulerian tour is built. This means that whenever a node that is part of the Eulerian tour is found, the two affected nodes have their number of edges reduced by one.

If the graph has cycles, it is possible that the first pass of find_path() doesn’t process part of the graph. In this case, the program checks whether the sum of the list “freq” is equal to zero. If it is, then the Eulerian tour has been computed since there are no unused edges left. On the other hand, if “sum(freq)” is greater than zero, then unused edges are left, so the program proceeds finding a node with unused edges, finds another cycle, and inserts it into the resulting Eulerian tour. This step will be repeated for as long as there are unused edges in the graph.

Please note that any node can be used as a staring point. This follows from the existence of an Eulerian path, which uses all edges. Since it contains all edges, you can pick any node and from there pick any edge to start the Eulerian tour with.

On the Udacity forums I found a very helpful test case, which is a graph with three cycles. When working on the program, I used this and two derivative cases to test and guide development. Here they are:

```graph1 = [(0, 1), (1, 2), (2, 0)]
# returns [0, 1, 2, 0]

graph2 = [(0, 1), (1, 2), (2, 0), (2, 3), (3, 4), (4, 2)]
# returns [0, 1, 2, 3, 4, 2, 0]

graph3 = [(0, 1), (1, 2), (2, 0), (2, 3), (3, 4), (4, 2), (4, 5), (5, 6), (6, 4)]
# returns [0, 1, 2, 3, 4, 5, 6, 4, 2, 0]
```

## 1 thought on “Finding an Eulerian Path”

1. MOOC@Udacity

please can you explain the find_path() function in detail ,i am stuck

This site uses Akismet to reduce spam. Learn how your comment data is processed.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8905957,"math_prob":0.9807599,"size":3524,"snap":"2023-40-2023-50","text_gpt3_token_len":972,"char_repetition_ratio":0.11818182,"word_repetition_ratio":0.04347826,"special_character_ratio":0.29909194,"punctuation_ratio":0.17197452,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9939422,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-04T06:28:55Z\",\"WARC-Record-ID\":\"<urn:uuid:e74a81bc-5140-4abb-a345-44974843f9ee>\",\"Content-Length\":\"57241\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:249bc56d-76d1-4e04-87f6-9830d1fd3d36>\",\"WARC-Concurrent-To\":\"<urn:uuid:a75389e3-06fd-4433-9c3d-1e25976ece3a>\",\"WARC-IP-Address\":\"198.187.29.25\",\"WARC-Target-URI\":\"https://gregorulm.com/finding-an-eulerian-path/\",\"WARC-Payload-Digest\":\"sha1:LRDF52IVFFX7VFG4NXI2456WGTXDOO5N\",\"WARC-Block-Digest\":\"sha1:THD2MK6YBYFT4QSQR4QW55KBY44QVY6H\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233511361.38_warc_CC-MAIN-20231004052258-20231004082258-00864.warc.gz\"}"}