disjoint-sets

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.

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

孤人 提交于 2019-12-20 07:48:47
问题 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

Union-Find/Disjoin-Set data structure for Directed Graph

孤人 提交于 2019-12-13 15:25:50
问题 I'm looking for an efficient Union-Find (aka Disjoint Set) data structure for my directed graph, which has cycles and forests. Given such a graph G , I want to to answer following queries: Can I reach node v from u ? From which nodes, u is not reachable? For a single "source" node u , I can run DFS search and answer whether I can reach v from it. A single DFS search will have an upper-bound cost of m + n in the worst-case, where m and n are the number of vertices and edges in the graph,

Disjoint-Set forests in Python alternate implementation

被刻印的时光 ゝ 提交于 2019-12-11 23:27:41
问题 I'm implementing a disjoint set system in Python, but I've hit a wall. I'm using a tree implementation for the system and am implementing Find(), Merge() and Create() functions for the system. I am implementing a rank system and path compression for efficiency. The catch is that these functions must take the set of disjoint sets as a parameter, making traversing hard. class Node(object): def __init__(self, value): self.parent = self self.value = value self.rank = 0 def Create(values): l =

Find number of pairs of disjoint sets in a list of sets

强颜欢笑 提交于 2019-12-08 01:18:25
问题 The problem statement is as follows: given a list of n sets, each containing k integers, find the number of pairs of disjoint sets. Suppose the possible elements of the sets are positive and bounded above by c > n, and suppose k << n. I'm trying to come up with an efficient algorithm to solve this faster than O(kn^2), which is the runtime of the naive solution. The best strategy I could come up with involves iterating through each set in the list, and hashing the elements of the set, such

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 ,

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

Deadly 提交于 2019-12-07 05:36:09
问题 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

Testing for a circuit when implementing Kruskalls algorithm

允我心安 提交于 2019-12-06 23:39:24
问题 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[

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

random sampling with Pandas data frame disjoint groups

。_饼干妹妹 提交于 2019-12-05 16:43:14
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.0 16.0 18.0 25.0 414 7.0 16.0 18.0 19.0 25.0 415 16.0 18.0 19.0 18.0 25.0 665 10.0 8.0 8.0 7.0 27.0 666