-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pruner.java
34 lines (32 loc) · 1011 Bytes
/
Pruner.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
import java.util.ArrayList;
public class Pruner {
public static int[][] prune(int[][] adjMatrix) {
ArrayList<Integer> pruneable = new ArrayList<>();
for (int i = 0; i < adjMatrix.length; i++) {
int counter = 0;
for (int j = 0; j < adjMatrix.length;j++){
if (adjMatrix[i][j] == 1) {
counter++;
}
if (j == adjMatrix.length - 1) {
if (counter == 1) {
pruneable.add(i);
}
}
}
}
for (int i = 0; i < pruneable.size(); i++) {
int delete = (int) pruneable.get(i);
for (int j = 0; j < adjMatrix.length; j++) {
adjMatrix[delete][j] = 0;
adjMatrix[j][delete] = 0;
}
}
if (pruneable.isEmpty()) {
return adjMatrix;
} else {
prune(adjMatrix);
}
return adjMatrix;
}
}