Download presentation
Presentation is loading. Please wait.
Published byMarcia Hodge Modified over 9 years ago
1
Disjoint-sets Abstract Data Type (ADT) that contains nonempty pairwise disjoint sets: For all sets X and Y in the container, – X != ϴ and Y != ϴ – And either X = Y or X∩Y = ϴ Each set has one of its members marked as a representative of that set 1
2
The Operations supported are: Make_set (i) – Construct the set {i} Find_set (i) – Return the marked number of the set to which i belongs Union (i, j) – Replace the sets containing i and j with their union. (it is assumed that i and j do not belong to the same set) 2
3
Example: Make_set(1) Make_set(2) Make_set(3) Make_set(4) Make_set(5) {1}, {2}, {3}, {4}, {5} Each element is marked as a representative Find_set(i)=i 3
4
Union(1, 4) Union (3, 5) {1,4},{2},{3,5} If we assume that 4 is marked as a representative Find_set(1)=Find_set(4)=4 4
5
Two different representations for Disjoint sets: 1.Tree-based 2.Linked-List 5
6
1) Tree-based representation –Each set is represented by a tree. –the root of that set's tree is marked as a representative –Not so good – could get a linear chain of nodes. Example: many ways to represent the set {2, 4, 5, 8}. 5 is marked as a representative 2 4 8 5 5 2 4 8 58245824 6
7
We represent disjoint sets using an array, Parent, in which the value Parent[i], is the parent of i unless i is the root (parent[root]=root) Example: if the disjoint sets {2, 4, 5, 8},{1}, {3, 6, 7} Are represented by the following trees the Parent array 5 2 4 8 7 3 6 1 12345678 15755772 7
8
Make_set(i) This algorithm represents the set {i} as a one- node tree. Input Parameter: i Output Parameters: None makeset(i) { parent[i] = i } O(1) 8
9
Find_set(i) This algorithm returns the root of the tree to which i belongs. Input Parameter: i Output Parameters: None findset(i) { while (i != parent[i]) i = parent[i] return i } O(n) 9
10
Merge_trees(i, j) This algorithm receives as input the roots of two distinct trees and combines them by making one root a child of the other root. Input Parameters: i,j Output Parameters: None mergetrees(i,j) { parent[i] = j } O(1) 10
11
Union(i, j) This algorithm receives as input two arbitrary values i and j and constructs the tree that represents the union of the sets to which i and j belong. The algorithm assumes that i and j belong to different sets. Input Parameters: i,j Output Parameters: None union (i,j) { mergetrees(findset(i), findset(j)) } O(n) 11
12
abc / head tail 2) disjoint-sets (Linked-List representation) Each set is a linked-list, with head and tail –Each linked list has a head pointer to the first element in the linked list –Each linked list also has a tail pointer to the last element in the linked list The representative of a linked list is the first element of that set Each element (node) contains –value, –next node pointer and, –back-to-representative node pointer. e.g.) set {a,b,c} 12
13
abc / head tail disjoint-sets operations (Linked-List representation) Make-set and Find-set take constant time, –MAKE-SET costs O(1): just create a single element list. –FIND-SET costs O(1): just return back-to- representative set pointer 13 O(1)
14
head tail che f c g head Set Y {f,g} UNION of two Sets h e Union (X, Y) –Append X’s list onto the end of Y. –Use tail pointer to find the end of Y. –New representative is the representative of Y –Have to update every pointer in X to point to the new representative –Each UNION takes time linear in the X’s length. head fg tail Set X {c,h,e} 14 O(n)
15
15 A priority queue is a data structure which allows us to perform the following operations on a collection Q of objects, each with an associated real number, called its key: – insert(Q, x): insert the object x into Q. – delete-min(Q, x): delete an object x of minimum key from Q. – decrease-key(Q, x, y): decrease the key of an object x in Q to the new value y. priority queues
16
16 Implementing a priority Queue decrease-key(Q, x, y)delete-min(Q, x)insert(Q, x)Q O(1)O(V)O(1) Unsorted Array O(V)O(1)O(V) sorted Array O(lgv) Binary Heap
17
17
18
18
19
19
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.