# Eulerian Path in undirected graph

Given an adjacency matrix representation of an undirected graph. Find if there is any Eulerian Path in the graph. If there is no path print “No Solution”. If there is any path print the path.

Examples:

```Input : [[0, 1, 0, 0, 1],
[1, 0, 1, 1, 0],
[0, 1, 0, 1, 0],
[0, 1, 1, 0, 0],
[1, 0, 0, 0, 0]]

Output : 5 -> 1 -> 2 -> 4 -> 3 -> 2

Input : [[0, 1, 0, 1, 1],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 1],
[1, 1, 1, 0, 0],
[1, 0, 1, 0, 0]]
Output : "No Solution"
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The base case of this problem is if number of vertices with odd number of edges(i.e. odd degree) is greater than 2 then there is no Eulerian path.
If it has solution and all the nodes has even number of edges then we can start our path from any of the nodes.
If it has solution and exactly two vertices has odd number of edges then we has to start our path from one of these two vertices.
There will not be the case where exactly one vertex has odd number of edges, as there is even number of edges in total.

Process to Find the Path:

1. First take an empty stack and an empty path.
2. If all the vertices has even number of edges then start from any of them. If two of the vertices has odd number of edges then start from one of them. Set variable current to this starting vertex.
3. If the current vertex has at least one adjacent node then first discover that node and then discover the current node by backtracking. To do so add the current node to stack, remove the edge between current node and neighbour node, set current to neighbour node.
4. If current node has not any neighbour then add it to path and pop stack set current to popped vertex.
5. Repeat process 3 and 4 until the stack is empty and current node has not any neighbour. After the process path variable holds the Eulerian path.

## C++

 `// Efficient C++ program to ` `// find out Eulerian path ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find out the path ` `// It takes the adjacency matrix  ` `// representation of the graph as input ` `void` `findpath(``int` `graph[], ``int` `n) ` `{ ` `    ``vector<``int``> numofadj; ` ` `  `    ``// Find out number of edges each vertex has ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``numofadj.push_back(accumulate(graph[i],  ` `                                      ``graph[i] + 5, 0)); ` ` `  `    ``// Find out how many vertex has odd number edges ` `    ``int` `startpoint = 0, numofodd = 0; ` `    ``for` `(``int` `i = n - 1; i >= 0; i--) ` `    ``{ ` `        ``if` `(numofadj[i] % 2 == 1) ` `        ``{ ` `            ``numofodd++; ` `            ``startpoint = i; ` `        ``} ` `    ``} ` ` `  `    ``// If number of vertex with odd number of edges ` `    ``// is greater than two return "No Solution". ` `    ``if` `(numofodd > 2)  ` `    ``{ ` `        ``cout << ``"No Solution"` `<< endl; ` `        ``return``; ` `    ``} ` ` `  `    ``// If there is a path find the path ` `    ``// Initialize empty stack and path ` `    ``// take the starting current as discussed ` `    ``stack<``int``> stack; ` `    ``vector<``int``> path; ` `    ``int` `cur = startpoint; ` ` `  `    ``// Loop will run until there is element in the stack ` `    ``// or current edge has some neighbour. ` `    ``while` `(!stack.empty() or  ` `            ``accumulate(graph[cur],  ` `                       ``graph[cur] + 5, 0) != 0) ` `    ``{ ` `        ``// If current node has not any neighbour ` `        ``// add it to path and pop stack ` `        ``// set new current to the popped element ` `        ``if` `(accumulate(graph[cur], ` `                       ``graph[cur] + 5, 0) == 0) ` `        ``{ ` `            ``path.push_back(cur); ` `            ``cur = stack.top(); ` `            ``stack.pop(); ` `        ``} ` ` `  `        ``// If the current vertex has at least one ` `        ``// neighbour add the current vertex to stack, ` `        ``// remove the edge between them and set the ` `        ``// current to its neighbour. ` `        ``else`  `        ``{ ` `            ``for` `(``int` `i = 0; i < n; i++) ` `            ``{ ` `                ``if` `(graph[cur][i] == 1)  ` `                ``{ ` `                    ``stack.push(cur); ` `                    ``graph[cur][i] = 0; ` `                    ``graph[i][cur] = 0; ` `                    ``cur = i; ` `                    ``break``; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// print the path ` `    ``for` `(``auto` `ele : path) cout << ele << ``" -> "``; ` `    ``cout << cur << endl; ` `} ` ` `  `// Driver Code ` `int` `main()  ` `{ ` `    ``// Test case 1 ` `    ``int` `graph1[] = {{0, 1, 0, 0, 1}, ` `                       ``{1, 0, 1, 1, 0}, ` `                       ``{0, 1, 0, 1, 0}, ` `                       ``{0, 1, 1, 0, 0}, ` `                       ``{1, 0, 0, 0, 0}}; ` `    ``int` `n = ``sizeof``(graph1) / ``sizeof``(graph1); ` `    ``findpath(graph1, n); ` ` `  `    ``// Test case 2 ` `    ``int` `graph2[] = {{0, 1, 0, 1, 1}, ` `                       ``{1, 0, 1, 0, 1}, ` `                       ``{0, 1, 0, 1, 1}, ` `                       ``{1, 1, 1, 0, 0}, ` `                       ``{1, 0, 1, 0, 0}}; ` `    ``n = ``sizeof``(graph1) / ``sizeof``(graph1); ` `    ``findpath(graph2, n); ` ` `  `    ``// Test case 3 ` `    ``int` `graph3[] = {{0, 1, 0, 0, 1}, ` `                       ``{1, 0, 1, 1, 1}, ` `                       ``{0, 1, 0, 1, 0}, ` `                       ``{0, 1, 1, 0, 1}, ` `                       ``{1, 1, 0, 1, 0}}; ` `    ``n = ``sizeof``(graph1) / ``sizeof``(graph1); ` `    ``findpath(graph3, n); ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

## Java

 `// Efficient Java program to  ` `// find out Eulerian path  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find out the path ` `    ``// It takes the adjacency matrix ` `    ``// representation of the graph as input ` `    ``static` `void` `findpath(``int``[][] graph, ``int` `n) ` `    ``{ ` `        ``Vector numofadj = ``new` `Vector<>(); ` ` `  `        ``// Find out number of edges each vertex has ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``numofadj.add(accumulate(graph[i], ``0``)); ` ` `  `        ``// Find out how many vertex has odd number edges ` `        ``int` `startPoint = ``0``, numofodd = ``0``; ` `        ``for` `(``int` `i = n - ``1``; i >= ``0``; i--) ` `        ``{ ` `            ``if` `(numofadj.elementAt(i) % ``2` `== ``1``)  ` `            ``{ ` `                ``numofodd++; ` `                ``startPoint = i; ` `            ``} ` `        ``} ` ` `  `        ``// If number of vertex with odd number of edges ` `        ``// is greater than two return "No Solution". ` `        ``if` `(numofodd > ``2``) ` `        ``{ ` `            ``System.out.println(``"No Solution"``); ` `            ``return``; ` `        ``} ` ` `  `        ``// If there is a path find the path ` `        ``// Initialize empty stack and path ` `        ``// take the starting current as discussed ` `        ``Stack stack = ``new` `Stack<>(); ` `        ``Vector path = ``new` `Vector<>(); ` `        ``int` `cur = startPoint; ` ` `  `        ``// Loop will run until there is element in the stack ` `        ``// or current edge has some neighbour. ` `        ``while` `(!stack.isEmpty() || accumulate(graph[cur], ``0``) != ``0``) ` `        ``{ ` ` `  `            ``// If current node has not any neighbour ` `            ``// add it to path and pop stack ` `            ``// set new current to the popped element ` `            ``if` `(accumulate(graph[cur], ``0``) == ``0``) ` `            ``{ ` `                ``path.add(cur); ` `                ``cur = stack.pop(); ` ` `  `                ``// If the current vertex has at least one ` `                ``// neighbour add the current vertex to stack, ` `                ``// remove the edge between them and set the ` `                ``// current to its neighbour. ` `            ``}  ` `            ``else`  `            ``{ ` `                ``for` `(``int` `i = ``0``; i < n; i++) ` `                ``{ ` `                    ``if` `(graph[cur][i] == ``1``)  ` `                    ``{ ` `                        ``stack.add(cur); ` `                        ``graph[cur][i] = ``0``; ` `                        ``graph[i][cur] = ``0``; ` `                        ``cur = i; ` `                        ``break``; ` `                    ``} ` `                ``} ` `            ``} ` `        ``} ` ` `  `        ``// print the path ` `        ``for` `(``int` `ele : path) ` `            ``System.out.print(ele + ``" -> "``); ` `        ``System.out.println(cur); ` `    ``} ` ` `  `    ``static` `int` `accumulate(``int``[] arr, ``int` `sum) ` `    ``{ ` `        ``for` `(``int` `i : arr) ` `            ``sum += i; ` `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// Test case 1 ` `        ``int``[][] graph1 = { { ``0``, ``1``, ``0``, ``0``, ``1` `}, ` `                        ``{ ``1``, ``0``, ``1``, ``1``, ``0` `}, ` `                        ``{ ``0``, ``1``, ``0``, ``1``, ``0` `}, ` `                        ``{ ``0``, ``1``, ``1``, ``0``, ``0` `}, ` `                        ``{ ``1``, ``0``, ``0``, ``0``, ``0` `} }; ` `        ``int` `n = graph1.length; ` `        ``findpath(graph1, n); ` ` `  `        ``// Test case 2 ` `        ``int``[][] graph2 = { { ``0``, ``1``, ``0``, ``1``, ``1` `}, ` `                        ``{ ``1``, ``0``, ``1``, ``0``, ``1` `}, ` `                        ``{ ``0``, ``1``, ``0``, ``1``, ``1` `}, ` `                        ``{ ``1``, ``1``, ``1``, ``0``, ``0` `}, ` `                        ``{ ``1``, ``0``, ``1``, ``0``, ``0` `} }; ` `        ``n = graph2.length; ` `        ``findpath(graph2, n); ` ` `  `        ``// Test case 3 ` `        ``int``[][] graph3 = { { ``0``, ``1``, ``0``, ``0``, ``1` `}, ` `                        ``{ ``1``, ``0``, ``1``, ``1``, ``1` `}, ` `                        ``{ ``0``, ``1``, ``0``, ``1``, ``0` `}, ` `                        ``{ ``0``, ``1``, ``1``, ``0``, ``1` `}, ` `                        ``{ ``1``, ``1``, ``0``, ``1``, ``0` `} }; ` `        ``n = graph3.length; ` `        ``findpath(graph3, n); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

## Python3

 `# Efficient python3 program to find out Eulerian path ` ` `  `# Function to find out the path ` `# It takes the adjacency matrix representation of the ` `# graph as input ` `def` `findpath(graph): ` `    ``n ``=` `len``(graph) ` `    ``numofadj ``=` `list``() ` ` `  `    ``# Find out number of edges each vertex has ` `    ``for` `i ``in` `range``(n): ` `        ``numofadj.append(``sum``(graph[i])) ` ` `  `    ``# Find out how many vertex has odd number edges ` `    ``startpoint ``=` `0` `    ``numofodd ``=` `0` `    ``for` `i ``in` `range``(n``-``1``, ``-``1``, ``-``1``): ` `        ``if` `(numofadj[i] ``%` `2` `=``=` `1``): ` `            ``numofodd ``+``=` `1` `            ``startpoint ``=` `i ` ` `  `    ``# If number of vertex with odd number of edges ` `    ``# is greater than two return "No Solution". ` `    ``if` `(numofodd > ``2``): ` `        ``print``(``"No Solution"``) ` `        ``return`  ` `  `    ``# If there is a path find the path ` `    ``# Initialize empty stack and path ` `    ``# take the starting current as discussed ` `    ``stack ``=` `list``() ` `    ``path ``=` `list``() ` `    ``cur ``=` `startpoint ` ` `  `    ``# Loop will run until there is element in the stack ` `    ``# or current edge has some neighbour. ` `    ``while``(stack !``=` `[] ``or` `sum``(graph[cur]) !``=` `0``): ` `         `  `        ``# If current node has not any neighbour ` `        ``# add it to path and pop stack ` `        ``# set new current to the popped element ` `        ``if` `(``sum``(graph[cur]) ``=``=` `0``): ` `            ``path.append(cur ``+` `1``) ` `            ``cur ``=` `stack.pop(``-``1``) ` ` `  `        ``# If the current vertex has at least one ` `        ``# neighbour add the current vertex to stack, ` `        ``# remove the edge between them and set the ` `        ``# current to its neighbour. ` `        ``else``: ` `            ``for` `i ``in` `range``(n): ` `                ``if` `graph[cur][i] ``=``=` `1``: ` `                    ``stack.append(cur) ` `                    ``graph[cur][i] ``=` `0` `                    ``graph[i][cur] ``=` `0` `                    ``cur ``=` `i ` `                    ``break` `    ``# print the path ` `    ``for` `ele ``in` `path: ` `        ``print``(ele, ``"-> "``, end ``=` `'') ` `    ``print``(cur ``+` `1``) ` ` `  `# Driver Program ` `# Test case 1 ` `graph1 ``=` `[[``0``, ``1``, ``0``, ``0``, ``1``], ` `         ``[``1``, ``0``, ``1``, ``1``, ``0``], ` `         ``[``0``, ``1``, ``0``, ``1``, ``0``], ` `         ``[``0``, ``1``, ``1``, ``0``, ``0``], ` `         ``[``1``, ``0``, ``0``, ``0``, ``0``]] ` `findpath(graph1) ` ` `  `# Test case 2 ` `graph2 ``=` `[[``0``, ``1``, ``0``, ``1``, ``1``], ` `         ``[``1``, ``0``, ``1``, ``0``, ``1``], ` `         ``[``0``, ``1``, ``0``, ``1``, ``1``], ` `         ``[``1``, ``1``, ``1``, ``0``, ``0``], ` `         ``[``1``, ``0``, ``1``, ``0``, ``0``]] ` `findpath(graph2) ` ` `  `# Test case 3 ` `graph3 ``=` `[[``0``, ``1``, ``0``, ``0``, ``1``], ` `         ``[``1``, ``0``, ``1``, ``1``, ``1``], ` `         ``[``0``, ``1``, ``0``, ``1``, ``0``], ` `         ``[``0``, ``1``, ``1``, ``0``, ``1``], ` `         ``[``1``, ``1``, ``0``, ``1``, ``0``]] ` `findpath(graph3) `

Output:

```4 -> 0 -> 1 -> 3 -> 2 -> 1
No Solution
4 -> 3 -> 2 -> 1 -> 4 -> 0 -> 1 -> 3
```

Time Complexity:
The runtime complexity of this algorithm is O(E). This algorithm can also be used to find the Eulerian circuit. If the first and last vertex of the path is same then it will be an Eulerian circuit.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : sanjeev2552

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.