Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 匈牙利算法:如何用最小行覆盖0个元素?_Java_Algorithm_Logic_Hungarian Algorithm - Fatal编程技术网

Java 匈牙利算法:如何用最小行覆盖0个元素?

Java 匈牙利算法:如何用最小行覆盖0个元素?,java,algorithm,logic,hungarian-algorithm,Java,Algorithm,Logic,Hungarian Algorithm,我试图用Java实现匈牙利算法。我有一个NxN成本矩阵。我正在一步一步地遵循指南。所以我有costMatrix[N][N]和2个数组来跟踪覆盖行和覆盖列-rowcolve[N],rowColumn[N](1表示覆盖,0表示未覆盖) 如何用最少的行数覆盖0?谁能给我指出正确的方向吗 如果您有任何帮助/建议,我们将不胜感激。请检查中算法的第3步,它们解释了计算覆盖所有0的最小行数的方法 更新:以下是获取覆盖0的最小行数的另一种方法: import java.util.ArrayList; 导入jav

我试图用Java实现匈牙利算法。我有一个NxN成本矩阵。我正在一步一步地遵循指南。所以我有costMatrix[N][N]和2个数组来跟踪覆盖行和覆盖列-rowcolve[N],rowColumn[N](1表示覆盖,0表示未覆盖)

如何用最少的行数覆盖0?谁能给我指出正确的方向吗


如果您有任何帮助/建议,我们将不胜感激。

请检查中算法的第3步,它们解释了计算覆盖所有0的最小行数的方法

更新:以下是获取覆盖
0的最小行数的另一种方法:

import java.util.ArrayList;
导入java.util.List;
公共类MinLines{
枚举线型{无,水平,垂直}
私有静态类行{
int-lineIndex;
线型行型;
行(int lineIndex,线型rowType){
this.lineIndex=lineIndex;
this.rowType=rowType;
}      
线型getLineType(){
返回行类型;
}
int getLineIndex(){
返回线索引;
}
布尔水平(){
返回rowType==LineType.HORIZONTAL;
}
}
私有静态布尔值为零(int[]数组){
for(int e:array){
如果(e!=0){
返回false;
}
}
返回true;
}
公共静态列表getMinLines(int[][]矩阵){
if(matrix.length!=矩阵[0].length){
抛出新的IllegalArgumentException(“矩阵应该是正方形的!”);
}
最终整数大小=矩阵长度;
int[]zerosPerRow=新的int[SIZE];
int[]zerosPerCol=新int[SIZE];
//计算每行0的数量和每列0的数量
对于(int i=0;imax | |(zerosPerRow[i]==max&&lastInsertedLineType==LineType.HORIZONTAL)){
lineWithMostZeros=新线(i,线型。水平);
最大值=零箭头[i];
}
}
对于(int i=0;imax | |(zerosPerCol[i]==max&&lastInsertedLineType==LineType.VERTICAL)){
lineWithMostZeros=新线(i,线型垂直);
最大值=zerosPerCol[i];
}
}
//从行中删除0计数
if(lineWithMostZeros.isHorizontal()){
zerosPerRow[lineWithMostZeros.getLineIndex()]=0;
}否则{
zerosPerCol[lineWithMostZeros.getLineIndex()]=0;
}
//一旦找到计数大于0的线(水平或垂直)
//迭代它的元素并从其他行中减去0
//例如:
//0的x列:
//           [ 0  1  2  3 ]  ->  1
//           [ 0  2  0  1 ]  ->  2
//           [ 0  4  3  5 ]  ->  1
//           [ 0  0  0  7 ]  ->  3
//             |  |  |  | 
//v v v v
//0的x行:{4}1 2 0
//[X 1 2 3]->0
//[X 2 0 1]->1
//[X 4 3 5]->0
//[X 0 0 7]->2
//             |  |  |  | 
//v v v v
//            {0} 1  2  0 
int index=lineWithMostZeros.getLineIndex();
if(lineWithMostZeros.isHorizontal()){
对于(int j=0;j/**
     * Step 3.1
     * Loop through all elements, and run colorNeighbors when the element visited is equal to zero
     * */
    public void coverZeros(){
        numLines = 0;
        lines = new int[values.length][values.length];

        for(int row=0; row<values.length;row++){
            for(int col=0; col<values.length;col++){
                if(values[row][col] == 0)
                    colorNeighbors(row, col, maxVH(row, col));
            }
        }
    }

    /**
     * Step 3.2
     * Checks which direction (vertical,horizontal) contains more zeros, every time a zero is found vertically, we increment the result
     * and every time a zero is found horizontally, we decrement the result. At the end, result will be negative, zero or positive
     * @param row Row index for the target cell
     * @param col Column index for the target cell
     * @return Positive integer means that the line passing by indexes [row][col] should be vertical, Zero or Negative means that the line passing by indexes [row][col] should be horizontal
     * */
    private int maxVH(int row, int col){
        int result = 0;
        for(int i=0; i<values.length;i++){
            if(values[i][col] == 0)
                result++;
            if(values[row][i] == 0)
                result--;
        }
        return result;
    }

    /**
     * Step 3.3
     * Color the neighbors of the cell at index [row][col]. To know which direction to draw the lines, we pass maxVH value.
     * @param row Row index for the target cell
     * @param col Column index for the target cell
     * @param maxVH Value return by the maxVH method, positive means the line to draw passing by indexes [row][col] is vertical, negative or zero means the line to draw passing by indexes [row][col] is horizontal
     * */
    private void colorNeighbors(int row, int col, int maxVH){
        if(lines[row][col] == 2) // if cell is colored twice before (intersection cell), don't color it again
            return;

        if(maxVH > 0 && lines[row][col] == 1) // if cell colored vertically and needs to be recolored vertically, don't color it again (Allowing this step, will color the same line (result won't change), but the num of line will be incremented (wrong value for the num of line drawn))
            return;

        if(maxVH <= 0 && lines[row][col] == -1) // if cell colored horizontally and needs to be recolored horizontally, don't color it again (Allowing this step, will color the same line (result won't change), but the num of line will be incremented (wrong value for the num of line drawn))
            return;

        for(int i=0; i<values.length;i++){ // Loop on cell at indexes [row][col] and its neighbors
            if(maxVH > 0)   // if value of maxVH is positive, color vertically
                lines[i][col] = lines[i][col] == -1 || lines[i][col] == 2 ? 2 : 1; // if cell was colored before as horizontal (-1), and now needs to be colored vertical (1), so this cell is an intersection (2). Else if this value was not colored before, color it vertically
            else            // if value of maxVH is zero or negative color horizontally
                lines[row][i] = lines[row][i] == 1 || lines[row][i] == 2 ? 2 : -1; // if cell was colored before as vertical (1), and now needs to be colored horizontal (-1), so this cell is an intersection (2). Else if this value was not colored before, color it horizontally
        }

        // increment line number
        numLines++;
//      printMatrix(lines); // Monitor the line draw steps
    }//End step 3