optimized calculateProperties
centre is also now an ArrayList
This commit is contained in:
parent
e1aa04e33c
commit
4cbb79fcac
@ -9,7 +9,7 @@ public class Graph {
|
|||||||
private int[] exzentrizitäten;
|
private int[] exzentrizitäten;
|
||||||
private int radius;
|
private int radius;
|
||||||
private int durchmesser;
|
private int durchmesser;
|
||||||
private int[] zentrum;
|
private ArrayList<Integer> centre;
|
||||||
private int[][] components;
|
private int[][] components;
|
||||||
private int[][] bridges;
|
private int[][] bridges;
|
||||||
private int[] articulations;
|
private int[] articulations;
|
||||||
@ -18,7 +18,6 @@ public class Graph {
|
|||||||
public static void main(String[] args) {}
|
public static void main(String[] args) {}
|
||||||
|
|
||||||
public Graph(String file) {
|
public Graph(String file) {
|
||||||
zusammenhaengend = true;
|
|
||||||
adjazenzMatrix = new Matrix(file);
|
adjazenzMatrix = new Matrix(file);
|
||||||
calculateDistanzMatrix();
|
calculateDistanzMatrix();
|
||||||
calculateWegMatrix();
|
calculateWegMatrix();
|
||||||
@ -102,33 +101,31 @@ public class Graph {
|
|||||||
public void calculateProperties() {
|
public void calculateProperties() {
|
||||||
radius = Integer.MAX_VALUE;
|
radius = Integer.MAX_VALUE;
|
||||||
durchmesser = -1;
|
durchmesser = -1;
|
||||||
int sum = 0;
|
zusammenhaengend = true;
|
||||||
|
centre = new ArrayList<>(1);
|
||||||
|
|
||||||
for(int rowIndex = 0; rowIndex < exzentrizitäten.length; rowIndex++) {
|
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) {
|
if(exzentrizitäten[rowIndex] < radius) {
|
||||||
radius = exzentrizitäten[rowIndex];
|
radius = exzentrizitäten[rowIndex];
|
||||||
}
|
}
|
||||||
if(exzentrizitäten[rowIndex] == durchmesser) {
|
if(exzentrizitäten[rowIndex] == durchmesser) {
|
||||||
sum++;
|
centre.add(rowIndex + 1);
|
||||||
}
|
}
|
||||||
if(exzentrizitäten[rowIndex] > durchmesser) {
|
if(exzentrizitäten[rowIndex] > durchmesser) {
|
||||||
durchmesser = exzentrizitäten[rowIndex];
|
durchmesser = exzentrizitäten[rowIndex];
|
||||||
sum = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(durchmesser == 0 && exzentrizitäten.length > 1) {
|
if(durchmesser == 0 && exzentrizitäten.length > 1) {
|
||||||
zusammenhaengend = false;
|
zusammenhaengend = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
zentrum = new int[sum];
|
centre.trimToSize();
|
||||||
|
|
||||||
for(int rowIndex = 0, index = 0; rowIndex < exzentrizitäten.length; rowIndex++) {
|
|
||||||
if(exzentrizitäten[rowIndex] == durchmesser) {
|
|
||||||
zentrum[index] = rowIndex + 1;
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void findComponents() {
|
public void findComponents() {
|
||||||
@ -167,22 +164,31 @@ public class Graph {
|
|||||||
public int[][] findComponents(Matrix matrix) {
|
public int[][] findComponents(Matrix matrix) {
|
||||||
int[][] newComponents;
|
int[][] newComponents;
|
||||||
ArrayList<int[]> tempComponents = new ArrayList<>(1);
|
ArrayList<int[]> tempComponents = new ArrayList<>(1);
|
||||||
int[] component = new int[wegMatrix.getRowLength()];
|
ArrayList<Integer> tempComponent = new ArrayList<>(1);
|
||||||
|
int[] component;
|
||||||
|
|
||||||
for(int rowIndex = 0; rowIndex < wegMatrix.getRowLength(); rowIndex++) {
|
for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) {
|
||||||
for(int columnIndex = 0, index = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) {
|
for(int columnIndex = 0; columnIndex < matrix.getColumnLength(); columnIndex++) {
|
||||||
if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) {
|
if(matrix.getValueAt(rowIndex, columnIndex) == 1) {
|
||||||
component[index] = columnIndex + 1;
|
tempComponent.add(columnIndex + 1);
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
component = new int[tempComponent.size()];
|
||||||
|
|
||||||
|
for(int index = 0; index < component.length; index++) {
|
||||||
|
component[index] = tempComponent.get(index);
|
||||||
|
}
|
||||||
|
|
||||||
if(tempComponents.contains(component)) {
|
if(tempComponents.contains(component)) {
|
||||||
continue;
|
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++) {
|
for(int rowIndex = 0; rowIndex < newComponents.length; rowIndex++) {
|
||||||
newComponents[rowIndex] = tempComponents.get(rowIndex);
|
newComponents[rowIndex] = tempComponents.get(rowIndex);
|
||||||
@ -277,7 +283,7 @@ public class Graph {
|
|||||||
|
|
||||||
s += "\nZentrum: ";
|
s += "\nZentrum: ";
|
||||||
if(zusammenhaengend) {
|
if(zusammenhaengend) {
|
||||||
s += Arrays.toString(zentrum);
|
s += centre;
|
||||||
} else {
|
} else {
|
||||||
s += "Kein zusammenhängender Graph";
|
s += "Kein zusammenhängender Graph";
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user