Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ff116c7

Browse files
add a solution for 785
1 parent 6144fd5 commit ff116c7

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/main/java/com/fishercoder/solutions/_785.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.LinkedList;
44
import java.util.Queue;
5+
import java.util.Stack;
56

67
public class _785 {
78
public static class Solution1 {
@@ -11,7 +12,7 @@ public static class Solution1 {
1112
public boolean isBipartite(int[][] graph) {
1213
int[] visited = new int[graph.length];
1314
//BFS
14-
//0 means never encountered before, 1 means traversing in progress, 2 means traversing completed
15+
//0 means never encountered before, 1 means we put this node into set A, 2 means we put this node into set B
1516
for (int i = 0; i < graph.length; i++) {
1617
if (graph[i].length != 0 && visited[i] == 0) {
1718
visited[i] = 1;
@@ -21,6 +22,7 @@ public boolean isBipartite(int[][] graph) {
2122
int current = queue.poll();
2223
for (int node : graph[current]) {
2324
if (visited[node] == 0) {
25+
//if the current node is in set A (1), then we put its neighbor in set B (2), otherwise set A (1)
2426
visited[node] = (visited[current] == 1) ? 2 : 1;
2527
queue.offer(node);
2628
} else {
@@ -35,4 +37,39 @@ public boolean isBipartite(int[][] graph) {
3537
return true;
3638
}
3739
}
40+
41+
public static class Solution2 {
42+
/**
43+
* credit: https://leetcode.com/problems/is-graph-bipartite/solution/
44+
* <p>
45+
* Let red indicate set A and blue indicate set B, if the graph is a bipartite,
46+
* we should be able to greedily color this graph: for each node, if we color it red, then color all of its neighbors blue, etc.
47+
*/
48+
public boolean isBipartite(int[][] graph) {
49+
//0 means uncolored, 1 means red and 2 means blue
50+
int[] colors = new int[graph.length];
51+
for (int start = 0; start < graph.length; start++) {
52+
if (colors[start] == 0) {
53+
Stack<Integer> stack = new Stack<>();
54+
stack.push(start);
55+
colors[start] = 1;//color it to be red
56+
57+
while (!stack.isEmpty()) {
58+
Integer curr = stack.pop();
59+
for (int neighbor : graph[curr]) {
60+
if (colors[neighbor] == 0) {
61+
stack.push(neighbor);
62+
//if the current node is red (1), then we color it to be blue (2), otherwise red (1)
63+
colors[neighbor] = (colors[curr] == 1) ? 2 : 1;
64+
} else if (colors[neighbor] == colors[curr]) {
65+
//this means the two connected nodes have the same color, so this is not a bipartite
66+
return false;
67+
}
68+
}
69+
}
70+
}
71+
}
72+
return true;
73+
}
74+
}
3875
}

src/test/java/com/fishercoder/_785Test.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,34 @@
55
import org.junit.BeforeClass;
66
import org.junit.Test;
77

8-
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertTrue;
910

1011
public class _785Test {
1112
private static _785.Solution1 solution1;
13+
private static _785.Solution2 solution2;
1214
private static int[][] graph;
1315

1416
@BeforeClass
1517
public static void setup() {
1618
solution1 = new _785.Solution1();
19+
solution2 = new _785.Solution2();
1720
graph = new int[][]{};
1821
}
1922

2023
@Test
2124
public void test1() {
2225
graph = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2,3],[0,2],[0,1,3],[0,2]");
2326
CommonUtils.print2DIntArray(graph);
24-
assertEquals(false, solution1.isBipartite(graph));
27+
assertFalse(solution1.isBipartite(graph));
28+
assertFalse(solution2.isBipartite(graph));
2529
}
2630

2731
@Test
2832
public void test2() {
2933
graph = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3],[0,2],[1,3],[0,2]");
3034
CommonUtils.print2DIntArray(graph);
31-
assertEquals(true, solution1.isBipartite(graph));
35+
assertTrue(solution1.isBipartite(graph));
36+
assertTrue(solution2.isBipartite(graph));
3237
}
3338
}

0 commit comments

Comments
 (0)