-
Notifications
You must be signed in to change notification settings - Fork 17
/
ds question 19.cpp
208 lines (187 loc) · 3.54 KB
/
ds question 19.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*Given an adjacency matrix of a graph, write a program to check whether a given set of
vertices {v1,v2,v3.....,vk} forms an Euler path / Euler Circuit (for circuit assume vk=v1).*/
#include<iostream>
using namespace std;
class euler
{
int **a;//contains adjacency matrix of graph
int **b;//constructed new matrix using the path given
int *c;//contains the path
public:
int n;//number of vertices
int p;//length of path
/*Constructor*/
euler()
{
cout<<"Enter the number of vertices"<<endl;
cin>>n;
/*dynamic allocation is used for the matrices*/
a=new int*[n];
b=new int*[n];
for (int i = 0; i < n; ++i)
{
a[i]=new int[n];
b[i]=new int[n];
}
cout<<"Enter the length of path"<<endl;
cin>>p;
c=new int[p];
/*intialised the matrices to 0*/
for(int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
a[i][j]=0;
b[i][j]=0;
}
}
}
void input();//for taking input
void newmatrix();//creating the new matrix using new matrix
int eulerpath();//checking for euler path
int eulercircuit();//checking for euler circuit
};
void euler::input()
{
cout<<"Enter the adjacency matrix for the graph"<<endl;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"Enter the path "<<endl;
for (int i=0;i<p;i++)
{
cin>>c[i];
}
}
void euler::newmatrix()
{
for(int i=0;i<p-1;i++)
{
int d=c[i],f=c[i+1];
b[d][f]=1;
}
for (int i = 0; i < n; ++i)
{
for(int j=0;j<n;j++)
cout<<b[i][j];
cout<<endl;
}
}
int euler::eulerpath()
{
int k=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
/*If both matrices are equal for directed graph and if have an edge for undirected graph counter will go ++ */
if(a[i][j]==b[i][j]||a[i][j]==a[j][i])
k++;
}
}
return k;
}
int euler::eulercircuit()
{
if (eulerpath()==(n*n))//if counter is equal to the length of matrix it is a euler path since the matrices will then be equal
{
if (c[0]==c[p-1])
{
return 1;
}
}
return 0;
}
int main()
{
int ch;
cout<<"****Menu****"<<endl;
cout<<"1. Euler path."<<endl;
cout<<"2. Euler circuit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
euler e1;
e1.input();
e1.newmatrix();
switch(ch)
{
case 1:
{
int m=e1.eulerpath();
int s=e1.n*e1.n;
if(m==s)
cout<<"The path is a euler path"<<endl;
else
cout<<"The path is not a euler path"<<endl;
break;
}
case 2:
{
int g=e1.eulercircuit();
if(g==1)
cout<<"The graph contains a euler circuit"<<endl;
else
cout<<"The graph does not contain a euler circuit"<<endl;
break;
}
default:
{
cout<<"Wrong choice"<<endl;
break;
}
}
return 0;
}
//OUTPUT
/*
****Menu****
1. Euler path.
2. Euler circuit
Enter your choice
1
Enter the number of vertices
5
Enter the length of path
6
Enter the adjacency matrix for the graph
0 1 1 1 0
1 0 1 0 0
1 1 0 0 0
1 0 0 0 1
0 0 0 1 0
Enter the path
4 3 0 1 2 0
01000
00100
10000
10000
00010
The path is a euler path
C:\Users\Lenovo-Laptop\Desktop\Backup\Second semester\discrete structures>
****Menu****
1. Euler path.
2. Euler circuit
Enter your choice
2
Enter the number of vertices
5
Enter the length of path
7
Enter the adjacency matrix for the graph
0 1 1 1 0
1 0 1 0 0
1 1 0 0 0
1 0 0 0 1
0 0 0 1 0
Enter the path
2 1 0 3 4 0 2
00110
10000
01000
00001
10000
The graph contains a euler circuit
C:\Users\Lenovo-Laptop\Desktop\Backup\Second semester\discrete structures>
*/