2
votes

I managed to create an algorithm that finds an eulerian path(if there is one) in an undirected connected graph with time complexity O(k^2 * n) where:

k: number of edges

n: number of nodes

I would like to know if there is a better algorithm, and if yes the idea behind it.

Thanks in advance! :)

1
You can get to O(k) with Hierholzer's algorithm. - Nico Schertler
Thanks for the answer :) - gdaras

1 Answers

4
votes

Hierholzer's algorithm runs in O(k) time: https://en.wikipedia.org/wiki/Eulerian_path#Hierholzer.27s_algorithm

First you find a path between the two vertices with odd degree. Then as long as you have a vertex on the path with unused edges, follow unused edges from that vertex until you get back to that vertex again, and then merge in the new path.

If there are no vertices with odd degree then you can just start with an empty path at any vertex.

If the number of vertices with odd degree is not 0 or 2, then there is no Eulerian path.