HungarianAlgorithm constructor

HungarianAlgorithm(
  1. List<List<int>> costMatrix
)

Constructor for the HungarianAlgorithm. Takes the original cost matrix as input. Note: The input costMatrix here is expected to be actual costs (positive values). The algorithm internally converts it to a profit matrix.

Implementation

HungarianAlgorithm(List<List<int>> costMatrix) {
  _n = costMatrix.length;

  // Deep copy the cost matrix to avoid modifying the original input
  _cost = List.generate(_n, (i) => List<int>.from(costMatrix[i]));

  // Convert cost matrix to profit matrix by multiplying each element by -1.
  // This allows finding minimum cost by finding maximum profit.
  for (int i = 0; i < _n; i++) {
    for (int j = 0; j < _n; j++) {
      _cost[i][j] *= -1;
    }
  }

  // Initialize all lists with default values
  _xy = List.filled(_n, -1);
  _yx = List.filled(_n, -1);
  _lx = List.filled(_n, 0);
  _ly = List.filled(_n, 0);
  _slack = List.filled(_n, 0);
  _slackX = List.filled(_n, 0);
  _prev = List.filled(_n, 0);
  _inTreeX = List.filled(_n, false);
  _inTreeY = List.filled(_n, false);
}