diff options
author | Ian Valentin Christensen <valentianchristensen@gmail.com> | 2025-04-30 22:59:27 +0200 |
---|---|---|
committer | Ian Valentin Christensen <valentianchristensen@gmail.com> | 2025-04-30 22:59:46 +0200 |
commit | 2aef5be440dc9ecb1a84c6c4da250ae3b8db0356 (patch) | |
tree | cbd76a4317b3d1e8520ee5e2f94237732507304d | |
parent | 95277e397394523f032c9a93db4829a43c347e00 (diff) |
refactor constructor in graph to happen in called demo function
-rw-r--r-- | src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java | 136 |
1 files changed, 70 insertions, 66 deletions
diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java index d052258..e6b582c 100644 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java @@ -32,71 +32,8 @@ public final class Graph { private AbstractGraph g; /// Default constructor - /// - /// @param path path to data file - private Graph(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); - - System.out.println("\n\nGraph of choices constructed:"); - GraphAlgorithms.printGraph(g); - - // ensure the graph is connected - assertConnected(g); - System.out.println( - "\n\nGraph is connected" - + " (otherwise an exception was thrown)"); - - // collect disjoint choice sets - ArrayList<Set<Vertex>> s = disjoint(g); - System.out.printf( - "\n\n%d disjoint choice sets collected:\n", - s.size()); - for (Set<Vertex> verticeSet: s) { - System.out.print( - "set(" + verticeSet.size() + "):"); - for (Vertex v: GraphAlgorithms.sortVertex( - verticeSet) - ) { - System.out.print(" " + v.toString()); - } - System.out.println(); - } - - // construct graph of disjoint choices - AbstractGraph h = moduleGroups(s, g); - System.out.println( - "\n\nGraph of disjoint choices constructed:"); - GraphAlgorithms.printGraph(h); - - // find path through disjoint choice graph - System.out.print( - "\n\nSolution with disjoint choices found: "); - System.out.println( - goodSolution(h)); - - // release temporary variables for garbage collection - _g = null; - _vertice_count = null; - } + // to silence javadoc + private Graph() {} /// JVM entry point /// @@ -109,7 +46,7 @@ public final class Graph { ? args[0] : "combi.txt"; - Graph combi = new Graph(path); + demo(path); } /// utility function to check that a graph is connected @@ -309,4 +246,71 @@ public final class Graph { } return cost; } + + /// 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); + + System.out.println("\n\nGraph of choices constructed:"); + GraphAlgorithms.printGraph(g); + + // ensure the graph is connected + assertConnected(g); + System.out.println( + "\n\nGraph is connected" + + " (otherwise an exception was thrown)"); + + // collect disjoint choice sets + ArrayList<Set<Vertex>> s = disjoint(g); + System.out.printf( + "\n\n%d disjoint choice sets collected:\n", + s.size()); + for (Set<Vertex> verticeSet: s) { + System.out.print( + "set(" + verticeSet.size() + "):"); + for (Vertex v: GraphAlgorithms.sortVertex( + verticeSet) + ) { + System.out.print(" " + v.toString()); + } + System.out.println(); + } + + // construct graph of disjoint choices + AbstractGraph h = moduleGroups(s, g); + System.out.println( + "\n\nGraph of disjoint choices constructed:"); + GraphAlgorithms.printGraph(h); + + // find path through disjoint choice graph + System.out.print( + "\n\nSolution with disjoint choices found: "); + System.out.println( + goodSolution(h)); + + // release temporary variables for garbage collection + _g = null; + _vertice_count = null; + } } |