File tree Expand file tree Collapse file tree 1 file changed +100
-0
lines changed Expand file tree Collapse file tree 1 file changed +100
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < list>
3
+
4
+ using namespace std ;
5
+
6
+
7
+ class Graph {
8
+
9
+ private:
10
+ int num_vertices;
11
+ list<int > *adj;
12
+ public:
13
+ Graph (int V);
14
+ bool is_Elulerian ();
15
+ void DFS (int g,bool visited[]);
16
+ bool connected ();
17
+ void add_edge (int x,int w);
18
+
19
+ };
20
+
21
+ Graph::Graph (int V){
22
+
23
+ this ->num_vertices =V;
24
+ this ->adj =new list<int >[V];
25
+ }
26
+ void Graph::DFS (int g,bool visited[]){
27
+
28
+ visited[g]=true ;
29
+
30
+ list<int >::iterator i;
31
+ for (i=adj[g].begin ();i!=adj[g].end ();i++){
32
+ if ( visited[*i] != true ){
33
+ DFS (*i,visited);
34
+ }
35
+ }
36
+
37
+ }
38
+ bool Graph::connected (){
39
+
40
+ bool visited[num_vertices];
41
+ for (int i=0 ;i<num_vertices;i++){
42
+ visited[i]=false ;
43
+ }
44
+ int k=0 ;
45
+ for (k=0 ;k<num_vertices;k++){
46
+ if (adj[k].size ()!=0 ){
47
+ break ;
48
+ }
49
+ }
50
+ DFS (k,visited);
51
+ for (k=0 ;k<num_vertices;k++){
52
+ if (adj[k].size ()!=0 ){
53
+ if (!visited[k]){
54
+ return false ;
55
+ }
56
+ }
57
+ }
58
+ return true ;
59
+
60
+
61
+ }
62
+ bool Graph::is_Elulerian (){
63
+
64
+
65
+ if (!connected ()){
66
+ return false ;
67
+ }
68
+ int odd=0 ;
69
+ for (int i=0 ;i<num_vertices;i++){
70
+ if (adj[i].size () & 1 ){
71
+ odd++;
72
+ }
73
+ }
74
+ if (odd>2 ){
75
+
76
+ return false ;
77
+ }
78
+ return true ;
79
+
80
+
81
+
82
+ }
83
+
84
+ void Graph::add_edge (int x,int w){
85
+
86
+ adj[x].push_back (w);
87
+ adj[w].push_back (x);
88
+
89
+ }
90
+
91
+ int main (){
92
+
93
+ // checking purpose
94
+ Graph g (2 );
95
+ g.add_edge (0 ,1 );
96
+ // return 1 if it is ElulerianGraph(fully or semi) and 0 if it is not
97
+ cout<<g.is_Elulerian ();
98
+
99
+
100
+ }
You can’t perform that action at this time.
0 commit comments