aboutsummaryrefslogtreecommitdiff
path: root/dk/abcdefghijklmnopqrstuvxyzæøå/bachelorizer/model/Combi.java
blob: 0fded8ae233fc30cb3d0240dc2d0a06ddc202658 (plain)
  1. // SPDX-FileCopyrightText: 2025 Jonas Smedegaard <dr@jones.dk>
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. package dk.abcdefghijklmnopqrstuvxyzæøå.bachelorizer.model;
  4. import java.util.Collection;
  5. import java.util.Set;
  6. import com.example.portfolio3.AdjListGraph;
  7. import com.example.portfolio3.Graph;
  8. import com.example.portfolio3.GraphAlgorithms;
  9. import com.example.portfolio3.MatrixGraph;
  10. import com.example.portfolio3.Vertex;
  11. /// Combi - static sample dataset of course combinations
  12. ///
  13. /// Slurps and parses data from upstream-provided comma-separated file.
  14. ///
  15. /// @version 0.0.1
  16. /// @see <https://moodle.ruc.dk/mod/assign/view.php?id=523186>
  17. public final class Combi {
  18. /// data about combinations as a Graph
  19. private Graph g;
  20. /// Default constructor
  21. ///
  22. /// @param path path to data file
  23. private Combi(final String path) {
  24. // read into temporary graph to resolve vertice count
  25. //
  26. // use Adjacency List Representation:
  27. // * cheap to bootstrap (done once)
  28. // * simple to count vertices (done once): O(1)
  29. Graph _g = new AdjListGraph();
  30. GraphAlgorithms.readGraph(_g, path);
  31. Integer _vertice_count = _g.vertices().size();
  32. // read into final graph
  33. //
  34. // use Adjacency Matrix Representation:
  35. // * expensive to bootstrap (done once)
  36. // * simple to add edges but needs vertice count
  37. // * simple to get edges (done repeatedly): O(1)
  38. //
  39. // TODO: avoid reading and parsing file twice
  40. Graph g = new MatrixGraph(_vertice_count);
  41. GraphAlgorithms.readGraph(g, path);
  42. // ensure the graph is connected
  43. isConnected(g);
  44. GraphAlgorithms.printGraph(g);
  45. // release temporary variables for garbage collection
  46. _g = null;
  47. _vertice_count = null;
  48. }
  49. /// JVM entry point
  50. ///
  51. /// @param args command-line arguments
  52. public static void main(final String[] args) {
  53. // first argument, if provided, is the data file path;
  54. // else use upstream named file in current directory.
  55. String path = (args.length > 0)
  56. ? args[0]
  57. : "combi.txt";
  58. Combi combi = new Combi(path);
  59. }
  60. /// utility function to check that a graph is connected
  61. ///
  62. /// If check fails, throw an unchecked exception,
  63. /// since it occurs at runtime and is unrecoverable.
  64. ///
  65. /// Time complexity of the operation is O(n²)
  66. /// where n is the amount of vertices,
  67. /// since visitDepthFirst() recurses out-edges of all vertices.
  68. ///
  69. /// @param g Graph to inspect
  70. /// @throws IllegalArgumentException
  71. /// https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
  72. public static void isConnected(Graph g) {
  73. // collect all vertices in the graph
  74. Collection<Vertex> c = g.vertices();
  75. // pick a random vertice in the graph
  76. Vertex v = g.vertices().iterator().next();
  77. // collect the set of visitable vertices
  78. Set<Vertex> visited = GraphAlgorithms.visitDepthFirst(
  79. g, v);
  80. // throw exception if not all vertices were visited
  81. if (visited.size() != c.size()) {
  82. throw new IllegalArgumentException(
  83. "graph is not connected");
  84. }
  85. }
  86. }