-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathEulerian Path in Undirected Graph.cpp
88 lines (77 loc) · 1.49 KB
/
Eulerian Path in Undirected Graph.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
#include <iostream>
#include <vector>
#include <string.h>
const int N = 1001;
std::vector < int > v[N];
bool visited[N];
void dfs(int s)
{
visited[s] = 1;
for(auto x : v[s]) {
if(!visited[x]) {
dfs(x);
}
}
}
bool connected(int n)
{
memset(visited, 0, sizeof(visited));
// Find a vertex with a non zero degree
int i;
for(i = 1; i <= n; i ++) {
if(v[i].size() > 0) {
break;
}
}
// If there are non zero edge in the graph return true
if(i == n) {
return 1;
}
dfs(i);
// Check if all non zero degree are visited;=
for(int i = 1; i <= n; i ++) {
if(visited[i] == 0 and v[i].size() > 0) {
return 0;
}
}
return 1;
}
// Check For Euler path
int euler(int n)
{
if(connected(n) == 0) {
return 0;
}
int odd;
for(int i = 1; i <= n; i ++) {
if(v[i].size() & 1) {
odd ++;
}
}
if(odd > 2) {
return 0;
}
return (odd) ? 1 : 2;
}
int main()
{
int m, n;
std::cin >> n >> m;
for(int i = 0; i < n ; i ++) {
int x, y;
std::cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
int flag = euler(n);
if(not flag) {
std::cout << "No eulerian path ";
}
else if(d == 1) {
std::cout << "Eulerian Path but no eulerian cycle";
}
else {
std::cout << "Eulerian Cycle";
}
return 0;
}