def get_groups(self): groups = {} global record_list, citation_list for r in record_list: groups['r' + str(r)] = DisjointSet() for c in citation_list: groups['c' + str(c)] = DisjointSet() for c,r in filter(lambda (x, y): -1 not in [x, y], self.results): cit = 'c' + str(c) rec = 'r' + str(r) groups[cit].union(groups[rec])
def solve_cluster(graph, k): sorted_list = sort_edge(graph) disjoint_set = DisjointSet(graph.nodes()) for vertice1, vertice2, space in sorted_list: if len(disjoint_set.return_parents()) > k: if disjoint_set.find(vertice1) != disjoint_set.find(vertice2): disjoint_set.union(vertice1, vertice2) else: break for edge in sorted_list: vertice1, vertice2, space = edge if disjoint_set.find(vertice1) != disjoint_set.find(vertice2): return space return None
def kruskal(G): S = DisjointSet() for v in G.vertexes: S.make_set(v) X = set() result = [] E = G.add_edge() E = sorted(E, key=lambda x: x[2]) for u, v, w in E: if S.find_set(u) != S.find_set(v): X.add(u) X.add(v) result.append((u, v)) S.union(u, v)
def test_add_item(self): # arrange dt = DisjointSet() # act dt.add(1) dt.add(2) dt.add(3) # assert self.assertEqual(sum(dt.sizes.values()), 3) self.assertEqual(dt.parents[3], 3)
def test_all(self): disjoint_set = DisjointSet() x = Node() disjoint_set.makeset(x) self.assertEqual(x, x.parent) self.assertEqual(0, x.rank) y = Node() disjoint_set.makeset(y) disjoint_set.link(x, y) self.assertEqual(y, x.parent) self.assertEqual(y, y.parent) self.assertEqual(1, y.rank) self.assertEqual(0, x.rank)
def kruskals_algorithm_mst(graph): """ Kruskal's algorithm to find the minimum spanning tree of a connected, undirected, weighted graph. This implementation runs in O(sort(e) + (e * a(v)) + v) where a is inverse Ackermann function (https://en.wikipedia.org/wiki/Ackermann_function) and is generally considered a constant The sort in this case is O(e * log(e)) but if you can use counting sort or radix sort it can be reduced to O(e) This algorithm makes its greedy choice starting with the globally minimum weighted edge, and then continues selecting edges until it forms a spanning tree. """ if not isinstance(graph, Graph): raise TypeError( 'kruskals_algorithm_mst expects a Graph as an argument') ds = DisjointSet() T = set() for u in graph.vertices: ds.make_set(u) edges = [] visited = set() for u, v in graph.weights: if (v, u) not in visited: visited.add((u, v)) edges.append((u, v)) edges.sort(key=lambda k: graph.weights[k], reverse=True) while edges: u, v = edges.pop() if ds.find_set(u) != ds.find_set(v): ds.union(u, v) T.add((u, v)) return T
def num_cluster(): file = open("big_distance.txt", "r") lst = file.readlines() lst = [l.strip("\n").replace(" ", "") for l in lst] lst = lst[1:] vertex = set(lst) ds = DisjointSet(vertex) for vertice in vertex: one_aways = one_distance_away(vertice) for v in one_aways: if v in vertex: ds.union(vertice, v) two_aways = two_distance_away(vertice) for v in two_aways: if v in vertex: ds.union(vertice, v) return len(ds.return_parents())
def combineSets(w, h, dsets, pix, g): ds = DisjointSet(w * h) for j in range(h): for i in range(w): ds.makeSet(j * w + i, pix[i, j]) for e in g.E: p1 = ds.find(e.v1) p2 = ds.find(e.v2) if p1 != p2: for d in dsets: r1 = d.find(e.v1) r2 = d.find(e.v2) if r1 != r2: break else: ds.unionNode(p1, p2) return ds
def combineSets(w, h, dsets, pix, g): ds = DisjointSet(w*h) for j in range(h): for i in range(w): ds.makeSet(j*w + i, pix[i,j]) for e in g.E: p1 = ds.find(e.v1) p2 = ds.find(e.v2) if p1 != p2: for d in dsets: r1 = d.find(e.v1) r2 = d.find(e.v2) if r1 != r2: break else: ds.unionNode(p1, p2) return ds
def main(im, K1, MinCC1, Sigma1): # if len(sys.argv) > 2: K = int(K1) # if len(sys.argv) > 3: MinCC = int(MinCC1) # if len(sys.argv) > 4: Sigma = float(Sigma1) # print('Processing image %s, K = %d' % (sys.argv[1], K)) start = time.time() # Apply gaussian filter to all color channels separately im = Image.open('b.jpg') (width, height) = im.size l1 = 0 # print im print('Image width = %d, height = %d' % (width, height)) print('Blurring with Sigma = %f' % Sigma) source = im.split() blurred = [] # print source print len(source) for c in range(len(source)): I = numpy.asarray(source[c]) I = filter(I, gaussian(Sigma)) blurred.append(Image.fromarray(numpy.uint8(I))) im = Image.merge(im.mode, tuple(blurred)) # print blurred im.show() pix = im.load() # for j in range(height): # l1=0 # for i in range(width): # print pix[i,j] # l1+=1 # print l1 # imshow(pix) ds = DisjointSet(width * height) # print ds for j in range(height): for i in range(width): ds.makeSet(j * width + i, pix[i, j]) # print ds print('Number of pixels: %d' % len(ds.data)) g = Graph(width, height, pix) print('Number of edges in the graph: %d' % len(g.E)) print('Time: %lf' % (time.time() - start)) segstart = time.time() segment(width, height, ds, g) print('Segmentation done in %lf, found %d segments' % (time.time() - segstart, ds.num)) print('Postprocessing small components, min = %d' % MinCC) postproc = time.time() postprocess(ds, g) print('Postprocessing done in %lf' % (time.time() - postproc)) l, mat = randomColour(width, pix, ds, height) print mat print('Regions produced: %d' % l) print print('Time total: %lf' % (time.time() - start)) im.show() # mat1=[[0 for i in range(height)] for j in range(width) ] mat1 = [list(x) for x in zip(*mat)] return mat
N += 1 g1.write("%s | %d\n" % (proc, strmap[node.key])) g2.write("%s | %d\n" % (orig, strmap[node.key])) g1.close() g2.close() print "digit nodes: %d" % len(digits) print "text nodes: %d" % len(strmap) print "total nodes: %d" % (len(strmap) + len(digits))#}}} if __name__ == '__main__': N = 0 digits = {} digit_in, digit_out = defaultdict(list), defaultdict(list) strings = [] ds = DisjointSet() for t in range(1, 15+1): with open('../data/train%d.txt' % t, 'r') as f: for line in f: parts = line.strip().split(' | ') part1, part2 = parts[0], parts[1] proc1, proc2 = process(part1), process(part2) # part 1 if part1.isdigit(): if part1 not in digits: digits[part1] = N N += 1 if not part2.isdigit(): digit_out[proc2].append(part1) else: strings.append( (proc1, part1) )
print('Blurring with Sigma = %f' % Sigma) source = im.split() dsets = [] blurred = [] gra = None for c in range(len(source)): # Apply gaussian filter to all color channels separately I = numpy.asarray(source[c]) I = filter(I, gaussian(Sigma)) blur = Image.fromarray(numpy.uint8(I)) blurred.append(blur) # Create a disjoint set from the channel pix = blur.load() ds = DisjointSet(width*height) for j in range(height): for i in range(width): ds.makeSet(j*width + i, pix[i,j]) print('Number of pixels: %d' % len(ds.data)) g = Graph(width, height, pix) print('Number of edges in the graph: %d' % len(g.E)) print('Time: %lf' % (time.time() - start)) # Segment this channel segstart = time.time() segment(width, height, ds, g) print('Segmentation done in %lf, found %d segments' % (time.time() - segstart, ds.num)) # Postprocess set
def test_connected(): ds = DisjointSet(5) assert not ds.connected(1, 2) ds.union(1, 2) assert ds.connected(1, 2)
def test_union(self): # arrange dt = DisjointSet() dt.add(1) dt.add(2) dt.add(3) dt.add(4) dt.add(5) # act dt.union(1, 4) dt.union(3, 5) dt.union(5, 2) # assert self.assertEqual(dt.parents[4], 1) self.assertEqual(dt.parents[1], 1) self.assertEqual(dt.sizes[1], 2) self.assertTrue(4 not in dt.sizes) self.assertEqual(dt.parents[3], 3) self.assertEqual(dt.parents[5], 3) self.assertEqual(dt.parents[2], 3) self.assertEqual(dt.sizes[3], 3) self.assertTrue(5 not in dt.sizes) self.assertTrue(2 not in dt.sizes)
def test_count(): ds = DisjointSet(5) assert ds.count() == 5
def test_union(): ds = DisjointSet(5) assert not ds.connected(2, 3) ds.union(2, 3) assert ds.connected(2, 3)
def main(): disj1 = DisjointSet() disj1.add("a") disj1.add("c") disj1.add("b") disj2 = DisjointSet() disj2.add("c") disj2.add(["a","b","c"]) disj3 = DisjointSet() print(disj1) print(disj2) try: # test len() print("Testing len()...") if (len(disj1) != 3) or (len(disj2) != 2): raise ValueError() else: print(" %20s %20s " % ("TEST len()", "PASSED")) # test union() print("Testing union()...") disj1.union("a","c") print(disj1) if len(disj1) != 3: raise ValueError() else: print("%20s %20s" % ("TEST union()", "PASSED")) # test subset() print("Testing subset()") testSet = Set() testSet.add("c") if testSet == disj2.subset("c"): print("%20s %20s" % ("TEST subset()", "PASSED")) else: raise ValueError() except ValueError: print("TEST FAILED")
from collections import defaultdict from disjointset import DisjointSet d = defaultdict(list) vertex = int(input('enter the number of vertices: ')) Edges = int(input('enter the number of edegs: ')) X = DisjointSet(vertex) X.makeset() X.printer() ed = [] edges = defaultdict(list) for i in range(Edges): x, y, w = list(map(int, input().split())) edges[x].append((w, y)) edges[y].append((w, x)) ed.append((w, x, y)) auxillary = sorted(ed) graph = defaultdict(list) for i in auxillary: w, u, v = i print(w, u, v) if X.findset(u) != X.findset(v): graph[u].append((v, w)) graph[v].append((u, w)) X.union(u, v) print(graph)
im = Image.open(sys.argv[1]) (width, height) = im.size print('Image width = %d, height = %d' % (width, height)) print('Blurring with Sigma = %f' % Sigma) source = im.split() blurred = [] for c in range(len(source)): I = numpy.asarray(source[c]) I = filter(I, gaussian(Sigma)) blurred.append(Image.fromarray(numpy.uint8(I))) im = Image.merge(im.mode, tuple(blurred)) #im.show() pix = im.load() ds = DisjointSet(width * height) for j in range(height): for i in range(width): ds.makeSet(j * width + i, pix[i, j]) print('Number of pixels: %d' % len(ds.data)) g = Graph(width, height, pix) print('Number of edges in the graph: %d' % len(g.E)) print('Time: %lf' % (time.time() - start)) segstart = time.time() segment(width, height, ds, g) print('Segmentation done in %lf, found %d segments' % (time.time() - segstart, ds.num)) print('Postprocessing small components, min = %d' % MinCC)
class box: left = True right = True up = True down = True height = int(input("Enter Maze Height: ")) width = int(input("Enter Maze Width : ")) print() current = 1 board = [] walls = [] handler = DisjointSet(height * width) # True means there is a wall there for walls # True means we can tear down the wall for board for i in range(0, height * width): temp = box() temp.left = False temp.right = False temp.down = False temp.up = False temp2 = box() board.append(temp) walls.append(temp2) print("Preparing Maze : ", "{:.{}f}".format((((i + 1) / (width * height)) * 100), 4), "%", end="\r")
def test_find(): ds = DisjointSet(5) assert ds.find(2) == 2 assert ds.find(3) == 3