import networkx


def nx_find_connected_limited(graph, start_set, end_set, max_depth=3):
    """Return the neurons in end_set reachable from start_set with limited depth."""
    reverse_graph = graph.reverse()
    reachable = []
    for e in end_set:

        preorder_nodes = list(
            (
                networkx.algorithms.traversal.depth_first_search.dfs_preorder_nodes(
                    reverse_graph, source=e, depth_limit=max_depth
                )
            )
        )

        for s in start_set:
            if s in preorder_nodes:
                reachable.append(e)
                break

    return reachable