0
0
DSA Cprogramming~20 mins

Bipartite Graph Check in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bipartite Graph Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Bipartite Check on Simple Graph
What is the output of the following C code that checks if a graph is bipartite?
DSA C
#include <stdio.h>
#include <stdbool.h>
#define V 4

int graph[V][V] = {
    {0, 1, 0, 1},
    {1, 0, 1, 0},
    {0, 1, 0, 1},
    {1, 0, 1, 0}
};

bool isBipartiteUtil(int v, int colorArr[], int c) {
    colorArr[v] = c;
    for (int i = 0; i < V; i++) {
        if (graph[v][i]) {
            if (colorArr[i] == -1) {
                if (!isBipartiteUtil(i, colorArr, 1 - c))
                    return false;
            } else if (colorArr[i] == c) {
                return false;
            }
        }
    }
    return true;
}

bool isBipartite() {
    int colorArr[V];
    for (int i = 0; i < V; i++)
        colorArr[i] = -1;

    for (int i = 0; i < V; i++) {
        if (colorArr[i] == -1) {
            if (!isBipartiteUtil(i, colorArr, 0))
                return false;
        }
    }
    return true;
}

int main() {
    if (isBipartite())
        printf("Graph is Bipartite\n");
    else
        printf("Graph is NOT Bipartite\n");
    return 0;
}
ACompilation error
BGraph is NOT Bipartite
CRuntime error
DGraph is Bipartite
Attempts:
2 left
💡 Hint
Check if the graph can be colored using two colors without adjacent nodes sharing the same color.
Predict Output
intermediate
2:00remaining
Output of Bipartite Check on Odd Cycle Graph
What is the output of this C code checking bipartiteness on a triangle graph?
DSA C
#include <stdio.h>
#include <stdbool.h>
#define V 3

int graph[V][V] = {
    {0, 1, 1},
    {1, 0, 1},
    {1, 1, 0}
};

bool isBipartiteUtil(int v, int colorArr[], int c) {
    colorArr[v] = c;
    for (int i = 0; i < V; i++) {
        if (graph[v][i]) {
            if (colorArr[i] == -1) {
                if (!isBipartiteUtil(i, colorArr, 1 - c))
                    return false;
            } else if (colorArr[i] == c) {
                return false;
            }
        }
    }
    return true;
}

bool isBipartite() {
    int colorArr[V];
    for (int i = 0; i < V; i++)
        colorArr[i] = -1;

    for (int i = 0; i < V; i++) {
        if (colorArr[i] == -1) {
            if (!isBipartiteUtil(i, colorArr, 0))
                return false;
        }
    }
    return true;
}

int main() {
    if (isBipartite())
        printf("Graph is Bipartite\n");
    else
        printf("Graph is NOT Bipartite\n");
    return 0;
}
AGraph is Bipartite
BGraph is NOT Bipartite
CCompilation error
DSegmentation fault
Attempts:
2 left
💡 Hint
Odd cycles cannot be bipartite.
🔧 Debug
advanced
2:30remaining
Identify the bug causing incorrect bipartite detection
This code is intended to check if a graph is bipartite. What is the bug causing it to always return true, even for non-bipartite graphs?
DSA C
#include <stdio.h>
#include <stdbool.h>
#define V 3

int graph[V][V] = {
    {0, 1, 1},
    {1, 0, 1},
    {1, 1, 0}
};

bool isBipartiteUtil(int v, int colorArr[], int c) {
    colorArr[v] = c;
    for (int i = 0; i < V; i++) {
        if (graph[v][i]) {
            if (colorArr[i] == -1) {
                if (!isBipartiteUtil(i, colorArr, 1 - c))
                    return false;
            } else if (colorArr[i] == c) {
                return false;
            }
        }
    }
    return true;
}

bool isBipartite() {
    int colorArr[V];
    for (int i = 0; i < V; i++)
        colorArr[i] = -1;

    for (int i = 0; i < V; i++) {
        if (colorArr[i] == -1) {
            if (!isBipartiteUtil(i, colorArr, 0))
                return false;
        }
    }
    return true;
}

int main() {
    if (isBipartite())
        printf("Graph is Bipartite\n");
    else
        printf("Graph is NOT Bipartite\n");
    return 0;
}
AGraph adjacency matrix is incorrect
BIncorrect initialization of colorArr array
CMissing return statement in recursive call of isBipartiteUtil
DWrong base case in isBipartiteUtil
Attempts:
2 left
💡 Hint
Check if the recursive call's return value is used properly.
🧠 Conceptual
advanced
1:30remaining
Which graph property guarantees bipartiteness?
Which of the following properties guarantees that a graph is bipartite?
AGraph contains no odd-length cycles
BGraph is complete
CGraph is connected
DGraph has an Eulerian path
Attempts:
2 left
💡 Hint
Think about cycles and coloring.
🚀 Application
expert
2:30remaining
Result of Bipartite Check on Disconnected Graph
Consider a graph with two disconnected components: one is a triangle (3 nodes all connected), the other is a line of two nodes connected. What will the bipartite check output be?
AGraph is NOT Bipartite
BRuntime error due to disconnected graph
CDepends on starting node
DGraph is Bipartite
Attempts:
2 left
💡 Hint
Check bipartiteness on all components.