-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ConnectedComponents.java
74 lines (63 loc) · 2.66 KB
/
ConnectedComponents.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
package com.jwetherell.algorithms.graph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jwetherell.algorithms.data_structures.Graph;
import com.jwetherell.algorithms.data_structures.Graph.Edge;
import com.jwetherell.algorithms.data_structures.Graph.Vertex;
/**
* In graph theory, 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. A vertex with no incident edges is itself a connected
* component. A graph that is itself connected has exactly one connected component, consisting of the whole graph.
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Connected_component_(graph_theory)">Connected Components (Wikipedia)</a>
* <br>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class ConnectedComponents {
private ConnectedComponents() { }
/**
* Finds the connected components subsets of the Graph.
*
* @param graph
* to find connected components.
* @return List of connected components in the Graph.
*/
public static final <T extends Comparable<T>> List<List<Vertex<T>>> getConnectedComponents(Graph<T> graph) {
if (graph == null)
throw new IllegalArgumentException("Graph is NULL.");
if (graph.getType() != Graph.TYPE.DIRECTED)
throw new IllegalArgumentException("Cannot perform a connected components search on a non-directed graph. graph type = "+graph.getType());
final Map<Vertex<T>,Integer> map = new HashMap<Vertex<T>,Integer>();
final List<List<Vertex<T>>> list = new ArrayList<List<Vertex<T>>>();
int c = 0;
for (Vertex<T> v : graph.getVertices())
if (map.get(v) == null)
visit(map, list, v, c++);
return list;
}
private static final <T extends Comparable<T>> void visit(Map<Vertex<T>,Integer> map, List<List<Vertex<T>>> list, Vertex<T> v, int c) {
map.put(v, c);
List<Vertex<T>> r = null;
if (c == list.size()) {
r = new ArrayList<Vertex<T>>();
list.add(r);
} else {
r = list.get(c);
}
r.add(v);
if (v.getEdges().size() > 0) {
boolean found = false;
for (Edge<T> e : v.getEdges()) {
final Vertex<T> to = e.getToVertex();
if (map.get(to) == null) {
visit(map, list, to, c);
found = true;
}
if (found)
break;
}
}
}
}