def _get_indices_connected_to_expr_index(exp_dag, expr_indices, expr_index):
    """
    Filter 'expr_indices' if they can be traced back to 'expr_index'. Typical use cases are:
    Does a READ_TENSOR expression have connection to an input or output tensor?
    Does an ASSIGN_TENSOR expression have connection to an output tensor?
    :param exp_dag: The expression dag.
    :param expr_indices: Expression indices to be tested if they have a connection to expr_index.
    :param expr_index: An expr_index in the dag.
    :return: A sub-list of 'expr_indices' that contains only those indices of expressions that are connected to the
    expression at the index 'expr_index'. Returned indices may not be in the same order than indices in 'expr_indices'.
    """
    connected_indices = set()
    indices = []
    for index in expr_indices:
        indices.append(index)
        while len(indices) > 0:
            i = indices.pop(0)
            for op_index in exp_dag.references[i].operand_indices:
                if op_index == expr_index:
                    connected_indices.add(index)
                    indices[:] = []
                    break
                else:
                    indices.append(op_index)
    return list(connected_indices)
