Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have a few comments on the code:</p> <p>1) Take those lines in the first code snippet:</p> <pre><code>while(st.hasMoreTokens()){ String currentToken = st.nextToken(); if(!cityList.contains(currentToken.trim())){ cityList.add(currentToken.trim()); }//if }//while hasMoreTokens </code></pre> <p>The <code>cityList.contains()</code> method consumes linear time on the number of cities, and the <code>while(st.hasMoreTokens())</code> might run <code>O(V^2)</code> times where V is the number of vertices, since you can have a dense graph. So, just in this one loop, you are consuming O(V^3) time, which is already worst than a DFS (<code>O(V + E)</code> which is <code>O(V^2)</code> in a dense graph). You can't speed up the O(V^2) loop because you have to read all the edges, but you can use a more efficient data structure to hold that city list, namely a hash (<code>O(1)</code> lookup, <code>O(1)</code> insertion).</p> <p>2) On the second code snippet:</p> <pre><code>while(st.hasMoreTokens()){ String firstToken = st.nextToken().trim(); String secondToken = st.nextToken().trim(); cityGraph[cityList.indexOf(firstToken)][cityList.indexOf(secondToken)] = 1; cityGraph[cityList.indexOf(secondToken)][cityList.indexOf(firstToken)] = 1; }//while hasMoreTokens </code></pre> <p>Exactly the same thing. Use a hash instead of a list.</p> <p>3) Inner loop of your DFS</p> <pre><code>if(cityGraph[startNode][i] == 1){ if( i == city2 ){ System.out.println("yes"); return; }//if city2 found q.add(i); cityGraph[startNode][i] = 0; //Set to visited }//if vertex exist </code></pre> <p>There are two problems. One is that you are overwriting your graph representation every time you run a DFS. By setting <code>cityGraph[startNode][i] = 0;</code> you are actually deleting an edge of your graph. If you are reconstructing the graph for every DFS, that is a huge problem.</p> <p>Second problem is that it seems to me you are marking visited nodes in the wrong way. You are just marking visited EDGES, not nodes. If you have the path 1 -> 2 and the path 1 -> 4 -> 2, you are going to visit (and add to queue) node 2 two times.</p> <p>To solve both problems, use a <code>boolean visited[#cities]</code> array. Everytime you start the DFS, you set all nodes to not visited. Everytime you check an edge, you check if you have already visited that node. If not, add it to the queue.</p> <p>On a final note, </p> <pre><code>q.remove();//remove the top element and start with new node if(!q.isEmpty()){ startNode = (Integer) q.element(); }//if </code></pre> <p>This is ugly since you are already checking if the queue is empty on the while loop. Instead, you can just move this code to the beggining of the while loop, removing the if condition (because you know the queue is not empty):</p> <pre><code>while(!q.isEmpty()){ startNode = (Integer) q.element(); q.remove(); </code></pre> <p>Hope that helps....</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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