Fleury's Algorithm for printing Eulerian Path or Circuit
Last Updated : 12 Jun, 2025
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:

Output: 4-3, 3-0, 0-1, 1-2, 2-0
Input:

Output: 2-1, 1-0, 0-3, 3-4, 4-0, 0-2
Input:

Output: 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.
Approach:
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'.
- We first find the starting point which must be an odd vertex (if there are odd vertices) and store it in variable ‘u’. If there are zero odd vertices, we start from vertex '0'.
- We call printEulerUtil() to print Euler tour starting with u. We traverse all adjacent vertices of u, if there is only one adjacent vertex, we immediately consider it. If there are more than one adjacent vertices, we consider an adjacent v only if edge u-v is not a bridge.
- How to find if a given edge is a bridge? We count vertices reachable from u. We remove edge u-v and again count the number of reachable vertices from u. If the number of reachable vertices is reduced, then edge u-v is a bridge. To count reachable vertices, we can either use BFS or DFS, we have used DFS in the below code.
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.
C++ // C++ program to print Eulerian Path or // Circuit using Fleury’s Algorithm #include <bits/stdc++.h> using namespace std; // Function to remove edge u-v from the graph void removeEdge(vector<int> adj[], int u, int v) { adj[u].erase(find(adj[u].begin(), adj[u].end(), v)); adj[v].erase(find(adj[v].begin(), adj[v].end(), u)); } // DFS to count reachable vertices from v void dfsCount(int v, vector<int> adj[], vector<bool> &visited) { visited[v] = true; for (int neighbor : adj[v]) { if (!visited[neighbor]) { dfsCount(neighbor, adj, visited); } } } // Check if edge u-v is a valid next edge to traverse bool isValidNextEdge(int u, int v, vector<int> adj[], int totalV) { if (adj[u].size() == 1) { return true; } vector<bool> visited(totalV, false); int count1 = 0; dfsCount(u, adj, visited); for (bool x : visited) { if (x) { count1++; } } removeEdge(adj, u, v); fill(visited.begin(), visited.end(), false); int count2 = 0; dfsCount(u, adj, visited); for (bool x : visited) { if (x) { count2++; } } adj[u].push_back(v); adj[v].push_back(u); return count1 == count2; } // Recursively collect the Eulerian // path/circuit starting from u void getEulerUtil(int u, vector<int> adj[], vector<vector<int>> &edges, int v) { for (int i = 0; i < adj[u].size(); ++i) { int next = adj[u][i]; if (isValidNextEdge(u, next, adj, v)) { edges.push_back({u, next}); removeEdge(adj, u, next); getEulerUtil(next, adj, edges, v); break; } } } // Function to return Eulerian trail or circuit vector<vector<int>> getEulerTour(int v, vector<int> adj[]) { int start = 0; // Find a vertex with odd degree if exists for (int i = 0; i < v; i++) { if (adj[i].size() % 2 != 0) { start = i; break; } } vector<vector<int>> edges; getEulerUtil(start, adj, edges, v); return edges; } // Driver code int main() { int v = 5; vector<int> adj[5] = {{1, 2}, {0, 2}, {0, 1, 3}, {2}}; vector<vector<int>> res = getEulerTour(v, adj); for (int i = 0; i < res.size(); i++) { cout << res[i][0] << "-" << res[i][1]; if (i != res.size() - 1) { cout << ", "; } } return 0; }
Java // Java program to print Eulerian Path or // Circuit using Fleury’s Algorithm import java.util.*; class GfG { // Function to remove edge u-v from the graph static void removeEdge(List<Integer>[] adj, int u, int v) { adj[u].remove(Integer.valueOf(v)); adj[v].remove(Integer.valueOf(u)); } // DFS to count reachable vertices from v static void dfsCount(int v, List<Integer>[] adj, boolean[] visited) { visited[v] = true; for (int neighbor : adj[v]) { if (!visited[neighbor]) { dfsCount(neighbor, adj, visited); } } } // Check if edge u-v is a valid next edge to traverse static boolean isValidNextEdge(int u, int v, List<Integer>[] adj, int totalV) { if (adj[u].size() == 1) { return true; } boolean[] visited = new boolean[totalV]; int count1 = 0; dfsCount(u, adj, visited); for (boolean x : visited) { if (x) { count1++; } } removeEdge(adj, u, v); Arrays.fill(visited, false); int count2 = 0; dfsCount(u, adj, visited); for (boolean x : visited) { if (x) { count2++; } } adj[u].add(v); adj[v].add(u); return count1 == count2; } // Recursively collect the Eulerian // path/circuit starting from u static void getEulerUtil(int u, List<Integer>[] adj, List<int[]> edges, int v) { for (int i = 0; i < adj[u].size(); ++i) { int next = adj[u].get(i); if (isValidNextEdge(u, next, adj, v)) { edges.add(new int[]{u, next}); removeEdge(adj, u, next); getEulerUtil(next, adj, edges, v); break; } } } // Function to return Eulerian trail or circuit static List<int[]> getEulerTour(int v, List<Integer>[] adj) { int start = 0; // Find a vertex with odd degree if exists for (int i = 0; i < v; i++) { if (adj[i].size() % 2 != 0) { start = i; break; } } List<int[]> edges = new ArrayList<>(); getEulerUtil(start, adj, edges, v); return edges; } public static void main(String[] args) { int v = 4; List<Integer>[] adj = new ArrayList[4]; for (int i = 0; i < 4; i++) { adj[i] = new ArrayList<>(); } adj[0].add(1); adj[0].add(2); adj[1].add(0); adj[1].add(2); adj[2].add(0); adj[2].add(1); adj[2].add(3); adj[3].add(2); List<int[]> res = getEulerTour(v, adj); for (int i = 0; i < res.size(); i++) { System.out.print(res.get(i)[0] + "-" + res.get(i)[1]); if (i != res.size() - 1) { System.out.print(", "); } } } }
Python # Python program to print Eulerian Path or # Circuit using Fleury’s Algorithm # Function to remove edge u-v from the graph def removeEdge(adj, u, v): adj[u].remove(v) adj[v].remove(u) # DFS to count reachable vertices from v def dfsCount(v, adj, visited): visited[v] = True for neighbor in adj[v]: if not visited[neighbor]: dfsCount(neighbor, adj, visited) # Check if edge u-v is a valid next edge to traverse def isValidNextEdge(u, v, adj, totalV): if len(adj[u]) == 1: return True visited = [False] * totalV count1 = 0 dfsCount(u, adj, visited) count1 = sum(visited) removeEdge(adj, u, v) visited = [False] * totalV count2 = 0 dfsCount(u, adj, visited) count2 = sum(visited) adj[u].append(v) adj[v].append(u) return count1 == count2 # Recursively collect the Eulerian # path/circuit starting from u def getEulerUtil(u, adj, edges, v): for i in range(len(adj[u])): next = adj[u][i] if isValidNextEdge(u, next, adj, v): edges.append([u, next]) removeEdge(adj, u, next) getEulerUtil(next, adj, edges, v) break # Function to return Eulerian trail or circuit def getEulerTour(v, adj): start = 0 # Find a vertex with odd degree if exists for i in range(v): if len(adj[i]) % 2 != 0: start = i break edges = [] getEulerUtil(start, adj, edges, v) return edges if __name__ == "__main__": v = 4 adj = [[1, 2], [0, 2], [0, 1, 3], [2]] res = getEulerTour(v, adj) for i in range(len(res)): print(f"{res[i][0]}-{res[i][1]}", end="") if i != len(res) - 1: print(", ", end="")
C# // C# program to print Eulerian Path or // Circuit using Fleury’s Algorithm using System; using System.Collections.Generic; class GfG { // Function to remove edge u-v from the graph static void removeEdge(List<int>[] adj, int u, int v) { adj[u].Remove(v); adj[v].Remove(u); } // DFS to count reachable vertices from v static void dfsCount(int v, List<int>[] adj, bool[] visited) { visited[v] = true; foreach (int neighbor in adj[v]) { if (!visited[neighbor]) { dfsCount(neighbor, adj, visited); } } } // Check if edge u-v is a valid next edge to traverse static bool isValidNextEdge(int u, int v, List<int>[] adj, int totalV) { if (adj[u].Count == 1) { return true; } bool[] visited = new bool[totalV]; int count1 = 0; dfsCount(u, adj, visited); foreach (bool x in visited) { if (x) { count1++; } } removeEdge(adj, u, v); Array.Clear(visited, 0, visited.Length); int count2 = 0; dfsCount(u, adj, visited); foreach (bool x in visited) { if (x) { count2++; } } adj[u].Add(v); adj[v].Add(u); return count1 == count2; } // Recursively collect the Eulerian // path/circuit starting from u static void getEulerUtil(int u, List<int>[] adj, List<int[]> edges, int v) { for (int i = 0; i < adj[u].Count; ++i) { int next = adj[u][i]; if (isValidNextEdge(u, next, adj, v)) { edges.Add(new int[] { u, next }); removeEdge(adj, u, next); getEulerUtil(next, adj, edges, v); break; } } } // Function to return Eulerian trail or circuit static List<int[]> getEulerTour(int v, List<int>[] adj) { int start = 0; // Find a vertex with odd degree if exists for (int i = 0; i < v; i++) { if (adj[i].Count % 2 != 0) { start = i; break; } } List<int[]> edges = new List<int[]>(); getEulerUtil(start, adj, edges, v); return edges; } static void Main() { int v = 4; List<int>[] adj = new List<int>[4]; for (int i = 0; i < 4; i++) { adj[i] = new List<int>(); } adj[0].Add(1); adj[0].Add(2); adj[1].Add(0); adj[1].Add(2); adj[2].Add(0); adj[2].Add(1); adj[2].Add(3); adj[3].Add(2); List<int[]> res = getEulerTour(v, adj); for (int i = 0; i < res.Count; i++) { Console.Write(res[i][0] + "-" + res[i][1]); if (i != res.Count - 1) { Console.Write(", "); } } } }
JavaScript // Javascript program to print Eulerian Path or // Circuit using Fleury’s Algorithm // Function to remove edge u-v from the graph function removeEdge(adj, u, v) { adj[u].splice(adj[u].indexOf(v), 1); adj[v].splice(adj[v].indexOf(u), 1); } // DFS to count reachable vertices from v function dfsCount(v, adj, visited) { visited[v] = true; for (let neighbor of adj[v]) { if (!visited[neighbor]) { dfsCount(neighbor, adj, visited); } } } // Check if edge u-v is a valid next edge to traverse function isValidNextEdge(u, v, adj, totalV) { if (adj[u].length === 1) { return true; } let visited = Array(totalV).fill(false); dfsCount(u, adj, visited); let count1 = visited.filter(x => x).length; removeEdge(adj, u, v); visited.fill(false); dfsCount(u, adj, visited); let count2 = visited.filter(x => x).length; adj[u].push(v); adj[v].push(u); return count1 === count2; } // Recursively collect the Eulerian // path/circuit starting from u function getEulerUtil(u, adj, edges, v) { for (let i = 0; i < adj[u].length; ++i) { let next = adj[u][i]; if (isValidNextEdge(u, next, adj, v)) { edges.push([u, next]); removeEdge(adj, u, next); getEulerUtil(next, adj, edges, v); break; } } } // Function to return Eulerian trail or circuit function getEulerTour(v, adj) { let start = 0; // Find a vertex with odd degree if exists for (let i = 0; i < v; i++) { if (adj[i].length % 2 !== 0) { start = i; break; } } let edges = []; getEulerUtil(start, adj, edges, v); return edges; } // Driver Code let v = 4; let adj = [[1, 2], [0, 2], [0, 1, 3], [2]]; let res = getEulerTour(v, adj); for (let i = 0; i < res.length; i++) { process.stdout.write(res[i][0] + "-" + res[i][1]); if (i !== res.length - 1) { process.stdout.write(", "); } }
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.
Similar Reads
Graph Algorithms Graph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Introduction to Graph Data Structure Graph Data Structure is a non-linear data structure consisting of vertices and edges. It is useful in fields such as social network analysis, recommendation systems, and computer networks. In the field of sports data science, graph data structure can be used to analyze and understand the dynamics of
15+ min read
Graph and its representations A Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is den
12 min read
Types of Graphs with Examples A graph is a mathematical structure that represents relationships between objects by connecting a set of points. It is used to establish a pairwise relationship between elements in a given set. graphs are widely used in discrete mathematics, computer science, and network theory to represent relation
9 min read
Basic Properties of a Graph A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph. The basic properties of a graph include: Vertices (nodes): The points where edges meet in a graph are kn
4 min read
Applications, Advantages and Disadvantages of Graph Graph is a non-linear data structure that contains nodes (vertices) and edges. A graph is a collection of set of vertices and edges (formed by connecting two vertices). A graph is defined as G = {V, E} where V is the set of vertices and E is the set of edges. Graphs can be used to model a wide varie
7 min read
Transpose graph Transpose of a directed graph G is another directed graph on the same set of vertices with all of the edges reversed compared to the orientation of the corresponding edges in G. That is, if G contains an edge (u, v) then the converse/transpose/reverse of G contains an edge (v, u) and vice versa. Giv
9 min read
Difference Between Graph and Tree Graphs and trees are two fundamental data structures used in computer science to represent relationships between objects. While they share some similarities, they also have distinct differences that make them suitable for different applications. Difference Between Graph and Tree What is Graph?A grap
2 min read
BFS and DFS on Graph
Breadth First Search or BFS for a GraphGiven a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Depth First Search or DFS for a GraphIn Depth First Search (or DFS) for a graph, we traverse all adjacent vertices one by one. When we traverse an adjacent vertex, we completely finish the traversal of all vertices reachable through that adjacent vertex. This is similar to a tree, where we first completely traverse the left subtree and
13 min read
Applications, Advantages and Disadvantages of Depth First Search (DFS)Depth First Search is a widely used algorithm for traversing a graph. Here we have discussed some applications, advantages, and disadvantages of the algorithm. Applications of Depth First Search:1. Detecting cycle in a graph: A graph has a cycle if and only if we see a back edge during DFS. So we ca
4 min read
Applications, Advantages and Disadvantages of Breadth First Search (BFS)We have earlier discussed Breadth First Traversal Algorithm for Graphs. Here in this article, we will see the applications, advantages, and disadvantages of the Breadth First Search. Applications of Breadth First Search: 1. Shortest Path and Minimum Spanning Tree for unweighted graph: In an unweight
4 min read
Iterative Depth First Traversal of GraphGiven a directed Graph, the task is to perform Depth First Search of the given graph.Note: Start DFS from node 0, and traverse the nodes in the same order as adjacency list.Note : There can be multiple DFS traversals of a graph according to the order in which we pick adjacent vertices. Here we pick
10 min read
BFS for Disconnected GraphIn the previous post, BFS only with a particular vertex is performed i.e. it is assumed that all vertices are reachable from the starting vertex. But in the case of a disconnected graph or any vertex that is unreachable from all vertex, the previous implementation will not give the desired output, s
14 min read
Transitive Closure of a Graph using DFSGiven a directed graph, find out if a vertex v is reachable from another vertex u for all vertex pairs (u, v) in the given graph. Here reachable means that there is a path from vertex u to v. The reach-ability matrix is called transitive closure of a graph. For example, consider below graph: GraphTr
8 min read
Difference between BFS and DFSBreadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used for traversing or searching graphs and trees. This article covers the basic difference between Breadth-First Search and Depth-First Search.Difference between BFS and DFSParametersBFSDFSStands forBFS stands fo
2 min read
Cycle in a Graph
Detect Cycle in a Directed GraphGiven the number of vertices V and a list of directed edges, determine whether the graph contains a cycle or not.Examples: Input: V = 4, edges[][] = [[0, 1], [0, 2], [1, 2], [2, 0], [2, 3]]Cycle: 0 â 2 â 0 Output: trueExplanation: The diagram clearly shows a cycle 0 â 2 â 0 Input: V = 4, edges[][] =
15+ min read
Detect cycle in an undirected graphGiven an undirected graph, the task is to check if there is a cycle in the given graph.Examples:Input: V = 4, edges[][]= [[0, 1], [0, 2], [1, 2], [2, 3]]Undirected Graph with 4 vertices and 4 edgesOutput: trueExplanation: The diagram clearly shows a cycle 0 â 2 â 1 â 0Input: V = 4, edges[][] = [[0,
8 min read
Detect Cycle in a directed graph using colorsGiven a directed graph represented by the number of vertices V and a list of directed edges, determine whether the graph contains a cycle.Your task is to implement a function that accepts V (number of vertices) and edges (an array of directed edges where each edge is a pair [u, v]), and returns true
9 min read
Detect a negative cycle in a Graph | (Bellman Ford)Given a directed weighted graph, your task is to find whether the given graph contains any negative cycles that are reachable from the source vertex (e.g., node 0).Note: A negative-weight cycle is a cycle in a graph whose edges sum to a negative value.Example:Input: V = 4, edges[][] = [[0, 3, 6], [1
15+ min read
Cycles of length n in an undirected and connected graphGiven an undirected and connected graph and a number n, count the total number of simple cycles of length n in the graph. A simple cycle of length n is defined as a cycle that contains exactly n vertices and n edges. Note that for an undirected graph, each cycle should only be counted once, regardle
10 min read
Detecting negative cycle using Floyd WarshallWe are given a directed graph. We need compute whether the graph has negative cycle or not. A negative cycle is one in which the overall sum of the cycle comes negative. Negative weights are found in various applications of graphs. For example, instead of paying cost for a path, we may get some adva
12 min read
Clone a Directed Acyclic GraphA directed acyclic graph (DAG) is a graph which doesn't contain a cycle and has directed edges. We are given a DAG, we need to clone it, i.e., create another graph that has copy of its vertices and edges connecting them. Examples: Input : 0 - - - > 1 - - - -> 4 | / \ ^ | / \ | | / \ | | / \ |
12 min read