disjoint-sets

Implementing equivalence relations in C++ (using boost::disjoint_sets)

为君一笑 提交于 2019-12-05 11:13:28
Assume you have many elements, and you need to keep track of the equivalence relations between them. If element A is equivalent to element B, it is equivalent to all the other elements B is equivalent to. I am looking for an efficient data structure to encode this information. It should be possible to dynamically add new elements through an equivalence with an existing element, and from that information it should be possible to efficiently compute all the elements the new element is equivalent to. For example, consider the following equivalence sets of the elements [0,1,2,3,4]: 0 = 1 = 2 3 = 4

Testing for a circuit when implementing Kruskalls algorithm

谁说胖子不能爱 提交于 2019-12-05 03:40:50
I'm trying to write a program that would find the minimum spanning tree. But one problem I am having with this algorithm, is testing for a circuit. What would be the best way to do this in java. Ok here is my code import java.io.*; import java.util.*; public class JungleRoads { public static int FindMinimumCost(ArrayList graph,int size) { int total = 0; int [] marked = new int[size]; //keeps track over integer in the mst //convert an arraylist to an array List<String> wrapper = graph; String[] arrayGraph = wrapper.toArray(new String[wrapper.size()]); String[] temp = new String[size]; HashMap

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

Algorithm: use union find to count number of islands

你离开我真会死。 提交于 2019-12-04 02:10:46
问题 Suppose you need to count the number of islands on a matrix {1, 1, 0, 0, 0}, {0, 1, 0, 0, 1}, {1, 0, 0, 1, 1}, {0, 0, 0, 0, 0}, {1, 0, 1, 0, 1} We could simply use DFS or BFS when the input matrix size can be fitting into the memory. However, what do we do if the input matrix is really large which could not be fitting into the memory? I could chunk/split the input matrix into different small files and read them respectively. But how to merge them? I got stuck at how to merge them. I have the

Union/find algorithm without union by rank for disjoint-set forests data structure

て烟熏妆下的殇ゞ 提交于 2019-12-03 15:44:42
问题 Here's a breakdown on the union/find algorithm for disjoint set forests on wikipedia: Barebone disjoint-set forests... ( O(n) ) ... with union by rank ... (now improved to O(log(n) ) ... with path compression (now improved to O(a(n)) , effectively O(1) ) Implementing union by rank necessitates that each node keeps a rank field for comparison purposes. My question is, is union by rank worth this additional space? What happens if I skip union by rank and just do path compression instead? Is it

Union/find algorithm without union by rank for disjoint-set forests data structure

佐手、 提交于 2019-12-03 06:03:55
Here's a breakdown on the union/find algorithm for disjoint set forests on wikipedia : Barebone disjoint-set forests... ( O(n) ) ... with union by rank ... (now improved to O(log(n) ) ... with path compression (now improved to O(a(n)) , effectively O(1) ) Implementing union by rank necessitates that each node keeps a rank field for comparison purposes. My question is, is union by rank worth this additional space? What happens if I skip union by rank and just do path compression instead? Is it good enough? What is the amortized complexity now? A comment is made that implies that union by rank

Detect if a graph is bipartite using union find (aka disjoint sets)

不想你离开。 提交于 2019-12-02 14:35:52
I'm doing a problem on Spoj that basically reduces to detecting if a graph is bipartite. I'm trying to just color the graph using dfs, but it is too slow. Some guy comments this No bfs, no dfs, no bipartie graph. Simple Union-Find Set would make it (with speed, indeed). Hint #1: Cycle of even length does not affect the divisibility by 2 ( wow, so many i in a word ) of length of paths between two node. Hint #2: (SPOILER) let dist[i] be the distance of a path from i to parent[i]. update it with the find and union function. It can be improved to make dist a bool array. Could someone explain what

Implementing Disjoint Set System In Python

不想你离开。 提交于 2019-12-01 20:23:04
问题 What I have so far is largely based off page 571 of "Introduction To Algorithms" by Cormen et al. I have a Node class in python that represents a set: class Node: def __init__(self, parent, rank = 0): self.parent = parent self.rank = rank This implementation is going to use a List of Nodes as the forest (I am open to better ways to store the sets). Initialize() returns a list of Nodes, that I will store in variable set and pass into the other functions. Find searches through the forest for

Implementing Disjoint Set System In Python

三世轮回 提交于 2019-12-01 19:51:19
What I have so far is largely based off page 571 of "Introduction To Algorithms" by Cormen et al. I have a Node class in python that represents a set: class Node: def __init__(self, parent, rank = 0): self.parent = parent self.rank = rank This implementation is going to use a List of Nodes as the forest (I am open to better ways to store the sets). Initialize() returns a list of Nodes, that I will store in variable set and pass into the other functions. Find searches through the forest for the value and returns the set that it appears in. I chose to use for s in range(len(set)): so that in the

c++ test if 2 sets are disjoint

早过忘川 提交于 2019-11-30 02:09:02
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... 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 &set2) { if(set1.empty() || set2.empty()) return true; typename Set1::const_iterator it1 = set1.begin(),