minimum requirements fullfilled (#4)
* 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 * Done, don't ask how (#2) * progress * about 50% done * In the middle of something * finished implementing findBridges method Still very much stuck at figuring out how to find all arcticualtions in a graph * unimportant changes * switched to ArrayLists The methods findComponents, findBridges and findArticulations are broken. The main problem lies with findComponents. * removed file paths * findComponents still broken I only have to figure out how to get rid of duplicate components * optimized calculateProperties centre is also now an ArrayList * made findComponents work It took way too long, now onto fixing bridges and articulations * heavy refactoring and findArticulations now works * shouldn't be here * minor refactoring findBridges still doesn't work * finished findBridges
This commit is contained in:
parent
8c31d48b2e
commit
f980094561
281
src/Graph.java
Normal file
281
src/Graph.java
Normal file
@ -0,0 +1,281 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Graph {
|
||||||
|
private Matrix adjazenzMatrix;
|
||||||
|
private Matrix distanzMatrix;
|
||||||
|
private Matrix wegMatrix;
|
||||||
|
private boolean connected;
|
||||||
|
private ArrayList<Integer> exzentrizitäten;
|
||||||
|
private int radius;
|
||||||
|
private int diameter;
|
||||||
|
private ArrayList<Integer> centre;
|
||||||
|
private ArrayList<ArrayList<Integer>> components;
|
||||||
|
private ArrayList<int[]> bridges;
|
||||||
|
private ArrayList<Integer> articulations;
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {}
|
||||||
|
|
||||||
|
public Graph(String file) {
|
||||||
|
adjazenzMatrix = new Matrix(file);
|
||||||
|
calculateDistanzMatrix();
|
||||||
|
calculateWegMatrix();
|
||||||
|
calculateExzentrizitäten();
|
||||||
|
calculateProperties();
|
||||||
|
findComponents();
|
||||||
|
findBridges(file);
|
||||||
|
findArticulations(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 ArrayList<>(1);
|
||||||
|
|
||||||
|
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.add(exzentrizität);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void calculateProperties() {
|
||||||
|
radius = Integer.MAX_VALUE;
|
||||||
|
diameter = -1;
|
||||||
|
connected = true;
|
||||||
|
centre = new ArrayList<>(1);
|
||||||
|
|
||||||
|
for(int rowIndex = 0; rowIndex < exzentrizitäten.size(); 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) == diameter) {
|
||||||
|
centre.add(rowIndex + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(radius == 0) {
|
||||||
|
connected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findComponents() {
|
||||||
|
components = new ArrayList<>(1);
|
||||||
|
ArrayList<Integer> 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) {
|
||||||
|
component.add(columnIndex + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!components.contains(component)) {
|
||||||
|
components.add(component);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<ArrayList<Integer>> findComponents(Matrix matrix) {
|
||||||
|
ArrayList<ArrayList<Integer>> newComponents = new ArrayList<>(1);
|
||||||
|
ArrayList<Integer> component = new ArrayList<>(1);
|
||||||
|
|
||||||
|
for(int rowIndex = 0; rowIndex < matrix.getRowLength(); rowIndex++) {
|
||||||
|
component = new ArrayList<>(1);
|
||||||
|
|
||||||
|
for(int columnIndex = 0; columnIndex < wegMatrix.getColumnLength(); columnIndex++) {
|
||||||
|
if(wegMatrix.getValueAt(rowIndex, columnIndex) == 1) {
|
||||||
|
component.add(columnIndex + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!newComponents.contains(component)) {
|
||||||
|
newComponents.add(component);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newComponents;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findBridges(String file) {
|
||||||
|
bridges = new ArrayList<>(1);
|
||||||
|
ArrayList<ArrayList<Integer>> 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];
|
||||||
|
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);
|
||||||
|
|
||||||
|
calculateWegMatrix();
|
||||||
|
|
||||||
|
newComponents = findComponents(wegMatrix);
|
||||||
|
|
||||||
|
contains = false;
|
||||||
|
for (int[] array : bridges) {
|
||||||
|
if(Arrays.equals(array, bridge)) {
|
||||||
|
contains = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(newComponents.size() > components.size() && !contains) {
|
||||||
|
bridges.add(bridge);
|
||||||
|
}
|
||||||
|
|
||||||
|
adjazenzMatrix = new Matrix(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
calculateWegMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findArticulations(String file) {
|
||||||
|
articulations = new ArrayList<>(1);
|
||||||
|
ArrayList<ArrayList<Integer>> newComponents;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
calculateWegMatrix();
|
||||||
|
|
||||||
|
newComponents = findComponents(wegMatrix);
|
||||||
|
|
||||||
|
if(newComponents.size() > components.size() + 1) {
|
||||||
|
articulations.add(i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
adjazenzMatrix = new Matrix(file);
|
||||||
|
}
|
||||||
|
calculateWegMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
String s = "";
|
||||||
|
|
||||||
|
s += "Adjazenzmatrix:\n" + adjazenzMatrix + "\nDistanzmatrix:\n" + distanzMatrix + "\nWegmatrix:\n" + wegMatrix;
|
||||||
|
|
||||||
|
if(!connected) {
|
||||||
|
s += "\nExzentrizitäten/Radius/Durchmesser: Kein zusammenhängender Graph";
|
||||||
|
} else {
|
||||||
|
s += "\nExzentrizitäten: " + exzentrizitäten.toString() + "\nRadius: " + radius + "\nDurchmesser: " + diameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
s += "\nZentrum: ";
|
||||||
|
if(!connected) {
|
||||||
|
s += "Kein zusammenhängender Graph";
|
||||||
|
} else {
|
||||||
|
s += centre;
|
||||||
|
}
|
||||||
|
|
||||||
|
s += "\nKomponente: {";
|
||||||
|
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.size(); rowIndex++) {
|
||||||
|
s += Arrays.toString(bridges.get(rowIndex));
|
||||||
|
if(rowIndex < bridges.size() - 1) {
|
||||||
|
if(rowIndex < bridges.size() - 1) {
|
||||||
|
s += ",";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s += "}";
|
||||||
|
|
||||||
|
s += "\nArtikulationen: " + articulations.toString();
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
92
src/Matrix.java
Normal file
92
src/Matrix.java
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
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(int[][] matrix) {
|
||||||
|
this.matrix = matrix;
|
||||||
|
rowLength = matrix.length;
|
||||||
|
columnLength = matrix[0].length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Matrix multiply(Matrix m) {
|
||||||
|
Matrix scalarProduct = new Matrix(new int[rowLength][m.columnLength]);
|
||||||
|
|
||||||
|
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.getRowLength(); k++) {
|
||||||
|
sum += this.getValueAt(rowIndex, k) * m.getValueAt(k, columnIndex);
|
||||||
|
}
|
||||||
|
scalarProduct.insert(rowIndex, columnIndex, sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return scalarProduct;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRowLength() {
|
||||||
|
return rowLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getColumnLength() {
|
||||||
|
return columnLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValueAt(int rowIndex, int columnIndex) {
|
||||||
|
return matrix[rowIndex][columnIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insert(int rowIndex, int columnIndex, int value) {
|
||||||
|
matrix[rowIndex][columnIndex] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readCSV(String file){
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||||
|
String line = br.readLine();
|
||||||
|
rowLength = line.trim().split(";").length;
|
||||||
|
columnLength = rowLength;
|
||||||
|
String[] lineArray = null;
|
||||||
|
|
||||||
|
matrix = new int[rowLength][columnLength];
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
6
src/TestGraph.java
Normal file
6
src/TestGraph.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public class TestGraph {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Graph g = new Graph("");
|
||||||
|
System.out.println(g);
|
||||||
|
}
|
||||||
|
}
|
14
src/TestMatrix.java
Normal file
14
src/TestMatrix.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
public class TestMatrix {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Matrix matrix = new Matrix("");
|
||||||
|
Matrix scalarProduct;
|
||||||
|
|
||||||
|
System.out.println("RowLength: " + matrix.getRowLength());
|
||||||
|
System.out.println("ColumnLength: " +matrix.getColumnLength());
|
||||||
|
|
||||||
|
System.out.println("\nMatrix A: \n" + matrix);
|
||||||
|
|
||||||
|
scalarProduct = matrix.multiply(matrix);
|
||||||
|
System.out.println("\nScalarProduct A²: \n" + scalarProduct);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user