aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2025-05-01 14:01:33 +0200
committerJonas Smedegaard <dr@jones.dk>2025-05-01 14:33:37 +0200
commit89a5acad8deef14be01a5ccfba3d036d94e8a6d4 (patch)
treeda9103cfc5ecade6d9bcafd6287662cb4b5f1597
parentd9104886d48c8a176997519dc6d6427d7a58795b (diff)
use long[] for solution (not int)
-rw-r--r--src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java28
1 files changed, 16 insertions, 12 deletions
diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java
index cebdb0e..2d36d14 100644
--- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java
+++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java
@@ -249,34 +249,38 @@ public final class Graph extends Storage {
///
/// @param g sets of disjoint choices as a graph
/// @return best path among many random ones
- private static int solveManyTimes(final AbstractGraph g) {
+ private static long[] solveManyTimes(final AbstractGraph g) {
// number higher than total students
- int bestPathCost = Integer.MAX_VALUE;
- for (int i = 0; i < DEMO_ITERATIONS; i++) {
- int cost = solve(g);
- if (cost < bestPathCost) {
+ long[] bestSolution = solve(g);
+ for (int i = 1; i < DEMO_ITERATIONS; i++) {
+ long[] solution = solve(g);
+ if (solution[0] < bestSolution[0]) {
// store shortest path
- bestPathCost = cost;
+ bestSolution = solution;
}
}
- return bestPathCost;
+ return bestSolution;
}
/// find total weight of random path through disjoint choice sets
///
/// @param g sets of disjoint choices as a graph
/// @return total weight of random path
- private static int solve(final AbstractGraph g) {
+ private static long[] solve(final AbstractGraph g) {
+
+ /// parameters for solution
+ long[] solution = new long[] {0};
+
List<Vertex> path = new ArrayList<>(g.vertices());
// order of list contents becomes path order
Collections.shuffle(path);
- int cost = 0;
for (int i = 0; i < path.size() - 1; i++) {
- cost += g.getWeight(path.get(i), path.get(i + 1));
+ solution[0] += g.getWeight(
+ path.get(i), path.get(i + 1));
}
- return cost;
+ return solution;
}
/// Demo function to solve assignment
@@ -317,6 +321,6 @@ public final class Graph extends Storage {
System.out.print(
"\n\nSolution with disjoint choices found: ");
System.out.println(
- solveManyTimes(h));
+ solveManyTimes(h)[0]);
}
}