Skip to content

Commit 1076e67

Browse files
authored
Added task 785.
1 parent a0adec7 commit 1076e67

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0701_0800.s0785_is_graph_bipartite;
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
4+
5+
public class Solution {
6+
public boolean isBipartite(int[][] graph) {
7+
int n = graph.length;
8+
int[] color = new int[n];
9+
for (int i = 0; i < n; i++) {
10+
if (color[i] == 0 && !helper(graph, i, -1, color)) {
11+
return false;
12+
}
13+
}
14+
return true;
15+
}
16+
17+
private boolean helper(int[][] graph, int curr, int c, int[] color) {
18+
if (color[curr] == c) {
19+
return true;
20+
}
21+
color[curr] = c;
22+
for (int x : graph[curr]) {
23+
if (color[x] == c || !helper(graph, x, c * -1, color)) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
785\. Is Graph Bipartite?
2+
3+
Medium
4+
5+
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:
6+
7+
* There are no self-edges (`graph[u]` does not contain `u`).
8+
* There are no parallel edges (`graph[u]` does not contain duplicate values).
9+
* If `v` is in `graph[u]`, then `u` is in `graph[v]` (the graph is undirected).
10+
* The graph may not be connected, meaning there may be two nodes `u` and `v` such that there is no path between them.
11+
12+
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`.
13+
14+
Return `true` _if and only if it is **bipartite**_.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2020/10/21/bi2.jpg)
19+
20+
**Input:** graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
21+
22+
**Output:** false
23+
24+
**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.
25+
26+
**Example 2:**
27+
28+
![](https://assets.leetcode.com/uploads/2020/10/21/bi1.jpg)
29+
30+
**Input:** graph = [[1,3],[0,2],[1,3],[0,2]]
31+
32+
**Output:** true
33+
34+
**Explanation:** We can partition the nodes into two sets: {0, 2} and {1, 3}.
35+
36+
**Constraints:**
37+
38+
* `graph.length == n`
39+
* `1 <= n <= 100`
40+
* `0 <= graph[u].length < n`
41+
* `0 <= graph[u][i] <= n - 1`
42+
* `graph[u]` does not contain `u`.
43+
* All the values of `graph[u]` are **unique**.
44+
* If `graph[u]` contains `v`, then `graph[v]` contains `u`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0701_0800.s0785_is_graph_bipartite;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void isBipartite() {
11+
assertThat(
12+
new Solution().isBipartite(new int[][] {{1, 2, 3}, {0, 2}, {0, 1, 3}, {0, 2}}),
13+
equalTo(false));
14+
}
15+
16+
@Test
17+
void isBipartite2() {
18+
assertThat(
19+
new Solution().isBipartite(new int[][] {{1, 3}, {0, 2}, {1, 3}, {0, 2}}),
20+
equalTo(true));
21+
}
22+
}

0 commit comments

Comments
 (0)