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.
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"
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:
- First take an empty stack and an empty path.
- 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.
- 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.
- If current node has not any neighbour then add it to path and pop stack set current to popped vertex.
- 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.
4 -> 0 -> 1 -> 3 -> 2 -> 1 No Solution 4 -> 3 -> 2 -> 1 -> 4 -> 0 -> 1 -> 3
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Eulerian path and circuit for undirected graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Find if there is a path between two vertices in an undirected graph
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Building an undirected graph and finding shortest path using Dictionaries in Python
- Fleury's Algorithm for printing Eulerian Path or Circuit
- Convert undirected connected graph to strongly connected directed graph
- Clone an Undirected Graph
- Connected Components in an undirected graph
- Number of Triangles in an Undirected Graph
- Find k-cores of an undirected graph
- Detect cycle in an undirected graph
- Maximal Independent Set in an Undirected Graph
- Print all the cycles in an undirected graph
- Sum of degrees of all nodes of a undirected graph
- Detect cycle in an undirected graph using BFS
- Check if there is a cycle with odd weight sum in an undirected graph
- Shortest cycle in an undirected unweighted graph
- Find if an undirected graph contains an independent set of a given size
- Product of lengths of all cycles in an undirected graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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