Tell us what’s happening:
‘dfs function should return the correct results’ error while getting all the results correct whats wrong with my code??
Your code so far
def dfs(matrix,node):
visited=[]
paths=[]
path=[]
for i in range(len(matrix)):
visited.clear()
path.clear()
visited.append(i)
j=i
path=[i]
while 1 in matrix[j]:
a=matrix[j]
if j==node:
path.append(j)
break
try:
while a.index(1) in visited:
a[a.index(1)]=0
j=a.index(1)
visited.append(j)
path.append(j)
except ValueError:
break
if path[len(path)-1]==node:
paths.append(i)
return paths
print(dfs([[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]], 3))
print(dfs([[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]], 3))
print(dfs([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], 0))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Challenge Information:
Implement the Depth-First Search Algorithm - Implement the Depth-First Search Algorithm