From 4aae69d885e99867af822a22202435954b5cba93 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Thu, 1 May 2025 11:42:14 +0200 Subject: make class Graph a subclass of Storage --- .../dk/biks/bachelorizer/Graph.java | 74 ++++++++++++---------- 1 file changed, 42 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java index 54dbd5d..5131b83 100644 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java @@ -26,11 +26,14 @@ import com.example.portfolio3.Vertex; /// /// @version 0.0.1 /// @see -public final class Graph { +public final class Graph extends Storage { /// amount of iterations in demo private static final int DEMO_ITERATIONS = 1000000; + /// path to source file for graph + private String path = "combi.txt"; + /// data about combinations as a Graph private AbstractGraph g; @@ -38,18 +41,51 @@ public final class Graph { // to silence javadoc private Graph() {} + /// constructor with custom file source + /// + /// @param path path to source file for graph + private Graph(String path) { + this.path = path; + } + /// JVM entry point /// /// @param args command-line arguments public static void main(final String[] args) { + Graph g; // first argument, if provided, is the data file path; // else use upstream named file in current directory. - String path = (args.length > 0) - ? args[0] - : "combi.txt"; + g = (args.length > 0) + ? new Graph(args[0]) + : new Graph(); + + g.initialize(); + g.demo(); + } - demo(path); + /// load graph from file + void initialize() { + + // read into temporary graph to resolve vertice count + // + // use Adjacency List Representation: + // * cheap to bootstrap (done once) + // * simple to count vertices (done once): O(1) + AbstractGraph _g = new AdjListGraph(); + GraphAlgorithms.readGraph(_g, path); + Integer _vertice_count = _g.vertices().size(); + + // read into final graph + // + // use Adjacency Matrix Representation: + // * expensive to bootstrap (done once) + // * simple to add edges but needs vertice count + // * simple to get edges (done repeatedly): O(1) + // + // TODO: avoid reading and parsing file twice + g = new MatrixGraph(_vertice_count); + GraphAlgorithms.readGraph(g, path); } /// utility function to check that a graph is connected @@ -252,29 +288,7 @@ public final class Graph { } /// Demo function to solve assignment - /// - /// @param path graph from combi.txt - public static void demo(final String path) { - - // read into temporary graph to resolve vertice count - // - // use Adjacency List Representation: - // * cheap to bootstrap (done once) - // * simple to count vertices (done once): O(1) - AbstractGraph _g = new AdjListGraph(); - GraphAlgorithms.readGraph(_g, path); - Integer _vertice_count = _g.vertices().size(); - - // read into final graph - // - // use Adjacency Matrix Representation: - // * expensive to bootstrap (done once) - // * simple to add edges but needs vertice count - // * simple to get edges (done repeatedly): O(1) - // - // TODO: avoid reading and parsing file twice - AbstractGraph g = new MatrixGraph(_vertice_count); - GraphAlgorithms.readGraph(g, path); + public void demo() { System.out.println("\n\nGraph of choices constructed:"); GraphAlgorithms.printGraph(g); @@ -312,9 +326,5 @@ public final class Graph { "\n\nSolution with disjoint choices found: "); System.out.println( goodSolution(h)); - - // release temporary variables for garbage collection - _g = null; - _vertice_count = null; } } -- cgit v1.2.3