Vogel's method java-android
<p>i am trying to create an application that implements the vogel's method in android. the task is</p>
<ol>
<li><p>Determine the penalty cost for each row and column by subtracting the lowest cell cost in the row or column from the next lowest cell cost in the same row or column.</p>
</li>
<li><p>Select the row or column with the highest penalty cost (breaking ties arbitrarily or choosing the lowest-cost cell).</p>
</li>
<li><p>Allocate as much as possible to the feasible cell with the lowest
transportation cost in the row or column with the highest penalty
cost.</p>
</li>
<li><p>Repeat steps 2, 3 and 4 until all requirements have been meet.</p>
</li>
<li><p>Compute total transportation cost for the feasible allocations.</p>
</li>
</ol>
<p>and here is my code.</p>
<pre><code>public  class activityvogel extends ActionBarActivity {


private Button button;
    static int[] stock;
    static int[] required;
    static int[][] converted2;
          static int nRows,nCols;



    static boolean[] rowDone;
   static boolean[] colDone ;
   static int[][] result;
    static ExecutorService es = Executors.newFixedThreadPool(2);



    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activityvogel);
                Bundle extras = getIntent().getExtras();
         stock= extras.getIntArray(&quot;stock&quot;);
         required= extras.getIntArray(&quot;required&quot;);
        final String[][] cost = (String[][]) extras.getSerializable(&quot;converted&quot;);
</code></pre>
<p>\converting array cost string to int</p>
<pre><code>        converted2=new int[stock.length][required.length];

       for (int index = 0; index &lt;stock.length; index++)

        {

            for (int subIndex = 0; subIndex &lt;required.length; subIndex++) {
                converted2[index][subIndex] = Integer.parseInt(cost[index][subIndex]);
                            }
        }
        System.out.println(Arrays.deepToString(converted2));
        System.out.println(Arrays.toString(stock));
        System.out.println(Arrays.toString(required));
</code></pre>
<p>\Here starts the Vogel's method</p>
<pre><code>        nRows=stock.length;
        nCols = required.length;
        rowDone = new boolean[nRows];
        colDone = new boolean[nCols];
        result = new int[nRows][nCols];
        int supplyLeft = 0;
        for (int i = 0; i &lt; stock.length; i++) {
            supplyLeft += stock[i];
        }
        int totalCost = 0;
        while (supplyLeft &gt; 0) {
            int[] cell = new int[0];
            try {
                cell = nextCell();
            } catch (Exception e) {
                e.printStackTrace();
            }
            int r = cell[0];
            int c = cell[1];
            int quantity = Math.min(required[c], stock[r]);
            required[c] -= quantity;
            if (required[c] == 0)
                colDone[c] = true;
            stock[r] -= quantity;
            if (stock[r] == 0)
                rowDone[r] = true;
            result[r][c] = quantity;
            supplyLeft -= quantity;
            totalCost += quantity * converted2[r][c];
        }
System.out.println(Arrays.deepToString(result));

int[] nextCell() throws Exception{
        Future&lt;int[]&gt; f1 = es.submit(new Callable&lt;int[]&gt;() {
            @Override
            public int[] call() {
                return  maxPenalty(nRows, nCols, true);
            }
        });
        Future&lt;int[]&gt; f2 = es.submit(new Callable&lt;int[]&gt;() {
            @Override
            public int[] call() {
                return maxPenalty(nCols, nRows, false);
            }
        });
        int[] res1 = f1.get();
        int[] res2 = f2.get();

        if (res1[3] == res2[3])
            return res1[2] &lt; res2[2] ? res1 : res2;

        return (res1[3] &gt; res2[3]) ? res2 : res1;
    }

    int[] diff(int j, int len, boolean isRow) {
        int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        int minP = -1;
        for (int i = 0; i &lt; len; i++) {
            if (isRow ? colDone[i] : rowDone[i])
                continue;
            int c = isRow ? converted2[j][i] : converted2[i][j];
            if (c &lt; min1) {
                min2 = min1;
                min1 = c;
                minP = i;
            } else if (c &lt; min2)
                min2 = c;
        }
        return new int[]{min2 - min1, min1, minP};
    }

    int[] maxPenalty(int len1, int len2, boolean isRow) {
        int md = Integer.MIN_VALUE;
        int pc = -1, pm = -1, mc = -1;
        for (int i = 0; i &lt; len1; i++) {
            if (isRow ? rowDone[i] : colDone[i])
                continue;
            int[] res = diff(i, len2, isRow);
            if (res[0] &gt; md) {
                md = res[0];  // max diff
                pm = i;       // pos of max diff
                mc = res[1];  // min cost
                pc = res[2];  // pos of min cost
            }
        }
        return isRow ? new int[]{pm, pc, mc, md} : new int[]{pc, pm, mc, md};
    }
</code></pre>
<p>My problem is that i don't get always the right result as I should get, and i can't find why. If anyone can help i would be thankfull.
<a href="http://www.universalteacherpublications.com/univ/ebooks/or/Ch5/vogel.htm" rel="nofollow noreferrer">Here</a> is how this algorithm works.</p>