Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

NAME : GOWTHAM.

D
REGNO : 20104047
SUBJECT : DATA STRUCTURES
SUB CODE : U20CS201
DEPT : CSE A
BATCH : II
Union and find functions
A disjoint-set data structure is a data structure that keeps track of a set of elements partitioned into a number of
disjoint (non-overlapping) subsets. A union-find algorithm is an algorithm that performs two useful operations on
such a data structure:
Find: Determine which subset a particular element is in. This can be used for determining if two elements are in
the same subset.
Union: Join two subsets into a single subset.
In this post, we will discuss the application of Disjoint Set Data Structure. The application is to check whether a
given graph contains a cycle or not.
Union-Find Algorithm can be used to check whether an undirected graph contains cycle or not. Note that we
have discussed an algorithm to detect cycle. This is another method based on Union-Find. This method assumes
that the graph doesn’t contain any self-loops.
We can keep track of the subsets in a 1D array, let’s call it parent[].
Let us consider the following graph:
For each edge, make subsets using both the vertices of the edge. If both the vertices are in the
same subset, a cycle is found.
Initially, all slots of parent array are initialized to -1 (means there is only one item in every
subset).
0 1 2 -1 -1 -1
Now process all edges one by one.3

Edge 0-1: Find the subsets in which vertices 0 and 1 are. Since they are in different subsets, we
take the union of them. For taking the union, either make node 0 as parent of node 1 or vice-
versa.
0 1 2 <----- 1 is made parent of 0 (1 is now representative of subset {0, 1})
1 -1 -1
Edge 1-2: 1 is in subset 1 and 2 is in subset 2. So, take union.
0 1 2 <----- 2 is made parent of 1 (2 is now representative of subset {0, 1, 2}) 1 2 -1

Edge 0-2: 0 is in subset 2 and 2 is also in subset 2. Hence, including this edge forms a cycle.
How subset of 0 is same as 2?
0->1->2 // 1 is parent of 0 and 2 is parent of 1
AVL TREE (ADELSON - VELSKILL AND LANDIS)

An AVL tree is a binary search tree except that for every node in the tree, the height of the left and right subtrees
can differ by atmost 1.
The height of the empty tree is defined to be - 1.
A balance factor is the height of the left subtree minus height of the right subtree. For an AVL tree all balance
factor should be +1, 0, or -1. If the balance factor of any node in an AVL tree becomes less than -1 or greater
than 1, the tree has to be balanced by making either single or double rotations.

BF = -1 7
BF = 1
7
BF = 0 5 12 BF = 1

BF = 0 BF = 0 10
5

(a)
(b)
7 BF = -1

BF = 1 5 12 BF = -1

10 14 BF = 1
2
BF = 0
BF = 0
11 BF = 0
AVL Tree

An AVL tree causes imbalance, when any one of the following conditions occur.
Case 1 : An insertion into the left subtree of the left child of node α . Case 2 : An insertion into the right
subtree of the left child of node α . Case 3 : An insertion into the left subtree of the right child of node α .
Case 4 : An insertion into the right subtree of the right child of node α .
These imbalances can be overcome by
1. Single Rotation
2. Double Rotation.
Single Rotation
Single Rotation is performed to fix case 1 and case 4.
Case 1. An insertion into the left subtree of the left child of K2. Single Rotation to fix Case 1.

General Representation K2 K1

K2
K1 z

x
y z
y INSERT

x (b) After rotation

INSERT
Fig. 3.8.2 (a) Before rotation
Double Rotation
Double Rotation is performed to fix case 2 and case 3.
Case 2 :
An insertion into the right subtree of the left child.

K3

K2

double rotation
K1
D
K1 K3
K2
A
C D
A B
B C
After
Before
This can be performed by 2 single rotations.

K3

K1
D

K2
A

B C

Single Rotation with right (K3 → left)

K3

K2
D

C
K1

A B

Single Rotation with left (K3)


Single Rotation with left (K3)

K2

K1 K3

C D
A B

Balanced AVL Tree


B-TREE

A B-Tree of order m is an m-way search tree with the following properties.

• The root node must have atleast two child nodes and atmost m child nodes.
• All internal nodes other than the root node must have atleast m/2 to m non-empty child nodes.
• The number of keys in each internal node is one less than its number of child nodes, which will partition the keys
of the tree into subtree.
• All internal nodes are at the same level.
General representation of B-Tree

16:-

8 11 12 16 17

Fig. 3.9.1
Here the non leaf nodes are represented as ellipses, which contain the two pieces of data
foreach node.

16:-

Fig. 3.9.1(a)

1. Represents the key value, which can be the largest element of the left sibling or the
smallest element of the right sibling. In this example we considered the smallest
elementin the right sibling as the key element in the parent node.

16:-

Fig. 3.9.1(b)

2. The dash line indicates that the node has only two children.
B-Tree can also be represented as

16 12
(or)

8 11 12 16 17 8 11 12 16 17

Key element in the parent node Key element in the parent node

hashas smallest element in the right sibling smalles element in the left

sibling

Operations on B-Trees
i) Insertion
ii) Deletion
Insertion
To insert a key k in the node X of the B-tree of order m can proceed in one of the two ways.
Case 1:
When the node X of the B-tree of order m can accommodate the key K, then it is inserted in that node and
the number of child pointer fields are appropriately upgraded.
EXAMPLE:
25 56

11 17 39 45 59 64 70 91

B-Tree of order 5 before insertion

To insert 23

25 56

11 17 23 39 45 59 64 70 91

B-Tree of order 5 after inserting 23


Case 2:
If the key K belongs to a non leaf node. Then replace K with the largest key KLmax in the left subtree of
K or the smallest key KRmin from the right subtree of K and then delete KRmin or KLmax from the node,
which in turn will trigger case 1 or 2.

25 56

KLmax
11 17 23 39 45 59 64 70 91

B-Tree of order 5 before deletion

You might also like