1 / 25

CSE - 5311 Advanced Algorithms

Explore the key concepts of Red-Black Trees, including structural properties, rotations, insertion, hashing, and the Union-Find algorithm. Dives into balancing, insertion rules, and operation complexities.

todddavid
Download Presentation

CSE - 5311 Advanced Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE - 5311AdvancedAlgorithms 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. y x x y 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.

  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 Case 1a: Father and Uncle are Red , problem node is right child C Recolor it to BLACK After Recoloring D A D A a d e e B B d a b c b c

  10. RED BLACK TREES Case 1b: Father and Uncle are Red , problem node is left child C Recolor it to BLACK C After Recoloring B D B D c A c d e d e A a b a b

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

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

  13. RED BLACK TREES Example: 11 Apply Case 1b 11 2 14 2 14 7 15 7 15 1 1 8 5 8 5 Insert Node 4 4 4

  14. RED BLACK TREES 11 11 Apply Case 2b 2 14 7 14 7 15 8 15 1 2 8 5 1 5 4 4

  15. RED BLACK TREES 11 Apply Case 2a 7 7 14 2 11 8 15 5 14 2 8 1 15 4 5 1 4

  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. UNION :Merge two sets and create a new set 3 1 4 2

  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. 5 2 1 3 4 7 ‘3’ is head of this set 3 4 7 5 1 2 ‘5’ is head if this set 10 10 ’10’ is head of this singleton set

  22. LINKED LIST REPRESENTATION • 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) ‘3’ is the head ‘5’ is the head

  23. LINKED LIST REPRESENTATION Each element pointing to the head i,e ‘3’ in this example The 2 sets are being merged by connecting 5 and 7 In this case the union takes O(n2+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

More Related