Questions tagged [disjoint-sets]

Anything related to disjoint sets, i.e. mathematical sets that have no element in common.

Anything related to disjoint sets, i.e. mathematical sets that have no element in common.

194 questions
0
votes
1 answer

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

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…
Blubber
  • 1,375
  • 1
  • 15
  • 32
0
votes
2 answers

How to generate the worst case for disjoint set with only path compression?

A disjoint set with only path compression implemented is like this: // In cpp. int Find(int x) { return f[x] == x ? x : f[x] = Find(f[x]); } int Union(int a, int b) { f[Find(a)] = Find(b); } From wiki I've learned about it has a worst complexity of…
karatoga
  • 513
  • 4
  • 14
0
votes
1 answer

Prim's algorithm with Fibonacci heap: why O(E + V*log(V))?

I know Prim's algorithm and how to implement it. I am also aware of why its time complexity is O(E + V log(V)). We add edges E times (that is in O(E)) and choose minimum V times (that is O(V*log(V)). But I don't understand a part of it: Why V…
0
votes
0 answers

Naive Functional Implementation of Union Find Disjoint Set has Poor Performance

The following implemention of UFDS has poor performance. Can someone enlighten me as to why this might be? Here is the profiling report: total time = 0.10 secs (98 ticks @ 1000 us, 1 processor) total alloc = 78,869,168 bytes …
therewillbecode
  • 7,090
  • 4
  • 35
  • 42
0
votes
1 answer

Find max cost edge between two nodes in tree

Task: Given a weighted tree graph and a set of nodes pairs. For each pair (u,v) from set I need to find(effectively) maximum edge between (u,v). My approach: Using Tarjan's algoritm for each pair (u,v) we can find lowest common ancestor LCA(u,v) =…
bordus
  • 35
  • 7
0
votes
1 answer

Union-Find path compression efficiency

I found some online union-find tutorial depicted path compression technique to get even less than O(log(N)) complexity for find(), below was the path compression implementation in this blog, int root (int Arr[], int i) { while(Arr[i] != i) { …
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
0
votes
1 answer

How should Union operation in disjoint-set data structure properly implemented?

There are many different descriptions and examples for the disjoint-set structure available on-line. In some cases, for each set, it stores "rank". When a set is merged into another set, the rank of the former is increased by 1, if they are of the…
user31264
  • 6,557
  • 3
  • 26
  • 40
0
votes
1 answer

Graph Traversal count clouds [python]

Given a 2D grid skyMap composed of '1's (clouds) and '0's (clear sky), count the number of clouds. A cloud is surrounded by clear sky, and is formed by connecting adjacent clouds horizontally or vertically. You can assume that all four edges of the…
aerin
  • 20,607
  • 28
  • 102
  • 140
0
votes
1 answer

How to detect a cycle in an Undirected Graph using Disjoint Sets?

Algorithm: For each edge (u, v) in the Adjacency list: If u and v do not belong to the same set: Union(u, v) else: return true // cycle detected return false Graph: (1)-------(2) Adjacency List: [1] -> [2] [2] -> [1] Disjoint-Sets: {{1},…
Abhishek
  • 432
  • 5
  • 19
0
votes
1 answer

How might Union/Find data structures be applied to Kruskal's algorithm?

http://en.wikipedia.org/wiki/Disjoint_sets http://en.wikipedia.org/wiki/Kruskal's_algorithm Union/Find data structure being used for disjoint sets...
Devoted
  • 177,705
  • 43
  • 90
  • 110
0
votes
0 answers

What is the time/space complexity of my code for DisjointSets

I have written this code for Disjoint sets class DisjointSet{ HashMap map; public DisjointSet(){ map = new HashMap<>(); } /*Time complexity for this is just O(1) * because we are just…
0
votes
1 answer

Disjoint Sets path compression running time error

I am taking an online data structure course now. Here is my Python implementation of a merge and find algorithm. I followed the instructions, but the running time far exceeds the limits. Can anyone take a look? It should be a simple one. Thanks. We…
0
votes
1 answer

Getting the maximum and minimum size of disjoint subsets

I am writing code to perform Union-Find on a graph, The first line of input is: n m [n is number of nodes, and m is number of edges] Then m lines follow, indicating which two nodes are connected When I encounter each edge, I perform an union…
user4822631
0
votes
1 answer

What is importance of 'rank' in union by rank and path compression algorithm?

I have an understanding of the algorithm in this way... path compression helps lower time for find operation and overtime the time complexity for path compression averages out to be O(1). we decide which of the two is a parent node (during union…
95_96
  • 341
  • 2
  • 12
0
votes
1 answer

Disjoint Set in a special ways?

We implement Disjoint Data structure with tree. in this data structure makeset() create a set with one element, merge(i, j) merge two tree of set i and j in such a way that tree with lower height become a child of root of the second tree. if we do n…
user6337930