remaining_elements set(adj_nodesdifference(set(visited_vertices)if len(remaining_elements for elem in sorted(remaining_elements)visited_vertices append(elemgraph_queue append(elemreturn visited_vertices when we want to find out whether set of nodes are in the list of visited nodeswe use the statement remaining_elements set(adj_nodesdifference(set(visited_vertices)this uses the set object' difference method to find the nodes that are in adj_nodes but not in visited_vertices in the worst-case scenarioeach vertex or node and edge will be traversedthus the time complexity of the algorithm is (| | |)where |vis the number of vertices or nodes while |eis the number of edges in the graph depth-first search as the name suggeststhis algorithm traverses the depth of any particular path in the graph before traversing its breadth as suchchild nodes are visited first before sibling nodes it works on finite graphs and requires the use of stack to maintain the state of the algorithmdef depth_first_search(graphroot)visited_vertices list(graph_stack list(graph_stack append(rootnode root the algorithm begins by creating list to store the visited nodes the graph_stack stack variable is used to aid the traversal process for continuity' sakewe are using regular python list as stack the starting nodecalled rootis passed with the graph' adjacency matrixgraph root is pushed onto the stack node root holds the first node in the stackwhile len(graph_stack if node not in visited_verticesvisited_vertices append(node