Title: CSE - 5311 Advanced Algorithms
1CSE - 5311 Advanced Algorithms
-
- Instructor Gautam Das
- Submitted by
- Raja Rajeshwari Anugula Srujana Tiruveedhi
2Contents
- RED BLACK Trees
- History
- Properties
- Rotations
- Insertion
- Hashing
- Union Find Algorithm
3RED 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.
4RED 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.
5RED 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(n1). - 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.
6RED 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.
7RED 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 ys left child and ys left child as xs right
child.
8RED 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.
9RED BLACK TREES
- Case 1a Father and Uncle are Red , problem node
is right child
C
C
Recolor it to BLACK
After Recoloring
D
A
D
A
a
d
e
e
B
B
d
a
b
c
b
c
10RED 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
11RED BLACK TREES
Case 2a Father red and Uncle Black, problem node
is left child
B
C
After Rotation
D
C
B
A
d
c
A
c
b
a
D
a
b
d
12RED BLACK TREES
Case 2b Father red and Uncle Black, problem node
is right child
C
C
After Rotation
D
A
B
D
d
B
d
c
A
a
c
b
b
a
apply 2a for the above tree
13RED BLACK TREES
Example
11
11
Apply Case 1b
2
14
2
14
7
15
7
15
1
1
8
5
8
5
Insert Node 4
4
4
14RED BLACK TREES
11
11
Apply Case 2b
2
14
7
14
7
15
8
15
1
2
8
5
5
1
4
4
15RED BLACK TREES
11
7
Apply Case 2a
7
14
2
11
8
15
14
5
2
8
1
15
4
5
1
4
16HASHING
- Has table is an effective data structure for
implementing dictionaries. - Although searching for an element in hash table
in the worst case is T(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 T0m-1) - h U 0,1,2..m-1
- Two keys may hash to the same slot. This is
called collision.
17UNION 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.
18MAKE-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)
-
19UNION 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
20FIND 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).
21LINKED 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
22LINKED LIST REPRESENTATION
- For n Unions and m Finds, then time taken is
nmn. - 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(nmn)
- Union O(1)
3 is the head
5 is the head
23LINKED 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(n2m) 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 L1gtL2 or L2gtL1, path length doesnt change
i.e. It is still the longer path. - If L1L2, then the path length is
- L21 if the head of tree1 is pointed to head of
tree2 - L11 if the head of tree2 is pointed to head of
tree1 -
-