I've been trying to understand BFS and DFS better, and I was wondering if I could get some help on this: I want to return the list of visited nodes for both of them. Since it would be redundant to post snippets for each and ask the same question, I'll just go with BFS:
visited = [] # List to keep track of visited nodes.
queue = [] #Initialize a queue
def bfs(visited, graph, node):
visited.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print (s, end = " ")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
This is from educative.io (python). Would I just delete the print function and add "return visited" at the very end? Thanks!