def Kruskals(G): #put all the vertices into a set so that we don't have duplicates in the parent or rank arrays for i in G: x = UnionFind(len(vertices)) if i[1] in vertices or i[2] not in vertices: vertices.add(i[2]) x.makeset(i[2]) print(vertices) if i[1] not in vertices or i[2] in vertices: vertices.add(i[1]) x.makeset(i[1]) print(vertices) if i[1] in vertices and i[2] in vertices: continue
def kruskal(self): Weight = {} Trees = [] UnionSet = UnionFind() UnionSet.makeset(self.V()) for v in self.V(): edge = self.Adj(v) for u in edge: uv = u + v Weight[uv] = self.Weight(u,v) #edges = [(self.Weight(u,v),u,v) for v in self.V() for u in self.Adj(v)].sort() edges = [(self.Weight(u,v),v,u) for v in self.V() for u in self.Adj(v)] edges.sort() for w,u,v in edges: if UnionSet.find(u) != UnionSet.find(v): Trees.append(u+v) UnionSet.union(u,v) return Trees