def _dfs(vertex, vertices_neighbours):
    """Generate all the vertices connected to the supplied vertex in
    depth-first-search order.
    """
    visited = set()
    to_visit = deque([vertex])
    while to_visit:
        vertex = to_visit.pop()
        if vertex not in visited:
            yield vertex
            visited.add(vertex)
            to_visit.extend(vertices_neighbours[vertex])
Generate all the vertices connected to the supplied vertex in
    depth-first-search order.