def reference_cluster(envs, name):
    edges = [
        set([env['name'], ref])
        for env in envs
        for ref in env['refs']
    ]
    prev, cluster = set(), set([name])
    while prev != cluster:
        prev = set(cluster)
        to_visit = []
        for edge in edges:
            if cluster & edge:
                cluster |= edge
            else:
                to_visit.append(edge)
        edges = to_visit
    return cluster