Given a directed graph, check whether it has an Eulerian path or not. An Eulerian path (or Eulerian trail) is a path in a graph that visits every edge exactly once.
The following graph has an Eulerian path since it is possible to construct a path that visits each edge exactly once. The Eulerian path is 0 —> 1 —> 2 —> 3 —> 0 —> 5 —> 4 —> 3 —> 1 —> 4 .
Related Post:
A directed graph has an Eulerian path if and only if the following conditions are satisfied:
For example, in the above graph, the only vertex 0 has out-degree one more than the in-degree, and vertex 4 has in-degree one more than the out-degree. Every other vertex has equal in-degree and out-degree, and the non-isolated vertices are weakly connected. The algorithm can be implemented as follows in C++, Java, and Python: