Depth first traversing graph

Could someone tell me what is wrong with my depth first graph

def searche(mgraph, visited_node):
    for key in list(mgraph.keys()):
        if key not in visited_node:
           return False
     return True

def dfs(graph, start_node):
    visited = [start_node]
    stack = []

    current = start_node

    while searche(graph, visited):
       decision = True
       for neighbour in graph[current]:
           if neighbour[0] not in visited:
               stack.append(current)
               current = neighbour[0]
               visited.append(current)
               decision = False
               break

           if decision == True and not searche(graph, visited):
                current = stack.pop()
    
        return visited

graph = {‘A': [('E', 3), ('B'', 13)], 'E': [('A', 3), ('D', 9)], 'D': [('E', 9), ('C', 7)1, 'C': [('D', 7)], 'B': [('A', 13)]}

print(dfs(graph, 'A'))

No need anymore, I figured out the problem!!

:wave: Hey @TraceyO77!

It’s great to hear that you’ve fixed your problem, but please provide an explanation to how you did, so that other users with the same issue can easily find the solution.

2 Likes

The problem was that searche was returning False thus while searche was not running, instead while not searche

Also, if decision == True could just be written as if decision

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.