{"id":27140,"date":"2018-01-03T21:42:10","date_gmt":"2018-01-03T16:12:10","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27140"},"modified":"2018-01-03T21:42:10","modified_gmt":"2018-01-03T16:12:10","slug":"c-programming-eulerian-path-circuit-undirected-graph","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-eulerian-path-circuit-undirected-graph\/","title":{"rendered":"C++ programming Eulerian path and circuit for undirected graph"},"content":{"rendered":"<p>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.<\/p>\n<p><strong>How to find whether a given graph is Eulerian or not?<\/strong><br \/>\nThe problem is same as following question. \u201cIs it possible to draw a given graph without lifting pencil from the paper and without tracing any of the edges more than once\u201d.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Eulerian Cycle<\/strong><br \/>\nAn undirected graph has Eulerian cycle if following two conditions are true.<br \/>\n\u2026.a) All vertices with non-zero degree are connected. We don\u2019t care about vertices with zero degree because they don\u2019t belong to Eulerian Cycle or Path (we only consider all edges).<br \/>\n\u2026.b) All vertices have even degree.<\/p>\n<p><strong>Eulerian Path<\/strong><br \/>\nAn undirected graph has Eulerian Path if following two conditions are true.<br \/>\n\u2026.a) Same as condition (a) for Eulerian Cycle<br \/>\n\u2026.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)<\/p>\n<p>Note that a graph with no edges is considered Eulerian because there are no edges to traverse.<\/p>\n<p><strong>How does this work?<\/strong><br \/>\nIn 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.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>C++ \u00a0programming:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A C++ program to check if a given graph is Eulerian or not<br\/>#include&lt;iostream&gt;<br\/>#include &lt;list&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ A class that represents an undirected graph<br\/>class Graph<br\/>{<br\/>    int V;    \/\/ No. of vertices<br\/>    list&lt;int&gt; *adj;    \/\/ A dynamic array of adjacency lists<br\/>public:<br\/>    \/\/ Constructor and destructor<br\/>    Graph(int V)   {this-&gt;V = V; adj = new list&lt;int&gt;[V]; }<br\/>    ~Graph() { delete [] adj; } \/\/ To avoid memory leak<br\/> <br\/>     \/\/ function to add an edge to graph<br\/>    void addEdge(int v, int w);<br\/> <br\/>    \/\/ Method to check if this graph is Eulerian or not<br\/>    int isEulerian();<br\/> <br\/>    \/\/ Method to check if all non-zero degree vertices are connected<br\/>    bool isConnected();<br\/> <br\/>    \/\/ Function to do DFS starting from v. Used in isConnected();<br\/>    void DFSUtil(int v, bool visited[]);<br\/>};<br\/> <br\/>void Graph::addEdge(int v, int w)<br\/>{<br\/>    adj[v].push_back(w);<br\/>    adj[w].push_back(v);  \/\/ Note: the graph is undirected<br\/>}<br\/> <br\/>void Graph::DFSUtil(int v, bool visited[])<br\/>{<br\/>    \/\/ Mark the current node as visited and print it<br\/>    visited[v] = true;<br\/> <br\/>    \/\/ Recur for all the vertices adjacent to this vertex<br\/>    list&lt;int&gt;::iterator i;<br\/>    for (i = adj[v].begin(); i != adj[v].end(); ++i)<br\/>        if (!visited[*i])<br\/>            DFSUtil(*i, visited);<br\/>}<br\/> <br\/>\/\/ Method to check if all non-zero degree vertices are connected.<br\/>\/\/ It mainly does DFS traversal starting from<br\/>bool Graph::isConnected()<br\/>{<br\/>    \/\/ Mark all the vertices as not visited<br\/>    bool visited[V];<br\/>    int i;<br\/>    for (i = 0; i &lt; V; i++)<br\/>        visited[i] = false;<br\/> <br\/>    \/\/ Find a vertex with non-zero degree<br\/>    for (i = 0; i &lt; V; i++)<br\/>        if (adj[i].size() != 0)<br\/>            break;<br\/> <br\/>    \/\/ If there are no edges in the graph, return true<br\/>    if (i == V)<br\/>        return true;<br\/> <br\/>    \/\/ Start DFS traversal from a vertex with non-zero degree<br\/>    DFSUtil(i, visited);<br\/> <br\/>    \/\/ Check if all non-zero degree vertices are visited<br\/>    for (i = 0; i &lt; V; i++)<br\/>       if (visited[i] == false &amp;&amp; adj[i].size() &gt; 0)<br\/>            return false;<br\/> <br\/>    return true;<br\/>}<br\/> <br\/>\/* The function returns one of the following values<br\/>   0 --&gt; If grpah is not Eulerian<br\/>   1 --&gt; If graph has an Euler path (Semi-Eulerian)<br\/>   2 --&gt; If graph has an Euler Circuit (Eulerian)  *\/<br\/>int Graph::isEulerian()<br\/>{<br\/>    \/\/ Check if all non-zero degree vertices are connected<br\/>    if (isConnected() == false)<br\/>        return 0;<br\/> <br\/>    \/\/ Count vertices with odd degree<br\/>    int odd = 0;<br\/>    for (int i = 0; i &lt; V; i++)<br\/>        if (adj[i].size() &amp; 1)<br\/>            odd++;<br\/> <br\/>    \/\/ If count is more than 2, then graph is not Eulerian<br\/>    if (odd &gt; 2)<br\/>        return 0;<br\/> <br\/>    \/\/ If odd count is 2, then semi-eulerian.<br\/>    \/\/ If odd count is 0, then eulerian<br\/>    \/\/ Note that odd count can never be 1 for undirected graph<br\/>    return (odd)? 1 : 2;<br\/>}<br\/> <br\/>\/\/ Function to run test cases<br\/>void test(Graph &amp;g)<br\/>{<br\/>    int res = g.isEulerian();<br\/>    if (res == 0)<br\/>        cout &lt;&lt; &quot;graph is not Eulerian\\n&quot;;<br\/>    else if (res == 1)<br\/>        cout &lt;&lt; &quot;graph has a Euler path\\n&quot;;<br\/>    else<br\/>        cout &lt;&lt; &quot;graph has a Euler cycle\\n&quot;;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    \/\/ Let us create and test graphs shown in above figures<br\/>    Graph g1(5);<br\/>    g1.addEdge(1, 0);<br\/>    g1.addEdge(0, 2);<br\/>    g1.addEdge(2, 1);<br\/>    g1.addEdge(0, 3);<br\/>    g1.addEdge(3, 4);<br\/>    test(g1);<br\/> <br\/>    Graph g2(5);<br\/>    g2.addEdge(1, 0);<br\/>    g2.addEdge(0, 2);<br\/>    g2.addEdge(2, 1);<br\/>    g2.addEdge(0, 3);<br\/>    g2.addEdge(3, 4);<br\/>    g2.addEdge(4, 0);<br\/>    test(g2);<br\/> <br\/>    Graph g3(5);<br\/>    g3.addEdge(1, 0);<br\/>    g3.addEdge(0, 2);<br\/>    g3.addEdge(2, 1);<br\/>    g3.addEdge(0, 3);<br\/>    g3.addEdge(3, 4);<br\/>    g3.addEdge(1, 3);<br\/>    test(g3);<br\/> <br\/>    \/\/ Let us create a graph with 3 vertices<br\/>    \/\/ connected in the form of cycle<br\/>    Graph g4(3);<br\/>    g4.addEdge(0, 1);<br\/>    g4.addEdge(1, 2);<br\/>    g4.addEdge(2, 0);<br\/>    test(g4);<br\/> <br\/>    \/\/ Let us create a graph with all veritces<br\/>    \/\/ with zero degree<br\/>    Graph g5(3);<br\/>    test(g5);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>graph has a Euler path\r\ngraph has a Euler cycle\r\ngraph is not Eulerian\r\ngraph has a Euler cycle\r\ngraph has a Euler cycle<\/pre>\n<div class=\"responsive-tabs-wrapper\">\u00a0[ad type=&#8221;banner&#8221;]<\/div>\n","protected":false},"excerpt":{"rendered":"<p>C++ programming Eulerian path and circuit &#8211; undirected graph. Eulerian Path is a path in graph that visits every edge exactly once.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,1,78385,73906],"tags":[83844,83843,83846,83845,82292,80982,83839,80979,83842,82294,82293,80983,73926,83841,80981,83840],"class_list":["post-27140","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-connectivity","category-graph-algorithms","tag-circuit-graph-theory","tag-cycle-graph-theory","tag-cyclic-graph-examples","tag-directed-cycle-graph","tag-eulerian-circuit-algorithm","tag-eulerian-graph-example","tag-eulerian-path-and-circuit","tag-eulerian-path-directed-graph","tag-eulerian-path-directed-graph-algorithm","tag-eulerian-path-undirected-graph","tag-fleury-algorithm-directed-graph","tag-fleurys-algorithm","tag-hamiltonian-circuit","tag-hierholzer-algorithm","tag-how-to-print-a-eulerian-path-or-circuit","tag-semi-eulerian"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27140"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27140\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}