Skip to content

Commit 3df0e84

Browse files
committed
fix: ensure nbRows <= nbColumns
1 parent 0dd6f27 commit 3df0e84

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/__tests__/test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ describe('linear sum problem', () => {
2727
const { columnAssignments, rowAssignments } = linearSumAssignment(diff, {
2828
maximaze: false,
2929
});
30-
expect(columnAssignments).toMatchCloseTo([2, 0, 1, 3, 4]);
31-
expect(rowAssignments).toMatchCloseTo([1, 2, 0, 3, 4, -1]);
30+
expect(columnAssignments).toMatchCloseTo([1, 2, 0, 3, 4, -1]);
31+
expect(rowAssignments).toMatchCloseTo([2, 0, 1, 3, 4]);
3232
});
3333
it('differents size: rows < columns', () => {
3434
const a = [3.1, 1.1, 1.9, 3.99, 5.2];
@@ -42,4 +42,16 @@ describe('linear sum problem', () => {
4242
expect(columnAssignments).toMatchCloseTo([1, 2, 0, 3, 4, -1]);
4343
expect(rowAssignments).toMatchCloseTo([2, 0, 1, 3, 4]);
4444
});
45+
it('before failing case', () => {
46+
const diff = [
47+
[0.10000000000000009, 0.5, 0.6000000000000001, 1.1],
48+
[0.3999999999999999, 0, 0.10000000000000009, 0.6000000000000001],
49+
[0.8999999999999999, 0.5, 0.3999999999999999, 0.10000000000000009],
50+
];
51+
const { columnAssignments, rowAssignments } = linearSumAssignment(diff, {
52+
maximaze: false,
53+
});
54+
expect(columnAssignments).toMatchCloseTo([0, 1, 2, -1]);
55+
expect(rowAssignments).toMatchCloseTo([0, 1, 2]);
56+
});
4557
});

src/getShortestPath.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export function getShortestPath(options: GetShortestPathOptions) {
3535
let delta = 0;
3636
let curColumn = currUnAssCol;
3737
let shortestPathCost = getArrayOfInfinity(nbRows);
38-
3938
while (sink === -1) {
4039
scannedColumns[curColumn] = 1;
4140
let minVal = Number.POSITIVE_INFINITY;

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ interface Options {
88
}
99

1010
export default function linearSumAssignment(
11-
input: DoubleArray[],
11+
input: DoubleArray[] | Matrix,
1212
options: Options = {},
1313
) {
1414
const { maximaze = true } = options;
1515

1616
let matrix = Matrix.checkMatrix(input);
1717

1818
let didFlip = false;
19-
if (matrix.columns > matrix.rows) {
19+
if (matrix.columns < matrix.rows) {
2020
didFlip = true;
2121
matrix = matrix.transpose();
2222
}
@@ -42,20 +42,20 @@ export default function linearSumAssignment(
4242
rowAssignments,
4343
columnAssignments,
4444
});
45-
4645
let { sink, pred } = currentAugmenting;
4746

4847
if (sink === -1) {
4948
return {
50-
rowAssignments: [],
51-
columnAssignments: [],
49+
rowAssignments,
50+
columnAssignments,
5251
gain: -1,
52+
dualVariableForColumns,
53+
dualVariableForRows,
5354
};
5455
}
5556

5657
dualVariableForColumns = currentAugmenting.dualVariableForColumns;
5758
dualVariableForRows = currentAugmenting.dualVariableForRows;
58-
5959
let j = sink;
6060
for (let i = pred[j]; true; i = pred[j]) {
6161
rowAssignments[j] = i;

0 commit comments

Comments
 (0)