#include "Img.h" #include #include #include #include using namespace icl; using namespace std; typedef icl32f real; typedef vector vec; //typedef vector > mat; typedef SimpleMatrix mat; typedef SimpleMatrix imat; /** Benchmark: 41ms for a 100² matrix 0.0375ms for 10² matrix 12s for a 500² matrix */ class HungarianAlgorithm { public: static void genereateRandomArray(mat &m, string method){ // {{{ open for(int i=0;i=mask.w()){//Should be cost.length but ok, because mask has same dimensions. step=7; }else { step=4; } return step; } // }}} static int hg_step4(int step, mat &cost, imat &mask, vec &rowCover, vec &colCover, vec &zero_RC){ // {{{ open //What STEP 4 does: //Find an uncovered zero in cost and prime it (if none go to step 6). Check for star in same row: //if yes, cover the row and uncover the star's column. Repeat until no uncovered zeros are left //and go to step 6. If not, save location of primed zero and go to step 5. vec row_col(2); //Holds row and col of uncovered zero. bool done = false; while (!done){ row_col = findUncoveredZero(row_col, cost, rowCover, colCover); if (row_col[0] == -1){ done = true; step = 6; }else{ mask[row_col[0]][row_col[1]] = 2; //Prime the found uncovered zero. bool starInRow = false; for (int j=0; j= cost.w()){ done = true; } }//end outer while return row_col; } // }}} static int hg_step5(int step, imat &mask, vec &rowCover, vec &colCover, vec &zero_RC){ // {{{ open //What STEP 5 does: //Construct series of alternating primes and stars. Start with prime from step 4. //Take star in the same column. Next take prime in the same row as the star. Finish //at a prime with no star in its column. Unstar all stars and star the primes of the //series. Erase any other primes. Reset covers. Go to step 3. int count = 0; //Counts rows of the path matrix. //imat path(mask.h()+2,2); imat path(mask.h()+20,2); path[count][0] = zero_RC[0];//Row of last prime. path[count][1] = zero_RC[1];//Column of last prime. bool done = false; while (!done){ int r = findStarInCol(mask,path[count][1]); if (r>=0){ count++; // printf("count is %d ---> max index is %d\n",count,mask.h()+2-1); path[count][0] = r; //Row of starred zero. path[count][1] = path[count-1][1];//Column of starred zero. }else{ done = true; } if(!done){ int c = findPrimeInRow(mask, path[count][0]); count++; path[count][0] = path [count-1][0]; //Row of primed zero. path[count][1] = c; //Col of primed zero. } }//end while convertPath(mask, path, count); clearCovers(rowCover, colCover); erasePrimes(mask); step = 3; return step; } // }}} static int findStarInCol(imat &mask, int col){ // {{{ open int r=-1; //Again this is a check value. for (int i=0; i cost[i][j])){ minval = cost[i][j]; } } } return minval; } // }}} }; void show_mat(mat &m){ // {{{ open for(int j=0;j