node is queued and added to the list of visited nodes afterwardwe use while loop to effect traversal of the graph in the while loopnode is dequeued its unvisited adjacent nodes bgand are sorted in alphabetical order and queued up the queue will now contain the nodes bdand these nodes are also added to the list of visited nodes at this pointwe start another iteration of the while loop because the queue is not emptywhich also means we are not really done with the traversal node is dequeued out of its adjacent nodes afand enode has already been visited thereforewe only enqueue the nodes and in alphabetical order nodes and are then added to the list of visited nodes our queue now holds the following nodes at this pointdgeand the list of visited nodes contains abdgef node is dequeued but all of its adjacent nodes have been visited so we simply dequeue it the next node at the front of the queue is we dequeue node but we also find out that all its adjacent nodes have been visited because they are in the list of visited nodes node is also dequeued we dequeue node too because all of its nodes have been visited the only node in the queue now is node node is dequeued and we realize that out of its adjacent nodes bdand conly node has not been visited we then enqueue node and add it to the list of visited nodes node is dequeued node has the adjacent nodes and but has already been visitedleaving node node is enqueued and added to the list of visited nodes finallythe last iteration of the while loop will lead to node being dequeued its only adjacent node has already been visited once the queue is completely emptythe loop breaks the output of the traversing the graph in the diagram is abdgefch the code for breadth-first search is given as followsfrom collections import deque def breadth_first_search(graphroot)visited_vertices list(graph_queue deque([root]visited_vertices append(rootnode root while len(graph_queue node graph_queue popleft(adj_nodes graph[node