finished implementing findBridges method

Still very much stuck at figuring out how to find all arcticualtions in a graph
This commit is contained in:
René Fuhry 2023-05-14 00:16:01 +02:00 committed by GitHub
parent 295f6f688b
commit 6dff42fe3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,7 +24,7 @@ public class Graph {
calculateExzentrizitäten(); calculateExzentrizitäten();
calculateProperties(); calculateProperties();
findComponents(); findComponents();
findComponents(); findBridges();
findArticulations(); findArticulations();
} }
@ -143,9 +143,6 @@ public class Graph {
} }
components[rowIndex] = component; components[rowIndex] = component;
} }
if(!zusammenhaengend) {
return;
}
for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { for(int rowIndex = 0; rowIndex < components.length; rowIndex++) {
for(int index = 1; index < components.length; index++) { for(int index = 1; index < components.length; index++) {
@ -156,8 +153,8 @@ public class Graph {
} }
} }
public int[] findComponents(Matrix matrix) { public int[][] findComponents(Matrix matrix) {
int[][] components = new int[][]; int[][] tempComponents = new int[matrix.getRowLength()][matrix.getColumnLength()];
int[] component = new int[matrix.getRowLength()]; int[] component = new int[matrix.getRowLength()];
for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) {
@ -167,53 +164,94 @@ public class Graph {
index++; index++;
} }
} }
components[rowIndex] = component; tempComponents[rowIndex] = component;
} }
if(!zusammenhaengend) { if(!zusammenhaengend) {
return null; return null;
} }
for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { for(int rowIndex = 0; rowIndex < tempComponents.length; rowIndex++) {
for(int index = 1; index < components.length; index++) { for(int index = 1; index < tempComponents.length; index++) {
if(components[index] != null && components[rowIndex].equals(components[index])) { if(tempComponents[index] != null && tempComponents[rowIndex].equals(tempComponents[index])) {
components[index] = null; tempComponents[index] = null;
} }
} }
} }
return tempComponents;
} }
public void findBridges() { public void findBridges() {
if(!zusammenhaengend) {
return;
}
bridges = new int[wegMatrix.getRowLength()][2]; bridges = new int[wegMatrix.getRowLength()][2];
Matrix matrix = wegMatrix; int[][] newComponents;
int[][] newComponents = new int[wegMatrix.getRowLength()][wegMatrix.getColumnLength()]; int[] bridge = new int[2];
for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { for(int i = 0, columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) {
for(int columnIndex = 0; columnIndex < matrix.getColumnLength(); columnIndex++) { for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) {
if(rowIndex != columnIndex) { if(rowIndex != 1) {
matrix.insert(rowIndex, columnIndex, 0); continue;
} }
wegMatrix.insert(rowIndex, columnIndex, 0);
wegMatrix.insert(columnIndex, rowIndex, 0);
bridge[0] = columnIndex + 1;
bridge[1] = rowIndex + 1;
newComponents = findComponents(wegMatrix);
if(!components.equals(newComponents)) {
bridges[i] = bridge;
i++;
}
wegMatrix.insert(rowIndex, columnIndex, 1);
wegMatrix.insert(columnIndex, rowIndex, 1);
} }
} }
if(components.length == newComponents.length) { for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) {
return; for(int index = 1; index < bridges.length; index++) {
if(bridges[index] != null && bridges[rowIndex].equals(bridges[index])) {
bridges[index] = null;
}
}
} }
} }
public void findArticulations() { public void findArticulations() {
articulations = new int[wegMatrix.getRowLength()]; if(!zusammenhaengend) {
Matrix matrix = wegMatrix;
int[][] newComponents = new int[wegMatrix.getRowLength()][wegMatrix.getColumnLength()];
for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) {
for(int columnIndex = 0; columnIndex < matrix.getColumnLength(); columnIndex++) {
}
}
if(components.length == newComponents.length) {
return; return;
} }
articulations = new int[wegMatrix.getRowLength()];
int[][] newComponents;
for(int i = 0, j = 0; i < wegMatrix.getRowLength(); i++) {
for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) {
for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) {
wegMatrix.insert(rowIndex, i, 0);
wegMatrix.insert(i, columnIndex, 0);
}
}
/*
* need to figure out what removing a node means
*/
newComponents = findComponents(wegMatrix);
if(!components.equals(newComponents)) {
articulations[j] = i + 1;
j++;
}
for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) {
for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) {
wegMatrix.insert(rowIndex, i, 1);
wegMatrix.insert(i, columnIndex, 1);
}
}
}
} }
public String toString() { public String toString() {
@ -248,18 +286,26 @@ public class Graph {
s += "}"; s += "}";
s += "\nBrücken: {"; s += "\nBrücken: {";
for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { if(bridges != null) {
if(components[rowIndex] != null) { for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) {
s += Arrays.toString(components[rowIndex]); if(bridges[rowIndex] != null) {
if(rowIndex < components.length - 1) { s += Arrays.toString(bridges[rowIndex]);
if(components[rowIndex + 1] != null) { if(rowIndex < bridges.length - 1) {
s += ","; if(bridges[rowIndex + 1] != null) {
s += ",";
}
} }
} }
} }
} }
s += "}"; s += "}";
s += "\nArtikulationen: {";
if(articulations[0] != 0) {
s += Arrays.toString(articulations);
}
s += "}";
return s; return s;
} }
} }