Data Structures: Disjoint Sets

Slides:



Advertisements
Similar presentations
CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!
Advertisements

1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x.
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
Union-Find: A Data Structure for Disjoint Set Operations
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Data Structure (III) GagGuy.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Lecture X Disjoint Set Operations
Union-find Algorithm Presented by Michael Cassarino.
Union Find ADT Data type for disjoint sets: makeSet(x): Given an element x create a singleton set that contains only this element. Return a locator/handle.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Nicki Dell Spring 2014.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
Partially Ordered Data ,Heap,Binary Heap
CSE 373: Data Structures and Algorithms
TCSS 342, Winter 2006 Lecture Notes
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
Top 50 Data Structures Interview Questions
Data Structures Binary Trees 1.
UNIT III TREES.
B+-Trees.
CSCI Trees and Red/Black Trees
B+ Tree.
Disjoint Sets Chapter 8.
Section 8.1 Trees.
CMSC 341 Lecture 13 Leftist Heaps
ITEC 2620M Introduction to Data Structures
Chapter Trees and B-Trees
Chapter Trees and B-Trees
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Review for Midterm Neil Tang 03/04/2010
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Data Structures: Segment Trees, Fenwick Trees
CMSC 341 Disjoint Sets Based on slides from previous iterations of this course.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Data Structures and Algorithms
Disjoint Set Neil Tang 02/23/2010
A Kind of Binary Tree Usually Stored in an Array
Disjoint Set Neil Tang 02/26/2008
B- Trees D. Frey with apologies to Tom Anastasio
CSE 373 Data Structures and Algorithms
Trees CMSC 202, Version 5/02.
CSE 332: Data Abstractions Union/Find II
CMSC 202 Trees.
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Union-Find Partition Structures
CSE373: Data Structures & Algorithms Implementing Union-Find
Union-Find.
Union-Find Partition Structures
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Lecture 20: Disjoint Sets
Running Time Analysis Union is clearly a constant time operation.
Lecture 21 Amortized Analysis
CSE 373: Data Structures and Algorithms
Data Structures Using C++ 2E
General Trees A general tree T is a finite set of one or more nodes such that there is one designated node r, called the root of T, and the remaining nodes.
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

Data Structures: Disjoint Sets

Disjoint Sets: the Union-Find tree Represent a set of disjoint sets Join sets together (union) Find which set an element belongs to Quickly test if two separate items are in the same set Note: Not the same as a vector of sets – that is much slower to go through. Does not support general set operations, mainly just union and find- set. But, this is sufficient for lots of operations It can also be augmented/adjusted to perform other operations

Implementing disjoint set Each set is a tree – store forest of trees. Tree is identified by the ID at the root. Each node knows its parent – follow to the root. Storage: array of values, array of parent indices, Initially, each item is its own parent array of tree height Actually an upper bound on height, not actual height Starts as height 0

Disjoint Set 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Union operation Merge two trees – set one tree’s root to have parent of other tree’s root. Pick the shorter tree to attach to the longer one If the trees are the same height, pick either Increase the height of the tree which is the new root by 1 Called “union-by-rank” (prevents trees getting too tall)

Union 5 & 7 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Union 5 & 7 – Find smallest tree 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Union 5 & 7 – Merge smaller into larger 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Union 7 & 11 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Union 7 & 11 – both are same size 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Union 7 & 11 – Either can be adjusted 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Find Operation Path Compression – shorten path to root when possible As you traverse the path to the root – do so recursively After determining root, set everything on path back down to that root. Over time, this compresses all paths to point to root more quickly – when lots of queries, makes them fast. Book: see section 2.4.2 for implementation Note: non-recursive versions are possible, but require keeping a stack/queue/vector of nodes along search path to update.

Find set of 8 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Find set of 8 – Visit parent of 8 = 1 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Find set of 8 – Visit parent of 1 = 2 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Find set of 8 – Visit parent of 2 = 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Find set of 8 – Parent of 5 is 5, so found set 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Find set of 8 – Update parent of 2 to 5 (same) 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Find set of 8 – Update parent of 1 to 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Find set of 8 – Update parent of 8 to 5 7 9 11 3 4 2 10 6 1 8 Node 1 2 3 4 5 6 7 8 9 10 11 Parent Size

Notice from example Size of set at 5 is still 4 (an overestimate) Everything on path from 8 to root is now going straight to root If item 8 had any children, they would also have shorter paths