Implement the Depth-First Search Algorithm - Implement the Depth-First Search Algorithm

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

GitHub Link: https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/lab-depth-first-search/68d275dd800f404d22a07564.md

this test is checking random graphs

this should be one your function is not managing to solve:

dfs([[0, 1, 1, 1, 1, 1], [1, 0, 0, 1, 0, 1], [1, 0, 0, 1, 1, 1], [1, 1, 1, 0, 1, 1], [1, 0, 1, 1, 0, 0], [1, 1, 1, 1, 0, 0]], 5)

your function returns only 5 in the output list, the expected output should contain 0, 1, 2, 3, 4 and 5