> For the complete documentation index, see [llms.txt](https://statduck.gitbook.io/statduck/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://statduck.gitbook.io/statduck/python-study/algorithm/dfs.md).

# DFS

Depth First Search

```python

def dfs(graph, start_node):
    visit = list() # Emtpy list
    stack = list() # Emtpy list

    stack.append(start_node)

    while stack: # When one element of stack is inserted
        node = stack.pop() # The last element of stack
        if node not in visit: 
            visit.append(node)
            stack.extend(graph[node])

    return visit

graph = {'A': ['B'], 'B': ['A','B']}
print(dfs(graph, 'A'))
```
