From 9baabad4d498d26830f24b70435a924715cbe747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Fri, 17 Mar 2023 15:31:57 +0100 Subject: [PATCH 01/16] Completed reading in the CSV (#1) * Created Matrix class Created Matrix class with an empty main method * Created Matrix class Created Matrix class with an empty main method * Created importCSV method Created the empty method importCSV * Completed importCSV method Added BufferedReader with FileReader surrounded by a try/catch * Added file parameter Added it so I don't have a hardcoded file location * Removed line variable It just wasn't needed * Created TestMatrix class For now only has one test for the importCSV method * Created empty printCSV method gonna use it to print out the array which contains the array * ReAdded line variable It was needed after all * Made result a global variable So it can be used by other methods * Array now initialized with correct size I still don't know how to put the csv into the array tough * done don't ask how --- src/Matrix.java | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/TestMatrix.java | 11 ++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/Matrix.java create mode 100644 src/TestMatrix.java diff --git a/src/Matrix.java b/src/Matrix.java new file mode 100644 index 0000000..ed3a668 --- /dev/null +++ b/src/Matrix.java @@ -0,0 +1,49 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.FileReader; +import java.io.FileNotFoundException; + +public class Matrix { + private int[][] matrix; + + public static void main(String[] args) {} + + public Matrix(String file) { + matrix = this.readCSV(file); + } + + public int[][] readCSV(String file){ + int[][] intMatrix = null; + try (BufferedReader br = new BufferedReader(new FileReader(file))) { + String line = br.readLine(); + int rowCount = line.trim().split(";").length; + int columnCount = rowCount; + String[] lineArray = null; + + intMatrix = new int[rowCount][columnCount]; + + for(int columnIndex = 0; line != null && columnIndex < intMatrix.length; columnIndex++, line = br.readLine()) { + lineArray = line.trim().split(";"); + for(int rowIndex=0; rowIndex < intMatrix[columnIndex].length; rowIndex++) { + intMatrix[columnIndex][rowIndex] = Integer.parseInt(lineArray[rowIndex]); + } + } + br.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return intMatrix; + } + + public void print() { + for(int columnIndex=0; columnIndex < matrix.length; columnIndex++) { + for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) { + System.out.print(matrix[columnIndex][rowIndex]); + } + System.out.println(); + } + } + +} diff --git a/src/TestMatrix.java b/src/TestMatrix.java new file mode 100644 index 0000000..7db94b0 --- /dev/null +++ b/src/TestMatrix.java @@ -0,0 +1,11 @@ +public class TestMatrix { + public static void main(String[] args) { + test1(""); + } + + public static void test1(String file) { + Matrix matrix = new Matrix(file); + + matrix.print(); + } +} -- 2.47.1 From 27e79eb1f6a107e4a9e5dbf8654ccc747bf90e4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Fri, 17 Mar 2023 15:37:26 +0100 Subject: [PATCH 02/16] Done, don't ask how (#2) --- src/Matrix.java | 64 +++++++++++++++++++++++++++++++++++++-------- src/TestMatrix.java | 6 +++++ 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/Matrix.java b/src/Matrix.java index ed3a668..3ad92a3 100644 --- a/src/Matrix.java +++ b/src/Matrix.java @@ -8,10 +8,61 @@ public class Matrix { public static void main(String[] args) {} + public Matrix(int rowLength, int columnLength) { + matrix = new int[rowLength][columnLength]; + } + public Matrix(String file) { matrix = this.readCSV(file); } + public Matrix multiply( Matrix m) { + Matrix scalarProduct = null; + + if(this.columnLength() != m.rowLength()) { + return scalarProduct; + } + + scalarProduct = new Matrix(this.rowLength(), m.columnLength()); + + for(int columnIndex = 0; columnIndex < this.columnLength(); columnIndex++) { + for(int rowIndex = 0; rowIndex < m.rowLength(); rowIndex++) { + int sum = 0; + for(int k=0; k < this.rowLength(); k++) { + sum += this.getValue(columnIndex, k) * m.getValue(k, rowIndex); + } + scalarProduct.insert(columnIndex, rowIndex, sum); + } + } + + return scalarProduct; + } + + public int rowLength() { + return matrix.length; + } + + public int columnLength() { + return matrix[0].length; + } + + public int getValue(int columnIndex, int rowIndex) { + return matrix[columnIndex][rowIndex]; + } + + public void insert(int columnIndex, int rowIndex, int value) { + matrix[rowIndex][columnIndex] = value; + } + + public void print() { + for(int columnIndex=0; columnIndex < matrix.length; columnIndex++) { + for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) { + System.out.print(matrix[columnIndex][rowIndex]); + } + System.out.println(); + } + } + public int[][] readCSV(String file){ int[][] intMatrix = null; try (BufferedReader br = new BufferedReader(new FileReader(file))) { @@ -19,7 +70,7 @@ public class Matrix { int rowCount = line.trim().split(";").length; int columnCount = rowCount; String[] lineArray = null; - + intMatrix = new int[rowCount][columnCount]; for(int columnIndex = 0; line != null && columnIndex < intMatrix.length; columnIndex++, line = br.readLine()) { @@ -37,13 +88,4 @@ public class Matrix { return intMatrix; } - public void print() { - for(int columnIndex=0; columnIndex < matrix.length; columnIndex++) { - for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) { - System.out.print(matrix[columnIndex][rowIndex]); - } - System.out.println(); - } - } - -} +} \ No newline at end of file diff --git a/src/TestMatrix.java b/src/TestMatrix.java index 7db94b0..b7679b4 100644 --- a/src/TestMatrix.java +++ b/src/TestMatrix.java @@ -5,7 +5,13 @@ public class TestMatrix { public static void test1(String file) { Matrix matrix = new Matrix(file); + Matrix scalarProduct; matrix.print(); + System.out.println(); + + scalarProduct = matrix.multiply( matrix); + scalarProduct.print(); + } } -- 2.47.1 From f2202f7ed90e323061f1982f78331dbc71ce46fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Thu, 20 Apr 2023 22:25:48 +0200 Subject: [PATCH 03/16] progress --- src/Graph.java | 120 ++++++++++++++++++++++++++++++++++++++++++++ src/Matrix.java | 86 +++++++++++++++---------------- src/TestGraph.java | 12 +++++ src/TestMatrix.java | 13 ++--- 4 files changed, 183 insertions(+), 48 deletions(-) create mode 100644 src/Graph.java create mode 100644 src/TestGraph.java diff --git a/src/Graph.java b/src/Graph.java new file mode 100644 index 0000000..6a03428 --- /dev/null +++ b/src/Graph.java @@ -0,0 +1,120 @@ +import java.util.Arrays; +import java.util.ArrayList; + +public class Graph { + private Matrix adjazenzMatrix; + private Matrix distanzMatrix; + private Matrix wegMatrix; + private int[] exzentrizitäten; + private int radius; + private int durchmesser; + private ArrayList zentrum; + + public static void main(String[] args) {} + + public Graph(String file) { + adjazenzMatrix = new Matrix(file); + calculateDistanzMatrix(adjazenzMatrix); + calculateWegMatrix(adjazenzMatrix); + calculateExzentrizitäten(); + calculateProperties(); + } + + public void calculateDistanzMatrix(Matrix matrix) { + distanzMatrix = new Matrix(matrix.getRowLength(), matrix.getColumnLength()); + Matrix potenzMatrix = adjazenzMatrix; + + for(int columnIndex=0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex=0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { + if(matrix.getValueAt(rowIndex, columnIndex) == 1) { + distanzMatrix.insert(rowIndex, columnIndex, 1); + } else if(columnIndex == rowIndex) { + distanzMatrix.insert(rowIndex, columnIndex, 0); + } else { + distanzMatrix.insert(rowIndex, columnIndex, -1); + } + } + } + + for(int k = 2; k < distanzMatrix.getRowLength(); k++) { + potenzMatrix = potenzMatrix.multiply(adjazenzMatrix); + + for(int columnIndex=0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex=0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { + if(potenzMatrix.getValueAt(rowIndex, columnIndex) != 0 && distanzMatrix.getValueAt(rowIndex, columnIndex) == -1) { + distanzMatrix.insert(rowIndex, columnIndex, k); + } + } + } + } + } + + public void calculateWegMatrix(Matrix matrix) { + wegMatrix = new Matrix(matrix.getRowLength(), matrix.getColumnLength()); + Matrix potenzMatrix = matrix; + + for(int columnIndex=0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex=0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + if(columnIndex == rowIndex) { + wegMatrix.insert(rowIndex, columnIndex, 1); + } else if(matrix.getValueAt(rowIndex, columnIndex) > 0 && wegMatrix.getValueAt(rowIndex, columnIndex) == 0) { + wegMatrix.insert(rowIndex, columnIndex, 1); + } + } + } + + for(int k = 2; k < wegMatrix.getRowLength(); k++) { + potenzMatrix = potenzMatrix.multiply(matrix); + + for(int columnIndex=0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex=0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + if(potenzMatrix.getValueAt(rowIndex, columnIndex) != 0) { + wegMatrix.insert(rowIndex, columnIndex, 1); + } + } + } + } + } + + public void calculateExzentrizitäten() { + exzentrizitäten = new int[distanzMatrix.getRowLength()]; + for(int rowIndex = 0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { + int exzentrizität = -1; + + for(int columnIndex = 0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { + if(distanzMatrix.getValueAt(columnIndex, rowIndex) > exzentrizität) { + exzentrizität = distanzMatrix.getValueAt(columnIndex, rowIndex); + } + } + exzentrizitäten[rowIndex] = exzentrizität; + } + } + + public void calculateProperties() { + Arrays.sort(exzentrizitäten); + radius = exzentrizitäten[0]; + durchmesser = exzentrizitäten[exzentrizitäten.length - 1]; + zentrum = new ArrayList<>(); + + for(int eRowIndex=exzentrizitäten.length - 1, index = 0; eRowIndex > 0 && index < exzentrizitäten.length; eRowIndex--, index++) { + if(exzentrizitäten[eRowIndex] == durchmesser) { + zentrum.add(index, durchmesser); + } + } + zentrum.toArray(); + } + + public String toString() { + String s = ""; + + s += "Adjazenzmatrix: \n" + adjazenzMatrix + "\nDistanzmatrix: \n" + distanzMatrix + "\nWegmatrix: \n" + wegMatrix + "\nExzentrizitäten: \n"; + + for(int rowIndex=0; rowIndex < exzentrizitäten.length; rowIndex++) { + s += exzentrizitäten[rowIndex] + " "; + } + + s += "\nRadius: " + radius + "\nDurchmesser: " + durchmesser + "\nZentrum: " + zentrum; + + return s; + } +} \ No newline at end of file diff --git a/src/Matrix.java b/src/Matrix.java index 3ad92a3..a8284c3 100644 --- a/src/Matrix.java +++ b/src/Matrix.java @@ -5,78 +5,71 @@ import java.io.FileNotFoundException; public class Matrix { private int[][] matrix; + private int rowLength; + private int columnLength; public static void main(String[] args) {} + public Matrix(String file) { + readCSV(file); + } + public Matrix(int rowLength, int columnLength) { matrix = new int[rowLength][columnLength]; + this.rowLength = rowLength; + this.columnLength = columnLength; } - public Matrix(String file) { - matrix = this.readCSV(file); + public Matrix(int[][] matrix) { + this.matrix = matrix; + rowLength = matrix.length; + columnLength = rowLength; } - public Matrix multiply( Matrix m) { - Matrix scalarProduct = null; - - if(this.columnLength() != m.rowLength()) { - return scalarProduct; - } + public Matrix multiply(Matrix m) { + Matrix scalarProduct = new Matrix(new int[rowLength][m.columnLength]); - scalarProduct = new Matrix(this.rowLength(), m.columnLength()); - - for(int columnIndex = 0; columnIndex < this.columnLength(); columnIndex++) { - for(int rowIndex = 0; rowIndex < m.rowLength(); rowIndex++) { + for(int columnIndex = 0; columnIndex < this.getColumnLength(); columnIndex++) { + for(int rowIndex = 0; rowIndex < m.getRowLength(); rowIndex++) { int sum = 0; - for(int k=0; k < this.rowLength(); k++) { - sum += this.getValue(columnIndex, k) * m.getValue(k, rowIndex); + for(int k=0; k < this.getRowLength(); k++) { + sum += this.getValueAt(rowIndex, k) * m.getValueAt(k, columnIndex); } - scalarProduct.insert(columnIndex, rowIndex, sum); + scalarProduct.insert(rowIndex, columnIndex, sum); } } - return scalarProduct; } - public int rowLength() { - return matrix.length; + public int getRowLength() { + return rowLength; } - public int columnLength() { - return matrix[0].length; + public int getColumnLength() { + return columnLength; } - public int getValue(int columnIndex, int rowIndex) { - return matrix[columnIndex][rowIndex]; + public int getValueAt(int rowIndex, int columnIndex) { + return matrix[rowIndex][columnIndex]; } - public void insert(int columnIndex, int rowIndex, int value) { + public void insert(int rowIndex, int columnIndex, int value) { matrix[rowIndex][columnIndex] = value; } - public void print() { - for(int columnIndex=0; columnIndex < matrix.length; columnIndex++) { - for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) { - System.out.print(matrix[columnIndex][rowIndex]); - } - System.out.println(); - } - } - - public int[][] readCSV(String file){ - int[][] intMatrix = null; + public void readCSV(String file){ try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line = br.readLine(); - int rowCount = line.trim().split(";").length; - int columnCount = rowCount; + rowLength = line.trim().split(";").length; + columnLength = rowLength; String[] lineArray = null; - intMatrix = new int[rowCount][columnCount]; + matrix = new int[rowLength][columnLength]; - for(int columnIndex = 0; line != null && columnIndex < intMatrix.length; columnIndex++, line = br.readLine()) { - lineArray = line.trim().split(";"); - for(int rowIndex=0; rowIndex < intMatrix[columnIndex].length; rowIndex++) { - intMatrix[columnIndex][rowIndex] = Integer.parseInt(lineArray[rowIndex]); + for(int columnIndex = 0; line != null && columnIndex < matrix.length; columnIndex++, line = br.readLine()) { + lineArray = line.split(";"); + for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) { + matrix[rowIndex][columnIndex] = Integer.parseInt(lineArray[rowIndex]); } } br.close(); @@ -85,7 +78,16 @@ public class Matrix { } catch (IOException e) { e.printStackTrace(); } - return intMatrix; } + public String toString() { + String s = ""; + for(int columnIndex=0; columnIndex < columnLength; columnIndex++) { + for(int rowIndex=0; rowIndex < rowLength; rowIndex++) { + s += matrix[rowIndex][columnIndex] + " "; + } + s += "\n"; + } + return s; + } } \ No newline at end of file diff --git a/src/TestGraph.java b/src/TestGraph.java new file mode 100644 index 0000000..06f5abb --- /dev/null +++ b/src/TestGraph.java @@ -0,0 +1,12 @@ +public class TestGraph { + public static void main(String[] args) { + test1("/home/old/projects/Java/graphprogram/24n_01.csv"); + } + + public static void test1(String file) { + Graph g = new Graph(file); + g.calculateExzentrizitäten(); + g.calculateProperties(); + System.out.println(g); + } +} \ No newline at end of file diff --git a/src/TestMatrix.java b/src/TestMatrix.java index b7679b4..e9807c1 100644 --- a/src/TestMatrix.java +++ b/src/TestMatrix.java @@ -1,17 +1,18 @@ public class TestMatrix { public static void main(String[] args) { - test1(""); + test1("/home/old/projects/Java/graphprogram/24n_01.csv"); } public static void test1(String file) { Matrix matrix = new Matrix(file); Matrix scalarProduct; - matrix.print(); - System.out.println(); - - scalarProduct = matrix.multiply( matrix); - scalarProduct.print(); + System.out.println(matrix.getRowLength()); + System.out.println(matrix.getColumnLength()); + + System.out.println(matrix); + scalarProduct = matrix.multiply(matrix); + System.out.println(scalarProduct); } } -- 2.47.1 From acedb7ddb8d853af8901fe05cf53b76d59908264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Tue, 25 Apr 2023 13:39:10 +0200 Subject: [PATCH 04/16] about 50% done --- src/Graph.java | 41 ++++++++++++++++++++++++----------------- src/TestGraph.java | 6 +----- src/TestMatrix.java | 6 +----- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index 6a03428..7c4ea4e 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -1,5 +1,4 @@ import java.util.Arrays; -import java.util.ArrayList; public class Graph { private Matrix adjazenzMatrix; @@ -8,7 +7,7 @@ public class Graph { private int[] exzentrizitäten; private int radius; private int durchmesser; - private ArrayList zentrum; + private int[] zentrum; public static void main(String[] args) {} @@ -91,29 +90,37 @@ public class Graph { } public void calculateProperties() { - Arrays.sort(exzentrizitäten); - radius = exzentrizitäten[0]; - durchmesser = exzentrizitäten[exzentrizitäten.length - 1]; - zentrum = new ArrayList<>(); + radius = Integer.MAX_VALUE; + durchmesser = 0; + int sum = 1; - for(int eRowIndex=exzentrizitäten.length - 1, index = 0; eRowIndex > 0 && index < exzentrizitäten.length; eRowIndex--, index++) { - if(exzentrizitäten[eRowIndex] == durchmesser) { - zentrum.add(index, durchmesser); + for(int rowIndex = 0; rowIndex < exzentrizitäten.length; rowIndex++) { + if(exzentrizitäten[rowIndex] < radius) { + radius = exzentrizitäten[rowIndex]; + } + if(exzentrizitäten[rowIndex] == durchmesser) { + sum++; + } + if(exzentrizitäten[rowIndex] > durchmesser) { + durchmesser = exzentrizitäten[rowIndex]; + sum = 1; + } + } + zentrum = new int[sum]; + + for(int rowIndex = 0, index = 0; rowIndex < exzentrizitäten.length; rowIndex++) { + if(exzentrizitäten[rowIndex] == durchmesser) { + zentrum[index] = rowIndex + 1; + index++; } } - zentrum.toArray(); } public String toString() { String s = ""; - s += "Adjazenzmatrix: \n" + adjazenzMatrix + "\nDistanzmatrix: \n" + distanzMatrix + "\nWegmatrix: \n" + wegMatrix + "\nExzentrizitäten: \n"; - - for(int rowIndex=0; rowIndex < exzentrizitäten.length; rowIndex++) { - s += exzentrizitäten[rowIndex] + " "; - } - - s += "\nRadius: " + radius + "\nDurchmesser: " + durchmesser + "\nZentrum: " + zentrum; + s += "Adjazenzmatrix:\n" + adjazenzMatrix + "\nDistanzmatrix:\n" + distanzMatrix + "\nWegmatrix:\n" + wegMatrix; + s += "\nExzentrizitäten: " + Arrays.toString(exzentrizitäten) + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser + "\nZentrum: " + Arrays.toString(zentrum); return s; } diff --git a/src/TestGraph.java b/src/TestGraph.java index 06f5abb..6cb180d 100644 --- a/src/TestGraph.java +++ b/src/TestGraph.java @@ -1,10 +1,6 @@ public class TestGraph { public static void main(String[] args) { - test1("/home/old/projects/Java/graphprogram/24n_01.csv"); - } - - public static void test1(String file) { - Graph g = new Graph(file); + Graph g = new Graph("/home/old/projects/Java/graphprogram/graph.csv"); g.calculateExzentrizitäten(); g.calculateProperties(); System.out.println(g); diff --git a/src/TestMatrix.java b/src/TestMatrix.java index e9807c1..faff181 100644 --- a/src/TestMatrix.java +++ b/src/TestMatrix.java @@ -1,10 +1,6 @@ public class TestMatrix { public static void main(String[] args) { - test1("/home/old/projects/Java/graphprogram/24n_01.csv"); - } - - public static void test1(String file) { - Matrix matrix = new Matrix(file); + Matrix matrix = new Matrix("/home/old/projects/Java/graphprogram/24n_01.csv"); Matrix scalarProduct; System.out.println(matrix.getRowLength()); -- 2.47.1 From 295f6f688b0bcbad0e57d600b1aad74b41037d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Fri, 28 Apr 2023 23:54:45 +0200 Subject: [PATCH 05/16] In the middle of something --- src/Graph.java | 170 ++++++++++++++++++++++++++++++++++++++++----- src/TestGraph.java | 4 +- 2 files changed, 155 insertions(+), 19 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index 7c4ea4e..5ce9e67 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -4,28 +4,37 @@ public class Graph { private Matrix adjazenzMatrix; private Matrix distanzMatrix; private Matrix wegMatrix; + private boolean zusammenhaengend; private int[] exzentrizitäten; private int radius; private int durchmesser; private int[] zentrum; + private int[][] components; + private int[][] bridges; + private int[] articulations; + public static void main(String[] args) {} public Graph(String file) { + zusammenhaengend = true; adjazenzMatrix = new Matrix(file); - calculateDistanzMatrix(adjazenzMatrix); - calculateWegMatrix(adjazenzMatrix); + calculateDistanzMatrix(); + calculateWegMatrix(); calculateExzentrizitäten(); calculateProperties(); + findComponents(); + findComponents(); + findArticulations(); } - public void calculateDistanzMatrix(Matrix matrix) { - distanzMatrix = new Matrix(matrix.getRowLength(), matrix.getColumnLength()); + public void calculateDistanzMatrix() { + distanzMatrix = new Matrix(adjazenzMatrix.getRowLength(), adjazenzMatrix.getColumnLength()); Matrix potenzMatrix = adjazenzMatrix; for(int columnIndex=0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { for(int rowIndex=0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { - if(matrix.getValueAt(rowIndex, columnIndex) == 1) { + if(adjazenzMatrix.getValueAt(rowIndex, columnIndex) == 1) { distanzMatrix.insert(rowIndex, columnIndex, 1); } else if(columnIndex == rowIndex) { distanzMatrix.insert(rowIndex, columnIndex, 0); @@ -48,22 +57,22 @@ public class Graph { } } - public void calculateWegMatrix(Matrix matrix) { - wegMatrix = new Matrix(matrix.getRowLength(), matrix.getColumnLength()); - Matrix potenzMatrix = matrix; + public void calculateWegMatrix() { + wegMatrix = new Matrix(adjazenzMatrix.getRowLength(), adjazenzMatrix.getColumnLength()); + Matrix potenzMatrix = adjazenzMatrix; for(int columnIndex=0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { for(int rowIndex=0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { if(columnIndex == rowIndex) { 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); } } } 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 rowIndex=0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { @@ -78,10 +87,10 @@ public class Graph { public void calculateExzentrizitäten() { exzentrizitäten = new int[distanzMatrix.getRowLength()]; for(int rowIndex = 0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { - int exzentrizität = -1; + int exzentrizität = 0; for(int columnIndex = 0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { - if(distanzMatrix.getValueAt(columnIndex, rowIndex) > exzentrizität) { + if(distanzMatrix.getValueAt(columnIndex, rowIndex) > exzentrizität && rowIndex != columnIndex) { exzentrizität = distanzMatrix.getValueAt(columnIndex, rowIndex); } } @@ -91,8 +100,8 @@ public class Graph { public void calculateProperties() { radius = Integer.MAX_VALUE; - durchmesser = 0; - int sum = 1; + durchmesser = -1; + int sum = 0; for(int rowIndex = 0; rowIndex < exzentrizitäten.length; rowIndex++) { if(exzentrizitäten[rowIndex] < radius) { @@ -106,8 +115,13 @@ public class Graph { sum = 1; } } - zentrum = new int[sum]; + if(durchmesser == 0 && exzentrizitäten.length > 1) { + zusammenhaengend = false; + return; + } + zentrum = new int[sum]; + for(int rowIndex = 0, index = 0; rowIndex < exzentrizitäten.length; rowIndex++) { if(exzentrizitäten[rowIndex] == durchmesser) { zentrum[index] = rowIndex + 1; @@ -116,11 +130,135 @@ public class Graph { } } + public void findComponents() { + components = new int[wegMatrix.getRowLength()][wegMatrix.getColumnLength()]; + int[] component = new int[wegMatrix.getRowLength()]; + + for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + for(int columnIndex = 0, index = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) { + component[index] = columnIndex + 1; + index++; + } + } + components[rowIndex] = component; + } + if(!zusammenhaengend) { + return; + } + + for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { + for(int index = 1; index < components.length; index++) { + if(components[index] != null && components[rowIndex].equals(components[index])) { + components[index] = null; + } + } + } + } + + public int[] findComponents(Matrix matrix) { + int[][] components = new int[][]; + int[] component = new int[matrix.getRowLength()]; + + for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { + for(int columnIndex = 0, index = 0; columnIndex < matrix.getColumnLength(); columnIndex++) { + if(matrix.getValueAt(rowIndex, columnIndex) == 1) { + component[index] = columnIndex + 1; + index++; + } + } + components[rowIndex] = component; + } + if(!zusammenhaengend) { + return null; + } + + for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { + for(int index = 1; index < components.length; index++) { + if(components[index] != null && components[rowIndex].equals(components[index])) { + components[index] = null; + } + } + } + } + + public void findBridges() { + bridges = new int[wegMatrix.getRowLength()][2]; + 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(rowIndex != columnIndex) { + matrix.insert(rowIndex, columnIndex, 0); + } + } + } + + if(components.length == newComponents.length) { + return; + } + } + + public void findArticulations() { + articulations = new int[wegMatrix.getRowLength()]; + 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; + } + } + public String toString() { String s = ""; s += "Adjazenzmatrix:\n" + adjazenzMatrix + "\nDistanzmatrix:\n" + distanzMatrix + "\nWegmatrix:\n" + wegMatrix; - s += "\nExzentrizitäten: " + Arrays.toString(exzentrizitäten) + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser + "\nZentrum: " + Arrays.toString(zentrum); + + if(!zusammenhaengend) { + s += "\nExzentrizitäten/Radius/Durchmesser: Kein zusammenhängender Graph"; + } else { + s += "\nExzentrizitäten: " + Arrays.toString(exzentrizitäten) + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser; + } + + s += "\nZentrum: "; + if(zusammenhaengend) { + s += Arrays.toString(zentrum); + } else { + s += "Kein zusammenhängender Graph"; + } + + s += "\nKomponente: {"; + for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { + if(components[rowIndex] != null) { + s += Arrays.toString(components[rowIndex]); + if(rowIndex < components.length - 1) { + if(components[rowIndex + 1] != null) { + s += ","; + } + } + } + } + s += "}"; + + s += "\nBrücken: {"; + for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { + if(components[rowIndex] != null) { + s += Arrays.toString(components[rowIndex]); + if(rowIndex < components.length - 1) { + if(components[rowIndex + 1] != null) { + s += ","; + } + } + } + } + s += "}"; return s; } diff --git a/src/TestGraph.java b/src/TestGraph.java index 6cb180d..68f8017 100644 --- a/src/TestGraph.java +++ b/src/TestGraph.java @@ -1,8 +1,6 @@ public class TestGraph { public static void main(String[] args) { - Graph g = new Graph("/home/old/projects/Java/graphprogram/graph.csv"); - g.calculateExzentrizitäten(); - g.calculateProperties(); + Graph g = new Graph("/home/old/projects/Java/graphprogram/24n_01.csv"); System.out.println(g); } } \ No newline at end of file -- 2.47.1 From 6dff42fe3cfab7694640380ec9d315cccb51b568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Sun, 14 May 2023 00:16:01 +0200 Subject: [PATCH 06/16] finished implementing findBridges method Still very much stuck at figuring out how to find all arcticualtions in a graph --- src/Graph.java | 118 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 36 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index 5ce9e67..3c1417a 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -24,7 +24,7 @@ public class Graph { calculateExzentrizitäten(); calculateProperties(); findComponents(); - findComponents(); + findBridges(); findArticulations(); } @@ -143,9 +143,6 @@ public class Graph { } components[rowIndex] = component; } - if(!zusammenhaengend) { - return; - } for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { for(int index = 1; index < components.length; index++) { @@ -156,8 +153,8 @@ public class Graph { } } - public int[] findComponents(Matrix matrix) { - int[][] components = new int[][]; + public int[][] findComponents(Matrix matrix) { + int[][] tempComponents = new int[matrix.getRowLength()][matrix.getColumnLength()]; int[] component = new int[matrix.getRowLength()]; for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { @@ -167,53 +164,94 @@ public class Graph { index++; } } - components[rowIndex] = component; + tempComponents[rowIndex] = component; } if(!zusammenhaengend) { return null; } - for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { - for(int index = 1; index < components.length; index++) { - if(components[index] != null && components[rowIndex].equals(components[index])) { - components[index] = null; + for(int rowIndex = 0; rowIndex < tempComponents.length; rowIndex++) { + for(int index = 1; index < tempComponents.length; index++) { + if(tempComponents[index] != null && tempComponents[rowIndex].equals(tempComponents[index])) { + tempComponents[index] = null; } } } + return tempComponents; } public void findBridges() { + if(!zusammenhaengend) { + return; + } bridges = new int[wegMatrix.getRowLength()][2]; - Matrix matrix = wegMatrix; - int[][] newComponents = new int[wegMatrix.getRowLength()][wegMatrix.getColumnLength()]; + int[][] newComponents; + int[] bridge = new int[2]; - for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { - for(int columnIndex = 0; columnIndex < matrix.getColumnLength(); columnIndex++) { - if(rowIndex != columnIndex) { - matrix.insert(rowIndex, columnIndex, 0); + for(int i = 0, columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + if(rowIndex != 1) { + 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) { - return; + for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) { + for(int index = 1; index < bridges.length; index++) { + if(bridges[index] != null && bridges[rowIndex].equals(bridges[index])) { + bridges[index] = null; + } + } } } public void findArticulations() { - articulations = new int[wegMatrix.getRowLength()]; - 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) { + if(!zusammenhaengend) { 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() { @@ -248,18 +286,26 @@ public class Graph { s += "}"; s += "\nBrücken: {"; - for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { - if(components[rowIndex] != null) { - s += Arrays.toString(components[rowIndex]); - if(rowIndex < components.length - 1) { - if(components[rowIndex + 1] != null) { - s += ","; + if(bridges != null) { + for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) { + if(bridges[rowIndex] != null) { + s += Arrays.toString(bridges[rowIndex]); + if(rowIndex < bridges.length - 1) { + if(bridges[rowIndex + 1] != null) { + s += ","; + } } } } } s += "}"; + s += "\nArtikulationen: {"; + if(articulations[0] != 0) { + s += Arrays.toString(articulations); + } + s += "}"; + return s; } } \ No newline at end of file -- 2.47.1 From a66c6af575954b32ae2be066c1e245f25e8a18c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Mon, 15 May 2023 15:29:53 +0200 Subject: [PATCH 07/16] unimportant changes --- src/Matrix.java | 5 ++--- src/TestMatrix.java | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Matrix.java b/src/Matrix.java index a8284c3..c62b760 100644 --- a/src/Matrix.java +++ b/src/Matrix.java @@ -23,7 +23,7 @@ public class Matrix { public Matrix(int[][] matrix) { this.matrix = matrix; rowLength = matrix.length; - columnLength = rowLength; + columnLength = matrix[0].length; } public Matrix multiply(Matrix m) { @@ -66,13 +66,12 @@ public class Matrix { matrix = new int[rowLength][columnLength]; - for(int columnIndex = 0; line != null && columnIndex < matrix.length; columnIndex++, line = br.readLine()) { + for(int columnIndex = 0; line != null; columnIndex++, line = br.readLine()) { lineArray = line.split(";"); for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) { matrix[rowIndex][columnIndex] = Integer.parseInt(lineArray[rowIndex]); } } - br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { diff --git a/src/TestMatrix.java b/src/TestMatrix.java index faff181..4e14b85 100644 --- a/src/TestMatrix.java +++ b/src/TestMatrix.java @@ -3,12 +3,12 @@ public class TestMatrix { Matrix matrix = new Matrix("/home/old/projects/Java/graphprogram/24n_01.csv"); Matrix scalarProduct; - System.out.println(matrix.getRowLength()); - System.out.println(matrix.getColumnLength()); + System.out.println("RowLength: " + matrix.getRowLength()); + System.out.println("ColumnLength: " +matrix.getColumnLength()); - System.out.println(matrix); + System.out.println("\nMatrix A: \n" + matrix); scalarProduct = matrix.multiply(matrix); - System.out.println(scalarProduct); + System.out.println("\nScalarProduct A²: \n" + scalarProduct); } } -- 2.47.1 From 62206a962b66447446b034f52d78abb4dc79e5c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Mon, 15 May 2023 17:25:15 +0200 Subject: [PATCH 08/16] switched to ArrayLists The methods findComponents, findBridges and findArticulations are broken. The main problem lies with findComponents. --- src/Graph.java | 104 ++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 54 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index 3c1417a..792690e 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -1,3 +1,4 @@ +import java.util.ArrayList; import java.util.Arrays; public class Graph { @@ -24,8 +25,8 @@ public class Graph { calculateExzentrizitäten(); calculateProperties(); findComponents(); - findBridges(); - findArticulations(); + //findBridges(); + //findArticulations(); } public void calculateDistanzMatrix() { @@ -131,7 +132,7 @@ public class Graph { } public void findComponents() { - components = new int[wegMatrix.getRowLength()][wegMatrix.getColumnLength()]; + ArrayList tempComponents = new ArrayList<>(1); int[] component = new int[wegMatrix.getRowLength()]; for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { @@ -141,81 +142,77 @@ public class Graph { index++; } } - components[rowIndex] = component; - } - - for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { - for(int index = 1; index < components.length; index++) { - if(components[index] != null && components[rowIndex].equals(components[index])) { - components[index] = null; - } + if(tempComponents.contains(component)) { + continue; } + tempComponents.add(component); + } + + 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) { - int[][] tempComponents = new int[matrix.getRowLength()][matrix.getColumnLength()]; - int[] component = new int[matrix.getRowLength()]; + int[][] newComponents; + ArrayList tempComponents = new ArrayList<>(1); + int[] component = new int[wegMatrix.getRowLength()]; - for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { - for(int columnIndex = 0, index = 0; columnIndex < matrix.getColumnLength(); columnIndex++) { - if(matrix.getValueAt(rowIndex, columnIndex) == 1) { + for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + for(int columnIndex = 0, index = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) { component[index] = columnIndex + 1; index++; } } - tempComponents[rowIndex] = component; - } - if(!zusammenhaengend) { - return null; - } - - for(int rowIndex = 0; rowIndex < tempComponents.length; rowIndex++) { - for(int index = 1; index < tempComponents.length; index++) { - if(tempComponents[index] != null && tempComponents[rowIndex].equals(tempComponents[index])) { - tempComponents[index] = null; - } + if(tempComponents.contains(component)) { + continue; } + tempComponents.add(component); } - return tempComponents; + + newComponents = new int[tempComponents.size()][wegMatrix.getColumnLength()]; + + for(int rowIndex = 0; rowIndex < newComponents.length; rowIndex++) { + newComponents[rowIndex] = tempComponents.get(rowIndex); + } + return newComponents; } public void findBridges() { if(!zusammenhaengend) { return; } - bridges = new int[wegMatrix.getRowLength()][2]; + ArrayList tempBridges = new ArrayList<>(10); int[][] newComponents; - int[] bridge = new int[2]; + int[] bridge; - for(int i = 0, columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { - if(rowIndex != 1) { - continue; - } wegMatrix.insert(rowIndex, columnIndex, 0); wegMatrix.insert(columnIndex, rowIndex, 0); + bridge = new int[2]; bridge[0] = columnIndex + 1; bridge[1] = rowIndex + 1; newComponents = findComponents(wegMatrix); - if(!components.equals(newComponents)) { - bridges[i] = bridge; - i++; + System.out.println(Arrays.toString(bridge)); + if(!components.equals(newComponents) && !tempBridges.contains(bridge)) { + System.out.println("bruh"); + tempBridges.add(bridge); } - + bridge = null; wegMatrix.insert(rowIndex, columnIndex, 1); wegMatrix.insert(columnIndex, rowIndex, 1); } } - + bridges = new int[tempBridges.size()][2]; + for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) { - for(int index = 1; index < bridges.length; index++) { - if(bridges[index] != null && bridges[rowIndex].equals(bridges[index])) { - bridges[index] = null; - } - } + bridges[rowIndex] = tempBridges.get(rowIndex); } } @@ -223,10 +220,10 @@ public class Graph { if(!zusammenhaengend) { return; } - articulations = new int[wegMatrix.getRowLength()]; + ArrayList tempArticulations = new ArrayList<>(10); int[][] newComponents; - for(int i = 0, j = 0; i < wegMatrix.getRowLength(); i++) { + for(int i = 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); @@ -241,8 +238,7 @@ public class Graph { newComponents = findComponents(wegMatrix); if(!components.equals(newComponents)) { - articulations[j] = i + 1; - j++; + tempArticulations.add(i + 1); } for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { @@ -252,6 +248,11 @@ public class Graph { } } } + articulations = new int[tempArticulations.size()]; + + for(int rowIndex = 0; rowIndex < articulations.length; rowIndex++) { + articulations[rowIndex] = tempArticulations.get(rowIndex); + } } public String toString() { @@ -300,12 +301,7 @@ public class Graph { } s += "}"; - s += "\nArtikulationen: {"; - if(articulations[0] != 0) { - s += Arrays.toString(articulations); - } - s += "}"; - + s += "\nArtikulationen: " + Arrays.toString(articulations); return s; } } \ No newline at end of file -- 2.47.1 From d690dd69d85ea32f94538c6d843fbcab21b29688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Mon, 15 May 2023 17:30:13 +0200 Subject: [PATCH 09/16] removed file paths --- src/TestGraph.java | 2 +- src/TestMatrix.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TestGraph.java b/src/TestGraph.java index 68f8017..0fa9a36 100644 --- a/src/TestGraph.java +++ b/src/TestGraph.java @@ -1,6 +1,6 @@ public class TestGraph { public static void main(String[] args) { - Graph g = new Graph("/home/old/projects/Java/graphprogram/24n_01.csv"); + Graph g = new Graph(""); System.out.println(g); } } \ No newline at end of file diff --git a/src/TestMatrix.java b/src/TestMatrix.java index 4e14b85..b8a244d 100644 --- a/src/TestMatrix.java +++ b/src/TestMatrix.java @@ -1,6 +1,6 @@ public class TestMatrix { public static void main(String[] args) { - Matrix matrix = new Matrix("/home/old/projects/Java/graphprogram/24n_01.csv"); + Matrix matrix = new Matrix(""); Matrix scalarProduct; System.out.println("RowLength: " + matrix.getRowLength()); -- 2.47.1 From e1aa04e33cf4d2d2cc39fc11888798c7a0e7a299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Tue, 16 May 2023 13:17:01 +0200 Subject: [PATCH 10/16] findComponents still broken I only have to figure out how to get rid of duplicate components --- src/Graph.java | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index 792690e..7de208a 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -133,21 +133,30 @@ public class Graph { public void findComponents() { ArrayList tempComponents = new ArrayList<>(1); - int[] component = new int[wegMatrix.getRowLength()]; + ArrayList tempComponent = new ArrayList<>(1); + int[] component; for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { - for(int columnIndex = 0, index = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) { - component[index] = columnIndex + 1; - index++; + tempComponent.add(columnIndex + 1); } } + component = new int[tempComponent.size()]; + + for(int index = 0; index < component.length; index++) { + component[index] = tempComponent.get(index); + } + if(tempComponents.contains(component)) { continue; } - tempComponents.add(component); - } + tempComponents.add(component); + + component = null; + tempComponent.clear(); + } components = new int[tempComponents.size()][wegMatrix.getColumnLength()]; for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { -- 2.47.1 From 4cbb79fcac0a41703088ad3703fa4ef1e8843c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Tue, 16 May 2023 14:05:19 +0200 Subject: [PATCH 11/16] optimized calculateProperties centre is also now an ArrayList --- src/Graph.java | 52 ++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index 7de208a..fc699f7 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -9,7 +9,7 @@ public class Graph { private int[] exzentrizitäten; private int radius; private int durchmesser; - private int[] zentrum; + private ArrayList centre; private int[][] components; private int[][] bridges; private int[] articulations; @@ -18,7 +18,6 @@ public class Graph { public static void main(String[] args) {} public Graph(String file) { - zusammenhaengend = true; adjazenzMatrix = new Matrix(file); calculateDistanzMatrix(); calculateWegMatrix(); @@ -102,33 +101,31 @@ public class Graph { public void calculateProperties() { radius = Integer.MAX_VALUE; durchmesser = -1; - int sum = 0; + zusammenhaengend = true; + centre = new ArrayList<>(1); for(int rowIndex = 0; rowIndex < exzentrizitäten.length; rowIndex++) { + if(exzentrizitäten[rowIndex] > durchmesser) { + centre.clear(); + centre.add(rowIndex + 1); + } if(exzentrizitäten[rowIndex] < radius) { radius = exzentrizitäten[rowIndex]; } if(exzentrizitäten[rowIndex] == durchmesser) { - sum++; + centre.add(rowIndex + 1); } if(exzentrizitäten[rowIndex] > durchmesser) { durchmesser = exzentrizitäten[rowIndex]; - sum = 1; } } + if(durchmesser == 0 && exzentrizitäten.length > 1) { zusammenhaengend = false; return; } - zentrum = new int[sum]; - - for(int rowIndex = 0, index = 0; rowIndex < exzentrizitäten.length; rowIndex++) { - if(exzentrizitäten[rowIndex] == durchmesser) { - zentrum[index] = rowIndex + 1; - index++; - } - } + centre.trimToSize(); } public void findComponents() { @@ -167,22 +164,31 @@ public class Graph { public int[][] findComponents(Matrix matrix) { int[][] newComponents; ArrayList tempComponents = new ArrayList<>(1); - int[] component = new int[wegMatrix.getRowLength()]; + ArrayList tempComponent = new ArrayList<>(1); + int[] component; - for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { - for(int columnIndex = 0, index = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { - if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) { - component[index] = columnIndex + 1; - index++; + for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { + for(int columnIndex = 0; columnIndex < matrix.getColumnLength(); columnIndex++) { + if(matrix.getValueAt(rowIndex, columnIndex) == 1) { + tempComponent.add(columnIndex + 1); } } + component = new int[tempComponent.size()]; + + for(int index = 0; index < component.length; index++) { + component[index] = tempComponent.get(index); + } + if(tempComponents.contains(component)) { continue; } - tempComponents.add(component); - } - newComponents = new int[tempComponents.size()][wegMatrix.getColumnLength()]; + 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); @@ -277,7 +283,7 @@ public class Graph { s += "\nZentrum: "; if(zusammenhaengend) { - s += Arrays.toString(zentrum); + s += centre; } else { s += "Kein zusammenhängender Graph"; } -- 2.47.1 From 5b72916732a022e40877171b0f560b2ee315486e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Tue, 16 May 2023 19:20:56 +0200 Subject: [PATCH 12/16] made findComponents work It took way too long, now onto fixing bridges and articulations --- Graph.java | 327 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 327 insertions(+) create mode 100644 Graph.java diff --git a/Graph.java b/Graph.java new file mode 100644 index 0000000..7648495 --- /dev/null +++ b/Graph.java @@ -0,0 +1,327 @@ +import java.util.ArrayList; +import java.util.Arrays; + +public class Graph { + private Matrix adjazenzMatrix; + private Matrix distanzMatrix; + private Matrix wegMatrix; + private boolean zusammenhaengend; + private int[] exzentrizitäten; + private int radius; + private int durchmesser; + private ArrayList centre; + private int[][] components; + private int[][] bridges; + private int[] articulations; + + + public static void main(String[] args) {} + + public Graph(String file) { + adjazenzMatrix = new Matrix(file); + calculateDistanzMatrix(); + calculateWegMatrix(); + calculateExzentrizitäten(); + calculateProperties(); + findComponents(); + //findBridges(); + //findArticulations(); + } + + public void calculateDistanzMatrix() { + distanzMatrix = new Matrix(adjazenzMatrix.getRowLength(), adjazenzMatrix.getColumnLength()); + Matrix potenzMatrix = adjazenzMatrix; + + for(int columnIndex=0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex=0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { + if(adjazenzMatrix.getValueAt(rowIndex, columnIndex) == 1) { + distanzMatrix.insert(rowIndex, columnIndex, 1); + } else if(columnIndex == rowIndex) { + distanzMatrix.insert(rowIndex, columnIndex, 0); + } else { + distanzMatrix.insert(rowIndex, columnIndex, -1); + } + } + } + + for(int k = 2; k < distanzMatrix.getRowLength(); k++) { + potenzMatrix = potenzMatrix.multiply(adjazenzMatrix); + + for(int columnIndex=0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex=0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { + if(potenzMatrix.getValueAt(rowIndex, columnIndex) != 0 && distanzMatrix.getValueAt(rowIndex, columnIndex) == -1) { + distanzMatrix.insert(rowIndex, columnIndex, k); + } + } + } + } + } + + public void calculateWegMatrix() { + wegMatrix = new Matrix(adjazenzMatrix.getRowLength(), adjazenzMatrix.getColumnLength()); + Matrix potenzMatrix = adjazenzMatrix; + + for(int columnIndex=0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex=0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + if(columnIndex == rowIndex) { + wegMatrix.insert(rowIndex, columnIndex, 1); + } else if(adjazenzMatrix.getValueAt(rowIndex, columnIndex) > 0 && wegMatrix.getValueAt(rowIndex, columnIndex) == 0) { + wegMatrix.insert(rowIndex, columnIndex, 1); + } + } + } + + for(int k = 2; k < wegMatrix.getRowLength(); k++) { + potenzMatrix = potenzMatrix.multiply(adjazenzMatrix); + + for(int columnIndex=0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex=0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + if(potenzMatrix.getValueAt(rowIndex, columnIndex) != 0) { + wegMatrix.insert(rowIndex, columnIndex, 1); + } + } + } + } + } + + public void calculateExzentrizitäten() { + exzentrizitäten = new int[distanzMatrix.getRowLength()]; + for(int rowIndex = 0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { + int exzentrizität = 0; + + for(int columnIndex = 0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { + if(distanzMatrix.getValueAt(columnIndex, rowIndex) > exzentrizität && rowIndex != columnIndex) { + exzentrizität = distanzMatrix.getValueAt(columnIndex, rowIndex); + } + } + exzentrizitäten[rowIndex] = exzentrizität; + } + } + + public void calculateProperties() { + radius = Integer.MAX_VALUE; + durchmesser = -1; + zusammenhaengend = true; + centre = new ArrayList<>(1); + + for(int rowIndex = 0; rowIndex < exzentrizitäten.length; rowIndex++) { + if(exzentrizitäten[rowIndex] > durchmesser) { + durchmesser = exzentrizitäten[rowIndex]; + centre.clear(); + centre.add(rowIndex + 1); + } + if(exzentrizitäten[rowIndex] < radius) { + radius = exzentrizitäten[rowIndex]; + } + if(exzentrizitäten[rowIndex] == durchmesser) { + centre.add(rowIndex + 1); + } + } + + if(durchmesser == 0 && exzentrizitäten.length > 1) { + zusammenhaengend = false; + return; + } + + centre.trimToSize(); + } + + public void findComponents() { + ArrayList tempComponents = new ArrayList<>(1); + ArrayList tempComponent = new ArrayList<>(1); + int[] component; + boolean contains; + + for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) { + tempComponent.add(columnIndex + 1); + } + } + component = new int[tempComponent.size()]; + + for(int index = 0; index < component.length; index++) { + component[index] = tempComponent.get(index); + } + + contains = false; + for(int index = 0; index < tempComponents.size(); index++) { + if(Arrays.equals(tempComponents.get(index), component)) { + contains = true; + } + } + + if(!contains) { + 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) { + int[][] newComponents; + ArrayList tempComponents = new ArrayList<>(1); + ArrayList tempComponent = new ArrayList<>(1); + int[] component; + + for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { + for(int columnIndex = 0; columnIndex < matrix.getColumnLength(); columnIndex++) { + if(matrix.getValueAt(rowIndex, columnIndex) == 1) { + tempComponent.add(columnIndex + 1); + } + } + component = new int[tempComponent.size()]; + + for(int index = 0; index < component.length; index++) { + 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; + } + + public void findBridges() { + if(!zusammenhaengend) { + return; + } + ArrayList tempBridges = new ArrayList<>(10); + int[][] newComponents; + int[] bridge; + + for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + wegMatrix.insert(rowIndex, columnIndex, 0); + wegMatrix.insert(columnIndex, rowIndex, 0); + bridge = new int[2]; + bridge[0] = columnIndex + 1; + bridge[1] = rowIndex + 1; + + newComponents = findComponents(wegMatrix); + + System.out.println(Arrays.toString(bridge)); + if(!components.equals(newComponents) && !tempBridges.contains(bridge)) { + System.out.println("bruh"); + tempBridges.add(bridge); + } + bridge = null; + wegMatrix.insert(rowIndex, columnIndex, 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() { + if(!zusammenhaengend) { + return; + } + ArrayList tempArticulations = new ArrayList<>(10); + int[][] newComponents; + + for(int i = 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)) { + tempArticulations.add(i + 1); + } + + 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); + } + } + } + articulations = new int[tempArticulations.size()]; + + for(int rowIndex = 0; rowIndex < articulations.length; rowIndex++) { + articulations[rowIndex] = tempArticulations.get(rowIndex); + } + } + + public String toString() { + String s = ""; + + s += "Adjazenzmatrix:\n" + adjazenzMatrix + "\nDistanzmatrix:\n" + distanzMatrix + "\nWegmatrix:\n" + wegMatrix; + + if(!zusammenhaengend) { + s += "\nExzentrizitäten/Radius/Durchmesser: Kein zusammenhängender Graph"; + } else { + s += "\nExzentrizitäten: " + Arrays.toString(exzentrizitäten) + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser; + } + + s += "\nZentrum: "; + if(zusammenhaengend) { + s += centre; + } else { + s += "Kein zusammenhängender Graph"; + } + + s += "\nKomponente: {"; + for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { + if(components[rowIndex] != null) { + s += Arrays.toString(components[rowIndex]); + if(rowIndex < components.length - 1) { + if(components[rowIndex + 1] != null) { + s += ","; + } + } + } + } + s += "}"; + + s += "\nBrücken: {"; + if(bridges != null) { + for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) { + if(bridges[rowIndex] != null) { + s += Arrays.toString(bridges[rowIndex]); + if(rowIndex < bridges.length - 1) { + if(bridges[rowIndex + 1] != null) { + s += ","; + } + } + } + } + } + s += "}"; + + s += "\nArtikulationen: " + Arrays.toString(articulations); + return s; + } +} \ No newline at end of file -- 2.47.1 From 23767a9ad74907ba01104914e11426cbba166037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Mon, 22 May 2023 15:38:54 +0200 Subject: [PATCH 13/16] heavy refactoring and findArticulations now works --- src/Graph.java | 205 +++++++++++++++++-------------------------------- 1 file changed, 72 insertions(+), 133 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index fc699f7..cddd86c 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -6,13 +6,13 @@ public class Graph { private Matrix distanzMatrix; private Matrix wegMatrix; private boolean zusammenhaengend; - private int[] exzentrizitäten; + private ArrayList exzentrizitäten; private int radius; private int durchmesser; private ArrayList centre; - private int[][] components; - private int[][] bridges; - private int[] articulations; + private ArrayList> components; + private ArrayList bridges; + private ArrayList articulations; public static void main(String[] args) {} @@ -25,7 +25,7 @@ public class Graph { calculateProperties(); findComponents(); //findBridges(); - //findArticulations(); + findArticulations(file); } public void calculateDistanzMatrix() { @@ -85,7 +85,8 @@ public class Graph { } public void calculateExzentrizitäten() { - exzentrizitäten = new int[distanzMatrix.getRowLength()]; + exzentrizitäten = new ArrayList<>(1); + for(int rowIndex = 0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { int exzentrizität = 0; @@ -94,7 +95,7 @@ public class Graph { 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; centre = new ArrayList<>(1); - for(int rowIndex = 0; rowIndex < exzentrizitäten.length; rowIndex++) { - if(exzentrizitäten[rowIndex] > durchmesser) { + for(int rowIndex = 0; rowIndex < exzentrizitäten.size(); rowIndex++) { + if(exzentrizitäten.get(rowIndex) > durchmesser) { + durchmesser = exzentrizitäten.get(rowIndex); centre.clear(); centre.add(rowIndex + 1); } - if(exzentrizitäten[rowIndex] < radius) { - radius = exzentrizitäten[rowIndex]; + if(exzentrizitäten.get(rowIndex) < radius) { + radius = exzentrizitäten.get(rowIndex); } - if(exzentrizitäten[rowIndex] == durchmesser) { + if(exzentrizitäten.get(rowIndex) == durchmesser) { 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; - return; } - - centre.trimToSize(); } public void findComponents() { - ArrayList tempComponents = new ArrayList<>(1); - ArrayList tempComponent = new ArrayList<>(1); - int[] component; + components = new ArrayList<>(1); + ArrayList component = new ArrayList<>(1); for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + component = new ArrayList<>(1); + for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) { - tempComponent.add(columnIndex + 1); + component.add(columnIndex + 1); } } - component = new int[tempComponent.size()]; - - for(int index = 0; index < component.length; index++) { - component[index] = tempComponent.get(index); + + if(!components.contains(component)) { + components.add(component); } - - 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) { - int[][] newComponents; - ArrayList tempComponents = new ArrayList<>(1); - ArrayList tempComponent = new ArrayList<>(1); - int[] component; + public ArrayList> findComponents(Matrix matrix) { + ArrayList> newComponents = new ArrayList<>(1); + ArrayList component = new ArrayList<>(1); for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { - for(int columnIndex = 0; columnIndex < matrix.getColumnLength(); columnIndex++) { - if(matrix.getValueAt(rowIndex, columnIndex) == 1) { - tempComponent.add(columnIndex + 1); + component = new ArrayList<>(1); + + for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { + if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) { + component.add(columnIndex + 1); } } - component = new int[tempComponent.size()]; - - for(int index = 0; index < component.length; index++) { - component[index] = tempComponent.get(index); + + if(!newComponents.contains(component)) { + newComponents.add(component); } - - 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; } public void findBridges() { - if(!zusammenhaengend) { - return; - } - ArrayList tempBridges = new ArrayList<>(10); - int[][] newComponents; + bridges = new ArrayList<>(1); + ArrayList> newComponents; int[] bridge; for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { - wegMatrix.insert(rowIndex, columnIndex, 0); - wegMatrix.insert(columnIndex, rowIndex, 0); bridge = new int[2]; bridge[0] = columnIndex + 1; bridge[1] = rowIndex + 1; + wegMatrix.insert(rowIndex, columnIndex, 0); + wegMatrix.insert(columnIndex, rowIndex, 0); + newComponents = findComponents(wegMatrix); - System.out.println(Arrays.toString(bridge)); - if(!components.equals(newComponents) && !tempBridges.contains(bridge)) { - System.out.println("bruh"); - tempBridges.add(bridge); + //System.out.println(Arrays.toString(bridge)); + if(newComponents.size() > components.size()) { + //System.out.println("bruh"); + bridges.add(bridge); } - bridge = null; + wegMatrix.insert(rowIndex, columnIndex, 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() { - if(!zusammenhaengend) { - return; - } - ArrayList tempArticulations = new ArrayList<>(10); - int[][] newComponents; + public void findArticulations(String file) { + articulations = new ArrayList<>(1); + ArrayList> newComponents; - for(int i = 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); + for(int i = 0; i < adjazenzMatrix.getRowLength(); i++) { + for(int rowIndex = 0; rowIndex < adjazenzMatrix.getRowLength(); rowIndex++) { + for(int columnIndex = 0; columnIndex < adjazenzMatrix.getColumnLength(); columnIndex++) { + adjazenzMatrix.insert(rowIndex, i, 0); + adjazenzMatrix.insert(i, columnIndex, 0); } } - /* - * need to figure out what removing a node means - */ - + calculateWegMatrix(); + newComponents = findComponents(wegMatrix); - if(!components.equals(newComponents)) { - tempArticulations.add(i + 1); + if(newComponents.size() > components.size() + 1) { + articulations.add(i + 1); } - 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); - } - } - } - articulations = new int[tempArticulations.size()]; - - for(int rowIndex = 0; rowIndex < articulations.length; rowIndex++) { - articulations[rowIndex] = tempArticulations.get(rowIndex); + adjazenzMatrix = new Matrix(file); } } @@ -278,45 +223,39 @@ public class Graph { if(!zusammenhaengend) { s += "\nExzentrizitäten/Radius/Durchmesser: Kein zusammenhängender Graph"; } else { - s += "\nExzentrizitäten: " + Arrays.toString(exzentrizitäten) + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser; + s += "\nExzentrizitäten: " + exzentrizitäten.toString() + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser; } s += "\nZentrum: "; - if(zusammenhaengend) { - s += centre; - } else { + if(!zusammenhaengend) { s += "Kein zusammenhängender Graph"; + } else { + s += centre; } s += "\nKomponente: {"; - for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { - if(components[rowIndex] != null) { - s += Arrays.toString(components[rowIndex]); - if(rowIndex < components.length - 1) { - if(components[rowIndex + 1] != null) { - s += ","; - } - } + for(int rowIndex = 0; rowIndex < components.size(); rowIndex++) { + s += components.get(rowIndex).toString(); + if(rowIndex < components.size() - 1) { + s += ","; } } s += "}"; s += "\nBrücken: {"; if(bridges != null) { - for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) { - if(bridges[rowIndex] != null) { - s += Arrays.toString(bridges[rowIndex]); - if(rowIndex < bridges.length - 1) { - if(bridges[rowIndex + 1] != null) { - s += ","; - } + for(int rowIndex = 0; rowIndex < bridges.size(); rowIndex++) { + s += Arrays.toString(bridges.get(rowIndex)); + if(rowIndex < bridges.size() - 1) { + if(rowIndex < bridges.size() - 1) { + s += ","; } } } - } + } s += "}"; - s += "\nArtikulationen: " + Arrays.toString(articulations); + s += "\nArtikulationen: " + articulations.toString(); return s; } } \ No newline at end of file -- 2.47.1 From 85dd40e64e875ce4be8be9a0ccd82512d3692699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Mon, 22 May 2023 15:39:32 +0200 Subject: [PATCH 14/16] shouldn't be here --- Graph.java | 327 ----------------------------------------------------- 1 file changed, 327 deletions(-) delete mode 100644 Graph.java diff --git a/Graph.java b/Graph.java deleted file mode 100644 index 7648495..0000000 --- a/Graph.java +++ /dev/null @@ -1,327 +0,0 @@ -import java.util.ArrayList; -import java.util.Arrays; - -public class Graph { - private Matrix adjazenzMatrix; - private Matrix distanzMatrix; - private Matrix wegMatrix; - private boolean zusammenhaengend; - private int[] exzentrizitäten; - private int radius; - private int durchmesser; - private ArrayList centre; - private int[][] components; - private int[][] bridges; - private int[] articulations; - - - public static void main(String[] args) {} - - public Graph(String file) { - adjazenzMatrix = new Matrix(file); - calculateDistanzMatrix(); - calculateWegMatrix(); - calculateExzentrizitäten(); - calculateProperties(); - findComponents(); - //findBridges(); - //findArticulations(); - } - - public void calculateDistanzMatrix() { - distanzMatrix = new Matrix(adjazenzMatrix.getRowLength(), adjazenzMatrix.getColumnLength()); - Matrix potenzMatrix = adjazenzMatrix; - - for(int columnIndex=0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { - for(int rowIndex=0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { - if(adjazenzMatrix.getValueAt(rowIndex, columnIndex) == 1) { - distanzMatrix.insert(rowIndex, columnIndex, 1); - } else if(columnIndex == rowIndex) { - distanzMatrix.insert(rowIndex, columnIndex, 0); - } else { - distanzMatrix.insert(rowIndex, columnIndex, -1); - } - } - } - - for(int k = 2; k < distanzMatrix.getRowLength(); k++) { - potenzMatrix = potenzMatrix.multiply(adjazenzMatrix); - - for(int columnIndex=0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { - for(int rowIndex=0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { - if(potenzMatrix.getValueAt(rowIndex, columnIndex) != 0 && distanzMatrix.getValueAt(rowIndex, columnIndex) == -1) { - distanzMatrix.insert(rowIndex, columnIndex, k); - } - } - } - } - } - - public void calculateWegMatrix() { - wegMatrix = new Matrix(adjazenzMatrix.getRowLength(), adjazenzMatrix.getColumnLength()); - Matrix potenzMatrix = adjazenzMatrix; - - for(int columnIndex=0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { - for(int rowIndex=0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { - if(columnIndex == rowIndex) { - wegMatrix.insert(rowIndex, columnIndex, 1); - } else if(adjazenzMatrix.getValueAt(rowIndex, columnIndex) > 0 && wegMatrix.getValueAt(rowIndex, columnIndex) == 0) { - wegMatrix.insert(rowIndex, columnIndex, 1); - } - } - } - - for(int k = 2; k < wegMatrix.getRowLength(); k++) { - potenzMatrix = potenzMatrix.multiply(adjazenzMatrix); - - for(int columnIndex=0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { - for(int rowIndex=0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { - if(potenzMatrix.getValueAt(rowIndex, columnIndex) != 0) { - wegMatrix.insert(rowIndex, columnIndex, 1); - } - } - } - } - } - - public void calculateExzentrizitäten() { - exzentrizitäten = new int[distanzMatrix.getRowLength()]; - for(int rowIndex = 0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { - int exzentrizität = 0; - - for(int columnIndex = 0; columnIndex < distanzMatrix.getColumnLength(); columnIndex++) { - if(distanzMatrix.getValueAt(columnIndex, rowIndex) > exzentrizität && rowIndex != columnIndex) { - exzentrizität = distanzMatrix.getValueAt(columnIndex, rowIndex); - } - } - exzentrizitäten[rowIndex] = exzentrizität; - } - } - - public void calculateProperties() { - radius = Integer.MAX_VALUE; - durchmesser = -1; - zusammenhaengend = true; - centre = new ArrayList<>(1); - - for(int rowIndex = 0; rowIndex < exzentrizitäten.length; rowIndex++) { - if(exzentrizitäten[rowIndex] > durchmesser) { - durchmesser = exzentrizitäten[rowIndex]; - centre.clear(); - centre.add(rowIndex + 1); - } - if(exzentrizitäten[rowIndex] < radius) { - radius = exzentrizitäten[rowIndex]; - } - if(exzentrizitäten[rowIndex] == durchmesser) { - centre.add(rowIndex + 1); - } - } - - if(durchmesser == 0 && exzentrizitäten.length > 1) { - zusammenhaengend = false; - return; - } - - centre.trimToSize(); - } - - public void findComponents() { - ArrayList tempComponents = new ArrayList<>(1); - ArrayList tempComponent = new ArrayList<>(1); - int[] component; - boolean contains; - - for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { - for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { - if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) { - tempComponent.add(columnIndex + 1); - } - } - component = new int[tempComponent.size()]; - - for(int index = 0; index < component.length; index++) { - component[index] = tempComponent.get(index); - } - - contains = false; - for(int index = 0; index < tempComponents.size(); index++) { - if(Arrays.equals(tempComponents.get(index), component)) { - contains = true; - } - } - - if(!contains) { - 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) { - int[][] newComponents; - ArrayList tempComponents = new ArrayList<>(1); - ArrayList tempComponent = new ArrayList<>(1); - int[] component; - - for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) { - for(int columnIndex = 0; columnIndex < matrix.getColumnLength(); columnIndex++) { - if(matrix.getValueAt(rowIndex, columnIndex) == 1) { - tempComponent.add(columnIndex + 1); - } - } - component = new int[tempComponent.size()]; - - for(int index = 0; index < component.length; index++) { - 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; - } - - public void findBridges() { - if(!zusammenhaengend) { - return; - } - ArrayList tempBridges = new ArrayList<>(10); - int[][] newComponents; - int[] bridge; - - for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { - for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { - wegMatrix.insert(rowIndex, columnIndex, 0); - wegMatrix.insert(columnIndex, rowIndex, 0); - bridge = new int[2]; - bridge[0] = columnIndex + 1; - bridge[1] = rowIndex + 1; - - newComponents = findComponents(wegMatrix); - - System.out.println(Arrays.toString(bridge)); - if(!components.equals(newComponents) && !tempBridges.contains(bridge)) { - System.out.println("bruh"); - tempBridges.add(bridge); - } - bridge = null; - wegMatrix.insert(rowIndex, columnIndex, 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() { - if(!zusammenhaengend) { - return; - } - ArrayList tempArticulations = new ArrayList<>(10); - int[][] newComponents; - - for(int i = 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)) { - tempArticulations.add(i + 1); - } - - 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); - } - } - } - articulations = new int[tempArticulations.size()]; - - for(int rowIndex = 0; rowIndex < articulations.length; rowIndex++) { - articulations[rowIndex] = tempArticulations.get(rowIndex); - } - } - - public String toString() { - String s = ""; - - s += "Adjazenzmatrix:\n" + adjazenzMatrix + "\nDistanzmatrix:\n" + distanzMatrix + "\nWegmatrix:\n" + wegMatrix; - - if(!zusammenhaengend) { - s += "\nExzentrizitäten/Radius/Durchmesser: Kein zusammenhängender Graph"; - } else { - s += "\nExzentrizitäten: " + Arrays.toString(exzentrizitäten) + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser; - } - - s += "\nZentrum: "; - if(zusammenhaengend) { - s += centre; - } else { - s += "Kein zusammenhängender Graph"; - } - - s += "\nKomponente: {"; - for(int rowIndex = 0; rowIndex < components.length; rowIndex++) { - if(components[rowIndex] != null) { - s += Arrays.toString(components[rowIndex]); - if(rowIndex < components.length - 1) { - if(components[rowIndex + 1] != null) { - s += ","; - } - } - } - } - s += "}"; - - s += "\nBrücken: {"; - if(bridges != null) { - for(int rowIndex = 0; rowIndex < bridges.length; rowIndex++) { - if(bridges[rowIndex] != null) { - s += Arrays.toString(bridges[rowIndex]); - if(rowIndex < bridges.length - 1) { - if(bridges[rowIndex + 1] != null) { - s += ","; - } - } - } - } - } - s += "}"; - - s += "\nArtikulationen: " + Arrays.toString(articulations); - return s; - } -} \ No newline at end of file -- 2.47.1 From 5a16504bd26a41f6873fb071a596aeb8785f471c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Mon, 22 May 2023 16:05:23 +0200 Subject: [PATCH 15/16] minor refactoring findBridges still doesn't work --- src/Graph.java | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index cddd86c..d62f8c3 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -5,10 +5,10 @@ public class Graph { private Matrix adjazenzMatrix; private Matrix distanzMatrix; private Matrix wegMatrix; - private boolean zusammenhaengend; + private boolean connected; private ArrayList exzentrizitäten; private int radius; - private int durchmesser; + private int diameter; private ArrayList centre; private ArrayList> components; private ArrayList bridges; @@ -24,7 +24,7 @@ public class Graph { calculateExzentrizitäten(); calculateProperties(); findComponents(); - //findBridges(); + findBridges(); findArticulations(file); } @@ -101,26 +101,26 @@ public class Graph { public void calculateProperties() { radius = Integer.MAX_VALUE; - durchmesser = -1; - zusammenhaengend = true; + diameter = -1; + connected = true; centre = new ArrayList<>(1); for(int rowIndex = 0; rowIndex < exzentrizitäten.size(); rowIndex++) { - if(exzentrizitäten.get(rowIndex) > durchmesser) { - durchmesser = exzentrizitäten.get(rowIndex); + if(exzentrizitäten.get(rowIndex) > diameter) { + diameter = exzentrizitäten.get(rowIndex); centre.clear(); centre.add(rowIndex + 1); } if(exzentrizitäten.get(rowIndex) < radius) { radius = exzentrizitäten.get(rowIndex); } - if(exzentrizitäten.get(rowIndex) == durchmesser) { + if(exzentrizitäten.get(rowIndex) == diameter) { centre.add(rowIndex + 1); } } if(radius == 0) { - zusammenhaengend = false; + connected = false; } } @@ -168,27 +168,28 @@ public class Graph { ArrayList> newComponents; int[] bridge; - for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) { - for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) { + for(int rowIndex = 0; rowIndex < adjazenzMatrix.getRowLength(); rowIndex++) { + for(int columnIndex = 0; columnIndex < adjazenzMatrix.getColumnLength(); columnIndex++) { bridge = new int[2]; bridge[0] = columnIndex + 1; bridge[1] = rowIndex + 1; - wegMatrix.insert(rowIndex, columnIndex, 0); - wegMatrix.insert(columnIndex, rowIndex, 0); + adjazenzMatrix.insert(rowIndex, columnIndex, 0); + adjazenzMatrix.insert(columnIndex, rowIndex, 0); + + calculateWegMatrix(); newComponents = findComponents(wegMatrix); - //System.out.println(Arrays.toString(bridge)); if(newComponents.size() > components.size()) { - //System.out.println("bruh"); bridges.add(bridge); } - wegMatrix.insert(rowIndex, columnIndex, 1); - wegMatrix.insert(columnIndex, rowIndex, 1); + adjazenzMatrix.insert(rowIndex, columnIndex, 1); + adjazenzMatrix.insert(columnIndex, rowIndex, 1); } } + calculateWegMatrix(); } public void findArticulations(String file) { @@ -213,6 +214,7 @@ public class Graph { adjazenzMatrix = new Matrix(file); } + calculateWegMatrix(); } public String toString() { @@ -220,14 +222,14 @@ public class Graph { s += "Adjazenzmatrix:\n" + adjazenzMatrix + "\nDistanzmatrix:\n" + distanzMatrix + "\nWegmatrix:\n" + wegMatrix; - if(!zusammenhaengend) { + if(!connected) { s += "\nExzentrizitäten/Radius/Durchmesser: Kein zusammenhängender Graph"; } else { - s += "\nExzentrizitäten: " + exzentrizitäten.toString() + "\nRadius: " + radius + "\nDurchmesser: " + durchmesser; + s += "\nExzentrizitäten: " + exzentrizitäten.toString() + "\nRadius: " + radius + "\nDurchmesser: " + diameter; } s += "\nZentrum: "; - if(!zusammenhaengend) { + if(!connected) { s += "Kein zusammenhängender Graph"; } else { s += centre; -- 2.47.1 From 7f0849e1b1b141f10e561c66279609f24ab75bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Tue, 23 May 2023 13:46:55 +0200 Subject: [PATCH 16/16] finished findBridges --- src/Graph.java | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index d62f8c3..21ff84a 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -24,7 +24,7 @@ public class Graph { calculateExzentrizitäten(); calculateProperties(); findComponents(); - findBridges(); + findBridges(file); findArticulations(file); } @@ -163,16 +163,27 @@ public class Graph { return newComponents; } - public void findBridges() { + public void findBridges(String file) { bridges = new ArrayList<>(1); ArrayList> newComponents; int[] bridge; + boolean contains; for(int rowIndex = 0; rowIndex < adjazenzMatrix.getRowLength(); rowIndex++) { for(int columnIndex = 0; columnIndex < adjazenzMatrix.getColumnLength(); columnIndex++) { + if(rowIndex == columnIndex) { + continue; + } + bridge = new int[2]; - bridge[0] = columnIndex + 1; - bridge[1] = rowIndex + 1; + if(columnIndex < rowIndex) { + bridge[0] = columnIndex + 1; + bridge[1] = rowIndex + 1; + } else { + bridge[1] = columnIndex + 1; + bridge[0] = rowIndex + 1; + } + adjazenzMatrix.insert(rowIndex, columnIndex, 0); adjazenzMatrix.insert(columnIndex, rowIndex, 0); @@ -181,12 +192,19 @@ public class Graph { newComponents = findComponents(wegMatrix); - if(newComponents.size() > components.size()) { + contains = false; + for (int[] array : bridges) { + if(Arrays.equals(array, bridge)) { + contains = true; + break; + } + } + + if(newComponents.size() > components.size() && !contains) { bridges.add(bridge); } - adjazenzMatrix.insert(rowIndex, columnIndex, 1); - adjazenzMatrix.insert(columnIndex, rowIndex, 1); + adjazenzMatrix = new Matrix(file); } } calculateWegMatrix(); -- 2.47.1