It is rather difficult to explain my algorithm. But I will try

I have virtual network of nodes. For example the network of 5 nodes ( A, B, C, D and E ) where:
A is connected to B and C;
B is connected to A and E;
C is connected to A;
D is connected to E;
E is connected to A, B and D;
A---------B
| | |
| | |
C E
|
|
D
My task here is to find average times of hops from all nodes of pairs. For example I am going randomly from A to B, from A to D. To go from A to D I can get two hops ( A -> E -> D ) or three hops ( A-> B -> E -> D) or four hops ( A -> C -> A -> E -> D).
My algorithm works perfect for small networks, but when I have a network of 20 nodes I get :
Exception in thread "main" java.lang.StackOverflowError
at java.nio.Buffer.limit(Unknown Source)
at java.nio.Buffer.<init>(Unknown Source)
at java.nio.CharBuffer.<init>(Unknown Source)
...............
A part of my code.
Loop in loop ( this.n is the size of the network ):
for (int i=0; i < this.n; i++) {
for (int j=0; j < this.n; j++) {
int ii = i;
int jj = j;
//this.counter = 0;
count = 0;
policy1(i, j, ii, jj, k, count);
count = 0;
}
}
public void policy1(int i, int j, int ii, int jj, int k, int count) {
if (i != jj) {
int setSize = neighbours[i].size();
r = randomNumber(0, setSize);
count++;
g = (Integer)neighbours[i].get(r);
if (count > 1400)
System.out.println(count);
policy1(g, j, ii, jj, k, count);
}
else {
this.hopsMatrix[k][ii][jj] = count;
count = 0;
}
}