def kruskal(g: Graph): q = HeapPriorityQueue() clusters = [] T = [] for v in g.vertices(): clusters.append([v]) v.pos = len(clusters) - 1 for e in g.edges(): q.add(e._element, e) while len(T) < g.num_vertices() - 1: (w, e) = q.remove_min() v1 = g.end_vertices(e)[0] v2 = g.end_vertices(e)[1] A = clusters[v1.pos] B = clusters[v2.pos] if A != B: _merege(A, B) T.append(e) return T
def kruskal(g: Graph): q = HeapPriorityQueue() T = [] #Holder listen af edges med minimal vægt trees = {} union = unionFind() for v in g.vertices(): trees[v] = union.makeset(v) for e in g.edges(): q.add(e._element, e) while len(T) < g.num_vertices() - 1: (u, e) = q.remove_min() v1 = g.end_vertices(e)[0] v2 = g.end_vertices(e)[1] leaderv1 = union.quickFind(trees[v1]) leaderv2 = union.quickFind(trees[v2]) if leaderv1._element != leaderv2._element: union.quickUnion(leaderv1, leaderv2) T.append(e) return T