-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.py
More file actions
38 lines (33 loc) · 830 Bytes
/
dfs.py
File metadata and controls
38 lines (33 loc) · 830 Bytes
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
## dfs on the graph
###### graph representation is dict of list that is dictonary of ############
###### nodes and the values of nodes is list of nodes with edge ############
def exp(l):
l1 = {}
i = 0
while (i < len(l)):
l1[l[i]] = 0
i += 1
return l1
def lis(l,k):
l1 = []
i = 0
while (i < len(l)):
l1 += [(l[i],k)]
i += 1
return l1
def dfs(g,s):
q = [] ##this represents queue
explored = exp(list(g.keys()))
explored[s] = 1
q += lis(g[s],1)
layers = [(s,0)]
while (q != []):
k = q[len(q)-1]
q = q[:(len(q)-1)]
if (explored[k[0]] == 0):
explored[k[0]] = 1
q += lis(g[k[0]],k[1] + 1)
layers += [k]
return layers
g = {1:[2],2:[1,3],3:[2,4],4:[3,5],5:[4]}
print(dfs(g,3))