Note that there are some explanatory texts on larger screens.

plurals
  1. POBreadth first search not working 100%
    primarykey
    data
    text
    <p>working on doing a bfs for an adjacency matrix which is buil as an array of linked lists.</p> <p>Here is my bfs method, receives the array of linked lists and starting location:</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++){ color[x] = white; d[x] = -1; pie[x] = -1; } color[start] = grey; d[start] = 0; pie[start] = -1; q.addAll(smallWorld[start]); //enqueue the adjacent items while(!q.isEmpty()){ u = q.remove(); for(int v = 0; v &lt; smallWorld[u].size(); v++){ //for every vertex u is adjacent to if(color[v] == white){ color[v] = grey; d[v] = d[u] + 1; pie[v] = u; q.addAll(smallWorld[v]); } } color[u] = black; } int x = 0; while(d[x] != -1){ distance = distance + d[x]; x++; } </code></pre> <p>Really smallWorld is of length 500, but for testing purposes im just doing a bfs on the first index in the array. (Ya know bfs is supposed to give back the shortest possible path between 2 indexes in the array). Mine has been running for about 14 min now and im not sure why, i mean i assume its because the !isEmpty() but that only adds stuff to the queue if its white so sooner or later that has to run out.</p> <p><strong>EDITED. FIGURED OUT ENDLESS LOOP PROBLEM BUT BFS METHOD STILL NOT 100%</strong></p> <p>any ideas why it isnt working? I mean i followed the algorithm to the T, but the algorithm generally isnt referring to an array of linked lists.</p> <p>Any ideas to solve this problem</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload