def links_to_sets(links):
    """
    :param links: Dict of [doc_id, set of connected doc ids]
    :return clusters: Frozen set of frozen sets, each frozen set contains doc ids in that cluster
    """
    removed = set()
    clusters = []
    for (pivot, linkage) in links.iteritems():
        if pivot not in removed:
            clusters.append(frozenset(linkage))
        removed.update(linkage)
        removed.add(pivot)
    clusters = frozenset(clusters)
    return clusters