0% found this document useful (0 votes)
15 views37 pages

M3 - Chapter 6 1

Chapter 6 discusses Transform-and-Conquer methods, which involve modifying a problem instance to make it easier to solve, with variations including instance simplification, representation change, and problem reduction. It provides examples such as presorting for checking uniqueness and computing modes, as well as searching algorithms that benefit from sorting. The chapter also covers balanced search trees like AVL and 2-3 trees, explaining their properties, insertion methods, and time complexities, along with an introduction to heaps and heapsort.

Uploaded by

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

M3 - Chapter 6 1

Chapter 6 discusses Transform-and-Conquer methods, which involve modifying a problem instance to make it easier to solve, with variations including instance simplification, representation change, and problem reduction. It provides examples such as presorting for checking uniqueness and computing modes, as well as searching algorithms that benefit from sorting. The chapter also covers balanced search trees like AVL and 2-3 trees, explaining their properties, insertion methods, and time complexities, along with an introduction to heaps and heapsort.

Uploaded by

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

Chapter 6

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.

• The brute-force solution here is sequential search, which needs ‘n’


comparisons in the worst case. If the array is sorted first, we can then
apply binary search, which requires only log2n + 1 comparisons in the
worst case.
• Assuming the most efficient nlog n sort, the total running time of such
a searching algorithm in the worst case will be,
2. Balanced Search Trees
There are two approaches:
1. The first approach is of the instance-simplification variety: an unbalanced binary search
tree is transformed into a balanced one. Because of this, such trees are called self-
balancing.
• An AVL tree (Named after their inventors, Adelson-Velskii and Landis) requires the
difference between the heights of the left and right subtrees of every node never
exceed 1 (it can be -1, 0, 1).
• A red-black tree tolerates the height of one subtree being twice as large as the other
subtree of the same node.
• If an insertion or deletion of a new node creates a tree with a violated balance
requirement, the tree is restructured by one of a family of special transformations
called rotations that restore the balance required.
Invented by: Rudolf Bayer
2. The second approach is of the representation-change variety: allow more than one
element in a node of a search tree. Specific cases of such trees are 2-3 trees, 2-3-4 trees,
and more general and important B (balance)-trees. They differ in the number of elements
admissible in a single node of a search tree, but all are perfectly balanced.

• 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 symmetric single left rotation, or L-rotation, is the mirror image


of the single R-rotation. It is performed after a new key is inserted into
the right subtree of the right 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.

• The double right-left rotation (RL-rotation) is the mirror image of


the double LR-rotation

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,

• Worst case possible height of AVL tree is 1.44*log n


• The inequalities immediately imply that the operations of searching, and insertion are ϴ(log n) in the worst
case. Getting an exact formula for the average height of an AVL tree constructed for random lists of keys
has proved to be difficult, but it is known from extensive experiments that it is about 1.01logn + 0.1 except
when n is small. Thus, searching in an AVL tree requires, on average, almost the same number of
comparisons as searching in a sorted array by binary search.
• The operation of key deletion in an AVL tree is considerably more difficult than insertion, but fortunately it
turns out to be in the same efficiency class as insertion, i.e., logarithmic.
• Advantages of AVL trees: Impressive efficiency characteristics come at a price.
• The drawbacks of AVL trees are frequent rotations and the need to maintain balances for its nodes.
2-3 trees
• The time complexity of search / insert / delete operation is ϴ(log N) in both worst and avg case.
Properties of 2-3 tree:
• Nodes with two children are called 2-nodes. The 2-nodes have one data value and two children
• Nodes with three children are called 3-nodes. The 3-nodes have two data values and three children.
• Data is in sorted order (ascending).
• It is a balanced tree.
• All the leaf nodes are at same level.
• Each node can either be leaf, 2 node, or 3 node.
• Always insertion is done at leaf.
Insertion: There are 3 possible cases in insertion
Case 1: Insert in a node with only one data element

Case 2: Insert in a node with two data elements


whose parent contains only one data element.

Case 3: Insert in a node with two data elements


whose parent also contains two data elements.
• Any 2-3 tree of height h with n nodes, we get the inequality n ≥ 1 + 2 + ... + 2ℎ = 2ℎ+1
− 1, and hence
h ≤ log2(n + 1) − 1.
• On the other hand, a 2-3 tree of height ‘h’ with the largest number of keys is a full tree
of 3-nodes, each with two keys and three children. Therefore, for any 2-3 tree with ‘n’
nodes,
n ≤ 2.1 + 2.3 +...+ 2.3ℎ = 2 (1+3+...+3ℎ ) =3ℎ+1 − 1 and hence , h ≥ log3 (n + 1) − 1.
• These lower and upper bounds on height h, log3(n + 1) − 1 ≤ h ≤ log2(n + 1) − 1,
• The time efficiencies of searching, insertion, and deletion are all in ϴ(log n) in both the
worst and average case.
Tutorial:
1. Construct AVL tree and 2-3 tree for the following data
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
https://www.youtube.com/watch?v=HqPJF2L5h9U

3. Heaps and Heapsort


Notion of the Heap
DEFINITION A heap can be defined as a binary tree with keys assigned to its nodes, one
key per node, provided the following two conditions are met:
1. The shape property—the binary tree is essentially complete (or simply complete), i.e.,
all its levels are full except possibly the last level, where only some rightmost leaves
may be missing. (If Right child is present without Left child then it is not complete.)
2. The parental dominance or heap property—the key in each node is greater than or
equal to the keys in its children. (This condition is considered automatically satisfied
for all leaves.)
Important properties of heaps:
1. There exists exactly one essentially complete binary tree with ‘n’ nodes.
Its height is equal to log2n
2. The root of a heap always contains its largest element.
3. A node of a heap considered with all its descendants is also a heap.
4. 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] either
unused or putting there a sentinel whose value is greater than every
element in the heap. 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) FIGURE : Heap and its array
will be in positions 2i and 2i + 1, and, correspondingly, the parent of a representation.
key in position i (2 ≤ i ≤ n) will be in position i/2.
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.,
How can we construct a heap for a given list of keys?
• There are two principal alternatives for doing this.
1. The first is the bottom-up heap construction
algorithm. It initializes the essentially 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 in this node.
• If it does not, the algorithm exchanges the node’s
key K with the larger key of its children and checks
whether the parental dominance holds for K in its
new position.
• This process continues until the parental dominance
for K is satisfied.
• After completing the “heapification” of the subtree
rooted at the current parental node, the algorithm
proceeds to do the same for the node’s immediate // Recursively heapify the affected sub-tree i.e,
predecessor. subtree with root as largest.
• The algorithm stops after this is done for the root of Heap BottomUp(H[1…n])
the tree.
How efficient is this algorithm in the worst case?
• Assume, for simplicity, that n = 2k − 1 so that a heap’s tree is full, i.e., the largest possible number
of nodes occurs on each level.
• Let ‘h’ be the height of the tree. According to the first property of heaps, h = log2n .
• Each key on level i of the tree will travel to the leaf level ‘h’ in the worst case of the heap
construction algorithm. Since moving to the 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

Note: 2(h-i) is constant.

• 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.

So how can we 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.
• 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

• Consider the problem of counting paths between two vertices in a graph.


• It is not difficult to prove by mathematical induction that the number of different paths of length k > 0 from
the ith vertex to the jth vertex of a graph (undirected or directed) equals the (i, j)th element of Ak where A is
the adjacency matrix of the 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.

EXAMPLE 1 Consider a university


endowment that needs to invest $100 million.
This sum has to be split between three types of
investments: stocks, bonds, and cash. The
endowment managers expect an annual return
of 10%, 7%, and 3% for their stock, bond, and Although this example is both small and simple, it does show
cash investments, respectively. Since stocks are how a problem of optimal decision making can be reduced to
more risky than bonds, the endowment rules an instance of the general linear programming problem
require the amount invested in stocks to be no
more than one-third of the moneys invested in
bonds. In addition, at least 25% of the total
amount invested in stocks and bonds must be
invested in cash. How should the managers
invest the money to maximize the return?
EXAMPLE 2: Reduction of knapsack problem to a linear
programming problem.
The knapsack problem can be posed as follows.
Given a knapsack of capacity W and ‘n’ items of weights
w1,...,wn and values v1,...,vn, find the most valuable
subset of the items that fits into the knapsack.
1. First consider the continuous (or fractional) version of the
problem, in which any fraction of any item given can be taken
into the knapsack. 2. In the discrete (or 0-1) version of the
knapsack problem, we are only allowed
• Let xj, j = 1,...,n, be a variable representing a fraction of either to take a whole item or not to take it at
item j taken into the knapsack. Obviously, xj must satisfy all. Hence, we have the following integer
the inequality 0 ≤ xj ≤ 1. linear programming problem for this version:
• Then the total weight of the selected items can be expressed
by the sum σ𝑛𝑗=1 𝑤𝑗 𝑥𝑗 , and their total value by the sum
σ𝑛𝑗=1 𝑣𝑗 𝑥𝑗 .
• Thus, the continuous version of the knapsack problem can
be posed as the following linear programming problem:
Example 5: Reduction to
Graph Problems

One of the graph’s vertices


represents an initial state and another
represents a goal state of the
problem. Such a graph is called a
state-space graph.

By:
Dr. Geeta S Hukkeri

You might also like