title: Find if there is a route between 2 nodes Depth first search
body: <p>I am using Depth first search and graphs to find if there is a route between 2 nodes or not.
  But For some reason my method always results false even if there is a path.</p>
  
  <p><strong>Here is my search code:</strong></p>
  
  <pre><code>static boolean search(Graph g, Node start, Node end)
  {
  
  if(start == null || end == null) return false;
  
  start.state = State.Visited;
  
  for(Node u: start.getChildNodes())
  {
      if(u.state != State.Visited)
      {
          if(u.equals(end))
          {
              return true;
          }
          else
          {
              u.state = State.Visited;
              search(g,u,end);
          }
      }
  }
  
  return false;
  </code></pre>
  
  <p>}</p>
  
  <p><strong>I am calling the method like this:</strong></p>
  
  <pre><code>public static void main(String[] args) {
  
          Graph gDfs = createNewGraph();
  
          System.out.println("path between A &amp; B");
          System.out.println(search(gDfs,gDfs.getNode()[0],gDfs.getNode()[1]));
      }
  </code></pre>
  
  <p><strong>createNewGraph()</strong></p>
  
  <pre><code>public static Graph createNewGraph()
  {
  
  Graph g = new Graph();        
  Node[] temp = new Node[8];
  
  temp[0] = new Node("A", 3);
  temp[1] = new Node("B", 3);
  temp[2] = new Node("C", 1);
  temp[3] = new Node("D", 1);
  temp[4] = new Node("E", 1);
  temp[5] = new Node("F", 1);
  
  temp[0].addChildNode(temp[1]);
  temp[0].addChildNode(temp[2]);
  temp[0].addChildNode(temp[3]);
  
  temp[1].addChildNode(temp[0]);
  temp[1].addChildNode(temp[4]);
  temp[1].addChildNode(temp[5]);
  
  temp[2].addChildNode(temp[0]);
  temp[3].addChildNode(temp[0]);
  temp[4].addChildNode(temp[1]);
  temp[5].addChildNode(temp[1]);
  
  for (int i = 0; i &lt; 7; i++) 
  {
      g.addNode(temp[i]);
  }
  return g;
  </code></pre>
  
  <p>}</p>
  
  <p><em>@amit => is this your suggestion?</em></p>
  
  <pre><code>for(Node u: start.getChildNodes())
          {
              if(u.state != State.Visited)
              {
                  if (search(g,u,end)) return true;
              }
          }
  </code></pre>