title: How to stop depth-first search when the target vertex of the graph is found?
body: <p>Here is the graph:</p>
  
  <pre><code>g = {
      0: [2, 5, 7],
      1: [7],
      2: [0, 6],
      3: [5, 4],
      4: [3, 6, 7],
      5: [3, 4, 0],
      6: [2, 4],
      7: [0, 1, 4]
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/4VPuI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4VPuI.jpg" alt="enter image description here"></a></p>
  
  <p>I have the following function in Python:</p>
  
  <pre><code>def dfs(graph, start, target, visited=None):
      if visited is None:
          visited = set()
      visited.add(start)
  
      for n in (set( graph[start] ) - visited):
          dfs(graph, n, target, visited)
      return visited
  </code></pre>
  
  <p>But it returns all the vertices that exists in the graph, and I want that it returns just the target vertex if it's present in the graph.
  Could someone help me?</p>