disjoint-sets

Implementing Disjoint Sets (Union Find) in C++

爱⌒轻易说出口 提交于 2019-11-29 22:28:39
I am trying to implement Disjoint Sets for use in Kruskal's algorithm, but I am having trouble understanding exactly how it should be done and in particular, how to manage the forest of trees. After reading the Wikipedia description of Disjoint Sets and after reading the description in Introduction to Algorithms (Cormen et al) I have come up with the following: class DisjointSet { public: class Node { public: int data; int rank; Node* parent; Node() : data(0), rank(0), parent(this) { } // the constructor does MakeSet }; Node* find(Node*); Node* merge(Node*, Node*); // Union }; DisjointSet:

Efficient algorithm to determine if two sets of numbers are disjoint

青春壹個敷衍的年華 提交于 2019-11-29 15:53:19
Practicing for software developer interviews and got stuck on an algorithm question. Given two sets of unsorted integers with array of length m and other of length n and where m < n find an efficient algorithm to determine if the sets are disjoint. I've found solutions in O(nm) time, but haven't found any that are more efficient than this, such as in O(n log m) time. Using a datastructure that has O(1) lookup/insertion you can easily insert all elements of first set. Then foreach element in second set, if it exists not disjoint, otherwise it is disjoint Pseudocode function isDisjoint(list1,

c++ test if 2 sets are disjoint

别等时光非礼了梦想. 提交于 2019-11-28 23:06:59
问题 I know the STL has set_difference , but I need to just know if 2 set s are disjoint. I've profiled my code and this is slowing my app down quite a bit. Is there an easy way to see if 2 sets are disjoint, or do I need to just roll my own code? EDIT: I also tried set_intersection but it took the same time... 回答1: Modified hjhill's code to reduce complexity by a factor of O(log n) by getting rid of the count() call. template<class Set1, class Set2> bool is_disjoint(const Set1 &set1, const Set2

Disjoint set as linked list

萝らか妹 提交于 2019-11-28 06:08:16
问题 Can anyone point me to some info on Disjoint sets as linked list? I cant find any code on this. Language C++ 回答1: Well I think you can find information in this page of Wikipedia. Of course, that information is written in pseudo-code, but is not difficult to translate it. 回答2: I just wrote this, if anyone is still interested. I was implementing CLRS Chap 21.1 /****************************************************************** * PROGRAM: Implementation of Linked-list representation of disjoi-*

Understanding boost::disjoint_sets

 ̄綄美尐妖づ 提交于 2019-11-27 18:41:06
I need to use boost::disjoint_sets, but the documentation is unclear to me. Can someone please explain what each template parameter means, and perhaps give a small example code for creating a disjoint_sets? As per the request, I am using disjoint_sets to implement Tarjan's off-line least common ancestors algorithm , i.e - the value type should be vertex_descriptor. What I can understand from the documentation : Disjoint need to associate a rank and a parent (in the forest tree) to each element. Since you might want to work with any kind of data you may,for example, not always want to use a map

How to code the maximum set packing algorithm?

笑着哭i 提交于 2019-11-27 02:20:33
问题 Suppose we have a finite set S and a list of subsets of S. Then, the set packing problem asks if some k subsets in the list are pairwise disjoint . The optimization version of the problem, maximum set packing, asks for the maximum number of pairwise disjoint sets in the list. http://en.wikipedia.org/wiki/Set_packing So, Let S = {1,2,3,4,5,6,7,8,9,10} and `Sa = {1,2,3,4}` and `Sb = {4,5,6}` and `Sc = {5,6,7,8}` and `Sd = {9,10}` Then the maximum number of pairwise disjoint sets are 3 ( Sa, Sc,

Understanding boost::disjoint_sets

不想你离开。 提交于 2019-11-26 19:34:01
问题 I need to use boost::disjoint_sets, but the documentation is unclear to me. Can someone please explain what each template parameter means, and perhaps give a small example code for creating a disjoint_sets? As per the request, I am using disjoint_sets to implement Tarjan's off-line least common ancestors algorithm, i.e - the value type should be vertex_descriptor. 回答1: What I can understand from the documentation : Disjoint need to associate a rank and a parent (in the forest tree) to each