-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectedComponents.java
69 lines (58 loc) · 1.74 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
import java.util.ArrayList;
/**
*
*/
/**
* @author Anica
*
*/
public class ConnectedComponents implements Algorithm {
private DFS dfs = new DFS();
private ArrayList<ArrayList<Integer>> connectedComponents;
public ConnectedComponents() {
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see Algorithm#execute(Graph)
*/
@Override
public ArrayList<Operation> execute(Graph graph) {
connectedComponents = new ArrayList<>();
if (graph == null) {
throw new NullPointerException();
}
if (graph.isEmpty()) {
throw new IllegalArgumentException();
}
ArrayList<Operation> changes = new ArrayList<Operation>();
ArrayList<Operation> currentChanges = new ArrayList<Operation>();
ArrayList<Integer> verticesConnectedComponent;
ArrayList<Integer> vertices = graph.getVertices();
Integer startvertex;
while(!vertices.isEmpty()) {
startvertex = vertices.get(0);
dfs = new DFS();
currentChanges = dfs.execute(graph, startvertex);
changes.addAll(currentChanges);
verticesConnectedComponent = new ArrayList<>();
verticesConnectedComponent = dfs.getVerticesOfConnectedComponent();
//System.out.println("number vertices in con comp= " + verticesConnectedComponent.size());
connectedComponents.add(verticesConnectedComponent);
for(int i=0; i<verticesConnectedComponent.size(); i++) {
vertices.remove(verticesConnectedComponent.get(i));
}
}
return changes;
}
/* (non-Javadoc)
* @see Algorithm#getResult(Graph)
*/
@Override
public Graph getResult(Graph graph) {
this.execute(graph);
return graph;
}
public ArrayList<ArrayList<Integer>> getConnectedComponents(){
return connectedComponents;
}
}