class DsuGraph: def __init__(self, n): self.n = n self.dsu = DSU(n) self.edges = dict() def refine(self, u, v): u, v = (u, v) if u < v else (v, u) if (u, v) not in self.edges: self.edges[(u, v)] = 0 return u, v def add_edge(self, u, v): u, v = self.refine(u, v) self.edges[(u, v)] += 1 def remove_edge(self, u, v): u, v = self.refine(u, v) self.edges[(u, v)] -= 1 if self.edges[(u, v)] < 0: print "Non-existing edge is deleted" raise def cc(self): for edge in self.edges.keys(): if self.edges[edge] > 0: u, v = edge self.dsu.union(u - 1, v - 1) result = dict() for i in xrange(0, self.n): p = self.dsu.find(i) if p not in result: result[p] = [] result[p].append(i + 1) return sorted([sorted(result[v]) for v in result.keys()])
def cc(self): cur_cc = dict([(i, [i]) for i in xrange(self.n)]) dsu = DSU(self.n) for lev in xrange(self.k): # computing sketches for level lev sketch_sum = dict() for key in cur_cc.keys(): component = cur_cc[key] sketch_sum[key] = self.sketch[lev][component[0]] for j in xrange(1, len(component)): add_sketch = self.sketch[lev][component[j]] sketch_sum[key] = sketch_sum[key].sum(add_sketch) # sampling and union for key in sketch_sum.keys(): edge_num, val = sketch_sum[key].sample() if (edge_num, val) != (0, 0): u, v = self.decode_edge(edge_num) print(u, v, edge_num, self.n) dsu.union(u, v) # forming new components cur_cc = dict() for i in xrange(self.n): parent = dsu.find(i) if parent not in cur_cc: cur_cc[parent] = [] cur_cc[parent].append(i) return sorted( [sorted([u + 1 for u in cur_cc[k]]) for k in cur_cc.keys()])
def cc(self): cur_cc = dict([(i, [i]) for i in xrange(self.n)]) dsu = DSU(self.n) for lev in xrange(self.k): # computing sketches for level lev sketch_sum = dict() for key in cur_cc.keys(): component = cur_cc[key] sketch_sum[key] = self.sketch[lev][component[0]] for j in xrange(1, len(component)): add_sketch = self.sketch[lev][component[j]] sketch_sum[key] = sketch_sum[key].sum(add_sketch) # sampling and union for key in sketch_sum.keys(): edge_num, val = sketch_sum[key].sample() if (edge_num, val) != (0, 0): u, v = self.decode_edge(edge_num) print (u, v, edge_num, self.n) dsu.union(u, v) # forming new components cur_cc = dict() for i in xrange(self.n): parent = dsu.find(i) if parent not in cur_cc: cur_cc[parent] = [] cur_cc[parent].append(i) return sorted([sorted([u + 1 for u in cur_cc[k]]) for k in cur_cc.keys()])
def kruskal(graph): """ Use union-find """ all_edges = [] for start in graph: for tail in graph[start]: all_edges.append(Edge(graph[start][tail], start, tail)) sorted_edges = sorted(all_edges) dsu = DSU(len(graph) + 1) result = [] for edge in sorted_edges: if dsu.find_set(edge.start) != dsu.find_set(edge.end): dsu.union_sets(edge.start, edge.end) result.append(edge) print(result)
def clustering(graph, k): """ Use union-find """ all_edges = [] for start in graph: for tail in graph[start]: all_edges.append(Edge(graph[start][tail], start, tail)) sorted_edges = sorted(all_edges) dsu = DSU(len(graph) + 1) result = [] num_clusters = len(graph) for edge in sorted_edges: if dsu.find_set(edge.start) != dsu.find_set(edge.end): if num_clusters == k: print(edge) return dsu.union_sets(edge.start, edge.end) result.append(edge) num_clusters -= 1
def __init__(self, n): self.n = n self.dsu = DSU(n) self.edges = dict()
def eller_optimized(maze_graphics, maze): dsu = DSU() dsu.set_size(maze.x_size * maze.y_size) #processing the rows except the last for y in range(1, maze.y_size): for x in range(1, maze.x_size): #if random > 0.5 delete wall if dsu.find(x - 1 + (y - 1) * maze.x_size) != dsu.find( x + (y - 1) * maze.x_size) and random.random() > 0.5: dsu.union(x - 1 + (y - 1) * maze.x_size, x + (y - 1) * maze.x_size) maze.set_wall(x, y, x + 1, y, ' ') classes = {} classes_count = {} for x in range(1, maze.x_size + 1): current_class = dsu.find(x - 1 + (y - 1) * maze.x_size) if current_class not in classes: classes[current_class] = [x] classes_count[current_class] = 0 else: classes[current_class].append(x) #maze.set_cell(x, y, current_class) if maze_graphics: input("press enter to continue") maze_graphics.redraw(maze) for x in range(1, maze.x_size + 1): #if random > 0.5 delete wall if random.random() > 0.5: dsu.union(x - 1 + (y - 1) * maze.x_size, x - 1 + y * maze.x_size) maze.set_wall(x, y, x, y + 1, ' ') current_class = dsu.find(x - 1 + (y - 1) * maze.x_size) classes_count[current_class] += 1 for key, val in classes_count.items(): if val == 0: x = random.choice(classes[key]) dsu.union(x - 1 + (y - 1) * maze.x_size, x - 1 + y * maze.x_size) maze.set_wall(x, y, x, y + 1, ' ') current_class = maze.get_cell(x, y) ''' for x in range(1, maze.x_size + 1): current_class = dsu.find(x-1 + y*maze.x_size) maze.set_cell(x, y + 1, current_class) ''' if maze_graphics: input("press enter to continue") maze_graphics.redraw(maze) #the last row processing y = maze.y_size for x in range(1, maze.x_size): if dsu.find(x - 1 + (y - 1) * maze.x_size) != dsu.find(x + (y - 1) * maze.x_size): maze.set_wall(x, y, x + 1, y, ' ') dsu.union(x - 1 + (y - 1) * maze.x_size, x + (y - 1) * maze.x_size) ''' for x in range(1, maze.x_size + 1): current_class = dsu.find(x-1 + (y-1)*maze.x_size) maze.set_cell(x, y, current_class) ''' if maze_graphics: input("press enter to continue") maze_graphics.redraw(maze)