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:
graph[u] does not contain u).graph[u] does not contain duplicate values).v is in graph[u], then u is in graph[v] (the graph is undirected).u and v such that there is no path between them.A graph is bipartite if the nodes can be partitioned into two independent sets A and B such that every edge in the graph connects a node in set A and a node in set B.
Return true if and only if it is bipartite.
Example 1:
Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]] Output: false Explanation: There is no way to partition the nodes into two independent sets such that every edge connects a node in one and a node in the other.
Example 2:
Input: graph = [[1,3],[0,2],[1,3],[0,2]]
Output: true
Explanation: We can partition the nodes into two sets: {0, 2} and {1, 3}.
Constraints:
graph.length == n1 <= n <= 1000 <= graph[u].length < n0 <= graph[u][i] <= n - 1graph[u] does not contain u.graph[u] are unique.graph[u] contains v, then graph[v] contains u.This approach uses breadth-first search (BFS) to try and color the graph using two colors, validating its bipartiteness. By starting at any unvisited node and attempting to color it with one color, you color all its neighbors with an alternating color. If at any point a neighbor is found to already have the same color, then the graph cannot be bipartite.
This implementation uses a function bfsCheck to perform a BFS from a specific node, attempting to color nodes with two colors. The visited nodes are stored in a queue, and adjacent nodes get an alternating color. The function immediately returns false if any adjacent nodes have the same color, indicating a non-bipartite graph.
C++
Java
Python
C#
JavaScript
The time complexity is O(V + E) where V is the number of vertices and E is the number of edges, as each edge and vertex is visited once in BFS. The space complexity is O(V) due to the storage needed for the color array and the BFS queue.
The depth-first search (DFS) approach is an alternative to BFS for examining bipartiteness. You use DFS recursively to color the graph and check for conflicts. The method still involves checking whether adjacent nodes have opposing colors, iterating through components one by one as needed.
In this C implementation, the dfsCheck function initiates a depth-first recursive coloring, verifying that no adjacent nodes share the same color. Each time a recursive call finds a color conflict, it will return false, propagating back through the recusrsion stack to the caller.
C++
Java
Python
C#
JavaScript
The time complexity is O(V + E), and the space complexity is O(V) due to recursive depth stack space utilization.
| Approach | Complexity |
|---|---|
| Two-Coloring/BFS Approach | The time complexity is O(V + E) where V is the number of vertices and E is the number of edges, as each edge and vertex is visited once in BFS. The space complexity is O(V) due to the storage needed for the color array and the BFS queue. |
| DFS Approach | The time complexity is O(V + E), and the space complexity is O(V) due to recursive depth stack space utilization. |
G-17. Bipartite Graph | BFS | C++ | Java • take U forward • 257,809 views views
Watch 9 more video solutions →Practice Is Graph Bipartite? with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor