progress
This commit is contained in:
parent
27e79eb1f6
commit
f2202f7ed9
120
src/Graph.java
Normal file
120
src/Graph.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -5,78 +5,71 @@ import java.io.FileNotFoundException;
|
|||||||
|
|
||||||
public class Matrix {
|
public class Matrix {
|
||||||
private int[][] matrix;
|
private int[][] matrix;
|
||||||
|
private int rowLength;
|
||||||
|
private int columnLength;
|
||||||
|
|
||||||
public static void main(String[] args) {}
|
public static void main(String[] args) {}
|
||||||
|
|
||||||
|
public Matrix(String file) {
|
||||||
|
readCSV(file);
|
||||||
|
}
|
||||||
|
|
||||||
public Matrix(int rowLength, int columnLength) {
|
public Matrix(int rowLength, int columnLength) {
|
||||||
matrix = new int[rowLength][columnLength];
|
matrix = new int[rowLength][columnLength];
|
||||||
|
this.rowLength = rowLength;
|
||||||
|
this.columnLength = columnLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Matrix(String file) {
|
public Matrix(int[][] matrix) {
|
||||||
matrix = this.readCSV(file);
|
this.matrix = matrix;
|
||||||
|
rowLength = matrix.length;
|
||||||
|
columnLength = rowLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Matrix multiply( Matrix m) {
|
public Matrix multiply(Matrix m) {
|
||||||
Matrix scalarProduct = null;
|
Matrix scalarProduct = new Matrix(new int[rowLength][m.columnLength]);
|
||||||
|
|
||||||
if(this.columnLength() != m.rowLength()) {
|
for(int columnIndex = 0; columnIndex < this.getColumnLength(); columnIndex++) {
|
||||||
return scalarProduct;
|
for(int rowIndex = 0; rowIndex < m.getRowLength(); rowIndex++) {
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
int sum = 0;
|
||||||
for(int k=0; k < this.rowLength(); k++) {
|
for(int k=0; k < this.getRowLength(); k++) {
|
||||||
sum += this.getValue(columnIndex, k) * m.getValue(k, rowIndex);
|
sum += this.getValueAt(rowIndex, k) * m.getValueAt(k, columnIndex);
|
||||||
}
|
}
|
||||||
scalarProduct.insert(columnIndex, rowIndex, sum);
|
scalarProduct.insert(rowIndex, columnIndex, sum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return scalarProduct;
|
return scalarProduct;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int rowLength() {
|
public int getRowLength() {
|
||||||
return matrix.length;
|
return rowLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int columnLength() {
|
public int getColumnLength() {
|
||||||
return matrix[0].length;
|
return columnLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getValue(int columnIndex, int rowIndex) {
|
public int getValueAt(int rowIndex, int columnIndex) {
|
||||||
return matrix[columnIndex][rowIndex];
|
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;
|
matrix[rowIndex][columnIndex] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void print() {
|
public void readCSV(String file){
|
||||||
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))) {
|
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||||
String line = br.readLine();
|
String line = br.readLine();
|
||||||
int rowCount = line.trim().split(";").length;
|
rowLength = line.trim().split(";").length;
|
||||||
int columnCount = rowCount;
|
columnLength = rowLength;
|
||||||
String[] lineArray = null;
|
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()) {
|
for(int columnIndex = 0; line != null && columnIndex < matrix.length; columnIndex++, line = br.readLine()) {
|
||||||
lineArray = line.trim().split(";");
|
lineArray = line.split(";");
|
||||||
for(int rowIndex=0; rowIndex < intMatrix[columnIndex].length; rowIndex++) {
|
for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) {
|
||||||
intMatrix[columnIndex][rowIndex] = Integer.parseInt(lineArray[rowIndex]);
|
matrix[rowIndex][columnIndex] = Integer.parseInt(lineArray[rowIndex]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
br.close();
|
br.close();
|
||||||
@ -85,7 +78,16 @@ public class Matrix {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
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
12
src/TestGraph.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,18 @@
|
|||||||
public class TestMatrix {
|
public class TestMatrix {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
test1("");
|
test1("/home/old/projects/Java/graphprogram/24n_01.csv");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void test1(String file) {
|
public static void test1(String file) {
|
||||||
Matrix matrix = new Matrix(file);
|
Matrix matrix = new Matrix(file);
|
||||||
Matrix scalarProduct;
|
Matrix scalarProduct;
|
||||||
|
|
||||||
matrix.print();
|
System.out.println(matrix.getRowLength());
|
||||||
System.out.println();
|
System.out.println(matrix.getColumnLength());
|
||||||
|
|
||||||
scalarProduct = matrix.multiply( matrix);
|
System.out.println(matrix);
|
||||||
scalarProduct.print();
|
|
||||||
|
|
||||||
|
scalarProduct = matrix.multiply(matrix);
|
||||||
|
System.out.println(scalarProduct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user