From 3b4a6f74b678b6e0e45085f50fff117ae795146e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Fri, 26 May 2023 12:39:54 +0200 Subject: [PATCH] can now create random graphs --- src/Graph.java | 2 +- src/Matrix.java | 24 ++++++++++++++++++++++++ src/TestGraph.java | 19 ++++++++++++++----- src/TestMatrix.java | 14 ++++++++++---- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/Graph.java b/src/Graph.java index fef94a6..e71ff6d 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -86,6 +86,7 @@ public class Graph { public void calculateExzentrizitäten() { exzentrizitäten = new ArrayList<>(1); + connected = true; for(int rowIndex = 0; rowIndex < distanzMatrix.getRowLength(); rowIndex++) { int exzentrizität = 0; @@ -110,7 +111,6 @@ public class Graph { radius = Integer.MAX_VALUE; diameter = -1; - connected = true; centre = new ArrayList<>(1); for(int rowIndex = 0; rowIndex < exzentrizitäten.size(); rowIndex++) { diff --git a/src/Matrix.java b/src/Matrix.java index c79aed9..d891d09 100644 --- a/src/Matrix.java +++ b/src/Matrix.java @@ -3,6 +3,8 @@ import java.io.IOException; import java.util.Random; import java.io.FileReader; import java.io.FileNotFoundException; +import java.io.BufferedWriter; +import java.io.FileWriter; public class Matrix { private int[][] matrix; @@ -13,6 +15,9 @@ public class Matrix { public Matrix(int rowLength, int columnLength, boolean random) { if(!random) { + matrix = new int[rowLength][columnLength]; + this.rowLength = rowLength; + this.columnLength = columnLength; return; } matrix = new int[rowLength][columnLength]; @@ -114,6 +119,25 @@ public class Matrix { } } + public void WriteToCsv(String filename) { + String s = ""; + + try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) { + for(int columnIndex=0; columnIndex < columnLength; columnIndex++) { + s = ""; + for(int rowIndex=0; rowIndex < rowLength; rowIndex++) { + s += matrix[columnIndex][rowIndex]; + if(rowIndex < rowLength - 1) { + s += ";"; + } + } + bw.write(s + "\n"); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + public String toString() { String s = ""; for(int columnIndex=0; columnIndex < columnLength; columnIndex++) { diff --git a/src/TestGraph.java b/src/TestGraph.java index d05dbf0..8d295b4 100644 --- a/src/TestGraph.java +++ b/src/TestGraph.java @@ -1,12 +1,13 @@ public class TestGraph { public static void main(String[] args) { - test1(); - test2(); - test3(); - test4(); + // test1(); + // test2(); + // test3(); + // test4(); + test5(); } - static String pathToProgramRoot = ""; + static String pathToProgramRoot = "/home/rene/projects/Java/graphprogram"; /* @@ -41,4 +42,12 @@ public class TestGraph { Graph g = new Graph(pathToProgramRoot + "/csv/art-brck.csv"); System.out.println(g); } + + /* + * randomly generated graph with x nodes + */ + public static void test5() { + Graph g = new Graph(pathToProgramRoot + "/csv/50n.csv"); + System.out.println(g); + } } \ No newline at end of file diff --git a/src/TestMatrix.java b/src/TestMatrix.java index 210da69..c714b6d 100644 --- a/src/TestMatrix.java +++ b/src/TestMatrix.java @@ -1,11 +1,12 @@ public class TestMatrix { public static void main(String[] args) { - test1(); - test2(); + // test1(); + // test2(); + createCsv(); } public static void test1() { - String pathToProgramRoot = ""; + String pathToProgramRoot = "/home/rene/projects/grpahprogram"; Matrix matrix = new Matrix(pathToProgramRoot + "/csv/graph.csv"); Matrix scalarProduct; @@ -22,5 +23,10 @@ public class TestMatrix { public static void test2() { Matrix bruh = new Matrix(10, 10, true); System.out.println(bruh); - } + } + + public static void createCsv() { + Matrix bruh = new Matrix(50, 50, true); + bruh.WriteToCsv("csv/50n.csv"); + } }