MODULE - II
Disjoint Sets: Disjoint set operations, union and find
algorithms, Priority Queue- Heaps, Heapsort
Backtracking: General method, applications, n-queen’s
problem, sum of subsets problem, graph Coloring,
hamitonian cycles.
What is a Disjoint Sets?
A pair of sets which does not have any common element are
called disjoint sets.
For example, set A={2,3} and set B={4,5} are disjoint sets. But
set C={3,4,5} and {3,6,7} are not disjoint as both the sets C
and D are having 3 as a common element.
What is a priority queue?
A priority queue is an abstract data
type that behaves similarly to the
normal queue except that each
element has some priority, i.e., the
element with the highest priority
would come first in a priority queue.
The priority of the elements in a
priority queue will determine the
order in which elements are removed
from the priority queue.
The priority queue supports only
comparable elements, which means
that the elements are either
arranged in an ascending or
descending order.
For example, suppose we have some
values like 1, 3, 4, 8, 14, 22 inserted
in a priority queue with an ordering
imposed on the values is from least
to the greatest. Therefore, the 1
number would be having the highest
priority while 22 will be having the
lowest priority.
Characteristics of a Priority queue
A priority queue is an extension of a queue that contains the following characteristics:
• Every element in a priority queue has some priority associated with it.
• An element with the higher priority will be deleted before the deletion of the lesser
priority.
• If two elements in a priority queue have the same priority, they will be arranged using
the FIFO principle.
Let's understand the priority queue through an example.
We have a priority queue that contains the following values:
1, 3, 4, 8, 14, 2
All the values are arranged in ascending order.
Now, we will observe how the priority queue will look after performing the following
operations:
• poll(): This function will remove the highest priority element from the priority queue.
In the above priority queue, the '1' element has the highest priority, so it will be
removed from the priority queue.
• add(2): This function will insert '2' element in a priority queue. As 2 is the smallest
element among all the numbers so it will obtain the highest priority.
• poll(): It will remove '2' element from the priority queue as it has the highest priority
queue.
• add(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it will
obtain the third highest priority in a priority queue.
Implementation of Priority Queue
The priority queue can be implemented in four ways that include arrays, linked list,
heap data structure and binary search tree.
The heap data structure is the most efficient way of implementing the priority queue,
so we will implement the priority queue using a heap data structure.
What is Heap?
• A heap is a tree-based data structure that forms a complete binary tree, and satisfies
the heap property.
• If A is a parent node of B, then A is ordered with respect to the node B for all nodes A
and B in a heap. It means that the value of the parent node could be more than or
equal to the value of the child node, or the value of the parent node could be less than
or equal to the value of the child node.
Therefore, we can say that there are two types of heaps:
1.MAX HEAP
2.MIN HEAP
Heap Sort
Binary Heap:
Binary Heap is an array object can be viewed as Complete
Binary Tree. Each node of the Binary Tree corresponds to an
element in an array.
1. Length [A],number of elements in array
2. Heap-Size[A], number of elements in a heap stored
within array A.
The root of tree A [1] and gives index 'i' of a node that indices
of its parents, left child, and the right child can be computed.
PARENT (i)
Return floor (i/2)
LEFT (i)
Return 2i
RIGHT (i)
Return 2i+1
In back tracking technique, we will get optimal solution with
a less number of steps.
So, by using back tracking technique, we will solve problems
in an efficient way, when compared to other methods like
Greedy and Dynamic Programming.
The name back track was first coined by D.H.Lehamen in the
1950’s.
Early workers who studied the process were R.J.Walker, who
gave an algorithmic account of it in 1960 and S.Golomb and
L.Baumart who presented a very general description of it as
well as a variety of applications.