Skip to content

Commit fbe380e

Browse files
adikul30tstreamDOTh
authored andcommittedOct 25, 2018
Add DFS in python (#302)
1 parent 947da87 commit fbe380e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎Graphs/DFS/Python/dfs.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import queue
2+
visited = []
3+
MAX = 20
4+
adj = {1: set([2,3,5]), 2: set([3]), 3: set([8,4]), 4:6}
5+
q = queue.Queue(MAX)
6+
7+
def dfs(s):
8+
if s in visited :
9+
return
10+
visited.append(s)
11+
print(s)
12+
children = adj.get(s)
13+
if children != None:
14+
if isinstance(children,int): # Case : values are not a set
15+
dfs(children)
16+
else : # Case : values are a set
17+
for i in children:
18+
dfs(i)
19+
return
20+
21+
def test():
22+
global visited
23+
visited = []
24+
print("dfs : ")
25+
dfs(1)
26+
27+
test()

0 commit comments

Comments
 (0)
Please sign in to comment.