import numpy as np
defvisit(v):returnprint(str(v)+"->",end='')defFirsNeighbor(v):
t =-1for i inrange(g.shape[0]):if g[v][i]==1:
t =1breakif(t ==1):return i
else:return t
defOpenDoor(p):if(visited[p]==True):return0else:return1defNextNeighbor(v,w):
l =[]for i inrange(g.shape[0]):if g[v][i]==1:
l.append(i)if(len(l)>1and OpenDoor(l[1])):return l[1]else:return-1defDFS(g, v):
visit(v)
visited[v]=True
w = FirsNeighbor(v)while(w >=0):if(visited[w]==False):
DFS(g,w)
w = NextNeighbor(v,w)if __name__ =="__main__":
g =np.array([[0,1,0,0,1,0],[1,0,0,0,0,1],[0,0,0,1,0,1],[0,0,1,0,0,0],[1,0,0,0,0,0],[0,1,1,0,0,0]])
visited = np.zeros(g.shape[0])
queue =[0]
DFS(g,0)