def _bfs_for_reachable_nodes(target_nodes, name_to_input_name):
    """Breadth first search for reachable nodes from target nodes."""
    nodes_to_keep = set()
    next_to_visit = target_nodes[:]
    while next_to_visit:
        node = next_to_visit[0]
        del next_to_visit[0]
        if node in nodes_to_keep:
            continue
        nodes_to_keep.add(node)
        if node in name_to_input_name:
            next_to_visit += name_to_input_name[node]
    return nodes_to_keep
