Ada Chapt6 Tronsform and Conquer
Ada Chapt6 Tronsform and Conquer
ALGORITHMS
UNIT-III
CHAPTER 6:
TRANSFORM-AND-CONQUER
1
OUTLINE
Presorting
2
Introduction
Transform-and-conquer technique
o These methods work as two-stage procedures.
3
Transform and Conquer
This group of techniques solves a problem by a transformation
Presorting :
Many problems involving lists are easier when list is sorted.
searching
checking if all elements are distinct (element uniqueness)
Also:
Topological sorting helps solving some problems for dags.
Presorting is used in many geometric algorithms.
5
Element Uniqueness with presorting
Presorting-based algorithm
Stage 1: sort by efficient sorting algorithm (e.g. merge sort).
Stage 2: scan array to check pairs of adjacent elements.
Efficiency:
T(n) = Tsort(n) + Tscan(n) Є Θ(nlog n) + Θ(n) = Θ(nlog n)
6
Example 1 Checking element uniqueness in an array.
We can sort the array first and then check only its consecutive
elements: if the array has equal elements, a pair of them must be
next to each other and vice versa.
7
Example 2 Computing a mode
A mode is a value that occurs most often in a given list of numbers.
For example, for 5, 1, 5, 7, 6, 5, 7, the mode is 5.
The brute-force approach for computing a mode would scan the list and
compute the frequencies of all its distinct values, then find the value with the
largest frequency.
To implement this, we can store the values already encountered, along with
their frequencies, in a separate list.
On each iteration, the ith element of the original list is compared with
the values already encountered by traversing this auxiliary list.
If a matching value is found, its frequency is incremented; otherwise,
the current element is added to the list of distinct values with a
frequency of 1. 8
Example 2 Computing a mode
Since list’s ith element is compared with i – 1 elements of the auxiliary
list of distinct values seen so far before being added to the list with a
frequency of 1, the worst-case number of comparisons made by this
algorithm in creating the frequency list is
n
C(n) = ∑ (i-1) = 0 + 1 + . . . + (n-1) = (n-1)n Є Θ(n2).
i=1 2
The additional n – 1 comparisons are needed to find the largest
frequency in the auxiliary list.
In presort mode computation algorithm, the list is sorted first. Then all
equal values will be adjacent to each other. To compute the mode, all
we need to do is to find the longest run of adjacent equal values in the
sorted array.
The efficiency of this presort algorithm will be nlogn, which is the time
spent on sorting.
9
Example 2 Computing a mode
10
Example 3: Searching Problem
Problem: Search for a given K in A[0..n-1]
Presorting-based algorithm:
Stage 1 Sort the array by an efficient sorting algorithm.
Stage 2 Apply binary search.
Efficiency:
T(n) = Tsort(n) + Tsearch(n) = Θ(nlog n) + Θ(log n) = Θ(nlog n)
11
Binary Trees
Binary Tree: A binary tree is a finite set of elements that
is either empty or is partitioned into three disjoint
subsets:
- The first subset contains a single element called the
ROOT of the tree.
- The other two subsets are themselves binary trees,
called the LEFT and RIGHT subtrees of the original
tree.
12
A
B E
C D F
BINARY TREE
subtrees.
BINARY SEARCH TREE
Binary Search Tree (Binary Sorted Tree) :
Suppose T is a binary tree. Then T is called a binary search
tree if each node of the tree has the following property :
the value of each node is greater than every value in the left
subtree of that node and is less than every value in the right
subtree of N.
All<x All≥x 14
Example BSTs:
5 2
3
3 7
7
2 4 8
5 8
15
Draw the BSTs of height 2, 3, 4, 5, 6 on the following set of keys:
{ 1, 4, 5, 10, 16, 17, 21 } 16 17
16 21
10 10 17
4 17 10
4 21
1 5 16 21 4
1 5
HEIGHT 2
1 5
HEIGHT 3
HEIGHT 4
17 21 1
16 21 21 17 4
17 16
10 5
16
5 10 10
or 10 or
4 5 16
4 4 17
1
1 5 1 2116
HEIGHT 5 HEIGHT 6
Operations of BSTs: Insert
17
Insert Operations: 1st of 3 steps
1) The function begins at the root node and compares item 32
with the root value 25. Since 32 > 25, we traverse the right
subtree and look at node 35.
p aren t 25
20 35 t
12 40
(a)
St ep 1 : C om p are 3 2 and 25.
T ravers e t h e righ t s ub t ree.
18
Insert Operations: 2nd of 3 steps
2) Considering 35 to be the root of its own subtree, we compare
item 32 with 35 and traverse the left subtree of 35.
25
20 35 p arent
12 t 40
(b)
St ep 2: C om p are 32 an d 35 .
T rav ers e t h e left s ubt ree.
19
Insert Operations: 3rd of 3 steps
3) Create a leaf node with data value 32. Insert the new node as
the left child of node 35.
25
20 35 p arent
12 32 40
(c)
St ep 3: Ins ert 3 2 as left ch ild
o f p aren t 3 5
20
Insert in BST
Start at root.
Insert Key 52. 52 > 51
Go right.
51
14 72
06 33 53 97
13 25 43 64 84 99
21
Insert in BST
06 33 53 97
13 25 43 64 84 99
22
Insert in BST
06 33 53 97
13 25 43 64 84 99
23
Insert in BST
06 33 53 97
13 25 43 64 84 99
24
Insert in BST
06 33 53 97
52 < 53
Go left.
13 25 43 64 84 99
25
Insert in BST
06 33 53 97
52 < 53
Go left.
13 25 43 52 64 84 99
26
Operations of BSTs: Search
06 33 53 97
13 25 43 64 84 99
28
Search in BST
Start at root.
Search for Key 43.
43 < 51 51
Go left.
14 72
06 33 53 97
13 25 43 64 84 99
29
Search in BST
Search for Key 43.
43 < 51
Go left. 51
14 72
06 33 53 97
13 25 43 64 84 99
30
Search in BST
Search for Key 43.
51
14 72
43 > 14
Go right.
06 33 53 97
13 25 43 64 84 99
31
Search in BST
Search for Key 43.
51
14 72
43 > 14
Go right.
06 33 53 97
13 25 43 64 84 99
32
Search in BST
06 33 53 97
43 > 33
Go right.
13 25 43 64 84 99
33
Search in BST
06 33 53 97
43 > 33
Go right.
13 25 43 64 84 99
34
Search in BST
Search for Key 43.
51
14 72
06 33 53 97
13 25 43 64 84 99
43 = 43
FOUND
35
Search in BST
Start at root.
Search for Key 52. 52 > 51
Go right.
51
14 72
06 33 53 97
13 25 43 64 84 99
36
Search in BST
06 33 53 97
13 25 43 64 84 99
37
Search in BST
Search for Key 52.
51
14 52 < 72 72
Go left.
06 33 53 97
13 25 43 64 84 99
38
Search in BST
Search for Key 52.
51
14 52 < 72 72
Go left.
06 33 53 97
13 25 43 64 84 99
39
Search in BST
Search for Key 52.
51
14 72
06 33 52 < 53 53 97
Go left.
13 25 43 64 84 99
40
Search in BST
Search for Key 52.
51
14 72
06 33 53 97
52 < 53
Go left.
13 25 43 64 84 99
35 75 35 75
20 65 90 20 65
KEY
50 50
70 70
KEY
35 75 35 90
20 65 90 20 65
50 50 42
Binary Search Trees
The transformation from a set to a binary search tree is
an example of the representation-change technique.
43
Balanced Search Trees
There are two approaches to balance a search tree:
44
AVL TREES
AVL trees were invented in 1962 by two Russian scientists, G. M. Adelson-
Velsky and E. M. Landis, after whom this data structure is named.
Definition: An AVL tree is a binary search tree in which the balance factor
of every node, which is defined as the difference between the heights of the
node’s left and right subtrees, is either 0, +1 or -1. (The height of the empty
tree is defined as -1). 2
1
10 10
0 1 0 0
5 20 5 20
-1 1 -1
1 0
4 7 12 4 7
0 0 0 0
2 8 2 8
(a) (b)
Figure: (a) AVL tree. (b) Binary search tree that is not an AVL tree. The number 45
above each node indicates that node’s balance factor.
AVL TREES
If an insertion of a new node makes an AVL tree unbalanced, we
transform the tree by a rotation.
46
AVL TREES
Single right rotation (R-rotation):
This rotation is performed after a new key is inserted into
the left subtree of the left child of a tree whose root had the
balance if +1 before the insertion.
(Imagine rotating the edge connecting the root and its left
child in the binary tree in below figure to the right.)
2 0
1 3 2
1
3 0 0
0 2 R
0 R 1 3
2
1 (c) Balanced tree
inserting 1
r single R-rotation c
c r
T3 T1
T1 T2 T2 T3
This rotation is performed after a new key is inserted into the right
subtree of the right child of a tree whose root had the balance if -1
before the insertion.
(Imagine rotating the edge connecting the root and its right
child in the binary tree in below figure to the left.)
-2 0
-1
1 2
1 -1 L
0 0 0
L 2 1
2 3
0
(c) Balanced tree
3
(a) Balanced tree (b) Unbalanced tree after 49
inserting 3
AVL TREES
Double left-right rotation (LR-rotation):
It is performed after a new key is inserted into the right
subtree of the left child of a tree whose root had the balance
2
of +1 before the insertion. 2
3
1 3
-1
3 1
1
0 2 R
1 0 0
L 1
(a) Balanced tree 2
0
(b) Unbalanced tree after inserting 2
2
0 0
c c r
g T4
T2 T3
T1 T4
T1 T2 T3 or
or
Figure: General form of the double LR-rotation in the AVL tree. A shaded
node is the last one inserted. It can be either in the left subtree or
in 51
AVL TREES
Double right-left rotation (RL-rotation):
It is the mirror image of the double LR-rotation.
0 0
52
If there are several nodes with the ±2 balance, the rotation is done for
the tree rooted at the unbalanced node that is the closest to the newly
inserted leaf.
53
Construction of an AVL tree
EXAMPLE 1: 100, 200, 300, 250, 270, 70, 40
Step1: Element 100 is inserted into the empty tree as shown below:
0
100
-1
100
0
200
54
Construction of an AVL tree
EXAMPLE 1: 100, 200, 300, 250, 270, 70, 40
Step3: Element 300 is inserted towards right of 200 as shown below:
-2
100
-1
L 200
0
300
Above tree is unbalanced since balance factor at node 100 is -2.
All nodes 100, 200 and 300 are in straight line. Hence single rotation
is sufficient to balance the tree.
Since the tree is heavy towards right, L-rotation is made. i.e., the edge
connecting 100 to 200 is rotated left making 200 as the root. 55
Construction of an AVL tree
EXAMPLE 1: 100, 200, 300, 250, 270, 70, 40
0
200
0 0
100 300
Step4: Element 250 is inserted as the left child of 300 as shown below:
-1
200
0 1
100 300
0
250 56
Construction of an AVL tree
EXAMPLE 1: 100, 200, 300, 250, 270, 70, 40
Step5: Element 270 is inserted as the right child of 250 as shown below:
-2
0
200
2
100 300
-1
250 0
270
L
The youngest ancestor which is out of balance is 300. So we consider
node 300 with two nodes below it i.e., 250 and 270.
Since all these nodes are not lying on a straight line, the tree requires
double rotation (LR-rotation). 57
Construction of an AVL tree
EXAMPLE 1: 100, 200, 300, 250, 270, 70, 40
The first rotation occurs at node 250.
Left rotation brings 270 up and 250 becomes left child of 270.
-2
-2
0
200
2
0
200
2
100 300
-1
100 300
1
250 0 270
R
270 0
L
250
Now, right rotation is made at node 300. This makes node 300 to
58
become right child of 270.
Construction of an AVL tree
EXAMPLE 1: 100, 200, 300, 250, 270, 70, 40
-1
0
200
0
100 270
0 0
250 300
1
200
0
100 270
0 0 0
70 250 300 59
Construction of an AVL tree
EXAMPLE 1: 100, 200, 300, 250, 270, 70, 40
Step7: Element 40 is inserted as left child of 70 as shown below:
1
2
200
0
100 270
1 0
0
70 300
0 250
R
40
The tree is unbalanced since node 100 has the balance factor of 2.
Hence we consider node 100 and its below nodes 70 and 40.
Since nodes 100, 70 and 40 lie on a straight line, a single rotation is made.
60
Construction of an AVL tree
EXAMPLE 1: 100, 200, 300, 250, 270, 70, 40
Right rotation is performed since the tree is left heavy.
1 0
200 200
2 0
0 0
100 270
1
0
70 270
0
70 0 0 0 0
R 250 300
0 40 100 250 300
40
The tree is unbalanced since node 100 has the balance factor of 2.
Hence we consider node 100 and its below nodes 70 and 40.
Since nodes 100, 70 and 40 lie on a straight line, a single rotation is made.
61
Construction of an AVL tree
EXAMPLE 2: 5, 6, 8, 3, 2, 4, 7
5
0
5
-1
6
0
65
Construction of an AVL tree
1
6
1 0
5 8
0
6
2 0
5 8
1
3
0
6 1
2 0 6
5 8 0 0
1
3 8
3 R R(5) 0 0
0
2 5
2
6
-1 0
3 8
0 1
2 5
0
5
0 -2
3 6
0 0 1
2 4 8
0
A 2-3 tree is a tree that can have nodes of two kinds: 2-nodes and 3-nodes.
A 2-node contains a single key K and has two children: the left child
serves as the root of a subtree whose keys are less than K and the right
child serves as the root of a subtree whose keys are greater than K.
2 - node
K
<K >K 74
2-3 TREES
A 3-node contains two ordered keys K1 and K2 (K1 < K2)
and has three children.
The leftmost child serves as the root of a subtree with
keys less than K1
The middle child serves as the root of a subtree with
keys between K1 and K2.
The rightmost child serves as the root of a subtree with
keys greater than K2.
3 - node
K1 , K 2
Step1: Insert the element 9 into the empty tree as shown below:
5, 9
5, 8, 9 78
An example of a 2-3 tree construction is given below:
Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7
Since a node in a 2-3 tree cannot contain more than 2 elements, we move
the middle node to the parent position, left element 5 towards left of the
parent and right element 9 towards right of the parent as shown below:
5 9
3, 5 9 79
An example of a 2-3 tree construction is given below:
Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7
2, 3, 5 9
But, a node cannot have more than 2 elements. So, move the middle
element 3 to its parent position as shown below:
3, 8
2, 5 9 80
An example of a 2-3 tree construction is given below:
Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7
But, if a node has two elements i.e., 3, 8 (except the leaf i.e., 2, 5), it
should have 3 children: left child containing all elements less than 3,
middle child containing all elements between 3 and 8, and the right
child containing the elements larger than 8. So, the above tree can be
modified as shown below:
3, 8
2 5 9
2 4, 5 9 81
An example of a 2-3 tree construction is given below:
Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7
Step 7: Element to be inserted is 7. Since, 7 is between 3 and 8, it can be
inserted in the middle node along with 4, 5 as shown below:
3, 8
2 4, 5, 7 9
Since a node cannot have more than 2 elements, move 5 to the parent
position and the resulting tree is shown below:
3, 5, 8
2 4 7 9
82
An example of a 2-3 tree construction is given below:
Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7
3 8
2 4 7 9
83
Efficiency of 2-3 Trees
Since a 2-3 tree is always height balanced, the efficiency of any
operation depends only on the height of the tree.
To find the time complexity, it is required to find the lower bound as
well as the upper bound.
To find the lower bound for 2-3 tree: Consider the 2-3 tree with each
node exhibiting the property of 2-nodes.
Level 0 Number of nodes at level 0 = 1 = 20
Level 2
Number of nodes at level 2 = 4 = 22
| | | |
Level i Number of nodes at level i = 2i
| | | |
84
Efficiency of 2-3 Trees
Therefore, for any 2-3 tree of height h with n nodes, we get the
inequality:
n ≥ 20 + 21 + 22 + . . . + 2h = 2h+1 - 1
n + 1 ≥ 2h+1
So, taking log on both sides we get,
log2 (n + 1) ≥ h + 1
h ≤ log2 (n+1) - 1
To find the upper bound for 2-3 tree: Consider the 2-3 tree with
each node exhibiting the property of 3-nodes with each node having
maximum of two elements as shown below: Level 0 : 2 * 30
K1 , K 2
Level 1 : 2 * 31
K1 , K 2 K1 , K 2 K1 , K 2
Level 2 : 2 * 32
K1 , K 2 K|1, K2 K|1, K2 K1,| K2 K |, K K |, K K1, |K2
| 1 2 1 2 K1, |K2 K1|, K2 85
| | | | | | | | | Level i : 2 * 3i
Efficiency of 2-3 Trees
Therefore, for any 2-3 tree of height h with n nodes, we get the
inequality:
n ≤ 2 . 3 0 + 2 . 3 1 + 2 . 32 + . . . + 2 . 3 h
= 2( 30 + 31 + 32 + 3h) = 3h+1 - 1
n + 1 ≤ 3h+1
So, taking log on both sides we get,
log3 (n + 1) ≤ h + 1
h ≥ log3 (n+1) - 1
86
Heaps
A heap is a special type of data structure which is suitable for
implementing priority queue (Using priority queue, elements can be
inserted into queue based on the priority and elements can be
deleted from the queue based on the priority) and also suitable for
important sorting technique called heap sort.
87
Heaps
10 10 10
5 7 5 7 5 7
4 2 1 2 1
6 2 1
(a) Heap (b) Not a Heap since tree’s (c) Not a Heap since the
shape requirement is parental dominance
requirement fails for the
violated node with key 5.
Figure: Illustration of the definition of “heap”: only the leftmost tree
is a heap.
Note: Key values in a heap are ordered top down. There is no left-to-right order in
key values. i.e., there is no relationship among key values for nodes either88
on same level of tree or in the left and right subtree of same node.
Important properties of Heaps
There exists exactly one almost complete binary tree with n nodes. Its
height is equal to log2 n .
The root of a heap always contains its largest element.
A node of a heap considered with all its descendants is also a heap.
A heap can be implemented as an array by recording its elements in
the top-down, left-to-right fashion. It is convenient to store the heap’s
elements in positions 1 through n of such an array, leaving H[0]
unused. In such a representation,
a) the parental node keys will be in the first n/2 positions of the
array, while the leaf keys will occupy the last n/2 positions;
b) the children of a key in the array’s parental position i
(1 ≤ i ≤ n/2 ) will be in positions 2i and 2i + 1, and
correspondingly, the parent of a key in position i ( 2 ≤ i ≤ n) will be in
position i/2 . 0 1 2 3 4 5 6
10 10 5 7 4 2 1
5 7 Parents leaves
Array Representation of heap shown in
89
4 2 1 figure.
Heaps
Thus, we could also define a heap as an array H[1 . . . n] in which
every element in position i in the first half of the array is greater than
or equal to the elements in positions 2i and 2i + 1, i.e.,
H[i] ≥ max{ H[2i], H[2i + 1]} for i = 1, . . . , n/2 .
Construction of a Heap
Bottom-up heap construction algorithm:
It initializes the almost complete binary tree with n nodes by placing
keys in the order given and then “heapifies” the tree as follows:
Starting with the last parental node, the algorithm checks whether
the parental dominance holds for the key at this node.
The algorithm stops after this is done for the tree’s root.
91
Heaps
Construction of a Heap
2 2
2
9 8 9 8
9 7
6 5 6 5 7
6 5 8 7
9 9
2
2 8 6 8
9 8
6 5 7 2 5 7
6 5 7
Each key on level i of the tree will traverse to the leaf level h in the
worst case of the heap construction algorithm.
Since moving to next level down requires two comparisons – one to find
the larger child and the other to determine whether the exchange is
required, the total number of key comparisons involving a key on level i
will be 2(h – i).
Therefore, the total number of key comparisons in the worst case will be
h -1 h-1
To insert a new key K into a heap, first attach a new node with key K
in it after the last leaf of the existing heap.
Since the height of a heap with n nodes is about log2 n, the time
efficiency of insertion is in O(log n).
95
9 9 10
6 8 6 10 6 9
2 5 7 10 2 5 7 8 2 5 7 8
Figure: Inserting a key (10) into the heap. The new key is shifted up via a
swap with its parent until it is not larger than its parent ( or is in the
root).
96
Deletion from a Heap
Consider the deletion of the root’s key.
Maximum Key Deletion from heap
Step1 Exchange the root’s key with the last key K of the heap.
Step2 Decrease the heap’s size by 1.
Step3 “Heapify” the smaller tree by shifting K down the tree exactly
in the same way we did it in the bottom-up heap construction
algorithm. i.e., verify the parental dominance for K: if it holds,
we are done; if not, swap K with the larger of its children and
repeat this operation until the parental dominance condition
holds for K in its new position.
8 6
2 5 1
Step1 Step3
1
Step2 1 8
8 8 6 5 6
6
2 2 5 2 1
5 9
Figure: Deleting root’s key from heap. The key to be deleted is swapped with the last
key, after which the smaller tree is “heapified” by exchanging the new key at
its root with the larger key at its children until the parental dominance 98
requirement is satisfied.
HEAPSORT
This sorting algorithm was discovered by J. W. Williams.
9 7 9 8 9 8
6 5 8 6 5 7 6 5 7
2 9 9
9 8 2 8 6 8
6 5 7 6 5 7 2 5 7
6 8 6 8 6 7
2 5 7 2 5 9 2 5 9
HEAP: Exchange 9 and 7 Heapify the list 7, 6, 8, 2, 5 HEAP: Exchange 8 and 5
5 7 2
6 7 6 5 6 5
2 8 9 2 8 9 7 8 9
Heapify the list 5, 6, 7, 2 HEAP: Exchange 7 and 2 Heapify the list 2, 6, 5
102
HEAPSORT
6 5 5
2 5 2 6 2 6
7 8 9 7 8 7
9 8 9
HEAP: Exchange 6 and 5 Heapify the list 5, 2 HEAP: Exchange 5 and 2
2 2
5 6 5 6
7 8 9 7 8 9
Therefore, the time complexity for the second stage = 2n log2 n ≈ n log2 n.
So, the time complexity of heap sort =time complexity of stage 1 + time
complexity of stage 2
= O(n) + O(n log2 n)
= O(max{ n, n log2 n })
= O(n log2 n)
Therefore, the time complexity of heapsort = O(nlog2 n)
105
End of Chapter 6
106