from typing import Optional
from typing import Hashable
from typing import Iterable


def _define_visited(
    already_visited: Optional[set[Hashable]],
    iter_start_ids: Iterable[Hashable],
    is_tree: bool,
) -> set[Hashable]:
    """Use already_visited, if provided, for storing visited vertices, and
    otherwise new dict. Mark start vertices as visited.
    """
    if already_visited is None:
        return set() if is_tree else set(iter_start_ids)

    if not is_tree:
        already_visited.update(iter_start_ids)
    return already_visited