Compare commits

..

2 Commits
dev ... main

Author SHA1 Message Date
René Fuhry
b636e22e69
new random graph feature (#6)
* new folder with csv

* more files

* improvements

* new method

* quick fix

* revert

* Too tired to write descriptive message

* can now create random graphs

* random graph with 50 nodes
2023-05-26 12:41:13 +02:00
René Fuhry
e95b94ec7b
small fix (#5) 2023-05-25 23:29:26 +02:00
2 changed files with 30 additions and 34 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(adjazenzMatrix); calculateWegMatrix();
calculateExzentrizitäten(); calculateExzentrizitäten();
calculateProperties(); calculateProperties();
findComponents(); findComponents();
findBridges(); findBridges(file);
findArticulations(); findArticulations(file);
} }
public void calculateDistanzMatrix() { public void calculateDistanzMatrix() {
@ -57,22 +57,22 @@ public class Graph {
} }
} }
public void calculateWegMatrix(Matrix matrix) { public void calculateWegMatrix() {
wegMatrix = new Matrix(matrix.getRowLength(), matrix.getColumnLength()); wegMatrix = new Matrix(adjazenzMatrix.getRowLength(), adjazenzMatrix.getColumnLength());
Matrix potenzMatrix = matrix; Matrix potenzMatrix = adjazenzMatrix;
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(matrix.getValueAt(rowIndex, columnIndex) > 0 && wegMatrix.getValueAt(rowIndex, columnIndex) == 0) { } else if(adjazenzMatrix.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(matrix); potenzMatrix = potenzMatrix.multiply(adjazenzMatrix);
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++) {
@ -167,15 +167,14 @@ public class Graph {
return newComponents; return newComponents;
} }
public void findBridges() { public void findBridges(String file) {
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 < tempMatrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < adjazenzMatrix.getRowLength(); rowIndex++) {
for(int columnIndex = 0; columnIndex < tempMatrix.getColumnLength(); columnIndex++) { for(int columnIndex = 0; columnIndex < adjazenzMatrix.getColumnLength(); columnIndex++) {
if(rowIndex == columnIndex) { if(rowIndex == columnIndex) {
continue; continue;
} }
@ -189,10 +188,11 @@ public class Graph {
bridge[0] = rowIndex + 1; bridge[0] = rowIndex + 1;
} }
tempMatrix.insert(rowIndex, columnIndex, 0);
tempMatrix.insert(columnIndex, rowIndex, 0);
calculateWegMatrix(tempMatrix); adjazenzMatrix.insert(rowIndex, columnIndex, 0);
adjazenzMatrix.insert(columnIndex, rowIndex, 0);
calculateWegMatrix();
newComponents = findComponents(wegMatrix); newComponents = findComponents(wegMatrix);
@ -208,26 +208,25 @@ public class Graph {
bridges.add(bridge); bridges.add(bridge);
} }
tempMatrix = adjazenzMatrix.clone(); adjazenzMatrix = new Matrix(file);
} }
} }
calculateWegMatrix(adjazenzMatrix); calculateWegMatrix();
} }
public void findArticulations() { public void findArticulations(String file) {
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 < tempMatrix.getRowLength(); i++) { for(int i = 0; i < adjazenzMatrix.getRowLength(); i++) {
for(int rowIndex = 0; rowIndex < tempMatrix.getRowLength(); rowIndex++) { for(int rowIndex = 0; rowIndex < adjazenzMatrix.getRowLength(); rowIndex++) {
for(int columnIndex = 0; columnIndex < tempMatrix.getColumnLength(); columnIndex++) { for(int columnIndex = 0; columnIndex < adjazenzMatrix.getColumnLength(); columnIndex++) {
tempMatrix.insert(rowIndex, i, 0); adjazenzMatrix.insert(rowIndex, i, 0);
tempMatrix.insert(i, columnIndex, 0); adjazenzMatrix.insert(i, columnIndex, 0);
} }
} }
calculateWegMatrix(tempMatrix); calculateWegMatrix();
newComponents = findComponents(wegMatrix); newComponents = findComponents(wegMatrix);
@ -235,9 +234,9 @@ public class Graph {
articulations.add(i + 1); articulations.add(i + 1);
} }
tempMatrix = adjazenzMatrix.clone(); adjazenzMatrix = new Matrix(file);
} }
calculateWegMatrix(adjazenzMatrix); calculateWegMatrix();
} }
public String toString() { public String toString() {
@ -280,10 +279,7 @@ public class Graph {
} }
s += "}"; s += "}";
s += "\nArtikulationen: "; s += "\nArtikulationen: " + articulations.toString();
if(articulations != null)
s += articulations.toString();
return s; return s;
} }
} }

View File

@ -95,7 +95,7 @@ public class Matrix {
} }
} }
public Matrix clone() { public int[][] 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 new Matrix(clone); return clone;
} }
public void randomAdjazenzMatrix() { public void randomAdjazenzMatrix() {