def get_clusters(G: nx.Graph): S = disjoint_set.DisjointSet() V = G.nodes() for u in V: minL = math.inf minE = (-1, -1) for v in V: if u == v: continue p0 = V[u]["pos"] p1 = V[v]["pos"] l = (p1 - p0).length if l < minL: minL = l minE = (u, v) (u, v) = minE S.union(u, v) T = G.copy() for s in list(S.itersets()): for u, v in [(u, v) for u in s for v in s]: T.add_edge( u, v, ) return T
def _screen_0(self): d = np.load(self._args.prescreen) samples = d[d.keys()[0]] cd = loadmat(self._args.connectivity) cm = cd['VM'] outfn = self._args.postscreen vert_ids = [i for i in range(samples.shape[0])] djs = disjoint_set.DisjointSet(vert_ids) nz_rows, nz_cols = cm.nonzero() for r, c in progressbar(zip(nz_rows, nz_cols)): djs.union(r, c) # Old method: pickup the root of each cluster. # However root is somehow arbitrary. # screened = samples[djs.get_roots()] # New method: pick up disentangled state in each cluster cluster = djs.get_cluster() uw = self._uw screened_index = [] for k in progressbar(cluster): nondis = [] for member in cluster[k]: if not uw.is_disentangled(samples[member]): nondis.append(member) screened_index += nondis[:1] # No effect if nondis == [] screened = samples[screened_index] print('screened array shape {}'.format(screened.shape)) np.savez(outfn, ReTouchQ=screened)
def boruvka_alg(g): u = su.DisjointSet(g.n) prev_count = None it = 0 selected_ids = {} while prev_count is None or prev_count != u.count: it += 1 print(f"\n\n------------- Iteration {it} ----------------\n") print(u.count, prev_count) prev_count = u.count component_min = {} for vertex in range(g.n): for edge_id in range(g.rowsIndices[vertex], g.rowsIndices[vertex+1]): start_root = u.find(vertex) end_root = u.find(g.endV[edge_id]) if start_root == end_root: continue weight = g.weights[edge_id] if start_root not in component_min or weight < component_min[start_root][2]: component_min[start_root] = (vertex, g.endV[edge_id], weight, edge_id) if end_root not in component_min or weight < component_min[end_root][2]: component_min[end_root] = (vertex, g.endV[edge_id], weight, edge_id) for root, min_edge in component_min.items(): print(f"marked [{min_edge[1]}], start [{min_edge[0]}], weight [{min_edge[2]}] [id {min_edge[3]}]\n") u.union(min_edge[0], min_edge[1]) selected_ids[min_edge[3]] = True total_weight = 0 edges = list(sorted(list(selected_ids))) remap_edges(g, edges) for edge_id in selected_ids: total_weight += g.weights[edge_id] print(f"Total weight: {total_weight}") return edges
def main(file): dj = disjoint_set.DisjointSet() for line in file: one, _, *rest = line.split() rest = [r.strip(',') for r in rest] for r in rest: dj.union(one, r) print(sum(1 for _ in dj.find_group('0'))) print(len(dj))
def kruskal(self): i, e = 0, 0 #our iterable variables ds = dst.DisjointSet(self.nodes) #create our disjointed set with nodes self.graph = sorted(self.graph, key=lambda item: item[2]) #sort the graph while e < self.V - 1: s, d, w = self.graph[i] #start, destination, weight i += 1 x = ds.find(s) #find source of start y = ds.find(d) #find source of distance if x != y: #if they dont have the same parent we add them to our minimum spanning tree e += 1 #increase our iterable self.MST.append([s, d, w]) ds.union(x, y) self.printSolution(s, d, w)
def kruskal_min_spanning_tree(G : nx.DiGraph): Q = [] S = disjoint_set.DisjointSet() for e in G.edges(): u,v = e w = G.edges()[u,v]["weight"] heapq.heappush(Q, (w, e)) T = nx.DiGraph() while Q: w, (u,v) = heapq.heappop(Q) if S.connected(u,v): continue S.union(u,v) T.add_edge(u,v, weight=w) return T
def _ComputeTree(self, edge_dict, vertex_mult_dict, vertices): weight_queues = self._ComputeWeightDegreeQueue(edge_dict, vertex_mult_dict) tree_edges = dict() num_processed_edges = 0 vertex_ds = disjoint_set.DisjointSet(vertices) for w in sorted(weight_queues.keys()): curr_queue = weight_queues[w] while not curr_queue.empty(): edge_pair = curr_queue.get() weight = edge_pair[0] edge = edge_pair[1] if vertex_ds.find_index(edge[0]) == vertex_ds.find_index( edge[1]): continue num_processed_edges += 1 tree_edges[edge] = edge_dict[edge] vertex_ds.union(edge[0], edge[1]) # print "# processed: " + str(num_processed_edges) + ' # original edges: ' + str(len(edge_dict)) return tree_edges
def setUp(self) -> None: self.dset = disjoint_set.DisjointSet()