Example #1
0
def spanning_tree(g: Graph) -> {(str, str, int)}:
    answer = set()
    edges = sorted((e[2], e[0], e[1]) for e in g if len(e) != 1)
    components = EquivalenceClass(g.all_nodes())
    n = len(g.all_nodes())
    for v, o, d in edges:
        if n == 1:
            return answer
        if not components.in_same_class(o, d):
            answer.add((o, d, v))
            components.merge_classes_containing(o, d)
            n -= 1
    return answer
Example #2
0
 def setup(size):
     global ec
     ec = EquivalenceClass([i for i in range(size)])