def METHOD_NAME(state_histories, hierarchy):
    # TODO: reduce duplication with above function
    work = set(state_histories)
    while len(work) > 0:
        working_history = bot_history = work.pop()
        while hierarchy.history_contains(working_history):
            parent_histories = hierarchy.history_predecessors(working_history)
            if not parent_histories:
                break
            parent_history = parent_histories[0]
            try:
                successors = hierarchy.history_successors(parent_history)
                if len(successors) > 1:
                    yield (parent_history, bot_history)
                    work.add(parent_history)
                    break
                else:
                    working_history = parent_history
            except KeyError:
                # the parent history is not found in the path mapping
                log.error("Parent history %s is not found", parent_history)
                break