Eulerian Path is a path in graph that visits every edge exactly once. Eulerian Circuit is an Eulerian Path which starts and ends on the same vertex.

How to find whether a given graph is Eulerian or not?
The problem is same as following question. “Is it possible to draw a given graph without lifting pencil from the paper and without tracing any of the edges more than once”.

A graph is called Eulerian if it has an Eulerian Cycle and called Semi-Eulerian if it has an Eulerian Path. The problem seems similar to Hamiltonian Path which is NP complete problem for a general graph. Fortunately, we can find whether a given graph has a Eulerian Path or not in polynomial time. In fact, we can find it in O(V+E) time.

Following are some interesting properties of undirected graphs with an Eulerian path and cycle. We can use these properties to find whether a graph is Eulerian or not.

[ad type=”banner”]

Eulerian Cycle
An undirected graph has Eulerian cycle if following two conditions are true.
….a) All vertices with non-zero degree are connected. We don’t care about vertices with zero degree because they don’t belong to Eulerian Cycle or Path (we only consider all edges).
….b) All vertices have even degree.

Eulerian Path
An undirected graph has Eulerian Path if following two conditions are true.
….a) Same as condition (a) for Eulerian Cycle
….b) If zero or two vertices have odd degree and all other vertices have even degree. Note that only one vertex with odd degree is not possible in an undirected graph (sum of all degrees is always even in an undirected graph)

Note that a graph with no edges is considered Eulerian because there are no edges to traverse.

How does this work?
In Eulerian path, each time we visit a vertex v, we walk through two unvisited edges with one end point as v. Therefore, all middle vertices in Eulerian Path must have even degree. For Eulerian Cycle, any vertex can be middle vertex, therefore all vertices must have even degree.

[ad type=”banner”]

C++  programming:

[pastacode lang=”cpp” manual=”%2F%2F%20A%20C%2B%2B%20program%20to%20check%20if%20a%20given%20graph%20is%20Eulerian%20or%20not%0A%23include%3Ciostream%3E%0A%23include%20%3Clist%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20class%20that%20represents%20an%20undirected%20graph%0Aclass%20Graph%0A%7B%0A%20%20%20%20int%20V%3B%20%20%20%20%2F%2F%20No.%20of%20vertices%0A%20%20%20%20list%3Cint%3E%20*adj%3B%20%20%20%20%2F%2F%20A%20dynamic%20array%20of%20adjacency%20lists%0Apublic%3A%0A%20%20%20%20%2F%2F%20Constructor%20and%20destructor%0A%20%20%20%20Graph(int%20V)%20%20%20%7Bthis-%3EV%20%3D%20V%3B%20adj%20%3D%20new%20list%3Cint%3E%5BV%5D%3B%20%7D%0A%20%20%20%20~Graph()%20%7B%20delete%20%5B%5D%20adj%3B%20%7D%20%2F%2F%20To%20avoid%20memory%20leak%0A%20%0A%20%20%20%20%20%2F%2F%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20void%20addEdge(int%20v%2C%20int%20w)%3B%0A%20%0A%20%20%20%20%2F%2F%20Method%20to%20check%20if%20this%20graph%20is%20Eulerian%20or%20not%0A%20%20%20%20int%20isEulerian()%3B%0A%20%0A%20%20%20%20%2F%2F%20Method%20to%20check%20if%20all%20non-zero%20degree%20vertices%20are%20connected%0A%20%20%20%20bool%20isConnected()%3B%0A%20%0A%20%20%20%20%2F%2F%20Function%20to%20do%20DFS%20starting%20from%20v.%20Used%20in%20isConnected()%3B%0A%20%20%20%20void%20DFSUtil(int%20v%2C%20bool%20visited%5B%5D)%3B%0A%7D%3B%0A%20%0Avoid%20Graph%3A%3AaddEdge(int%20v%2C%20int%20w)%0A%7B%0A%20%20%20%20adj%5Bv%5D.push_back(w)%3B%0A%20%20%20%20adj%5Bw%5D.push_back(v)%3B%20%20%2F%2F%20Note%3A%20the%20graph%20is%20undirected%0A%7D%0A%20%0Avoid%20Graph%3A%3ADFSUtil(int%20v%2C%20bool%20visited%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20Mark%20the%20current%20node%20as%20visited%20and%20print%20it%0A%20%20%20%20visited%5Bv%5D%20%3D%20true%3B%0A%20%0A%20%20%20%20%2F%2F%20Recur%20for%20all%20the%20vertices%20adjacent%20to%20this%20vertex%0A%20%20%20%20list%3Cint%3E%3A%3Aiterator%20i%3B%0A%20%20%20%20for%20(i%20%3D%20adj%5Bv%5D.begin()%3B%20i%20!%3D%20adj%5Bv%5D.end()%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20if%20(!visited%5B*i%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20DFSUtil(*i%2C%20visited)%3B%0A%7D%0A%20%0A%2F%2F%20Method%20to%20check%20if%20all%20non-zero%20degree%20vertices%20are%20connected.%0A%2F%2F%20It%20mainly%20does%20DFS%20traversal%20starting%20from%0Abool%20Graph%3A%3AisConnected()%0A%7B%0A%20%20%20%20%2F%2F%20Mark%20all%20the%20vertices%20as%20not%20visited%0A%20%20%20%20bool%20visited%5BV%5D%3B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%20false%3B%0A%20%0A%20%20%20%20%2F%2F%20Find%20a%20vertex%20with%20non-zero%20degree%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(adj%5Bi%5D.size()%20!%3D%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20there%20are%20no%20edges%20in%20the%20graph%2C%20return%20true%0A%20%20%20%20if%20(i%20%3D%3D%20V)%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20%2F%2F%20Start%20DFS%20traversal%20from%20a%20vertex%20with%20non-zero%20degree%0A%20%20%20%20DFSUtil(i%2C%20visited)%3B%0A%20%0A%20%20%20%20%2F%2F%20Check%20if%20all%20non-zero%20degree%20vertices%20are%20visited%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20if%20(visited%5Bi%5D%20%3D%3D%20false%20%26%26%20adj%5Bi%5D.size()%20%3E%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20return%20true%3B%0A%7D%0A%20%0A%2F*%20The%20function%20returns%20one%20of%20the%20following%20values%0A%20%20%200%20–%3E%20If%20grpah%20is%20not%20Eulerian%0A%20%20%201%20–%3E%20If%20graph%20has%20an%20Euler%20path%20(Semi-Eulerian)%0A%20%20%202%20–%3E%20If%20graph%20has%20an%20Euler%20Circuit%20(Eulerian)%20%20*%2F%0Aint%20Graph%3A%3AisEulerian()%0A%7B%0A%20%20%20%20%2F%2F%20Check%20if%20all%20non-zero%20degree%20vertices%20are%20connected%0A%20%20%20%20if%20(isConnected()%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Count%20vertices%20with%20odd%20degree%0A%20%20%20%20int%20odd%20%3D%200%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(adj%5Bi%5D.size()%20%26%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%2B%2B%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20count%20is%20more%20than%202%2C%20then%20graph%20is%20not%20Eulerian%0A%20%20%20%20if%20(odd%20%3E%202)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20odd%20count%20is%202%2C%20then%20semi-eulerian.%0A%20%20%20%20%2F%2F%20If%20odd%20count%20is%200%2C%20then%20eulerian%0A%20%20%20%20%2F%2F%20Note%20that%20odd%20count%20can%20never%20be%201%20for%20undirected%20graph%0A%20%20%20%20return%20(odd)%3F%201%20%3A%202%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20run%20test%20cases%0Avoid%20test(Graph%20%26g)%0A%7B%0A%20%20%20%20int%20res%20%3D%20g.isEulerian()%3B%0A%20%20%20%20if%20(res%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22graph%20is%20not%20Eulerian%5Cn%22%3B%0A%20%20%20%20else%20if%20(res%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22graph%20has%20a%20Euler%20path%5Cn%22%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22graph%20has%20a%20Euler%20cycle%5Cn%22%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Let%20us%20create%20and%20test%20graphs%20shown%20in%20above%20figures%0A%20%20%20%20Graph%20g1(5)%3B%0A%20%20%20%20g1.addEdge(1%2C%200)%3B%0A%20%20%20%20g1.addEdge(0%2C%202)%3B%0A%20%20%20%20g1.addEdge(2%2C%201)%3B%0A%20%20%20%20g1.addEdge(0%2C%203)%3B%0A%20%20%20%20g1.addEdge(3%2C%204)%3B%0A%20%20%20%20test(g1)%3B%0A%20%0A%20%20%20%20Graph%20g2(5)%3B%0A%20%20%20%20g2.addEdge(1%2C%200)%3B%0A%20%20%20%20g2.addEdge(0%2C%202)%3B%0A%20%20%20%20g2.addEdge(2%2C%201)%3B%0A%20%20%20%20g2.addEdge(0%2C%203)%3B%0A%20%20%20%20g2.addEdge(3%2C%204)%3B%0A%20%20%20%20g2.addEdge(4%2C%200)%3B%0A%20%20%20%20test(g2)%3B%0A%20%0A%20%20%20%20Graph%20g3(5)%3B%0A%20%20%20%20g3.addEdge(1%2C%200)%3B%0A%20%20%20%20g3.addEdge(0%2C%202)%3B%0A%20%20%20%20g3.addEdge(2%2C%201)%3B%0A%20%20%20%20g3.addEdge(0%2C%203)%3B%0A%20%20%20%20g3.addEdge(3%2C%204)%3B%0A%20%20%20%20g3.addEdge(1%2C%203)%3B%0A%20%20%20%20test(g3)%3B%0A%20%0A%20%20%20%20%2F%2F%20Let%20us%20create%20a%20graph%20with%203%20vertices%0A%20%20%20%20%2F%2F%20connected%20in%20the%20form%20of%20cycle%0A%20%20%20%20Graph%20g4(3)%3B%0A%20%20%20%20g4.addEdge(0%2C%201)%3B%0A%20%20%20%20g4.addEdge(1%2C%202)%3B%0A%20%20%20%20g4.addEdge(2%2C%200)%3B%0A%20%20%20%20test(g4)%3B%0A%20%0A%20%20%20%20%2F%2F%20Let%20us%20create%20a%20graph%20with%20all%20veritces%0A%20%20%20%20%2F%2F%20with%20zero%20degree%0A%20%20%20%20Graph%20g5(3)%3B%0A%20%20%20%20test(g5)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output:

graph has a Euler path
graph has a Euler cycle
graph is not Eulerian
graph has a Euler cycle
graph has a Euler cycle
 [ad type=”banner”]