title: Recursively walk through all connected nodes in a graph
body: <p>I am trying to write a function that finds all nodes that is connected to the given node in a graph <strong>without importing other modules</strong>.</p>
  
  <p>Currently, my setting looks like the following:</p>
  
  <pre><code>class Node:
      self.name, self.routes, self.neighbours
  
  class Edge:
      self.nodes, self.distance
  </code></pre>
  
  <p>I have a recursive function:</p>
  
  <pre><code>def search_graph(start):
      def aux(start, visited):
          if start in visited:
              return visited
          visited.add(start)
          if set(start.neighbours) &lt; visited:
              return visited
          for node in set(start.neighbours) -visited:
              return visited | aux(node, visited)
      return aux(start, set())
  </code></pre>
  
  <p>I tried some graphs with it, it turns out that when the function reaches some endpoint of a graph, it won't trace back to the last node. For example:
      graph:
          Tokyo - Shanghai - Melbourne - Singapore
  With <code>search_graph(Shanghai)</code> I can only get <code>{Shanghai, Melbourne, Singapore}</code>.</p>
  
  <p>Interestingly, after some search, I find that my function resembles a depth-first-search a lot, still I cannot figure what is wrong with my function.</p>
  
  <p>Thank you!</p>