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

Tell us what’s happening:

It passes all tests but the last one. I also checked the results by printing each test and they are correct. I don’t understand what the issue is with the last test.

Your code so far

visited = set()
result = []

def dfs(matrix, node_label):
    visited.add(node_label)
    print(visited)

    for ind, neigh in enumerate(matrix[node_label]):
        if ind == node_label:
            continue
        if neigh == 1 and ind not in visited:
            visited.add(ind)
            dfs(matrix, ind)
    result.append(visited.pop())

    return result

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0

Challenge Information:

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

Don’t use global variables in your function.

Try testing your function twice in a row.

print(dfs([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 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]], 3))