adj_nodes graph[nodeif set(adj_nodesissubset(set(visited_vertices))graph_stack pop(if len(graph_stack node graph_stack[- continue elseremaining_elements set(adj_nodesdifference(set(visited_vertices)first_adj_node sorted(remaining_elements)[ graph_stack append(first_adj_nodenode first_adj_node return visited_vertices the body of the while loop will be executed provided the stack is not empty if node is not in the list of visited nodeswe add it all adjacent nodes to node are collected by adj_nodes graph[nodeif all the adjacent nodes have been visitedwe pop that node from the stack and set node to graph_stack[- graph_stack[- is the top node on the stack the continue statement jumps back to the beginning of the while loop' test condition ifon the other handnot all the adjacent nodes have been visitedthe nodes that are yet to be visited are obtained by finding the difference between the adj_nodes and visited_vertices with the statement remaining_elements set(adj_nodesdifference(set(visited_vertices)the first item within sorted(remaining_elementsis assigned to first_adj_nodeand pushed onto the stack we then point the top of the stack to this node when the while loop existswe will return the visited_vertices