C++ Program to Implement Euler Circuit Problem

This C++ program displays the Euler Circuit Problem in which a particular circuit starts and ends on the same vertex by visiting each edge exactly once.

Here is the source code of the C++ program to display whether the particular undirected graphs given as input are Eulerian or not. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C++ Program to Implement Euler Circuit Problem
  3.  */
  4. #include<iostream>
  5. #include<conio.h>
  6. #include<list>
  7. using namespace std;
  8.  
  9. class Graph
  10. {
  11.     int V;   
  12.     list<int> *adj;
  13. public:
  14.     Graph(int V)
  15.     {
  16.         this->V = V;
  17.         adj = new list<int>[V];
  18.     }
  19.     void addEdge(int v, int w);
  20.  
  21.     int isEulerian();
  22.  
  23.     bool isConnected();
  24.  
  25.     void DFSUtil(int v, bool visited[]);
  26. };
  27.  
  28. void Graph::addEdge(int v, int w)
  29. {
  30.     adj[v].push_back(w);
  31.     adj[w].push_back(v); 
  32. }
  33.  
  34. void Graph::DFSUtil(int v, bool visited[])
  35. {
  36.     visited[v] = true;
  37.  
  38.     list<int>::iterator i;
  39.     for (i = adj[v].begin(); i != adj[v].end(); ++i)
  40.     {
  41.         if (!visited[*i])
  42.         {
  43.             DFSUtil(*i, visited);
  44.         }
  45.     }
  46. }
  47. bool Graph::isConnected()
  48. {
  49.     bool visited[V];
  50.     int i;
  51.     for (i = 0; i < V; i++)
  52.     {
  53.         visited[i] = false;
  54.     }
  55.     for (i = 0; i < V; i++)
  56.     {
  57.         if (adj[i].size() != 0)
  58.         {
  59.             break;
  60.         }
  61.     }
  62.     if (i == V)
  63.     {
  64.         return true;
  65.     }
  66.     DFSUtil(i, visited);
  67.     for (i = 0; i < V; i++)
  68.     {
  69.         if (visited[i] == false && adj[i].size() > 0)
  70.         {
  71.             return false;
  72.         }
  73.     }
  74.     return true;
  75. }
  76. int Graph::isEulerian()
  77. {
  78.     if (isConnected() == false)
  79.     {
  80.         return 0;
  81.     }
  82.     int odd = 0;
  83.     for (int i = 0; i < V; i++)
  84.     {
  85.         if (adj[i].size() & 1)
  86.         {
  87.             odd++;
  88.         }
  89.     }
  90.     if (odd > 2)
  91.     {
  92.         return 0;
  93.     }
  94.     return (odd)? 1 : 2;
  95. }
  96. void test(Graph &g)
  97. {
  98.     int res = g.isEulerian();
  99.     if (res == 0)
  100.     {
  101.         cout << "Graph is not Eulerian\n";
  102.     }
  103.     else if (res == 1)
  104.     {
  105.         cout << "Graph has a Euler path\n";
  106.     }
  107.     else
  108.     {
  109.         cout << "Graph has a Euler cycle\n";
  110.     }
  111. }
  112. int main()
  113. {
  114.     Graph g1(5);
  115.     g1.addEdge(1, 0);
  116.     g1.addEdge(0, 2);
  117.     g1.addEdge(2, 1);
  118.     g1.addEdge(0, 3);
  119.     g1.addEdge(3, 4);
  120.     test(g1);
  121.  
  122.     Graph g2(5);
  123.     g2.addEdge(1, 0);
  124.     g2.addEdge(0, 2);
  125.     g2.addEdge(2, 1);
  126.     g2.addEdge(0, 3);
  127.     g2.addEdge(3, 4);
  128.     g2.addEdge(4, 0);
  129.     test(g2);
  130.  
  131.     Graph g3(5);
  132.     g3.addEdge(1, 0);
  133.     g3.addEdge(0, 2);
  134.     g3.addEdge(2, 1);
  135.     g3.addEdge(0, 3);
  136.     g3.addEdge(3, 4);
  137.     g3.addEdge(1, 3);
  138.     test(g3);
  139.  
  140.     Graph g4(3);
  141.     g4.addEdge(0, 1);
  142.     g4.addEdge(1, 2);
  143.     g4.addEdge(2, 0);
  144.     test(g4);
  145.  
  146.     Graph g5(3);
  147.     test(g5);
  148.  
  149.     getch();
  150. }

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

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.