aboutsummaryrefslogtreecommitdiff
path: root/src/dk.biks.bachelorizer/dk/biks
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2025-05-01 11:42:14 +0200
committerJonas Smedegaard <dr@jones.dk>2025-05-01 12:08:53 +0200
commit4aae69d885e99867af822a22202435954b5cba93 (patch)
tree94e634f24605eedb6a472a0aa705371e0655965d /src/dk.biks.bachelorizer/dk/biks
parentaf21afd482a358ff206872de3e5ce3d8ab249c05 (diff)
make class Graph a subclass of Storage
Diffstat (limited to 'src/dk.biks.bachelorizer/dk/biks')
-rw-r--r--src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java74
1 files changed, 42 insertions, 32 deletions
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 <https://moodle.ruc.dk/mod/assign/view.php?id=523186>
-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;
}
}