heavy refactoring and findArticulations now works

This commit is contained in:
René Fuhry 2023-05-22 15:38:54 +02:00 committed by GitHub
parent 5b72916732
commit 23767a9ad7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,13 +6,13 @@ public class Graph {
private Matrix distanzMatrix; private Matrix distanzMatrix;
private Matrix wegMatrix; private Matrix wegMatrix;
private boolean zusammenhaengend; private boolean zusammenhaengend;
private int[] exzentrizitäten; private ArrayList<Integer> exzentrizitäten;
private int radius; private int radius;
private int durchmesser; private int durchmesser;
private ArrayList<Integer> centre; private ArrayList<Integer> centre;
private int[][] components; private ArrayList<ArrayList<Integer>> components;
private int[][] bridges; private ArrayList<int[]> bridges;
private int[] articulations; private ArrayList<Integer> articulations;
public static void main(String[] args) {} public static void main(String[] args) {}
@ -25,7 +25,7 @@ public class Graph {
calculateProperties(); calculateProperties();
findComponents(); findComponents();
//findBridges(); //findBridges();
//findArticulations(); findArticulations(file);
} }
public void calculateDistanzMatrix() { public void calculateDistanzMatrix() {
@ -85,7 +85,8 @@ public class Graph {
} }
public void calculateExzentrizitäten() { public void calculateExzentrizitäten() {
exzentrizitäten = new int[distanzMatrix.getRowLength()]; exzentrizitäten = new ArrayList<>(1);
for(int rowIndex = 0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) {
int exzentrizität = 0; int exzentrizität = 0;
@ -94,7 +95,7 @@ public class Graph {
exzentrizität = distanzMatrix.getValueAt(columnIndex, rowIndex); exzentrizität = distanzMatrix.getValueAt(columnIndex, rowIndex);
} }
} }
exzentrizitäten[rowIndex] = exzentrizität; exzentrizitäten.add(exzentrizität);
} }
} }
@ -104,169 +105,113 @@ public class Graph {
zusammenhaengend = true; zusammenhaengend = true;
centre = new ArrayList<>(1); centre = new ArrayList<>(1);
for(int rowIndex = 0; rowIndex < exzentrizitäten.length; rowIndex++) { for(int rowIndex = 0; rowIndex < exzentrizitäten.size(); rowIndex++) {
if(exzentrizitäten[rowIndex] > durchmesser) { if(exzentrizitäten.get(rowIndex) > durchmesser) {
durchmesser = exzentrizitäten.get(rowIndex);
centre.clear(); centre.clear();
centre.add(rowIndex + 1); centre.add(rowIndex + 1);
} }
if(exzentrizitäten[rowIndex] < radius) { if(exzentrizitäten.get(rowIndex) < radius) {
radius = exzentrizitäten[rowIndex]; radius = exzentrizitäten.get(rowIndex);
} }
if(exzentrizitäten[rowIndex] == durchmesser) { if(exzentrizitäten.get(rowIndex) == durchmesser) {
centre.add(rowIndex + 1); centre.add(rowIndex + 1);
} }
if(exzentrizitäten[rowIndex] > durchmesser) {
durchmesser = exzentrizitäten[rowIndex];
}
} }
if(durchmesser == 0 && exzentrizitäten.length > 1) { if(radius == 0) {
zusammenhaengend = false; zusammenhaengend = false;
return;
} }
centre.trimToSize();
} }
public void findComponents() { public void findComponents() {
ArrayList<int[]> tempComponents = new ArrayList<>(1); components = new ArrayList<>(1);
ArrayList<Integer> tempComponent = new ArrayList<>(1); ArrayList<Integer> component = new ArrayList<>(1);
int[] component;
for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) {
component = new ArrayList<>(1);
for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) {
if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) { if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) {
tempComponent.add(columnIndex + 1); component.add(columnIndex + 1);
} }
} }
component = new int[tempComponent.size()];
if(!components.contains(component)) {
for(int index = 0; index < component.length; index++) { components.add(component);
component[index] = tempComponent.get(index);
} }
if(tempComponents.contains(component)) {
continue;
}
tempComponents.add(component);
component = null;
tempComponent.clear();
}
components = new int[tempComponents.size()][wegMatrix.getColumnLength()];
for(int rowIndex = 0; rowIndex < components.length; rowIndex++) {
components[rowIndex] = tempComponents.get(rowIndex);
} }
} }
public int[][] findComponents(Matrix matrix) { public ArrayList<ArrayList<Integer>> findComponents(Matrix matrix) {
int[][] newComponents; ArrayList<ArrayList<Integer>> newComponents = new ArrayList<>(1);
ArrayList<int[]> tempComponents = new ArrayList<>(1); ArrayList<Integer> component = new ArrayList<>(1);
ArrayList<Integer> tempComponent = new ArrayList<>(1);
int[] component;
for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) {
for(int columnIndex = 0; columnIndex < matrix.getColumnLength(); columnIndex++) { component = new ArrayList<>(1);
if(matrix.getValueAt(rowIndex, columnIndex) == 1) {
tempComponent.add(columnIndex + 1); for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) {
if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) {
component.add(columnIndex + 1);
} }
} }
component = new int[tempComponent.size()];
if(!newComponents.contains(component)) {
for(int index = 0; index < component.length; index++) { newComponents.add(component);
component[index] = tempComponent.get(index);
} }
if(tempComponents.contains(component)) {
continue;
}
tempComponents.add(component);
component = null;
tempComponent.clear();
}
newComponents = new int[tempComponents.size()][matrix.getColumnLength()];
for(int rowIndex = 0; rowIndex < newComponents.length; rowIndex++) {
newComponents[rowIndex] = tempComponents.get(rowIndex);
} }
return newComponents; return newComponents;
} }
public void findBridges() { public void findBridges() {
if(!zusammenhaengend) { bridges = new ArrayList<>(1);
return; ArrayList<ArrayList<Integer>> newComponents;
}
ArrayList<int[]> tempBridges = new ArrayList<>(10);
int[][] newComponents;
int[] bridge; int[] bridge;
for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) {
for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) {
wegMatrix.insert(rowIndex, columnIndex, 0);
wegMatrix.insert(columnIndex, rowIndex, 0);
bridge = new int[2]; bridge = new int[2];
bridge[0] = columnIndex + 1; bridge[0] = columnIndex + 1;
bridge[1] = rowIndex + 1; bridge[1] = rowIndex + 1;
wegMatrix.insert(rowIndex, columnIndex, 0);
wegMatrix.insert(columnIndex, rowIndex, 0);
newComponents = findComponents(wegMatrix); newComponents = findComponents(wegMatrix);
System.out.println(Arrays.toString(bridge)); //System.out.println(Arrays.toString(bridge));
if(!components.equals(newComponents) && !tempBridges.contains(bridge)) { if(newComponents.size() > components.size()) {
System.out.println("bruh"); //System.out.println("bruh");
tempBridges.add(bridge); bridges.add(bridge);
} }
bridge = null;
wegMatrix.insert(rowIndex, columnIndex, 1); wegMatrix.insert(rowIndex, columnIndex, 1);
wegMatrix.insert(columnIndex, rowIndex, 1); wegMatrix.insert(columnIndex, rowIndex, 1);
} }
} }
bridges = new int[tempBridges.size()][2];
for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) {
bridges[rowIndex] = tempBridges.get(rowIndex);
}
} }
public void findArticulations() { public void findArticulations(String file) {
if(!zusammenhaengend) { articulations = new ArrayList<>(1);
return; ArrayList<ArrayList<Integer>> newComponents;
}
ArrayList<Integer> tempArticulations = new ArrayList<>(10);
int[][] newComponents;
for(int i = 0; i < wegMatrix.getRowLength(); i++) { for(int i = 0; i < adjazenzMatrix.getRowLength(); i++) {
for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < adjazenzMatrix.getRowLength(); rowIndex++) {
for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { for(int columnIndex = 0; columnIndex < adjazenzMatrix.getColumnLength(); columnIndex++) {
wegMatrix.insert(rowIndex, i, 0); adjazenzMatrix.insert(rowIndex, i, 0);
wegMatrix.insert(i, columnIndex, 0); adjazenzMatrix.insert(i, columnIndex, 0);
} }
} }
/* calculateWegMatrix();
* need to figure out what removing a node means
*/
newComponents = findComponents(wegMatrix); newComponents = findComponents(wegMatrix);
if(!components.equals(newComponents)) { if(newComponents.size() > components.size() + 1) {
tempArticulations.add(i + 1); articulations.add(i + 1);
} }
for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { adjazenzMatrix = new Matrix(file);
for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) {
wegMatrix.insert(rowIndex, i, 1);
wegMatrix.insert(i, columnIndex, 1);
}
}
}
articulations = new int[tempArticulations.size()];
for(int rowIndex = 0; rowIndex < articulations.length; rowIndex++) {
articulations[rowIndex] = tempArticulations.get(rowIndex);
} }
} }
@ -278,45 +223,39 @@ public class Graph {
if(!zusammenhaengend) { if(!zusammenhaengend) {
s += "\nExzentrizitäten/Radius/Durchmesser: Kein zusammenhängender Graph"; s += "\nExzentrizitäten/Radius/Durchmesser: Kein zusammenhängender Graph";
} else { } else {
s += "\nExzentrizitäten: " + Arrays.toString(exzentrizitäten) + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser; s += "\nExzentrizitäten: " + exzentrizitäten.toString() + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser;
} }
s += "\nZentrum: "; s += "\nZentrum: ";
if(zusammenhaengend) { if(!zusammenhaengend) {
s += centre;
} else {
s += "Kein zusammenhängender Graph"; s += "Kein zusammenhängender Graph";
} else {
s += centre;
} }
s += "\nKomponente: {"; s += "\nKomponente: {";
for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { for(int rowIndex = 0; rowIndex < components.size(); rowIndex++) {
if(components[rowIndex] != null) { s += components.get(rowIndex).toString();
s += Arrays.toString(components[rowIndex]); if(rowIndex < components.size() - 1) {
if(rowIndex < components.length - 1) { s += ",";
if(components[rowIndex + 1] != null) {
s += ",";
}
}
} }
} }
s += "}"; s += "}";
s += "\nBrücken: {"; s += "\nBrücken: {";
if(bridges != null) { if(bridges != null) {
for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) { for(int rowIndex = 0; rowIndex < bridges.size(); rowIndex++) {
if(bridges[rowIndex] != null) { s += Arrays.toString(bridges.get(rowIndex));
s += Arrays.toString(bridges[rowIndex]); if(rowIndex < bridges.size() - 1) {
if(rowIndex < bridges.length - 1) { if(rowIndex < bridges.size() - 1) {
if(bridges[rowIndex + 1] != null) { s += ",";
s += ",";
}
} }
} }
} }
} }
s += "}"; s += "}";
s += "\nArtikulationen: " + Arrays.toString(articulations); s += "\nArtikulationen: " + articulations.toString();
return s; return s;
} }
} }