breadth first search trouble [solved it] <p>Working on a breadth first search but cant seem to get it to work. The bfs is searching through an array of linked lists (an adjacency matrix). I followed the algorithm in my text book as well as looked over a few on the internet, but i think since that I am using an array of linked lists it's having trouble vs the standard of using a graph. Could anyone please help me resolve where the issues are occurring?</p> <p>This is my BFS method:</p> <pre><code>public static int bfs(List&lt;Integer&gt;[] smallWorld, int start){ int distance = 0; int size = smallWorld.length; //for each location in smallWorld do a bfs to every other location int u; int white = 0, grey = 1, black = 2; int [] color, d, pie; Queue &lt;Integer&gt; q = new &lt;Integer&gt; LinkedList(); color = new int [size]; d = new int [size]; pie = new int [size]; for( int x = 0; x &lt; size; x++){ //for all indexes in the smallWorld array color[x] = white; //color them white d[x] = -1; //make the distance array @ each index infinity (-1) pie[x] = -1; //make the pie array @ each index null (-1) } color[start] = grey; //At the starting / current index color it grey d[start] = 0; //the distance @ starting array is 0 obviously pie[start] = -1; //make pie array @ current index / starting index null (-1) q.addAll(smallWorld[start]); //enqueue the items adjacent to current index while(!q.isEmpty()){ //while the queue is not empty u = q.remove(); //dequeue the first item and set it to u for(int v = 0; v &lt; smallWorld[u].size(); v++){ //for every vertex u is adjacent to if(color[smallWorld[u].get(v)] == white){ //if the color is white of that adjacent vertex color[smallWorld[u].get(v)] = grey; //color it grey d[v] = d[u] + 1; //make the distance pie[v] = u; q.addAll(smallWorld[v]); } } color[u] = black; } int x = 0; while(d[x] != -1){ distance = distance + d[x]; x++; } return distance; } </code></pre> <p>I just dont see why the algorithm isn't working and I'm pretty sure I adjusted it to work with array of <code>LinkedList</code>s.</p>