M3 - Chapter 6 1
M3 - Chapter 6 1
Transform-and-Conquer
• Transform-and-Conquer methods work as two-stage procedures.
• First, in the transformation stage, the problem’s instance is modified to be, for one reason or another,
more manageable to solution.
• In the second stage, it is solved.
• There are three major variations of this idea:
1. Instance simplification: Transformation to a simpler or more convenient instance of the same problem
2. Representation change: Transformation to a different representation of the same instance.
3. Problem reduction: Transformation to an instance of a different problem for which an algorithm is
already available.
1. Presorting:
EXAMPLE 1 : Checking element uniqueness in an array
• Its worst-case efficiency was in θ (n2) using brute-force algorithm.
• Alternatively, 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.
• The running time of this algorithm is the sum of the time spent on sorting and the time spent on checking
consecutive elements. If we use a good sorting algorithm, such as mergesort, with worst-case efficiency in
θ(n log n), the worst-case efficiency of the entire presorting-based algorithm will be also in θ(n log n):
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. (If several different values occur most often, any of them can be considered
a mode.) The brute-force approach to computing a mode would scan the list and compute the frequencies of all its distinct
values, then find the value with the largest frequency.
• In order to implement this idea, 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 seen so far with a frequency of 1.
• It is not difficult to see that the worst-case input for this algorithm is a list with no equal elements. For such
a list, its 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. As a result, the worst-case number of comparisons
made by this algorithm in creating the frequency list is,
• The additional (n −1) comparisons needed to find the largest frequency in the auxiliary list do not change
the quadratic worst-case efficiency class of the algorithm.
• As an alternative, let us first sort the input. 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.
Ex: 5, 1, 5, 7, 6, 5, 7
EXAMPLE 3 Searching problem : Consider the problem of searching
for a given value ‘v’ in a given array of ‘n’ sortable items.
• Nodes on the outside of the tree i.e. the leaves have no children and have either one or
two data elements. All its leaves must be on the same level so that a 2-3 tree is always
height balanced. A 2-3 tree is a special case of a B-Tree of order 3.
http://www.btechsmartclass.com/data_structures/avl-trees.html
• AVL trees:
• If an insertion of a new node makes
DEFINITION An AVL tree is a binary search
tree in which the balance factor of every node, an AVL tree unbalanced, we
which is defined as the difference between the transform the tree by a rotation.
heights of the node’s left and right subtrees, is
either 0 or +1 or −1. (The height of the empty tree • A rotation in an AVL tree is a local
is defined as −1. Of course, the balance factor can transformation of its subtree rooted
also be computed as the difference between the at a node whose balance has become
numbers of levels rather than the height either +2 or −2.
difference of the node’s left and right subtrees.)
• If there are several such nodes, we
rotate the tree rooted at the
unbalanced node that is the closest to
the newly inserted leaf.
• There are only four types of
rotations; in fact, two of them are
mirror images of the other two.
• The first rotation type is called the single right rotation, or 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 of
+1 before the insertion.
• The second rotation type is called the double left-right rotation (LR-
rotation). It is, in fact, a combination of two rotations: we perform the
L-rotation of the left subtree of root r followed by the R-rotation of the
new tree rooted at r. 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 of
+1 before the insertion.
https://www.youtube.com/watch?v=YWqla0UX-38&t=749s
The time complexity of search / insert /
delete operation is ϴ(log N) in both
worst and avg case.
https://www.youtube.com/watch?v=YWqla0UX-38&t=749s
• How efficient are AVL trees? As with any search tree, the critical characteristic is the tree’s height. It turns
out that it is bounded both above and below by logarithmic functions. Specifically, the height ‘h’ of any
AVL tree with ‘n’ nodes satisfies the inequalities,
• With this bottom-up algorithm, a heap of size n can be constructed with fewer than 2n
comparisons.
https://www.youtube.com/watch?v=OuScmMXLNsU
2. The top-down heap construction algorithm : The alternative (and less efficient)
algorithm constructs a heap by successive insertions of a new key into a previously
constructed heap.
First, attach a new node with key K in it after the last leaf of the existing heap.
• Then sift K up to its appropriate place in the new heap as follows.
➢Compare K with its parent’s key:
- If the latter is greater than or equal to K, stop (the structure is a heap);
- otherwise, swap these two keys and compare K with its new parent. This
swapping continues until K is not greater than its last parent or it reaches
the root.
➢Obviously, this insertion operation cannot require more key comparisons
than the heap’s height. Since the height of a heap with ‘n’ nodes is about
log2n, the time efficiency of insertion is in O(log n).
https://www.youtube.com/watch?v=CzKE2C1UkxI
How can we delete an item from a heap?
We consider here only the most important case of deleting the root’s key.
Maximum Key Deletion from a heap
Step 1 Exchange the root’s key with the last key K of the heap.
Step 2 Decrease the heap’s size by 1.
Step 3 “Heapify” the smaller tree by sifting K down the tree exactly in the same way
we did it in the bottom-up heap construction algorithm. That is, 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.
• The efficiency of deletion is determined by the number of key comparisons needed to
“heapify” the tree after the swap has been made and the size of the tree is decreased by
1. Since this cannot require more key comparisons than twice the heap’s height, the
time efficiency of deletion is in O(log n) as well.
Tutorial:
1. Construct a heap (tree-based data structure) for the following arrays and find the
minimum number of interchanges needed to convert into a heap.
a. 55, 30, 15, 75, 65, 45, 5
b. 50, 60, 70, 40, 30, 20, 10, 80, 100
c. 21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7
d. 14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
2. Delete root node from the constructed heap of the above arrays.
Heapsort
This is a two-stage algorithm that works as
follows.
Stage 1- (heap construction): Construct a
heap for a given array.
Stage 2- (maximum deletions):
Apply the root-deletion operation n − 1
times to the remaining heap.
• As a result, the array elements are
eliminated in decreasing order. But since
under the array implementation of heaps
an element being deleted is placed last, the
resulting array will be exactly the original
array sorted in increasing order.
Note: Heapsort is based on priority queue.
FIGURE : Sorting the array 2, 9, 7, 6, 5, 8 by heapsort.
4. Problem Reduction
• A well-known joke about mathematicians.
• Professor X, a noted mathematician, noticed that when his wife wanted to boil water
for their tea, she took their kettle from their cupboard, filled it with water, and put it on
the stove. Once, when his wife was away (if you have to know, she was signing her
best-seller in a local bookstore), the professor had to boil water by himself. He saw
that the kettle was sitting on the kitchen counter. What did Professor X do? He put the
kettle in the cupboard first and then proceeded to follow his wife’s routine.
• The way Professor X approached his task is an example of an important problem-solving
strategy called problem reduction. If you need to solve a problem, reduce it to another
problem that you know how to solve.
Example1: Computing the Least Common Multiple :
• The least common multiple of two positive integers m and n, denoted lcm(m, n), is defined as the smallest
integer that is divisible by both m and n.
• For example, lcm(24, 60) = 120, and lcm(11, 5) = 55. The least common multiple is one of the most
important notions in elementary arithmetic and algebra.
• Perhaps you remember the middle-school method for computing it:
• Given the prime factorizations of m and n, compute the product of all the common prime factors of m and
n, all the prime factors of m that are not in ‘n’, and all the prime factors of n that are not in ‘m’.
• For example, 24 = 2 . 2 . 2 . 3, 60 = 2 . 2 . 3 . 5, lcm(24, 60) = (2 . 2 . 3) . 2 . 5 = 120.
• A much more efficient algorithm for computing the least common multiple can be devised by using problem
reduction. After all, there is a very efficient algorithm (Euclid’s algorithm) for finding the greatest common
divisor, which is a product of all the common prime factors of m and n.
• Can we find a formula relating lcm(m, n) and gcd(m, n)? It is not difficult to see that the product of lcm(m, n)
and gcd(m, n) includes every factor of m and n exactly once and hence is simply equal to the product of m and
n. This observation leads to the formula,
lcm(m, n) = (m . n) / gcd(m, n),
where gcd(m, n) can be computed very efficiently by Euclid’s algorithm.
Example2: Counting Paths in a Graph
• There are three paths of length 2 that start and end at vertex a (a − b − a, a − c − a, and a − d − a);
• but there is only one path of length 2 from a to c (a − d − c).
Example3: Reduction of Optimization Problems
• If a problem asks to find a maximum of some function, it
is said to be a maximization problem; if it asks to find a
function’s minimum, it is called a minimization problem.
• Suppose that you need to find a minimum of some
function f (x) and you have an algorithm for function
maximization.
The answer lies in the simple formula
min f (x) = − max[−f (x)].
• In other words, to minimize a function, we can maximize
its negative instead and, to get a correct minimal value of
the function itself, change the sign of the answer.
Reference:
max f (x) = − min[−f (x)]
https://www.whitman.edu/mathematics/calculus_onli
ne/section06.01.html
https://openstax.org/books/calculus-volume-
1/pages/4-7-applied-optimization-problems
Example4: Linear Programming Let x, y, and z be the amounts (in millions of dollars) invested
in stocks, bonds, and cash, respectively. By using these
Linear programming problem—a problem of
optimizing a linear function of several variables, we can pose the following optimization problem:
variables subject to constraints in the form of
linear equations and linear inequalities.
By:
Dr. Geeta S Hukkeri