0% found this document useful (0 votes)
44 views38 pages

DAA B.Tech 4th Unit 2 2

The document discusses augmenting data structures. It defines augmenting a data structure as adapting an existing data structure to better suit one's needs. It provides examples of augmenting queues, tries, AVL trees, and disjoint sets by adding additional information to each node. The document notes augmenting allows taking advantage of a partial solution from an original data structure and customizing it for a specific problem.

Uploaded by

yshivani844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views38 pages

DAA B.Tech 4th Unit 2 2

The document discusses augmenting data structures. It defines augmenting a data structure as adapting an existing data structure to better suit one's needs. It provides examples of augmenting queues, tries, AVL trees, and disjoint sets by adding additional information to each node. The document notes augmenting allows taking advantage of a partial solution from an original data structure and customizing it for a specific problem.

Uploaded by

yshivani844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 38

UNIT – 2

Advanced Data Structure:

Red-Black Trees
Red-Black tree is a binary search tree in which every node is colored with either red or black. It is a
type of self balancing binary search tree. It has a good efficient worst case running time complexity.
Properties of Red Black Tree:

Rules That Every Red-Black Tree Follows:


The Red-Black tree satisfies all the properties of binary search tree in addition to that it satisfies
following additional properties –

1. Every node has a color either red or black.


2. The root of the tree is always black.
3. There are no two adjacent red nodes (A red node cannot have a red parent or red child).
4. Every path from a node (including root) to any of its descendants NULL nodes has the same
number of black nodes.
5. Every leaf (e.i. NULL node) must be colored BLACK.

Why Red-Black Trees?


Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is
the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree. If
we make sure that the height of the tree remains O(log n) after every insertion and deletion, then we
can guarantee an upper bound of O(log n) for all these operations. The height of a Red-Black tree is
always O(log n) where n is the number of nodes in the tree.
S.N Algorithm Time Complexity
1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)
“n” is the total number of elements in the red-black tree.

Comparison with AVL Tree:


The AVL trees are more balanced compared to Red-Black Trees, but they may cause more rotations
during insertion and deletion.

So if your application involves frequent insertions and deletions, then Red-Black trees should be
preferred.

And if the insertions and deletions are less frequent and search is a more frequent operation, then
AVL tree should be preferred over the Red-Black Tree.

How does a Red-Black Tree ensure balance?


A simple example to understand balancing is, that a chain of 3 nodes is not possible in the Red-
Black tree. We can try any combination of colors and see if all of them violate the Red-Black tree
property.

Proper structure of three noded Red-black tree

Search Operation in Red-black Tree:


As every red-black tree is a special case of a binary tree so the searching algorithm of a red-black
tree is similar to that of a binary tree.
Algorithm:
searchElement (tree, val)
Step 1:
If tree -> data = val OR tree = NULL
Return tree
Else
If val < data
Return searchElement (tree -> left, val)
Else
Return searchElement (tree -> right, val)
[ End of if ]
[ End of if ]

Step 2: END

Example: Searching 11 in the following red-black tree.

Solution:
1. Start from the root.
2. Compare the inserting element with root, if less than root, then recurse for left, else recurse
for right.
3. If the element to search is found anywhere, return true, else return false.
we introduced Red-Black trees and discussed how balance is ensured. The hard part is to maintain
balance when keys are added and removed. We have also seen how to search an element from the
red-black tree. We will soon be discussing insertion and deletion operations in coming posts on the
Red-Black tree.
Exercise:
1) Is it possible to have all black nodes in a Red-Black tree?
2) Draw a Red-Black Tree that is not an AVL tree structure-wise?

Insertion in Red-Black Tree

In the Red-Black tree, we use two tools to do the balancing.


1. Recoloring
1. Rotation
Recolouring is the change in colour of the node i.e. if it is red then change it to black and vice versa.
It must be noted that the colour of the NULL node is always black. Moreover, we always try
recolouring first, if recolouring doesn’t work, then we go for rotation. Following is a detailed
algorithm. The algorithms have mainly two cases depending upon the colour of the uncle. If the
uncle is red, we do recolour. If the uncle is black, we do rotations and/or recolouring.
The representation we will be working with is:

This representation is based on X

Logic:
First, you have to insert the node similarly to that in a binary tree and assign a red colour to it. Now,
if the node is a root node then change its colour to black, but if it is not then check the colour of the
parent node. If its colour is black then don’t change the colour but if it is not i.e. it is red then check
the colour of the node’s uncle. If the node’s uncle has a red colour then change the colour of the
node’s parent and uncle to black and that of grandfather to red colour and repeat the same process
for him (i.e. grandfather).If grandfather is root then don’t change grandfather to red colour.

But, if the node’s uncle has black colour then there are 4 possible cases:
 Left Left Case (LL rotation):

Left Right Case (LR rotation):

Right Right
Case (RR rotation):

Right Left Case (RL rotation):


Now, after these rotations, re-color according to rotation case (check algorithm for details).
Algorithm:
Let x be the newly inserted node.
1. Perform standard BST insertion and make the colour of newly inserted nodes as RED.
1. If x is the root, change the colour of x as BLACK (Black height of complete tree increases
by 1).
1. Do the following if the color of x’s parent is not BLACK and x is not the root.
a) If x’s uncle is RED (Grandparent must have been black from property 4)
(i) Change the colour of parent and uncle as BLACK.
(ii) Colour of a grandparent as RED.
(iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x.
b) If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s
grandparent (g) (This is similar to AVL Tree)
(i) Left Left Case (p is left child of g and x is left child of p)
(ii) Left Right Case (p is left child of g and x is the right child of p)
(iii) Right Right Case (Mirror of case i)
(iv) Right Left Case (Mirror of case ii)

Re-coloring after rotations:


For Left Left Case [3.b (i)] and Right Right case [3.b (iii)], swap colors of grandparent and parent
after rotations
For Left Right Case [3.b (ii)]and Right Left Case [3.b (iv)], swap colors of grandparent and inserted
node after rotations

Example: Creating a red-black tree with elements 3, 21, 32 and 15 in an empty tree.
Solution:

When the
first

element is inserted it is inserted as a root node and as root node has black colour so it acquires the
colour black.
The new

element is always inserted with a red colour and as 21 > 3 so it becomes the part of the right subtree
of the root node.

Now, as we
insert 32 we
see there is
a red father-
child pair
which violates
the Red-
Black tree
rule so we
have to rotate
it.
Moreover, we see the conditions of RR rotation (considering the null node of the root node as
black) so after rotation as the root node can’t be Red so we have to perform recolouring in the tree
resulting in the tree shown above.

Final Tree

Structure:
Augmenting Data Structure
What is an Augmented Data Structure?
Augmenting a Data Structure (or Augmented Data Structure) means adapting an existing data
structure to our needs. This allows us to take advantage of an original data structure that solves our
problem partially and make changes such that it fits to our problem completely.
Examples of Augmenting a Data Structure:
There are many examples of augmenting data structures, and augmenting is simply changing the
existing data structure to solve our problem.

Augmented Queue for Maximum Element: In an augmented queue, each element not only stores
its value but also maintains information about the maximum element in the current queue up
to that point.

Augmented Trie for Prefix Sums: Consider a trie (prefix tree) data structure where each node
stores the sum of values of all elements with the same prefix up to that node.

Augmented AVL Tree for Rank Queries: In an AVL tree (a self-balancing binary search tree),
each node can store the rank of that node in the tree, i.e., the number of nodes in its left
subtree plus one.

Augmented Disjoint Set (Union-Find) for Set Size: In a disjoint set data structure, each set
representative (root) could store the size of its corresponding set.

Prerequisites for Augmenting a Data Structure:


Augmenting a data structure involve adding new information or functionality to an existing data
structure in order to improve its capabilities. While this can be a useful technique for improving an
algorithm’s performance or functionality, it is critical to carefully consider the implications of
augmenting a data structure before doing so. Here are some important considerations:

1. Determine what additional information or functionality is required: Define precisely what


information or functionality you want to add to the data structure. This could imply storing
more data, keeping auxiliary information, or enabling new operations.
1. Impact on performance and complexity: Augmentation may result in additional time and space
complexity. Examine how the proposed augmentation will affect the operation of the data
structure. Ensure that the benefits outweigh the performance costs.
1. Consistency: The augmented data structure must be consistent with the original data structure.
This means that the augmentation should not violate any of the original structure’s invariants
or properties.
1. Compatibility with existing code: If the data structure is already used in existing code, make
sure that the augmentation does not break compatibility. Consider whether backward
compatibility or migration strategies are required.
1. Test and validate: The augmented data structure should be tested thoroughly to ensure its
correctness, efficiency, and compatibility with existing code. Verify that the augmentation
results in the desired performance or functionality improvements.
How to Augment a Data Structure?
The process of augmenting a data structure are as follows:
1. Define the Need:
 Identify the problem: Clearly define the problem you want the augmented structure to solve.
What specific inefficiency or limitation are we addressing?
 Justify the augmentation: Is augmentation the most effective solution? Can simpler
optimizations achieve the desired results?
2. Choose the Underlying Structure:
 Select the appropriate data structure: Consider the existing structure’s strengths and
weaknesses. Can it efficiently support the augmentation you want to implement?
 Evaluate compatibility: Ensure the chosen structure allows efficient updates and maintenance
of the added information during its basic operations (insertion, deletion, search, etc.).
3. Design the Augmentation:
 Define the data storage: Decide how the added information will be stored within the existing
structure. Will it be part of each element, attached to nodes, or stored separately?
 Update operations: Modify existing operations (insertion, deletion, search, etc.) to incorporate
the added information and ensure its consistency.
4. Implement and Test:
 Code the augmentation: Implement the designed modifications carefully, focusing on clarity,
efficiency, and maintainability.
Thorough testing: Test the augmented structure extensively for functionality, performance, and data
integrity across various scenarios.
 Document and share: Document your design decisions, rationale, and limitations for future
reference and to facilitate collaboration.
A Problem using Augmentation of Data Structure:
Given a set of integers, the task is to design a data structure that supports two operations efficiently:
1. Insertion: Insert an integer into the data structure.
1. Query: Given an integer k, find the number of elements in the data structure that are less
than or equal to k.

Naive Approach: A simple way to solve this problem is to use an array or a list to store the integers.
For insertion, you can just append the integer to the end of the list. For the query operation, iterate
through the list and count the elements less than or equal to k. This approach takes linear time for
the query operation.

Augmenting the Data Structure: To improve the efficiency of the query operation, we can augment
the data structure with additional information. One way to do this is by maintaining a sorted order
of elements. This can be achieved using a balanced Binary Search Tree (BST).
Structure of Augmented Tree: Each node in the BST should store the value of the element, the size of
the subtree rooted at that node, and pointers to the left and right children.

1. Insertion:
 Insert the element into the BST as you normally would.
 While inserting, update the size of each visited node.
2. Query:
 Start at the root of the BST.
 Traverse the tree:
 If the current node’s value is less than or equal to k, add the size of its left subtree
(including itself) to the count.
 Move to the left or right child accordingly.
B-Trees
B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree of order m can
have at most m-1 keys and m children. One of the main reason of using B tree is its capability to
store large number of keys in a single node and large key values by keeping the height of the tree
relatively small.

A B tree of order m contains all the properties of an M way tree. In addition, it contains the
following properties.

1. Every node in a B-Tree contains at most m children.


2. Every node in a B-Tree except the root node and the leaf node contain at least m/2 children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.

It is not necessary that, all the nodes contain the same number of children but, each node must have
m/2 number of nodes.

A B tree of order 4 is shown in the following image.


While performing some operations on B Tree, any property of B Tree may violate such as number
of minimum children a node can have. To maintain the properties of B Tree, the tree may split or
join.
Operations
Searching :

Searching in B Trees is similar to that in Binary search tree. For example, if we search for an item
49 in the following B Tree. The process will something like following :

1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
4. match found, return.

Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log n) time
to search any element in a B tree.

Inserting

Insertions are done at the leaf node level. The following algorithm needs to be followed in order to
insert an item into B Tree.

1. Traverse the B Tree in order to find the appropriate leaf node at which the node can be
inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
 Insert the new element in the increasing order of elements.
 Split the node into the two nodes at the median.
 Push the median element upto its parent node.
 If the parent node also contain m-1 number of keys, then split it too by following the
same steps.

Example:

Insert the node 8 into the B Tree of order 5 shown in the following image.

8 will be inserted to the right of 5, therefore insert 8.

The node, now contain 5 keys which is greater than (5 -1 = 4 ) keys. Therefore split the node from
the median i.e. 8 and push it up to its parent node shown as follows.
Deletion

Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a leaf
node or an internal node. Following algorithm needs to be followed in order to delete a node from a
B tree.

1. Locate the leaf node.


2. If there are more than m/2 keys in the leaf node then delete the desired key from the node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the element from
right or left sibling.
 If the left sibling contains more than m/2 elements then push its largest element up to
its parent and move the intervening element down to the node where the key is
deleted.
 If the right sibling contains more than m/2 elements then push its smallest element up
to the parent and move intervening element down to the node where the key is
deleted.
4. If neither of the sibling contain more than m/2 elements then create a new leaf node by
joining two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the parent too.

If the the node which is to be deleted is an internal node, then replace the node with its in-order
successor or predecessor. Since, successor or predecessor will always be on the leaf node hence, the
process will be similar as the node is being deleted from the leaf node.

Example 1

Delete the node 53 from the B Tree of order 5 shown in the following figure.
53 is present in the right child of element 49. Delete it.

Now, 57 is the only element which is left in the node, the minimum number of elements that must
be present in a B tree of order 5, is 2. it is less than that, the elements in its left and right sub-tree are
also not sufficient therefore, merge it with the left sibling and intervening element of parent i.e. 49.

The final B tree is shown as follows.


Application of B tree
B tree is used to index the data and provides fast access to the actual data stored on the disks since,
the access to value stored in a large database that is stored on a disk is a very time consuming
process.

Searching an un-indexed and unsorted database containing n key values needs O(n) running time in
worst case. However, if we use B Tree to index this database, it will be searched in O(log n) time in
worst case.

Binomial Heap
The main application of Binary Heap is as implement a priority queue. Binomial Heap is an
extension of Binary Heap that provides faster union or merge operation with other operations
provided by Binary Heap. A Binomial Heap is a collection of Binomial Trees

What is a Binomial Tree?


A Binomial Tree of order 0 has 1 node. A Binomial Tree of order k can be constructed by taking two
binomial trees of order k-1 and making one the leftmost child of the other.
A Binomial Tree of order k the has following properties.

 It has exactly 2k nodes.


 It has depth as k.
 There are exactly kaiCi nodes at depth i for i = 0, 1, . . . , k.
 The root has degree k and children of the root are themselves Binomial Trees with order k-1,
k-2,.. 0 from left to right.
k = 0 (Single Node)
o
k = 1 (2 nodes)
[We take two k = 0 order Binomial Trees, and make one as a child of other]
o
/
o
k = 2 (4 nodes)
[We take two k = 1 order Binomial Trees, and make one as a child of other]
o
/ \
o o
/
o
k = 3 (8 nodes)
[We take two k = 2 order Binomial Trees, and make one as a child of other]
o
/ |\
o o o
/\ |
ooo
/
o

A Binomial Heap is a set of Binomial Trees where each Binomial Tree follows the Min Heap
property. And there can be at most one Binomial Tree of any degree.

Examples Binomial Heap:


12------------10-----------------20
/ \ / |\
15 50 70 50 40
| /| |
30 80 85 65
|
100
A Binomial Heap with 13 nodes. It is a collection of 3
Binomial Trees of orders 0, 2, and 3 from left to right.
10--------------------20
/ \ / |\
15 50 70 50 40
| /| |
30 80 85 65
|
100
A Binomial Heap with 12 nodes. It is a collection of 2 Binomial Trees of orders 2 and 3 from left to
right.

Binary Representation of a number and Binomial Heaps

A Binomial Heap with n nodes has the number of Binomial Trees equal to the number of set bits in
the binary representation of n. For example, let n be 13, there are 3 set bits in the binary
representation of n (00001101), hence 3 Binomial Trees. We can also relate the degree of these
Binomial Trees with positions of set bits. With this relation, we can conclude that there are O(Logn)
Binomial Trees in a Binomial Heap with ‘n’ nodes.

Operations of Binomial Heap:


The main operation in Binomial Heap is a union(), all other operations mainly use this operation.
The union() operation is to combine two Binomial Heaps into one. Let us first discuss other
operations, we will discuss union later.
1. insert(H, k): Inserts a key ‘k’ to Binomial Heap ‘H’. This operation first creates a Binomial
Heap with a single key ‘k’, then calls union on H and the new Binomial heap.
1. getting(H): A simple way to get in() is to traverse the list of the roots of Binomial Trees and
return the minimum key. This implementation requires O(Logn) time. It can be optimized to
O(1) by maintaining a pointer to the minimum key root.
1. extracting(H): This operation also uses a union(). We first call getMin() to find the minimum
key Binomial Tree, then we remove the node and create a new Binomial Heap by connecting
all subtrees of the removed minimum node. Finally, we call union() on H and the newly
created Binomial Heap. This operation requires O(Logn) time.
1. delete(H): Like Binary Heap, the delete operation first reduces the key to minus infinite,
then calls extracting().
1. decrease key(H): decrease key() is also similar to Binary Heap. We compare the decreased
key with its parent and if the parent’s key is more, we swap keys and recur for the parent.
We stop when we either reach a node whose parent has a smaller key or we hit the root node.
The time complexity of the decrease key() is O(Logn).
Union operation in Binomial Heap:
Given two Binomial Heaps H1 and H2, union(H1, H2) creates a single Binomial Heap.
1. The first step is to simply merge the two Heaps in non-decreasing order of degrees. In the
following diagram, figure(b) shows the result after merging.
1. After the simple merge, we need to make sure that there is at most one Binomial Tree of any
order. To do this, we need to combine Binomial Trees of the same order. We traverse the list
of merged roots, we keep track of three-pointers, prev, x, and next-x. There can be the
following 4 cases when we traverse the list of roots.
—–Case 1: Orders of x and next-x are not the same, we simply move ahead.
In the following 3 cases, orders of x and next-x are the same.
Time Complexity Analysis:
Operations Binary Heap Binomial Heap Fibonacci Heap

Procedure Worst-case Worst-case Amortized

Making Heap Θ(1) Θ(1) Θ(1)


Inserting a node Θ(log(n)) O(log(n)) Θ(1)
Finding Minimum key Θ(1) O(log(n)) O(1)
Extract-Minimum key Θ(log(n)) Θ(log(n)) O(log(n))
Union or merging Θ(n) O(log(n)) Θ(1)
Decreasing a Key Θ(log(n)) Θ(log(n)) Θ(1)
Deleting a node Θ(log(n)) Θ(log(n)) O(log(n))

How to represent Binomial Heap?

A Binomial Heap is a set of Binomial Trees. A Binomial Tree must be represented in a way that
allows sequential access to all siblings, starting from the leftmost sibling (We need this in and
extracting() and delete()). The idea is to represent Binomial Trees as the leftmost child and right-
sibling representation, i.e., every node stores two pointers, one to the leftmost child and the other to
the right sibling.

Fibonacci Heap
A fibonacci heap is a data structure that consists of a collection of trees which follow min heap or
max heap property. These two properties are the characteristics of the trees present on a fibonacci
heap.
In a fibonacci heap, a node can have more than two children or no children at all. Also, it has more
efficient heap operations than that supported by the binomial and binary heaps.
The fibonacci heap is called a fibonacci heap because the trees are constructed in a way such that a

tree of order n has at least Fn+2 nodes in it, where Fn+2 is the (n + 2)th Fibonacci number.

Properties of a Fibonacci Heap


Important properties of a Fibonacci heap are:
1. It is a set of min heap-ordered trees. (i.e. The parent is always smaller than the children.)
2. A pointer is maintained at the minimum element node.
3. It consists of a set of marked nodes. (Decrease key operation)
4. The trees within a Fibonacci heap are unordered but rooted.

Memory Representation of the Nodes in a Fibonacci Heap


The roots of all the trees are linked together for faster access. The child nodes of a parent node are
connected to each other through a circular doubly linked list as shown below.
There are two main advantages of using a circular doubly linked list.
1. Deleting a node from the tree takes O(1) time.
2. The concatenation of two such lists takes O(1) time.

Operations on a Fibonacci Heap


Insertion
Algorithm
insert(H, x)
degree[x] = 0
p[x] = NIL
child[x] = NIL
left[x] = x
right[x] = x
mark[x] = FALSE
concatenate the root list containing x with root list H
if min[H] == NIL or key[x] < key[min[H]]
then min[H] = x
n[H] = n[H] + 1
Inserting a node into an already existing heap follows the steps below.
1. Create a new node for the element.
2. Check if the heap is empty.
3. If the heap is empty, set the new node as a root node and mark it min.
4. Else, insert the node into the root list and update min.

Find Min
The minimum element is always given by the min pointer.
Union
Union of two fibonacci heaps consists of following steps.
1. Concatenate the roots of both the heaps.
2. Update min by selecting a minimum key from the new root lists.

Extract Min
It is the most important operation on a fibonacci heap. In this operation, the node with minimum
value is removed from the heap and the tree is re-adjusted.
The following steps are followed:
1. Delete the min node.
2. Set the min-pointer to the next root in the root list.
3. Create an array of size equal to the maximum degree of the trees in the heap before deletion.
4. Do the following (steps 5-7) until there are no multiple roots with the same degree.
5. Map the degree of current root (min-pointer) to the degree in the array.
6. Map the degree of next root to the degree in array.
7. If there are more than two mappings for the same degree, then apply union operation to
those roots such that the min-heap property is maintained (i.e. the minimum is at the root).
An implementation of the above steps can be understood in the example below.
1. We will perform an extract-min operation on the heap below.

2. Delete the min node, add all its child nodes to the root list and set the min-pointer to the next
root in the root list.

3. The maximum degree in the tree is 3. Create an array of size 4 and map degree of the next
roots with the array.
4. Here, 23 and 7 have the same degrees, so unite them.

5. Again, 7 and 17 have the same degrees, so unite them as well.

6. Again 7 and 24 have the same degree, so unite them.

7. Map the next nodes.


8. Again, 52 and 21 have the same degree, so unite them

9. Similarly, unite 21 and 18.

10. Map the remaining root.


11. The final heap is.

Decreasing a Key and Deleting a Node


These are the most important operations which are discussed in Decrease Key and Delete Node
Operations.
Complexities
Insertion O(1)
Find Min O(1)
Union O(1)
Extract Min O(log n)
Decrease Key O(1)
Delete Node O(log n)

Data structure for Disjoint set


The disjoint set data structure is also known as union-find data structure and merge-find set. It is a
data structure that contains a collection of disjoint or non-overlapping sets. The disjoint set means
that when the set is partitioned into the disjoint subsets. The various operations can be performed on
the disjoint subsets. In this case, we can add new sets, we can merge the sets, and we can also find
the representative member of a set. It also allows to find out whether the two elements are in the
same set or not efficiently.
The disjoint set can be defined as the subsets where there is no common element between the two
sets. Let's understand the disjoint sets through an example.

s1 = {1, 2, 3, 4}
s2 = {5, 6, 7, 8}
We have two subsets named s1 and s2. The s1 subset contains the elements 1, 2, 3, 4, while s2
contains the elements 5, 6, 7, 8. Since there is no common element between these two sets, we will
not get anything if we consider the intersection between these two sets. This is also known as a
disjoint set where no elements are common. Now the question arises how we can perform the
operations on them. We can perform only two operations, i.e., find and union.
In the case of find operation, we have to check that the element is present in which set. There are
two sets named s1 and s2 shown below:
Suppose we want to perform the union operation on these two sets. First, we have to check whether
the elements on which we are performing the union operation belong to different or same sets. If
they belong to the different sets, then we can perform the union operation; otherwise, not. For
example, we want to perform the union operation between 4 and 8. Since 4 and 8 belong to different
sets, so we apply the union operation. Once the union operation is performed, the edge will be
added between the 4 and 8 shown as below:
When the union operation is applied, the set would be represented as:
s1Us2 = {1, 2, 3, 4, 5, 6, 7, 8}
Suppose we add one more edge between 1 and 5. Now the final set can be represented as:
s3 = {1, 2, 3, 4, 5, 6, 7, 8}
If we consider any element from the above set, then all the elements belong to the same set; it
means that the cycle exists in a graph.

How can we detect a cycle in a graph?


We will understand this concept through an example. Consider the below example to detect a cycle
with the help of using disjoint sets.

U = {1, 2, 3, 4, 5, 6, 7, 8}
Each vertex is labelled with some weight. There is a universal set with 8 vertices. We will consider
each edge one by one and form the sets.
First, we consider vertices 1 and 2. Both belong to the universal set; we perform the union operation
between elements 1 and 2. We will add the elements 1 and 2 in a set s1 and remove these two
elements from the universal set shown below:
s1 = {1, 2}
The vertices that we consider now are 3 and 4. Both the vertices belong to the universal set; we
perform the union operation between elements 3 and 4. We will form the set s3 having elements 3
and 4 and remove the elements from the universal set shown as below:
s2 = {3, 4}
The vertices that we consider now are 5 and 6. Both the vertices belong to the universal set, so we
perform the union operation between elements 5 and 6. We will form the set s3 having elements 5
and 6 and will remove these elements from the universal set shown as below:
s3 = {5, 6}
The vertices that we consider now are 7 and 8. Both the vertices belong to the universal set, so we
perform the union operation between elements 7 and 8. We will form the set s4 having elements 7
and 8 and will remove these elements from the universal set shown as below:
s4 = {7, 8}
The next edge that we take is (2, 4). The vertex 2 is in set 1, and vertex 4 is in set 2, so both the
vertices are in different sets. When we apply the union operation, then it will form the new set
shown as below:
s5 = {1, 2, 3, 4}
The next edge that we consider is (2, 5). The vertex 2 is in set 5, and the vertex 5 is in set s3, so
both the vertices are in different sets. When we apply the union operation, then it will form the new
set shown as below:
s6 = {1, 2, 3, 4, 5, 6}
Now, two sets are left which are given below:
s4 = {7, 8}
s6 = {1, 2, 3, 4, 5, 6}
The next edge is (1, 3). Since both the vertices, i.e.,1 and 3 belong to the same set, so it forms a
cycle. We will not consider this vertex.
The next edge is (6, 8). Since both vertices 6 and 8 belong to the different vertices s4 and s6, we
will perform the union operation. The union operation will form the new set shown as below:
s7 = {1, 2, 3, 4, 5, 6, 7, 8}
The last edge is left, which is (5, 7). Since both the vertices belong to the same set named s7, a
cycle is formed.

How can we represent the sets graphically?


We have a universal set which is given below:
U = {1, 2, 3, 4, 5, 6, 7, 8}
We will consider each edge one by one to represent graphically.
First, we consider the vertices 1 and 2, i.e., (1, 2) and represent them through graphically shown as
below:
In the above figure, vertex 1 is the parent of vertex 2.
Now we consider the vertices 3 and 4, i.e., (3, 4) and represent them graphically shown as below:
In the above figure, vertex 3 is the parent of vertex 4.

Consider the vertices 5 and 6, i.e., (5, 6) and represent them graphically shown as below:
In the above figure, vertex 5 is the parent of vertex 6.

Now, we consider the vertices 7 and 8, i.e., (7, 8) and represent them through graphically shown as
below:

In the above figure, vertex 7 is the parent of vertex 8.

Now we consider the edge (2, 4). Since 2 and 4 belong to different sets, so we need to perform the
union operation. In the above case, we observe that 1 is the parent of vertex 2 whereas vertex 3 is
the parent of vertex 4. When we perform the union operation on the two sets, i.e., s1 and s2, then 1
vertex would be the parent of vertex 3 shown as below:
The next edge is (2, 5) having weight 6. Since 2 and 5 are in two different sets so we will perform
the union operation. We make vertex 5 as a child of the vertex 1 shown as below:
We have chosen vertex 5 as a child of vertex 1 because the vertex of the graph having parent 1 is
more than the graph having parent 5.

The next edge is (1, 3) having weight 7. Both vertices 1 and 3 are in the same set, so there is no
need to perform any union operation. Since both the vertices belong to the same set; therefore, there
is a cycle. We have detected a cycle, so we will consider the edges further.

How can we detect a cycle with the help of an array?

Consider the below graph:


The above graph contains the 8 vertices. So, we represent all these 8 vertices in a single array. Here,
indices represent the 8 vertices. Each index contains a -1 value. The -1 value means the vertex is the
parent of itself.

Now we will see that how we can represent the sets in an array.
First, we consider the edge (1, 2). When we find 1 in an array, we observe that 1 is the parent of
itself. Similarly, vertex 2 is the parent of itself, so we make vertex 2 as the child of vertex 1. We add
1 at the index 2 as 2 is the child of 1. We add -2 at the index 1 where '-' sign that the vertex 1 is the
parent of itself and 2 represents the number of vertices in a set.
The next edge is (3, 4). When we find 3 and 4 in array; we observe that both the vertices are parent
of itself. We make vertex 4 as the child of the vertex 3 so we add 3 at the index 4 in an array. We
add -2 at the index 3 shown as below:

The next edge is (5, 6). When we find 5 and 6 in an array; we observe that both the vertices are
parent of itself. We make 6 as the child of the vertex 5 so we add 5 at the index 6 in an array. We
add -2 at the index 5 shown as below:

The next edge is (7, 8). Since both the vertices are parent of itself, so we make vertex 8 as the child
of the vertex 7. We add 7 at the index 8 and -2 at the index 7 in an array shown as below:
The next edge is (2, 4). The parent of the vertex 2 is 1 and the parent of the vertex is 3. Since both
the vertices have different parent, so we make the vertex 3 as the child of vertex 1. We add 1 at the
index 3. We add -4 at the index 1 as it contains 4 vertices.
Graphically, it can be represented as

The next edge is ( 2,5 ). When we find vertex 2 in an array, we observe that 1 is the parent of the
vertex 2 and the vertex 1 is the parent of itself. When we find 5 in an array, we find -2 value which
means vertex 5 is the parent of itself. Now we have to decide whether the vertex 1 or vertex 5
would become a parent. Since the weight of vertex 1, i.e., -4 is greater than the vertex of 5, i.e., -2,
so when we apply the union operation then the vertex 5 would become a child of the vertex 1 shown
as below:

In an array, 1 would be added at the index 5 as the vertex 1 is now becomes a parent of vertex 5. We
add -6 at the index 1 as two more nodes are added to the node 1.
The next edge is (1,3). When we find vertex 1 in an array, we observe that 1 is the parent of itself.
When we find 3 in an array, we observe that 1 is the parent of vertex 3. Therefore, the parent of both
the vertices are same; so, we can say that there is a formation of cycle if we include the edge (1,3).
The next edge is (6,8). When we find vertex 6 in an array, we observe that vertex 5 is the parent of
vertex 6 and vertex 1 is the parent of vertex 5. When we find 8 in an array, we observe that vertex 7
is the parent of the vertex 8 and 7 is the parent of itself. Since the weight of vertex 1, i.e., -6 is
greater than the vertex 7, i.e., -2, so we make the vertex 7 as the child of the vertex and can be
represented graphically as shown as below:
We add 1 at the index 7 because 7 becomes a child of the vertex 1. We add -8 at the index 1 as the
weight of the graph now becomes 8.

The last edge to be included is (5, 7). When we find vertex 5 in an array, we observe that vertex 1 is
the parent of the vertex 5. Similarly, when we find vertex 7 in an array, we observe that vertex 1 is
the parent of vertex 7. Therefore, we can say that the parent of both the vertices is same, i.e., 1. It
means that the inclusion (5,7) edge would form a cycle.
Till now, we have learnt the weighted union where we perform the union operation according to the
weights of the vertices. The higher weighted vertex becomes a parent and the lower weighted vertex
becomes a child. The disadvantage of using this approach is that some nodes take more time to
reach its parent. For example, in the above graph, if we want to find the parent of vertex 6, vertex 5
is the parent of vertex 6 so we move to the vertex 5 and vertex 1 is the parent of the vertex 5. To
overcome such problem, we use the concept 'collapsing find'.
How collapsing find technique works?
Consider the above example. Once we know the parent of the vertex 6 which is 1 then we directly
add the vertex 6 to the vertex 1. We will also update the array. In an array, add 1 at the index 6
because the parent of 6 is now 1. The process of directly linking a node to the direct parent of a set
is known as a collapsing find. Similarly, we can link the nodes 8 and 4 to the node 1.

You might also like