union-find

Find the number of connected components in undirected graph using Union Find

落爺英雄遲暮 提交于 2021-01-29 14:39:49
问题 I've been staring at this algorithm for a while but couldn't spot what I've missed. Logic: Init all isolated components union frm, to component in each edge and decrement # components if those components aren't already connected. Question: how come that I get this -48 when running this particular test case? def countComponents(self, n, edges): """ :type n: int :type edges: List[List[int]] :rtype: int """ roots = [i for i in range(n)] # track disconnected components # n isolated islands for u,

How do path compression and union by rank complement each other?

谁说胖子不能爱 提交于 2020-08-20 07:43:26
问题 I have been reading about union-find problem. The two main improvements are path compression and union by rank. As far as I understand union by rank is used to determine how to combine disjoint trees. If we have two disjoint trees T1 and T2, then we attach the root of the tree with smaller rank to the tree with higher rank. If we don't use path compression then rank is just the depth of a tree. This makes sense since we don't want to increase the depth of out tree,since it directly affects

Finding cycles: DFS versus union-find?

南楼画角 提交于 2020-04-13 06:07:40
问题 DFS with coloring would take O(V+E) vs union find would take O(ElogV) reference: http://www.geeksforgeeks.org/detect-cycle-undirected-graph/ So union find approach is slower. If V = 100, E = 100, DFS = 200, Union find is 1,000. Is there a reason to use Union find? I personally like it because it produces a clean code. Or anything I missed that union find is better in real practice? 回答1: I suspect that you may be misinterpreting how big-O notation works. The notation O(V + E) doesn't mean "the

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

我只是一个虾纸丫 提交于 2019-12-21 19:57:05
问题 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.

Avoiding IORefs in pure code

萝らか妹 提交于 2019-12-20 11:12:41
问题 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? 回答1: Is there a canonical cleaner approach to such data structures? Perhaps a wrapper

Avoiding IORefs in pure code

邮差的信 提交于 2019-12-20 11:12:36
问题 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? 回答1: Is there a canonical cleaner approach to such data structures? Perhaps a wrapper

Union-find data structure

不羁岁月 提交于 2019-12-18 12:22:35
问题 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

Do I need to take explicit actions to facilitate sharing with persistent data structures?

拜拜、爱过 提交于 2019-12-07 14:32:33
问题 I come from an imperative background and am trying to implement a simple disjoint sets (“union-find”) data structure to get some practice with creating and modifying (persistent) data structures in Haskell. The goal is to have a simple implementation, but I am also concerned about efficiency, and my question is related to this. First, I created a disjoint-set forest implementation with union by rank and started by defining a data type for a “point”: data Point = Point { _value :: Int ,

Do I need to take explicit actions to facilitate sharing with persistent data structures?

爱⌒轻易说出口 提交于 2019-12-06 04:07:04
I come from an imperative background and am trying to implement a simple disjoint sets (“union-find”) data structure to get some practice with creating and modifying (persistent) data structures in Haskell. The goal is to have a simple implementation, but I am also concerned about efficiency, and my question is related to this. First, I created a disjoint-set forest implementation with union by rank and started by defining a data type for a “point”: data Point = Point { _value :: Int , _parent :: Maybe Point , _rank :: Int } deriving Show A disjointed set forest is an IntMap with Int → Point