Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE - 5311 Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.

Similar presentations


Presentation on theme: "CSE - 5311 Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi."— Presentation transcript:

1 CSE - 5311 Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi

2 Contents  RED – BLACK Trees  History  Properties  Rotations  Insertion  Hashing  Union Find Algorithm

3 RED BLACK TREES  RB trees is a Binary Tree with one extra bit of storage per node; its color, which can be either RED or BLACK.  Each node of the tree contains fields color, key, left, right, parent.  Red-Black Trees ensure that longest path is no more than twice as long as any other path so that the tree is approximately BALANCED.

4 RED BLACK TREES STRUCTURAL PROPERTIES: Every node is colored red or black. The Root is Black. Every “leaf” (Nil) is colored black. Both children of a red node are black. Every simple path from a child of node X to a leaf has the same number of black nodes.

5 RED BLACK TREES Points to remember: This number is known as the black-height of X(bh(X)). A RB tree with n internal nodes has a height of almost 2log(n+1). Maximum path length is O(log n). Finding an element is real quick in RB trees, i.e,., it takes O(log n) time. Insertion and Deletion take O(log n) time.

6 RED BLACK TREES ROTATIONS Insertion and Deletion modify the tree, the result may violate the properties of red black trees. To restore these properties rotations are done. We can have either LEFT rotation or RIGHT rotation by which we must change colors of some of the nodes in the tree and also change the pointer structure.

7 RED BLACK TREES LEFT ROTATE RIGHT ROTATE a c a b b c  When we do a LEFT rotation on a node x we assume that its right child y is not nil,i.e x may be any node in the tree whose right child is not Nil.  It makes y the new root of the sub tree, with x as y’s left child and y’s left child as x’s right child. x y y x

8 RED BLACK TREES  The Idea to insert is that we traverse the tree to see where it fits, assuming that it fits at the end, and initially color the inserted node RED, then traverse up again.  Coloring rule while insertion  If the father node and uncle node of the inserted node are red then make father and uncle as BLACK and grand father as RED.

9 RED BLACK TREES C A D B a bc d e C D A a bc de B After Recoloring Case 1a: Father and Uncle are Red, problem node is right child Recolor it to BLACK

10 RED BLACK TREES Case 1b: Father and Uncle are Red, problem node is left child C BD A ab cde C B D A ab c de After Recoloring Recolor it to BLACK

11 RED BLACK TREES Case 2a: Father red and Uncle Black, problem node is left child C B A a b d B C A d cba After Rotation c D D

12 RED BLACK TREES Case 2b: Father red and Uncle Black, problem node is right child C A B a b d C A B dc ba After Rotation c D D apply 2a for the above tree

13 RED BLACK TREES 11 214 1 4 715 5 8 11 214 1 4 715 5 8 Insert Node 4 Apply Case 1b Example:

14 RED BLACK TREES 11 214 1 4 715 5 8 11 714 2 4 815 51 Apply Case 2b

15 RED BLACK TREES 11 714 2 4 815 5 1 7 211 1 4 15 145 8 Apply Case 2a

16 HASHING  Has table is an effective data structure for implementing dictionaries.  Although searching for an element in hash table in the worst case is Θ(n) time, under reasonable assumptions the expected time to search for an element is O(1).  With hashing this element is stored in slot h(k) i.e we use a hash function h to compute the slot from the key k. (h maps the universe U of keys into the slots of a hash table T[0…m-1]) h :U {0,1,2…..m-1}  Two keys may hash to the same slot. This is called collision.

17 UNION FIND Basics: Applications involve grouping n elements into a collection of disjoint sets. The important operations are  MAKE-SET(x): Creates a new set  UNION(x,y): Unites the dynamic sets that contain x and y into a new set that is the union of these two sets.  FIND-SET(x): Returns a pointer to the representative of the set containing x The number of union operations is atmost n-1.

18 MAKE-SET OPERATION  Makes a singleton set  Every set should have a name which should be any element of the set Make-Set(1) Make-Set(2) * Make-Set(n)

19 UNION OPERATION Initially each number is a set by itself. From n singleton sets gradually merge to form a set. After n-1 union operations we get a single set of n numbers. 1 3 2 4 UNION :Merge two sets and create a new set

20 FIND OPERATION  Every set has a name.  Thus Find(number) returns name of the set.  It can be performed any number of times on the sets.  The time taken for a find operation is O(n) whereas for Union operation it is O(1).

21 LINKED LIST REPRESENTATION  Every Set is represented as linked list where the first element is the name of the set.  We have the array of elements which have pointers to the elements in the linked lists. 51 347 10 5 2 1 3 4 7 10 2 ‘3’ is head of this set ‘5’ is head if this set ’10’ is head of this singleton set

22  For ‘n’ Unions and ‘m’ Finds, then time taken is n+mn.  If we have a pointer from each element in the set to the head, then the time to find operation is O(1). NOTE: If m is large, Find : O(n+mn) Union: O(1) LINKED LIST REPRESENTATION ‘3’ is the head ‘5’ is the head

23 LINKED LIST REPRESENTATION The 2 sets are being merged by connecting 5 and 7 Each element pointing to the head i,e ‘3’ in this example In this case the union takes O(n 2 +m) time.

24 If we assume that the smaller set is attached to the end of the larger set in Union operation, then Union  O(n) and Find  O(1) But in the AMORTIZED ANALYSIS, Average time taken for union is O(log n). So ‘n’ Unions and ‘m’ Finds take (nlog n +m) time. To guarantee O(log n) time for Union, instead of pointing each element to the main head, point the Heads’ of the individual sets to a main head.

25  If we consider path lengths to combine two trees(L1 is the path length of tree1 and L2 is the path length of tree2), then  If L1>L2 or L2>L1, path length doesn’t change i.e. It is still the longer path.  If L1=L2, then the path length is  L2+1 if the head of tree1 is pointed to head of tree2  L1+1 if the head of tree2 is pointed to head of tree1


Download ppt "CSE - 5311 Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi."

Similar presentations


Ads by Google