union-find

Printing out nodes in a disjoint-set data structure in linear time

风流意气都作罢 提交于 2019-12-04 11:43:33
I'm trying to do this exercise in Introduction to Algorithms by Cormen et al that has to do with the Disjoin Set data structure: Suppose that we wish to add the operation PRINT-SET(x) , which is given a node x and prints all the members of x 's set, in any order. Show how we can add just a single attribute to each node in a disjoint-set forest so that PRINT-SET(x) takes time linear in the number of members of x 's set , and the asymptotic running times of the other operations are unchanged. Assume that we can print each member of the set in O(1) time. Now, I'm quite sure that the attribute

Weighted Quick-Union with Path Compression algorithm

让人想犯罪 __ 提交于 2019-12-03 09:41:09
问题 There is a "Weighted Quick-Union with Path Compression" algorithm. The code: public class WeightedQU { private int[] id; private int[] iz; public WeightedQU(int N) { id = new int[N]; iz = new int[N]; for(int i = 0; i < id.length; i++) { iz[i] = i; id[i] = i; } } public int root(int i) { while(i != id[i]) { id[i] = id[id[i]]; // this line represents "path compression" i = id[i]; } return i; } public boolean connected(int p, int q) { return root(p) == root(q); } public void union(int p, int q)

Avoiding IORefs in pure code

僤鯓⒐⒋嵵緔 提交于 2019-12-03 01:18:29
I noticed that Data.UnionFind uses the IO monad to provide pointers via IORefs. I imagine everyone happily calls unsafePerformIO when using it locally in pure code, since the data structure is so well understood, but .. Is there a canonical cleaner approach to such data structures? Perhaps a wrapper around IO that makes the inevitable unsafePerformIO less unsafe "looking" by prohibiting most IO operations? Don Stewart Is there a canonical cleaner approach to such data structures? Perhaps a wrapper around IO that makes the inevitable unsafePerformIO less unsafe "looking" by prohibiting most IO

Weighted Quick-Union with Path Compression algorithm

主宰稳场 提交于 2019-12-03 01:13:49
There is a "Weighted Quick-Union with Path Compression" algorithm. The code: public class WeightedQU { private int[] id; private int[] iz; public WeightedQU(int N) { id = new int[N]; iz = new int[N]; for(int i = 0; i < id.length; i++) { iz[i] = i; id[i] = i; } } public int root(int i) { while(i != id[i]) { id[i] = id[id[i]]; // this line represents "path compression" i = id[i]; } return i; } public boolean connected(int p, int q) { return root(p) == root(q); } public void union(int p, int q) // here iz[] is used to "weighting" { int i = root(p); int j = root(q); if(iz[i] < iz[j]) { id[i] = j;

Set union algorithm using vector in C++

£可爱£侵袭症+ 提交于 2019-12-01 20:18:21
问题 I'm only using std::vector in this problem, and I can guarantee no duplicates in each vector (but there isn't any order in each vector). How do I union the vectors I have? Example: If I have following vectors... 1 1 3 2 5 5 4 2 4 4 2 After the union I should have only two vectors left: 1 2 3 4 5 Again I'm only using vector, std::set isn't allowed. 回答1: You can use std::set_union algorithm. int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); // 0 0 0 0 0 0 0

Set union algorithm using vector in C++

只愿长相守 提交于 2019-12-01 18:25:45
I'm only using std::vector in this problem, and I can guarantee no duplicates in each vector (but there isn't any order in each vector). How do I union the vectors I have? Example: If I have following vectors... 1 1 3 2 5 5 4 2 4 4 2 After the union I should have only two vectors left: 1 2 3 4 5 Again I'm only using vector, std::set isn't allowed. You can use std::set_union algorithm. int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0 std::vector<int>::iterator it; std::sort (first,first+5); // 5 10 15 20 25 std::sort (second,second+5

Union-Find: retrieve all members of a set efficiently

血红的双手。 提交于 2019-11-30 13:38:53
I'm working with an union-find algorithm. In the first part of my program, the algorithm computes a partition of a big set E . After that, I want to retrieve all the members of the set S , which contains a given node x . Until now, naively, I was testing membership of all elements of E to the set S . But yesterday I was reading "Introduction to Algorithms" (by CLRS, 3rd edition, ex. 21.3-4), and, in the exercises, I found that: Suppose that we wish to add the operation PRINT-SET(x) , which is given a node x and prints all the members of x 's set, in any order. Show how we can add just a single

Union-find data structure

☆樱花仙子☆ 提交于 2019-11-30 07:05:27
For many problems I see the solution recommended is to use a union-find data structure. I tried to read about it and think about how it is implemented (using C++). My current understanding is that it is nothing but a list of sets. So to find which set an element belongs we require n*log n operations. And when we have to perform union, then we have to find the two sets which needs to be merged and do a set_union on them. This doesn't look terribly efficient to me. Is my understanding of this data structure correct or am I missing something? Atif Hussain This is quite late reply, but this has

Union-Find: retrieve all members of a set efficiently

江枫思渺然 提交于 2019-11-29 19:46:00
问题 I'm working with an union-find algorithm. In the first part of my program, the algorithm computes a partition of a big set E . After that, I want to retrieve all the members of the set S , which contains a given node x . Until now, naively, I was testing membership of all elements of E to the set S . But yesterday I was reading "Introduction to Algorithms" (by CLRS, 3rd edition, ex. 21.3-4), and, in the exercises, I found that: Suppose that we wish to add the operation PRINT-SET(x) , which is

Union find implementation using Python

好久不见. 提交于 2019-11-27 15:11:06
So here's what I want to do: I have a list that contains several equivalence relations: l = [[1, 2], [2, 3], [4, 5], [6, 7], [1, 7]] And I want to union the sets that share one element. Here is a sample implementation: def union(lis): lis = [set(e) for e in lis] res = [] while True: for i in range(len(lis)): a = lis[i] if res == []: res.append(a) else: pointer = 0 while pointer < len(res): if a & res[pointer] != set([]) : res[pointer] = res[pointer].union(a) break pointer +=1 if pointer == len(res): res.append(a) if res == lis: break lis,res = res,[] return res And it prints [set([1, 2, 3, 6,