not better, just different

This commit is contained in:
René Fuhry 2023-05-27 20:02:00 +02:00 committed by GitHub
parent e6c365aecf
commit 5ae8c0d2ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 30 deletions

View File

@ -20,12 +20,12 @@ public class Graph {
public Graph(String file) { public Graph(String file) {
adjazenzMatrix = new Matrix(file); adjazenzMatrix = new Matrix(file);
calculateDistanzMatrix(); calculateDistanzMatrix();
calculateWegMatrix(); calculateWegMatrix(adjazenzMatrix);
calculateExzentrizitäten(); calculateExzentrizitäten();
calculateProperties(); calculateProperties();
findComponents(); findComponents();
findBridges(file); findBridges();
findArticulations(file); findArticulations();
} }
public void calculateDistanzMatrix() { public void calculateDistanzMatrix() {
@ -57,22 +57,22 @@ public class Graph {
} }
} }
public void calculateWegMatrix() { public void calculateWegMatrix(Matrix matrix) {
wegMatrix = new Matrix(adjazenzMatrix.getRowLength(), adjazenzMatrix.getColumnLength()); wegMatrix = new Matrix(matrix.getRowLength(), matrix.getColumnLength());
Matrix potenzMatrix = adjazenzMatrix; Matrix potenzMatrix = matrix;
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++) {
if(columnIndex == rowIndex) { if(columnIndex == rowIndex) {
wegMatrix.insert(rowIndex, columnIndex, 1); wegMatrix.insert(rowIndex, columnIndex, 1);
} else if(adjazenzMatrix.getValueAt(rowIndex, columnIndex) > 0 && wegMatrix.getValueAt(rowIndex, columnIndex) == 0) { } else if(matrix.getValueAt(rowIndex, columnIndex) > 0 && wegMatrix.getValueAt(rowIndex, columnIndex) == 0) {
wegMatrix.insert(rowIndex, columnIndex, 1); wegMatrix.insert(rowIndex, columnIndex, 1);
} }
} }
} }
for(int k = 2; k < wegMatrix.getRowLength(); k++) { for(int k = 2; k < wegMatrix.getRowLength(); k++) {
potenzMatrix = potenzMatrix.multiply(adjazenzMatrix); potenzMatrix = potenzMatrix.multiply(matrix);
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++) {
@ -164,17 +164,18 @@ public class Graph {
newComponents.add(component); newComponents.add(component);
} }
} }
return newComponents; return newComponents;
} }
public void findBridges(String file) { public void findBridges() {
Matrix tempMatrix = adjazenzMatrix.clone();
bridges = new ArrayList<>(1); bridges = new ArrayList<>(1);
ArrayList<ArrayList<Integer>> newComponents; ArrayList<ArrayList<Integer>> newComponents;
int[] bridge; int[] bridge;
boolean contains; boolean contains;
for(int rowIndex = 0; rowIndex < adjazenzMatrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < tempMatrix.getRowLength(); rowIndex++) {
for(int columnIndex = 0; columnIndex < adjazenzMatrix.getColumnLength(); columnIndex++) { for(int columnIndex = 0; columnIndex < tempMatrix.getColumnLength(); columnIndex++) {
if(rowIndex == columnIndex) { if(rowIndex == columnIndex) {
continue; continue;
} }
@ -187,12 +188,11 @@ public class Graph {
bridge[1] = columnIndex + 1; bridge[1] = columnIndex + 1;
bridge[0] = rowIndex + 1; bridge[0] = rowIndex + 1;
} }
adjazenzMatrix.insert(rowIndex, columnIndex, 0); tempMatrix.insert(rowIndex, columnIndex, 0);
adjazenzMatrix.insert(columnIndex, rowIndex, 0); tempMatrix.insert(columnIndex, rowIndex, 0);
calculateWegMatrix(); calculateWegMatrix(tempMatrix);
newComponents = findComponents(wegMatrix); newComponents = findComponents(wegMatrix);
@ -208,25 +208,26 @@ public class Graph {
bridges.add(bridge); bridges.add(bridge);
} }
adjazenzMatrix = new Matrix(file); tempMatrix = adjazenzMatrix.clone();
} }
} }
calculateWegMatrix(); calculateWegMatrix(adjazenzMatrix);
} }
public void findArticulations(String file) { public void findArticulations() {
Matrix tempMatrix = adjazenzMatrix.clone();
articulations = new ArrayList<>(1); articulations = new ArrayList<>(1);
ArrayList<ArrayList<Integer>> newComponents; ArrayList<ArrayList<Integer>> newComponents;
for(int i = 0; i < adjazenzMatrix.getRowLength(); i++) { for(int i = 0; i < tempMatrix.getRowLength(); i++) {
for(int rowIndex = 0; rowIndex < adjazenzMatrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < tempMatrix.getRowLength(); rowIndex++) {
for(int columnIndex = 0; columnIndex < adjazenzMatrix.getColumnLength(); columnIndex++) { for(int columnIndex = 0; columnIndex < tempMatrix.getColumnLength(); columnIndex++) {
adjazenzMatrix.insert(rowIndex, i, 0); tempMatrix.insert(rowIndex, i, 0);
adjazenzMatrix.insert(i, columnIndex, 0); tempMatrix.insert(i, columnIndex, 0);
} }
} }
calculateWegMatrix(); calculateWegMatrix(tempMatrix);
newComponents = findComponents(wegMatrix); newComponents = findComponents(wegMatrix);
@ -234,9 +235,9 @@ public class Graph {
articulations.add(i + 1); articulations.add(i + 1);
} }
adjazenzMatrix = new Matrix(file); tempMatrix = adjazenzMatrix.clone();
} }
calculateWegMatrix(); calculateWegMatrix(adjazenzMatrix);
} }
public String toString() { public String toString() {
@ -279,7 +280,10 @@ public class Graph {
} }
s += "}"; s += "}";
s += "\nArtikulationen: " + articulations.toString(); s += "\nArtikulationen: ";
if(articulations != null)
s += articulations.toString();
return s; return s;
} }
} }

View File

@ -95,7 +95,7 @@ public class Matrix {
} }
} }
public int[][] clone() { public Matrix clone() {
int[][] clone = new int[rowLength][columnLength]; int[][] clone = new int[rowLength][columnLength];
for(int columnIndex=0; columnIndex < columnLength; columnIndex++) { for(int columnIndex=0; columnIndex < columnLength; columnIndex++) {
@ -104,7 +104,7 @@ public class Matrix {
} }
} }
return clone; return new Matrix(clone);
} }
public void randomAdjazenzMatrix() { public void randomAdjazenzMatrix() {