-
- // Add running sums to queues
- for (int i = 0; i < running_row_sums.length; i++)
- row_sums.add(new Tuple(i, running_row_sums[i]));
-
- for (int i = 0; i < running_col_sums.length; i++)
- col_sums.add(new Tuple(i, running_col_sums[i]));
-
-
- // Select smallest sum (in rows/cols)
- // Get 1-indices for that sum's index
- // Select smallest from those indices
- while (!row_sums.isEmpty() && !col_sums.isEmpty()){
- Tuple lowestIndexRow = row_sums.peek();
- Tuple lowestIndexCol = col_sums.peek();
-
- boolean useRow = false;
-
- if (lowestIndexRow.v < lowestIndexCol.v)
- useRow = true;
-
- // Get 1-indices
- if (useRow)
- {
- row_sums.remove();
-
- // Get smallest
- int smallestValue = Integer.MAX_VALUE;
- Tuple smallestKey = null;
-
- for (Tuple e : col_sums)
- {
- if (e.v < smallestValue && compatibility[lowestIndexRow.k][e.k] == 1)
- {
- smallestKey = e;
- smallestValue = e.v;
+ for (int i = 0; i < m; i++) {
+ cols.add(new SimpleEntry<Integer, Integer>(i, col_sums[i]));
+ }
+ //Sort the lists so the rows and columns with the least amount of positive compatibilities
+ Comparator<SimpleEntry<Integer, Integer>> comp = new Comparator<SimpleEntry<Integer, Integer>>() {
+ @Override
+ public int compare(SimpleEntry<Integer, Integer> a, SimpleEntry<Integer, Integer> b) {
+ return a.getValue().compareTo(b.getValue());
+ }
+ };
+ Collections.sort(rows, comp);
+ Collections.sort(cols, comp);
+
+ /* 1. Find the row or column with the lowest sum(least available matches)
+ * Because the rows and columns are sorted on value the first item is the lowest
+ * 2. Find the match with the lowest sum and still positive compatibility for that row or column
+ * 3. Select smallest from those indices
+ */
+ SimpleEntry<Integer, Integer> MINIMUM = new SimpleEntry<Integer, Integer>(-1, Integer.MAX_VALUE);
+ while (!(rows.isEmpty() || cols.isEmpty())) {
+ //1. Find the row or column with the lowest sum(least available matches)
+ SimpleEntry<Integer, Integer> row_l = rows.peekFirst();
+ SimpleEntry<Integer, Integer> col_l = cols.peekFirst();
+
+ //2. The row is the lowest
+ if (row_l.getValue() < col_l.getValue()) {
+ //Remove the current lowest row sum
+ rows.remove(row_l);
+ //3. Find smallest column that has compatibility 1
+ SimpleEntry<Integer, Integer> smallest = MINIMUM;
+ for (SimpleEntry<Integer, Integer> e : cols) {
+ if (e.getValue() < smallest.getValue() && compatibility[row_l.getKey()][e.getKey()] == 1) {
+ smallest = e;