This commit is contained in:
René Fuhry 2023-04-20 22:25:48 +02:00 committed by GitHub
parent 27e79eb1f6
commit f2202f7ed9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 183 additions and 48 deletions

120
src/Graph.java Normal file
View File

@ -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<Integer> 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;
}
}

View File

@ -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(int rowLength, int columnLength) {
matrix = new int[rowLength][columnLength];
public Matrix(String file) {
readCSV(file);
}
public Matrix(String file) {
matrix = this.readCSV(file);
public Matrix(int rowLength, int columnLength) {
matrix = new int[rowLength][columnLength];
this.rowLength = rowLength;
this.columnLength = columnLength;
}
public Matrix(int[][] matrix) {
this.matrix = matrix;
rowLength = matrix.length;
columnLength = rowLength;
}
public Matrix multiply(Matrix m) {
Matrix scalarProduct = null;
Matrix scalarProduct = new Matrix(new int[rowLength][m.columnLength]);
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++) {
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;
}
}

12
src/TestGraph.java Normal file
View File

@ -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);
}
}

View File

@ -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();
System.out.println(matrix.getRowLength());
System.out.println(matrix.getColumnLength());
System.out.println(matrix);
scalarProduct = matrix.multiply(matrix);
scalarProduct.print();
System.out.println(scalarProduct);
}
}