-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBipartite.java
75 lines (59 loc) · 1.6 KB
/
Bipartite.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
/*
Determine if an undirected graph is bipartite. A bipartite graph is one in which the nodes can be
divided into two groups such that no nodes have direct edges to other nodes in the same group.
Examples
1 -- 2
/
3 -- 4
is bipartite (1, 3 in group 1 and 2, 4 in group 2).
1 -- 2
/ |
3 -- 4
is not bipartite.
time = O(v^2) // self and each neighbor
space = O(v) // hashmap visited
*/
/**
* public class GraphNode {
* public int key;
* public List<GraphNode> neighbors;
* public GraphNode(int key) {
* this.key = key;
* this.neighbors = new ArrayList<GraphNode>();
* }
* }
*/
public class Solution {
public boolean isBipartite(List<GraphNode> graph) {
// write your solution here
Map<GraphNode, Integer> visited= new HashMap<GraphNode, Integer>();
for (GraphNode node : graph) {
if (!BFS(node, visited)) {
return false;
}
}
return true;
}
public boolean BFS(GraphNode node, Map<GraphNode, Integer> visited) {
if (visited.containsKey(node)) {
return true;
}
Queue<GraphNode> queue = new LinkedList<GraphNode>();
queue.offer(node);
visited.put(node, 0);
while (!queue.isEmpty()) {
GraphNode cur = queue.poll();
int curGroup = visited.get(cur);
int neiGroup = curGroup == 0 ? 1 : 0;
for (GraphNode nei : cur.neighbors) {
if (!visited.containsKey(nei)) {
queue.offer(nei);
visited.put(nei, neiGroup);
} else if (visited.get(nei) != neiGroup) {
return false;
}
}
}
return true;
}
}