Data Structure (III) GagGuy.

Slides:



Advertisements
Similar presentations
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Union-Find: A Data Structure for Disjoint Set Operations
Disjoint Union / Find CSE 373 Data Structures Lecture 17.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Heaps Heaps are used to efficiently implement two operations:
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Chapter 14 Advanced Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Featuring Minimum Spanning Trees HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx)
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >
Trees By Charl du Plessis. Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 9. Disjoint Sets.
CMSC 341 Disjoint Sets. 8/3/2007 UMBC CMSC 341 DisjointSets 2 Disjoint Set Definition Suppose we have an application involving N distinct items. We will.
CMSC 341 Disjoint Sets Textbook Chapter 8. Equivalence Relations A relation R is defined on a set S if for every pair of elements (a, b) with a,b  S,
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
1 Searching the dictionary ADT binary search binary search trees.
Union-find Algorithm Presented by Michael Cassarino.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Set Representation S 1 ={0, 6, 7, 8}, S 2 ={1, 4, 9}, S 3 ={2, 3, 5} Two operations considered here  Disjoint set union S 1  S 2 ={0,6,7,8,1,4,9}  Find(i):
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.
Segment Trees Basic data structure in computational geometry. Computational geometry.  Computations with geometric objects.  Points in 1-, 2-, 3-, d-space.
Fundamental Data Structures and Algorithms Peter Lee April 24, 2003 Union-Find.
1 Disjoint Set Data Structure. 2 Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and.
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
1 The Disjoint Set ADT CS146 Chapter 8 Yan Qing Lei.
1 Today’s Material The dynamic equivalence problem –a.k.a. Disjoint Sets/Union-Find ADT –Covered in Chapter 8 of the textbook.
Minimum Spanning Trees Featuring Disjoint Sets HKOI Training 2006 Liu Chi Man (cx) 25 Mar 2006.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
CMSC 341 Disjoint Sets. 2 Disjoint Set Definition Suppose we have N distinct items. We want to partition the items into a collection of sets such that:
Interval S = [3,10]  {x | 3 ≤ x ≤ 10} Closed segment S = (3,10)  {x | 3 < x < 10} Opened segment S = [3,3]  {3} Point.
Exam 3 Review Data structures covered: –Hashing and Extensible hashing –Priority queues and binary heaps –Skip lists –B-Tree –Disjoint sets For each of.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
CSE 373: Data Structures and Algorithms
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
Disjoint Sets Chapter 8.
Review for Midterm Neil Tang 03/04/2010
Data Structures: Disjoint Sets
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Course Outline Introduction and Algorithm Analysis (Ch. 2)
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.
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
Heaps and Priority Queues
CSE 332: Data Structures Disjoint Set Union/Find
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
CSE373: Data Structures & Algorithms Implementing Union-Find
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Dynamic Equivalence Problem
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

Data Structure (III) GagGuy

Overview Disjoint sets Segment tree Treap (If we have time)

Review Binary Search Tree Heap (Priority Queue) Hash Tables

The Union-Find Problem N balls initially, each ball in its own bag Label the balls 1, 2, 3, ..., N Two kinds of operations: Pick two bags, put all balls in bag 1 into bag 2 (Union) Given 2 balls, ask whether they belongs to the same bag (Find)

Disjoint Set A forest is a collection of trees Each bag is represented by a rooted tree, with the root being the representative ball 1 5 3 6 4 2 7

Disjoint Set Find(x) Union(x, y) Traverse from x up to the root Merge the two trees containing x and y

Disjoint Set Initial: Union 1 3: Union 2 4: Find 4: 1 3 4 2 1 3 4 2 1

Disjoint Set Representing the tree Parent array Parent[x] := parent of x If x is a root, then parent[x] = x

Disjoint Set int find(int x){ while (parent[x]!=x) x = parent[x]; return x; } void union(int x,int y){ parent[y] = x;

Disjoint Set Worst case Improvements O(NM) Union by rank Path compressions

Disjoint Set – Union by rank We should avoid tall trees Root of the taller tree becomes the new root when union So, keep track of tree heights (ranks) Good Bad

Disjoint Set – Path Compression Find(4) 3 5 1 6 4 2 7 The root is 3 3 5 1 6 4 2 7 3 5 1 6 4 2 7

Disjoint Set Time complexity using Union by rank + Path compression O((N)) for each query Amortized time (N)  5 for practically large N

Range Maximum Query Given an integer array A Query(x,y) Update(x,val) Ask for the maximum element in A[x] to A[y] Update(x,val) Set A[x] to val

Segment Tree Binary Tree Each node represent a segment Root = [1,N] Parent = [x,y] Left child = [x, (x+y)/2] Right child = [(x+y)/2+1, y] Tree height = lg N

Segment Tree

Range Maximum Query Given an integer array A Query(x,y) Ask for the maximum element in A[x] to A[y] Each node with interval [l,h] Store the maximum element in A[l] to A[h]

Segment Tree Build O(N) Query O(log N) Update

Example 1 Given an array A with N elements. Q operations Update(x,y,v): add v to A[x], A[x+1], .. , A[y] Query(x): find the value of A[x]

Example 2 Given an array A with N elements. Q operations Find(): return the maximum subsequence sum Update(x,v): change the value of A[x] to v

Example 3 There is a 1 x N wall Each time the painter will paint color c from grid x to grid y Return the color of each grid after all paint operations

Example 4 Given N rectangles on 2D plane Find the union area

Implementations struct node{ int left, right, maxval; } Tree[MAX_N]; /*Call build(1, range_x, range_y)*/ void build(int ind, int x, int y){ Tree[ind].left = x; Tree[ind].right = y; if (x!=y){ build(ind*2, x, (x+y)/2); build(ind*2+1, (x+y)/2+1, y); Tree[ind].maxval = max(Tree[ind*2].maxval, Tree[ind*2+1].maxval); } else Tree[ind].maxval = a[x]; /*Return the largest value in a[x]..a[y]*/ int query(int ind, int x, int y){ if (Tree[ind].left<=x && y<=Tree[ind].right) return Tree[ind].maxval; int leftmax, rightmax; leftmax = -INF; rightmax = -INF; if (x<=Tree[ind*2].right) leftmax = query(ind*2, x, y); if (y>=Tree[ind*2+1].left) rightmax = query(ind*2+1, x, y); return max(leftmax, rightmax);

Further reading http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor