![]() |
VOOZH | about |
Given an undirected connected graph with v nodes, and e edges, with adjacency list adj. The task is to print an Eulerian trail or circuit using Fleury's Algorithm
A graph is said to be Eulerian if it contains an Eulerian Cycle, a cycle that visits every edge exactly once and starts and ends at the same vertex.
If a graph contains an Eulerian Path, a path that visits every edge exactly once but starts and ends at different vertices
Examples:
Input:
π Euler1
Output: 4-3, 3-0, 0-1, 1-2, 2-0
Input:
π Euler2
Output: 2-1, 1-0, 0-3, 3-4, 4-0, 0-2
Input:
π Euler3Output: 0
Before diving into this approach, it's highly recommended to explore this comprehensive article on Eulerian Path and Circuit. Please refer to this Link: Eulerian path and circuit for undirected graph
The article clearly explains the fundamentals and conditions for identifying whether a graph contains an Eulerian Path or Eulerian Circuit, laying a strong foundation for the solution weβre about to implement.
The idea is to use Fleuryβs Algorithm to print an Eulerian Path or Eulerian Circuit from a graph by carefully selecting edges during traversal. Here's how the algorithm works:
- Ensure the graph has either 0 or 2 vertices of odd degree.
- If there are 0 odd-degree vertices, you can start from any vertex.
- If there are 2 odd-degree vertices, you must start from one of them.
- At each step, follow one edge at a time, making sure to prefer non-bridge edges whenever possible. A bridge is an edge that, if removed, increases the number of disconnected components.
- Continue traversing until all edges are used exactly once.
This approach ensures that the path remains valid and no part of the graph becomes unreachable prematurely.
The idea is, "don't burn bridges" so that we can come back to a vertex and traverse the remaining edges.
For example, let us consider the following graph.
There are two vertices with odd degrees, '2' and '3', and we can start paths from any of them. Let us start the tour from vertex '2'.
Three edges are going out from vertex '2', which one to pick? We don't pick the edge '2-3' because that is a bridge (we won't be able to come back to '3'). We can pick any of the remaining two edges. Let us say we pick '2-0'. We remove this edge and move to vertex '0'.
There is only one edge from vertex '0', so we pick it, remove it and move to vertex '1'. Euler tour becomes '2-0 0-1'.
There is only one edge from vertex '1', so we pick it, remove it and move to vertex '2'. Euler tour becomes '2-0 0-1 1-2'
Again there is only one edge from vertex 2, so we pick it, remove it and move to vertex 3. Euler tour becomes '2-0 0-1 1-2 2-3'
There are no more edges left, so we stop here. Final tour is '2-0 0-1 1-2 2-3'.
Once an edge is processed (included in the Euler tour), we remove it from the graph. To remove the edge, we replace the vertex entry with -1 in the adjacency list. Note that simply deleting the node may not work as the code is recursive and a parent call may be in the middle of the adjacency list.
2-0, 0-1, 1-2, 2-3
Time Complexity: O(eΒ²), each edge is checked for bridge status using DFS before traversal.
Space complexity: O(v + e), adjacency list, visited array, and result storage require linear.