-
Notifications
You must be signed in to change notification settings - Fork 17
/
DFS.py
40 lines (29 loc) · 1012 Bytes
/
DFS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#DFS Algorithm
#Traversal means visiting all the nodes of a graph. Depth first traversal or Depth first Search is a recursive algorithm for searching all the vertices of a graph or tree data structure. In this article, you will learn with the help of examples the DFS algorithm, DFS pseudocode, and the code of the depth first search algorithm with Python program.
#DFS pseudocode
#DFS(G, u)
# u.visited = true
# for each v ∈ G.Adj[u]
# if v.visited == false
# DFS(G,v)
#init() {
# For each u ∈ G
# u.visited = false
# For each u ∈ G
# DFS(G, u)
#}
#DFS Algorithm
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start)
for next in graph[start] - visited:
dfs(graph, next, visited)
return visited
graph = {'0': set(['1', '2']),
'1': set(['0', '3', '4']),
'2': set(['0']),
'3': set(['1']),
'4': set(['2', '3'])}
dfs(graph, '0')