Assume that we have a set of n disjoint nodes {node1,node1,...,noden}
What is the fastest data structure and algorithm for the following 3 operations:
Union(x,y): add an un-directed edge between nodex and nodey, there can be at most one edge between two nodes.
IsConnected(x,y): returns true if nodex and nodey are connected directly or indirectly, i.e nodex and nodey are in the the same connected component.
Un-union(x,y): remove the edge between nodex and nodey if it exists.
Disjoint-set is a perfect data structure for the first two operations, but it cannot support the 3rd operation directly. What is the alternative?
If we simulate the process, the first and 3rd operations can be implemented in O(1) but 2nd operation is O(n), so I would like to see if it is possible to make all three operations run in O(logn) time or less.
To answer your question, if time to delete is the most important perspective, the LinkedList should be chosen.
In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that stores a collection of disjoint (non-overlapping) sets. Equivalently, it stores a partition of a set into disjoint subsets.
The disjoint set data structure is also known as union-find data structure and merge-find set. It is a data structure that contains a collection of disjoint or non-overlapping sets.
Link/cut tree may perform these 3 operations in O(log N) time.
You can read about Link/cut tree and related data structures in this book: "Handbook of Data Structures and Applications" (Chapter 35).
Link/cut tree does not allow adding an edge between nodes, that are already (indirectly) connected. If you need "Union(x,y)" operation to support this, the problem becomes more complicated, and you can solve it as dynamic connectivity problem in undirected graphs. (See chapter 36.4 in the same book or this pdf). In this case insertion/deletion complexity increases to O(log2 N).
Kaplan, Shafrir and Tarjan propose a general technique to add deletion to union-find data structures by incremental rebuilding and show deletion takes O(t_f(n) + t_i(n)) which are the costs for finding and inserting a node, respectively. The general idea is keeping the number of deleted items in each set and rebuilding the set when this number reaches a certain threshold.
Alstrup ,Gørtz , Rauhe, Thorup and Zwick show how to implement deletions in constant time by noting which elements in a tree are occupied and which are vacant and "tidying up" the tree after delete operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With