def METHOD_NAME(self):
    # Get related entities from source entity
    source_node = get_object_or_404(Entity, uid=self.kwargs.get("uid"))
    neighbors = defaultdict(set)
    for (entity_1_id, entity_2_id) in Comparison.objects.filter(
        poll=self.poll_from_url, user=self.request.user
    ).values_list("entity_1_id", "entity_2_id"):
        neighbors[entity_1_id].add(entity_2_id)
        neighbors[entity_2_id].add(entity_1_id)
    distances = {source_node.id: 0}
    bfs_queue = deque([(source_node.id, 0)])
    while bfs_queue:
        node, distance = bfs_queue.popleft()
        for next_node in neighbors[node]:
            if next_node not in distances:
                distances[next_node] = distance+1
                bfs_queue.append((next_node, distance+1))
    # The entities filtered are the entities which don't have a distance
    # because they are unreachable if the strict option has been left to
    # True (the default). Otherwise all the entities except the source are
    # selected.
    strict = self.request.query_params.get("strict") != "false"
    entity_ids = set(e_id for e_id in neighbors if (
        (e_id not in distances) if strict else (distances.get(e_id, 2) > 1)
    ))
    sorted_entity_ids = sorted(
        entity_ids,
        # Sorting first by distance and secondly by number of comparisons
        # and thirdly by entity_id such that we avoid non-determinism
        key=lambda x: (-distances.get(x, math.inf), len(neighbors.get(x, [])), x)
    )
    return sorted_entity_ids