### DFS

Posted:

**Mon Jun 11, 2012 1:12 am**Here an example of DFS in Python:

Code: Select all

```
def DFS(graph, pos):
marks = [False] * len(graph)
marks[pos] = True
for i in __DFS(graph, pos, marks):yield i
def __DFS(graph, pos, marks):
for i in graph[pos]:
if not marks[i]:
marks[i] = True
yield i
for j in __DFS(graph, i, marks):yield j
#Example:
graph = [[3], [0, 2, 5], [3, 4], [1, 0], [5, 3], [0, 1, 2, 3]]
for i in DFS(graph, 1): print i
```