Check Bipartite Graph
Check Bipartite Graph
1. Idea
2. Implementation
public static boolean isBipartieGraph(int[][] graph, int start) {
int n = graph.length;
int[] colors = new int[n];
Arrays.fill(colors, -1);
colors[start] = 1;
Queue<Integer> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
int curVertex = queue.remove();
for (int nextVertex = 0; nextVertex < n; nextVertex++) {
if (graph[curVertex][nextVertex] == 1 && colors[nextVertex] == -1) {
colors[nextVertex] = 1 - colors[curVertex];
queue.add(nextVertex);
}
else if (graph[curVertex][nextVertex] == 1 && colors[curVertex] == colors[nextVertex]) {
return false;
}
}
}
return true;
}3. Time & Space Complexity
Last updated