/** * constructs a matrix */ public Matrix(double[,] m) { this.row = m.GetLength(0);//m.Length; if (this.row > 0) { this.col = m.GetLength(1);//m[0].Length; } else { this.col = this.row; } this.matrix = new MatrixElement[this.row, this.col]; for (int i = 0; i < this.row; i++) { for (int j = 0; j < this.col; j++) { MatrixElement e = new MatrixElement(m[i, j]); e.setMyRow(i); e.setMyCol(j); this.matrix[i, j] = e; } } this.coveredCols = new bool[this.col]; this.coveredRows = new bool[this.row]; for (int i = 0; i < this.row; i++) { this.coveredRows[i] = false; } for (int j = 0; j < this.col; j++) { this.coveredCols[j] = false; } }
/** * creates a serie of alternating primed and starred zeros * starting with @param Z0 */ private double step3(MatrixElement Z0) { LinkedList <MatrixElement> s = new LinkedList <MatrixElement>(); s.AddFirst(Z0); MatrixElement act = Z0; while (this.m.isStarredCol(act)) { MatrixElement Z1 = this.m.getStarredZeroInC(act); s.AddLast(Z1); MatrixElement Z2 = this.m.getPrimedZeroInR(Z1); s.AddLast(Z2); act = Z2; } foreach (MatrixElement e in s) { this.m.handleElement(e); } /*Iterator iter = s.iterator(); * while (iter.hasNext()) { * MatrixElement e = (MatrixElement) iter.next(); * this.m.handleElement(e); * }*/ this.m.unPrimeAll(); this.m.unCoverAll(); return(this.step1()); }
/** * Step 2 - searchs an uncovered zero and prime it * if no such element is found the min element is saved */ private double step2() { MatrixElement Z0 = this.m.getUncoveredZero(); if (Z0 != null) { this.m.primeElement(Z0); MatrixElement e = this.m.getStarredZeroInR(Z0); if (e == null) { return(this.step3(Z0)); } else { this.m.coverRow(Z0); this.m.unCoverCol(e); return(-1); // repeat step 2 } } else { double e_min = this.m.getUncoveredMin(); return(this.step4(e_min)); } }
/** * delete star or map prime to star of @param e */ public void handleElement(MatrixElement e) { e.setStarred(false); if (e.isPrimed()) { e.setPrimed(false); e.setStarred(true); } }
/** * @return true if the col of @param e is starred */ public bool isStarredCol(MatrixElement e) { int c = e.getMyCol(); for (int i = 0; i < this.row; i++) { if (this.matrix[i, c].isStarred()) { return(true); } } return(false); }
/** * @return the primed zero in the row of @param e */ public MatrixElement getPrimedZeroInR(MatrixElement e) { int r = e.getMyRow(); for (int j = 0; j < this.col; j++) { if (this.matrix[r, j].getValue() == 0) { if (this.matrix[r, j].isPrimed()) { return(this.matrix[r, j]); } } } return(null); }
/** * @return the primed zero in the col of @param e */ public MatrixElement getPrimedZeroInC(MatrixElement e) { int c = e.getMyCol(); for (int i = 0; i < this.row; i++) { if (this.matrix[i, c].getValue() == 0) { if (this.matrix[i, c].isPrimed()) { return(this.matrix[i, c]); } } } return(null); }
/** * print method for debugging */ public void printMe() { for (int i = 0; i < this.row; i++) { for (int j = 0; j < this.col; j++) { MatrixElement e = this.matrix[i, j]; if (this.coveredRows[e.getMyRow()]) { Console.Out.Write("|"); } if (this.coveredCols[e.getMyCol()]) { Console.Out.Write("|"); } if (e.getValue() < Double.MaxValue) { double temp = ((int)(e.getValue() * 1000.0)) / 1000.0; Console.Out.Write(temp); } else { Console.Out.Write("inf"); } if (e.isPrimed()) { Console.Out.Write("'"); } if (e.isStarred()) { Console.Out.Write("*"); } if (this.coveredRows[e.getMyRow()]) { Console.Out.Write("|"); } if (this.coveredCols[e.getMyCol()]) { Console.Out.Write("|"); } Console.Out.Write("\t"); } Console.Out.WriteLine(); } Console.Out.Write("\n"); }
/** * @return a int[][] with the indices representing the mappings */ public int[,] getStarredIndices(int dim) { int[,] indices = new int[dim, 2]; int counter = 0; for (int i = 0; i < this.row; i++) { for (int j = 0; j < this.col; j++) { MatrixElement e = this.matrix[i, j]; if (e.isStarred()) { indices[counter, 0] = i; indices[counter, 1] = j; counter++; } } } return(indices); }
/** * uncover the row of element @param e */ public void unCoverRow(MatrixElement e) { int r = e.getMyRow(); this.coveredRows[r] = false; }
/** * uncover the col of element @param e */ public void unCoverCol(MatrixElement e) { int c = e.getMyCol(); this.coveredCols[c] = false; }
/** * covers the row of element @param e */ public void coverRow(MatrixElement e) { int r = e.getMyRow(); this.coveredRows[r] = true; }
/** * primes the element @param e */ public void primeElement(MatrixElement e) { e.setPrimed(true); }