title: Implementing Depth Limited Path Finding with Stack
body: <p>Hey like the title says I'm trying to implement a Depth limited search in Python3 that returns a path given a graph, start vertex and goal vertex. I'm struggling a bit with how to enforce the limit for a search. So far I have:</p>
  
  <pre><code>def dfs(g, v, goal, limit=-1):
  
      SENTINEL = object()    
      visitedStack = [v]
      path = ""
  
      while visitedStack:
          currentVertex = visitedStack.pop()    
  
          if g.getVertex(currentVertex) != None:
              if g.getVertex(currentVertex).visited == False:
                  path += currentVertex + ' -&gt; '
  
                  g.getVertex(currentVertex).hasBeenVisited()
  
                  if currentVertex == goal: 
                      return path[:-3]
  
                  elif currentVertex == SENTINEL:
                      limit += 1
  
                  elif limit != 0:
                      limit -= 1
                      visitedStack.append(SENTINEL)
                      visitedStack.extend(g.getVertex(currentVertex).getConnections()) 
  
       return "Depth limit was reached"
  </code></pre>
  
  <p>EDIT: I changed some of the code to check for visited vertices. After my edit the search getting returned doesn't work properly sometimes. For example, I'll set the depth limit to 3, but have a path of 4 or 5 getting returned. Other times I'll set the limit to 7 and have "limit reached" returned. NOTE: the smallest path is 3 </p>