disjoint-sets

set of vertex-disjoint cycles so that each vertex belongs to a cycle

徘徊边缘 提交于 2021-02-07 20:56:59
问题 Here I have a directed graph G. I need to to determine whether there exists a set of vertex-disjoint cycles so that each vertex belongs to a cycle. I'm not sure if this can be done in polynomial time or if its NP-Complete? Can anyone atleast point me in the right direction? 回答1: Split each vertex into an "in" vertex and an "out" vertex. Then a vertex-disjoint cycle cover corresponds to a perfect matching on this graph. You can find out the answer to your question as fast as you can find

How to properly implement disjoint set data structure for finding spanning forests in Python?

落爺英雄遲暮 提交于 2021-02-05 08:34:51
问题 Recently, I was trying to implement the solutions of google kickstater's 2019 programming questions and tried to implement Round E's Cherries Mesh by following the analysis explanation. Here is the link to the question and the analysis. https://codingcompetitions.withgoogle.com/kickstart/round/0000000000050edb/0000000000170721 Here is the code I implemented: t = int(input()) for k in range(1,t+1): n, q = map(int,input().split()) se = list() for _ in range(q): a,b = map(int,input().split()) se

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

Implementing Disjoint Sets (Union Find) in C++

余生长醉 提交于 2019-12-29 14:20:11
问题 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

Implementing Disjoint Sets (Union Find) in C++

自作多情 提交于 2019-12-29 14:20:11
问题 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

Efficient algorithm to determine if two sets of numbers are disjoint

醉酒当歌 提交于 2019-12-29 09:12:28
问题 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. 回答1: Using a datastructure that has O(1) lookup/insertion you can easily insert all elements of first set. Then foreach element

How to get list of all elements from a 'Disjoint Sets'

时光总嘲笑我的痴心妄想 提交于 2019-12-25 07:06:02
问题 In my problem, I have a bunch of Elements (Class Element). Say I have 1000 Elements. These Elements are initially un-associated which means they are in their own sets. Later I need to use the union operation to merge some of these sets based of my program flow. I plan to use the boost library's disjoint_set (http://www.boost.org/doc/libs/1_57_0/libs/disjoint_sets/disjoint_sets.html) My question is how is it possible to list the Elements in a set given the representative of the set. Is

Disjoint set implementation in Python

扶醉桌前 提交于 2019-12-23 20:12:02
问题 I am relatively new to Python. I am studying Disjoint sets, and implemented it as follows: class DisjointSet: def __init__(self, vertices, parent): self.vertices = vertices self.parent = parent def find(self, item): if self.parent[item] == item: return item else: return self.find(self.parent[item]) def union(self, set1, set2): self.parent[set1] = set2 Now in the driver code: def main(): vertices = ['a', 'b', 'c', 'd', 'e', 'h', 'i'] parent = {} for v in vertices: parent[v] = v ds =

Disjoint set implementation in Python

大城市里の小女人 提交于 2019-12-23 19:50:27
问题 I am relatively new to Python. I am studying Disjoint sets, and implemented it as follows: class DisjointSet: def __init__(self, vertices, parent): self.vertices = vertices self.parent = parent def find(self, item): if self.parent[item] == item: return item else: return self.find(self.parent[item]) def union(self, set1, set2): self.parent[set1] = set2 Now in the driver code: def main(): vertices = ['a', 'b', 'c', 'd', 'e', 'h', 'i'] parent = {} for v in vertices: parent[v] = v ds =

random sampling with Pandas data frame disjoint groups

淺唱寂寞╮ 提交于 2019-12-22 09:24:04
问题 I need to randomly separate a data frame into two disjoint sets by the attribute 'ids' . For example, consider the following data frame: df= Out[470]: 0 1 2 3 ids 0 17.0 18.0 16.0 15.0 13.0 1 18.0 16.0 15.0 15.0 13.0 2 16.0 15.0 15.0 16.0 13.0 131 12.0 8.0 21.0 19.0 14.0 132 8.0 21.0 19.0 20.0 14.0 133 21.0 19.0 20.0 9.0 14.0 248 NaN NaN 12.0 11.0 17.0 249 NaN 12.0 11.0 10.0 17.0 250 12.0 11.0 10.0 NaN 17.0 287 3.0 3.0 1.0 8.0 20.0 288 3.0 1.0 8.0 3.0 20.0 289 1.0 8.0 3.0 3.0 20.0 413 21.0 7