forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisGraphBipartile.java
103 lines (86 loc) · 3.36 KB
/
isGraphBipartile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Given an undirected graph, return true if and only if it is bipartite.
// Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and
// B such that every edge in the graph has one node in A and another node in B.
// The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes
// i and j exists. Each node is an integer between 0 and graph.length - 1. There are no self edges or
// parallel edges: graph[i] does not contain i, and it doesn't contain any element twice.
// Example 1:
// Input: [[1,3], [0,2], [1,3], [0,2]]
// Output: true
// Explanation:
// The graph looks like this:
// 0----1
// | |
// | |
// 3----2
// We can divide the vertices into two groups: {0, 2} and {1, 3}.
// Example 2:
// Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
// Output: false
// Explanation:
// The graph looks like this:
// 0----1
// | \ |
// | \ |
// 3----2
// We cannot find a way to divide the set of nodes into two independent subsets.
Note, graph is represented where graph[0] is array of nodes. THIS IS ADJACENCY LIST representation
USE BFS COLORING, and every node neighbor should be diff COLORING
note, if we have multiple disconnected components, will need to run on each of them
// We'll keep an array (or hashmap) to lookup the color of each node: color[node].
// The colors could be 1, -1, or uncolored (0)
//TC: O(V+E)
//SC: O(V) for the stack space
BEST SOLUTION
Recursive DFS WAY!!
class Solution {
public boolean isBipartite(int[][] graph) {
int n = graph.length;
int[] colors = new int[n];
for (int i = 0; i < n; i++) { //Might be a disconnected graph. So check each unvisited node.
if (colors[i] == 0 && !validColor(graph, colors, 1, i)) {. //if node isnt colored yet, run dfs
return false;
}
}
return true;
}
public boolean validColor(int[][] graph, int[] colors, int color, int node) {
if (colors[node] != 0) { //has already been colored so don't process again, stopping condition
return colors[node] == color;
}
colors[node] = color;
for (int next : graph[node]) {
if (!validColor(graph, colors, -color, next)) {
return false;
}
}
return true;
}
}
BFS WAY!!
//TC: O(V+E)
//SC: O(V) for the space in the queue
class Solution {
public boolean isBipartite(int[][] graph) {
int len = graph.length;
int[] colors = new int[len];
for (int i = 0; i < len; i++) {
if (colors[i] != 0) continue;
Queue<Integer> queue = new LinkedList<>();
queue.offer(i);
colors[i] = 1; // Blue: 1; Red: -1.
while (!queue.isEmpty()) {
int cur = queue.poll();
for (int next : graph[cur]) {
if (colors[next] == 0) { // If this node hasn't been colored;
colors[next] = -colors[cur]; // Color it with a different color;
queue.offer(next);
} else if (colors[next] != -colors[cur]) { // If it is colored and its color is different, return false;
return false;
}
}
}
}
return true;
}
}