#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define V 4
typedef enum { false, true } bool;
struct Queue
{
int front, rear, capacity;
int* array;
};
struct Queue* createQueue(int capacity)
{
struct Queue
* queue
= (struct Queue
*) malloc(sizeof(struct Queue
));
queue->capacity = capacity;
queue->front = queue->rear = -1;
queue
->array
= (int*) malloc(queue
->capacity
* (sizeof(int)));
return queue;
}
bool isEmpty(struct Queue* queue)
{
return queue->front == -1 ? true : false;
}
bool isFull(struct Queue* queue)
{
return queue->rear == queue->capacity - 1 ? true : false;
}
void enQueue(struct Queue* queue, int item)
{
if (isFull(queue))
return;
queue->array[++queue->rear] = item;
if (isEmpty(queue))
++queue->front;
}
int deQueue(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
int item = queue->array[queue->front];
if (queue->front == queue->rear)
queue->front = queue->rear = -1;
else
++queue->front;
return item;
}
int initColorMatrix(int color[], int N)
{
int i;
for (i = 0; i < N; ++i)
color[i] = -1;
}
bool isBipartite(int G[][V], int src)
{
int colorMatrix[V], color, temp, u;
initColorMatrix(colorMatrix, V);
struct Queue* queue = createQueue(V);
color = 1;
colorMatrix[src] = color;
enQueue(queue, src);
while (!isEmpty(queue))
{
temp = deQueue(queue);
// assign alternate color to its neighbor
color = 1 - colorMatrix[temp];
for (u = 0; u < V; ++u)
{
// an edge exists and destination not colored
if (G[temp][u] && colorMatrix[u] == -1)
{
colorMatrix[u] = color;
enQueue(queue, u);
}
else if (G[temp][u] && colorMatrix[u] == colorMatrix[temp])
return false;
}
}
return true;
}
int main()
{
int G[][V] = {{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0}};
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPGxpbWl0cy5oPgoKI2RlZmluZSBWIDQKCnR5cGVkZWYgZW51bSB7IGZhbHNlLCB0cnVlIH0gYm9vbDsKCnN0cnVjdCBRdWV1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A9PSAtMSkKCSAgICAgICAgewoJCSAgICAgICAgY29sb3JNYXRyaXhbdV0gPSBjb2xvcjsKICAgICAgICAgICAgICAgIGVuUXVldWUocXVldWUsIHUpOwoJICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBlbHNlIGlmIChHW3RlbXBdW3VdICYmIGNvbG9yTWF0cml4W3VdID09IGNvbG9yTWF0cml4W3RlbXBdKQogICAgICAgICAgICAgICAgcmV0dXJuIGZhbHNlOyAKICAgICAgICB9CiAgICB9CiAgICAKICAgIHJldHVybiB0cnVlOwp9CgppbnQgbWFpbigpCnsKICAgIGludCBHW11bVl0gPSB7ezAsIDEsIDAsIDF9LAogICAgICAgICAgICAgICAgICB7MSwgMCwgMSwgMH0sCiAgICAgICAgICAgICAgICAgIHswLCAxLCAwLCAxfSwKICAgICAgICAgICAgICAgICAgezEsIDAsIDEsIDB9fTsKICAgIAogICAgaXNCaXBhcnRpdGUoRywgMCkgPyBwcmludGYoIlllcyIpIDogcHJpbnRmKCJObyIpOwogICAgCiAgICByZXR1cm4gMDsKfQ==