@pankajyd/

# Check Bipartite Graph

## No description

Files
• main.c
main.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
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
```
```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

/************************************
white = 0
grey = 1
black = 2
************************************/

typedef struct vertex{
int v;
int d;
int color;
}vertex;

typedef struct list_entry{
struct vertex* self;
struct list_entry* next;
}list_entry;

typedef struct Queue{
int n;
int* Q;
int tail;
}Queue;

int IsEmpty(Queue* Q){
return 1;
return 0;
}

int IsFull(Queue* Q){
int i = Q->tail;

if(Q->tail == Q->n - 1)
i = 0;
else
i++;

return 1;
return 0;
}

void Enqueue(Queue* Q, int x){
if(!IsFull(Q)){
Q->Q[Q->tail] = x;
}
else{
return;
}
if(Q->tail == Q->n - 1){
Q->tail = 0;
}
else{
Q->tail++;
}
}

int Dequeue(Queue* Q){
int x;
if(!IsEmpty(Q))
else
return x;
}

int BFS(vertex* V, int i, int nv){
int q[nv];
Queue QQ;
Queue* Q = &QQ;
Q->n = nv;
Q->Q = q;
Q->tail = 0;

V[i].d = 0;
V[i].color = 1;
Enqueue(Q, i);
//printf("\nEnqueued %d", i);
while(!IsEmpty(Q)){
int u = Dequeue(Q);
//printf("\nDequeued %d", u);
//printf("\nTraversing Adjaceny list of vertex %d", u);
//printf("\n\t Now at vertex %d", head->self->v);
//printf("\nEnqueued %d", i);
}
//printf("\nBFS exited. Not bipartite.");
return 0;
}
}
}
return 1;
}

int check_bipartite(vertex* V, int nv){
for(int i = 1; i < nv; i++){
//printf("\nStarting BFS with vertex: %d\n", i);
if(V[i].color == 0){
if(!BFS(V, i, nv)){
return 0;
}
}
}
return 1;
}

int main() {
int t;
scanf("%d", &t);

vertex* V;

while(t--){
int nv, ne;
scanf("%d %d", &nv, &ne);
nv++;
vertex G[nv];
V = G;

list_entry* v_tail[nv];

//Initializing colors to white and pointers to NULL
for(int i = 1; i < nv; i++){
V[i].v = i;

V[i].color = 0;

v_tail[i] = NULL;
}

//Scanning Edges
for(int i = 0; i < ne; i++){
int v1, v2;
scanf("%d %d", &v1, &v2);

list_entry* ptr;

ptr = (list_entry*)malloc(sizeof(list_entry));
ptr->self = &V[v2];
ptr->next = NULL;

list_entry* ptr2;

ptr2 = (list_entry*)malloc(sizeof(list_entry));
ptr2->self = &V[v1];
ptr2->next = NULL;

//Edge u to v
}
else{
v_tail[v1]->next = ptr;
}
v_tail[v1] = ptr;

//Edge v to u
}
else{
v_tail[v2]->next = ptr2;
}
v_tail[v2] = ptr2;
}

if(check_bipartite(V, nv))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

```