Kruskal (MST): Really Special Subtree

  • + 0 comments

    My python solution

    def kruskals(g_nodes, g_from, g_to, g_weight):
        edges = sorted(zip(g_from, g_to, g_weight), key=lambda x: x[2])
        mst = {}
        cost = 0
        for c, (u, v, w) in enumerate(edges):
            d = {}
            for idx, conn in mst.items():
                if (u in conn):
                    d[u] = idx
                if (v in conn):
                    d[v] = idx
            if d.get(u, -1) != d.get(v, -2):
                cost += w
        return cost
                mst[c] = mst.pop(d.get(u, -1), set([u])) | mst.pop(d.get(v, -1), set([v]))
            
        return cost
    
    `