From 89a5acad8deef14be01a5ccfba3d036d94e8a6d4 Mon Sep 17 00:00:00 2001
From: Jonas Smedegaard <dr@jones.dk>
Date: Thu, 1 May 2025 14:01:33 +0200
Subject: use long[] for solution (not int)

---
 .../dk/biks/bachelorizer/Graph.java                | 28 ++++++++++++----------
 1 file changed, 16 insertions(+), 12 deletions(-)

(limited to 'src/dk.biks.bachelorizer')

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]);
 	}
 }
-- 
cgit v1.2.3