-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathFind the Connected Component in the Undirected Graph.java
executable file
·183 lines (153 loc) · 5.95 KB
/
Find the Connected Component in the Undirected Graph.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
M
1527969793
tags: BFS, DFS
给一个undirected graph, return 所有的component. (这道题找不到了)
#### BFS
- BFS遍历,把每个node的neighbor都加进来.
- 一定注意要把visit过的node Mark一下。因为curr node也会是别人的neighbor,会无限循环。
- Component的定义:所有Component内的node必须被串联起来via path (反正这里是undirected, 只要链接上就好)
- 这道题:其实component在input里面都已经给好了,所有能一口气visit到的,全部加进queue里面,他们就是一个component里面的了。
- 而我们这里不需要判断他们是不是Component
#### DFS
- DFS 应该也可以 visit all nodes, mark visited.
```
/*
Find the number connected component in the undirected graph.
Each node in the graph contains a label and a list of its neighbors.
(a connected component (or just component) of an undirected graph is a subgraph
in which any two vertices are connected to each other by paths,
and which is connected to no additional vertices in the supergraph.)
Example
Given graph:
A------B C
\ | |
\ | |
\ | |
\ | |
D E
Return {A,B,D}, {C,E}. Since there are two connected component which is {A,B,D}, {C,E}
Note
Tags Expand
Breadth First Search
*/
/**
* Definition for Undirected graph.
* class UndirectedGraphNode {
* int label;
* ArrayList<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
/*
OPTS: 11.07.2015
Try to use ae map<Integer, false> to mark the nodes. Then do a BFS with queue
1. Mark each node in map.
2. BFS each node
3. Whenver one node is checked, mark it check
*/
public class Solution {
/**
* @param nodes a array of Undirected graph node
* @return a connected set of a Undirected graph
*/
public List<List<Integer>> connectedSet(ArrayList<UndirectedGraphNode> nodes) {
List<List<Integer>> rst = new ArrayList<List<Integer>>();
if (nodes == null || nodes.size() == 0) {
return rst;
}
HashMap<Integer, Boolean> map = new HashMap<>();
for (UndirectedGraphNode node : nodes) {
map.put(node.label, false);
}
for (UndirectedGraphNode node : nodes) {
if (!map.get(node.label)) {
bfs(rst, node, map);
}
}
return rst;
}
public void bfs(List<List<Integer>> rst, UndirectedGraphNode node, HashMap<Integer, Boolean> map) {
Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
List<Integer> list = new ArrayList<Integer>();
queue.add(node);
map.put(node.label, true);
UndirectedGraphNode temp;
while (!queue.isEmpty()) {
temp = queue.poll();
list.add(temp.label);
for (UndirectedGraphNode neighbor : temp.neighbors) {
if (!map.get(neighbor.label)) {
queue.offer(neighbor);
map.put(neighbor.label, true);
}
}
}
Collections.sort(list);
rst.add(list);
}
}
/*
Thoughts:
How do we check for a connected graph (any two nodes are connected)?
Maybe check for each node: each node represents a lead to a subgraph, then check if this subgraph
is valid.
1. In real case, need to ask the intervier: can we assume the given nodes are valid, so that we only
need to check for success case? That means, we assume for example a linear list A-B-C does not exist.
2. Then, we can use a 'set' to mark: we've checked this node.
3. Use a queue for BFS
4. Use a arraylist to save the results.
5. Key point: when the queue is empty(), that means one set of connected component is ready to go
6. Iterate through nodes, when it's not empty.
More Notes:Have to do Collections.sort()....somehow it want me to sort the results?
Note2: Get rid of a node from nodes, whenever add it to component ... don't forget this.
Note3: Well, there is a chance that compoents are added, queue is cleaned, but nodes are empty as well..
that means, need to catch the last case of 'remaining component' and add it to rst.
Review:
How list, ArrayList, Set, Queue work.
How to do: add, remove, sort
Collections: Set, List, Queue
List: ArrayList
Set methods: add(), contains(?)
Queue methods: offer(E e), add(E e), poll()
ArrayList method: add(E e), isEmpty(), remove(object o)
*/
public class Solution {
/**
* @param nodes a array of Undirected graph node
* @return a connected set of a Undirected graph
*/
public List<List<Integer>> connectedSet(ArrayList<UndirectedGraphNode> nodes) {
List<List<Integer>> rst = new ArrayList<>();
if (nodes == null || nodes.size() == 0) {
return rst;
}
//Init:
Set<UndirectedGraphNode> checked = new HashSet();
Queue<UndirectedGraphNode> queue = new LinkedList();
ArrayList<Integer> component = new ArrayList<Integer>();
queue.offer(nodes.get(0));
while (!nodes.isEmpty()) {
if (queue.isEmpty()) {
Collections.sort(component);
rst.add(component);
queue.offer(nodes.get(0));
component = new ArrayList<Integer>();
} else {
UndirectedGraphNode curr = queue.poll();
if (!checked.contains(curr)) {
checked.add(curr);
component.add(curr.label);
nodes.remove(curr);
for (UndirectedGraphNode node : curr.neighbors) {
queue.add(node);
}
}
}
}
if (!component.isEmpty()) {
rst.add(component);
}
return rst;
}
}
```