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] 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