-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEulerian-Path-and-Cycle.c
137 lines (118 loc) · 3.5 KB
/
Eulerian-Path-and-Cycle.c
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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
//Structure of Node in Adjacency List
struct node {
int data;
struct node* next;
};
//Function to Insert Adjacent vertex in Adjacency List (It will insert node at the end of the List)
struct node* Insert(struct node* head,int v){
struct node* nw = (struct node*)malloc(sizeof(struct node));
nw->data=v;
nw->next=NULL;
if(head==NULL){
head=nw;
}
else{
struct node* temp = head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=nw;
}
return head;
}
//Depth-First-Search to Visit Connected Vertices
void DFS(int v,struct node* head[],bool visited[]){
//visiting vertex v
visited[v]=true;
//visiting unvisited Vertices from adjacency list of "v"
for(struct node* i=head[v];i!=NULL;i=i->next){
if(!visited[i->data]){
DFS(i->data,head,visited);
}
}
}
//To Find Degree of Vertex
int Degree(struct node* head){
struct node* temp=head;
int degree=0;
while(temp){
++degree;
temp=temp->next;
}
return degree;
}
int main(){
//Number of Vertices
int n;
printf("\nEnter Number of Vertices : ");
scanf("%d",&n);
//Head-Pointers of Adjacency-Lists
struct node* head[n+1];
//Initializing Head-Pointer with NULL Value
for(int i=1;i<=n;i++){
head[i]=NULL;
}
int x,y,q;
//Taking Edges as Input from user
printf("\n\nEnter Edges in Graph (ex: 1 v1 v2 to add Edge | 0 to finish adding Edges) :\n");
do{
scanf("%d",&q);
if(q==1){
scanf("%d %d",&x,&y);
head[x]=Insert(head[x],y); //Adding "y" in the Adjacency List of "x"
head[y]=Insert(head[y],x); //Adding "x" in the Adjacency List of "y"
}
}while(q!=0);
//To keep Track on Visited and unvisited Vertices
bool visited[n+1];
//Initializing "Visited" array with False Value
for(int i=1;i<=n;i++){
visited[i]=false;
}
//Finding First Non-Zero Degree Vertex
int i;
for(i=1;i<=n;i++){
//Breaking the loop when 1st Non-Zero Degree Vertex is Found
if(head[i]!=NULL){
break;
}
}
//Checking if all the Vertex are of Zero Degree or Not => If yes then Graph is Eulerian
if(i==n+1){
printf("\nEulerian\n");
}
else{
//Starting Depth-First-Search from 1st Vertex of Non-Zero Degree which is => i
DFS(i,head,visited);
//checking if all Vertex with non-zero degree are Visited after DFS
for(i=1;i<=n;i++){
if(!visited[i] && head[i]!=NULL){
printf("\nNot-Eulerian\n"); //If there is a unvisited Vertex with non-zero degree then the Graph is Not-Eulerian
exit(0);
}
}
//Else counting Number of Vertices with Odd Degree
int odd=0;
for(i=1;i<=n;i++){
if(Degree(head[i])%2!=0){
++odd;
}
}
//If more than two are of odd degree => Graph is not Eulerian
if(odd>2){
printf("\nNot-Eulerian\n");
}
//else if number of Vertices with odd degree are Two => Eulerian Path
else if(odd==2){
printf("\nEulerian Path\n");
}
//else Eulerian Cycle
else{
printf("\nEulerian Cycle\n");
}
}
return 0;
}