-4

I want to run my function many times and get different answers(because it deals with random numbers), and get the minimum from all possible answers. But the function always returns the same value. How can I correct the loop to get different answers each time and find their minimum? Here is my code

    int n = 0;
    KargerMinimumCut karger = new KargerMinimumCut();
    ArrayList<Integer> answers = new ArrayList<Integer>();
    for(int i = 0; i < 10; i++) {
        n = karger.minCut(vertices);
        answers.add(n);
    }
    int min = minimum(answers);
    System.out.println("Minimum Number is: " + min);

and the minimum function

public static int minimum(ArrayList<Integer> array) {
    int min = array.get(0);
    for(int i = 1; i < array.size(); i++) {
        if(array.get(i) < min)
            min = array.get(i);
    }
    return min;
}
4

1 回答 1

1

编辑:

好的,在看到您的算法后,您需要执行此操作。(顺便说一下,我会使用一个简单的二维整数数组)。

int min = Integer.MAX_VALUE;
KargerMinimumCut karger = new KargerMinimumCut();
for(int i = 0; i < 10; i++) {
    int cut = karger.minCut(copyOfVertices(vertices));
    System.err.println(cut);
    min = Math.min(min,cut);
}
System.out.println("Minimum Number is: " + min);

.

public ArrayList<ArrayList<Integer>> copyOfVertices(ArrayList<ArrayList<Integer>> vertices) {
  ArrayList<ArrayList<Integer>> copy = new ArrayList<ArrayList<Integer>>();
  int size = vertices.size();
  for (int i=0; i<size; i++) {
    int innerSize = vertices.get(i).size();
    ArrayList<Integer> row = new ArrayList<Integer>();
    for (int j=0; j<innerSize; j++) {
      row.add(new Integer(vertices.get(i).get(j)));
    }
    copy.add(row);
  }
  return copy;
}
于 2013-02-18T13:07:59.220 回答