-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
67 lines (60 loc) · 2.09 KB
/
Main.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
package medium.connectedComponentsCount;
import java.util.*;
public class Main {
public static int connectedComponentsCount(HashMap<Character, List<Character>> graph){
if (graph.isEmpty())
return 0;
Set<Character> notVisited = new HashSet<>();
notVisited.addAll(graph.keySet());
int count = 0;
while (!notVisited.isEmpty()){
char curr = notVisited.toArray(new Character[graph.size()])[0];
Queue<Character> queue = new LinkedList<>();
queue.offer(curr);
while (!queue.isEmpty()){
curr = queue.poll();
// now visited
notVisited.remove(curr);
System.out.println(curr + ": " + Arrays.toString(graph.get(curr).toArray()));
for (Character c :
graph.get(curr)) {
if (notVisited.contains(c)) {
queue.offer(c);
}
}
}
count++;
}
return count;
}
public static void main(String[] args){
// must not have directions
HashMap<Character, List<Character>> graphWithCycles = new HashMap<>();
List<Character> i2 = new ArrayList<>();
i2.add('j');
i2.add('k');
graphWithCycles.put('i', i2);
List<Character> j2 = new ArrayList<>();
j2.add('i');
j2.add('k');
graphWithCycles.put('j', j2);
List<Character> k2 = new ArrayList<>();
k2.add('i');
k2.add('j');
k2.add('l');
graphWithCycles.put('k', k2);
List<Character> l2 = new ArrayList<>();
l2.add('k');
graphWithCycles.put('l', l2);
// disconnected o
graphWithCycles.put('o', new ArrayList<>());
// disconnected b-a
List<Character> a = new ArrayList<>();
a.add('b');
graphWithCycles.put('a', a);
List<Character> b = new ArrayList<>();
a.add('a');
graphWithCycles.put('b', b);
System.out.println(connectedComponentsCount(graphWithCycles));
}
}