-
-
Save amankharwal/2c9541f232d661e2ff2eabe504b10617 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def breadth_first(graph, root): | |
queue = Queue() | |
visited_nodes = list() | |
queue.enqueue(root) | |
visited_nodes.append(root) | |
current_node = root | |
while queue.size > 0: | |
current_node = queue.dequeue() | |
adj_nodes = graph[current_node] | |
remaining_elements = sorted(set(adj_nodes) - set(visited_nodes)) | |
if len(remaining_elements) > 0: | |
for element in remaining_elements: | |
visited_nodes.append(element) | |
queue.enqueue(element) | |
return visited_nodes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment