Is Graph Bipartite? - Complete Solution Guide
Is Graph Bipartite? is LeetCode problem 785, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1 . You are given a 2D array graph , where graph[u] is an array of nodes that node u is adjacent to. More formally, for each v in graph[u] , there is an undirected edge between node u and node v . The graph has the following properties: There are no self-edges ( graph[u] does not contain u ). There are no parallel edges ( graph[u] does not contain duplicate values). If v is in graph[u] , then u is in graph[v
Detailed Explanation
The problem asks us to determine if a given undirected graph is bipartite. A graph is bipartite if its nodes can be divided into two disjoint sets A and B such that every edge connects a node in A to a node in B. The input is a 2D array 'graph' representing the adjacency list of the graph. `graph[u]` contains a list of nodes that node `u` is connected to. The graph may not be connected, meaning there may be multiple disconnected components. The output should be `true` if the graph is bipartite and `false` otherwise. The nodes are numbered from 0 to n-1.
Solution Approach
The provided solutions use Breadth-First Search (BFS) to color the nodes of the graph. We iterate through each node in the graph. If a node hasn't been colored yet, we start a BFS traversal from that node. We assign a color (1 or -1) to the starting node and then alternate colors for each neighbor. If we encounter a neighbor that has already been colored and has the same color as the current node, it means the graph is not bipartite. We repeat this process for each uncolored node in the graph until we have checked all connected components.
Step-by-Step Algorithm
- Step 1: Initialize a 'colors' array of size n, filled with 0. 0 means uncolored, 1 and -1 represent the two colors.
- Step 2: Iterate through each node 'i' from 0 to n-1.
- Step 3: If colors[i] is 0 (uncolored), it means we need to start a BFS traversal from this node.
- Step 4: Create a queue and add the starting node 'i' to it. Assign colors[i] = 1 (or any arbitrary color).
- Step 5: While the queue is not empty:
- Step 6: Dequeue a node 'u' from the queue.
- Step 7: Iterate through each neighbor 'v' of node 'u' in graph[u].
- Step 8: If colors[v] is 0 (uncolored), assign colors[v] = -colors[u] (opposite color of 'u') and enqueue 'v'.
- Step 9: If colors[v] is equal to colors[u], it means there's a conflict, and the graph is not bipartite. Return false.
- Step 10: After the BFS traversal completes, if no conflict was found, continue to the next uncolored node.
- Step 11: If all nodes have been processed without finding any conflicts, return true.
Key Insights
- Insight 1: Bipartiteness can be determined by assigning colors to nodes such that no adjacent nodes have the same color. We only need two colors.
- Insight 2: If a graph is not connected, we need to check each connected component independently.
- Insight 3: A graph with a cycle of odd length is not bipartite.
Complexity Analysis
Time Complexity: O(V+E)
Space Complexity: O(V)
Topics
This problem involves: Depth-First Search, Breadth-First Search, Union Find, Graph.
Companies
Asked at: Pinterest, Samsung.