title: Python - Depth First Search Non recursive approach
body: <p>I have implemented DFS using the recursive approach. However, my program breaks right after it is executed.</p>
  <pre><code># Non Recursive approach
  def Non_Recursive_dfs(graph, source):
      path = []
      stack = []
      stack.append(source)
      
      while stack:
          s = stack.pop()
          if s not in path:
              path.append(s)
  
          if s in path:
              #leaf node
              continue
          for neighbour in graph[s]:
              stack.append(neighbour)
      
      return &quot; &quot;.join(path)
  </code></pre>
  <p>Input and output:</p>
  <pre><code>print(Non_Recursive_dfs(graph, &quot;A&quot;))
  O/p: A
  </code></pre>
  <p>Can anyone explain why is this happening?</p>