0% found this document useful (0 votes)
12 views

Module III

enginneeing

Uploaded by

sandeshnaik525
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Module III

enginneeing

Uploaded by

sandeshnaik525
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

MODULE-III

Contents

1. TRANSFORM-AND-CONQUER .......................................................................................................1
1.1 BALANCED SEARCH TREES.........................................................................................................2
1.2 HEAPS AND HEAPSORT..................................................................................................................4
2. SPACE AND TIME TRADEOFFS:.....................................................................................................6
2.1 SORTING BY COUNTING..........................................................................................................7
2.2 TOPOLOGICAL SORTING........................................................................................................8

LECTURE 23:

1. TRANSFORM AND CONQUEER


The general technique of group of design methods that are based on the idea of transformation
are called transform-and-conquer because these 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 amenable to solution. Then, in the second or conquering stage, it is solved.

There are three major variations of this idea that differ by what we transform a given instance
to :

 Transformation to a simpler or more convenient instance of the same problem—we


call it instance simplification.
 Transformation to a different representation of the same instance—we call it
representation change.
 Transformation to an instance of a different problem for which an algorithm is already
available—we call it problem reduction.

1.1 BALANCED SEARCH TREE


It is a binary tree whose nodes contain elements of a set of orderable items, one element per node,
so that all elements in the left subtree are smaller than the element in the subtree’s root, and all the
elements in the right subtree are greater than it. This transformation from a set to a binary search

SOWMYA K, DEPARTMENT OF CSE,CEC 1


tree is an example of the representation-change technique. By such transformation we gain in the
time efficiency of searching, insertion, and deletion, which are all θ(log n), but only in the average
case. In the worst case, these operations are in θ(n) because the tree ca degenerate into a severely
unbalanced one with its height equal to n − 1.

Computer scientists have come up with two approaches to preserves the good properties of the
classical binary search tree are:

 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. Specific implementations of this idea differ by their definition of balance. An
AVL tree requires the difference between the heights of the left and right subtrees of every
node never exceed 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.
 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-trees. They differ in the number of elements admissible in a single
node of a search tree, but all are perfectly balanced.

AVL Trees:
AVL trees were invented in 1962 by two Russian scientists, G. M. Adelson-Velsky and E. M.
Landis [Ade62], after whom this data structure is named.
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 or +1 or −1. The
height of the empty tree is defined as −1.

For example, the binary search tree in Figure 6.2a is an AVL tree but the one in Figure 6.2b is not.

SOWMYA K, DEPARTMENT OF CSE,CEC 2


If an insertion of a new node makes an AVL tree unbalanced, we transform the tree by a rotation.
A rotation in an AVL tree is a local transformation of its subtree rooted at a node whose balance
has become either +2 or −2. 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. In their simplest form, the four
rotations are shown in Figure 6.3.
The first rotation type is called the single right rotation, or R-rotation. Figure 6.4 presents the
single R-rotation in its most general form. 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 (Figure 6.5). 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

SOWMYA K, DEPARTMENT OF CSE,CEC 3


.

Review Questions:

1. Define transform and conquer.


2. What are the three variations of transform and conquer?
3. What is Balanced Search tree.
4. What is AVL Tree?
5. What is R – rotation and L – rotation?

SOWMYA K, DEPARTMENT OF CSE,CEC 4


LECTURE 24:

Construction of AVL Tree:

Construction of an AVL Tree for the list 5,6,8,3,2,4,7 by successive insertions.

An example of constructing an AVL tree for a given list of numbers is shown in the above figure.
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.
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.
[log 2 n] ≤ h < 1.4405 log 2(n + 2) − 1.3277.

SOWMYA K, DEPARTMENT OF CSE,CEC 5


The operations of searching and insertion are θ(log n) in the worst case. The average height of an
AVL tree constructed for random lists of keys is about 1.01log 2 n + 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 has same efficiency class as insertion, i.e.,
logarithmic.
The drawbacks of AVL trees are frequent rotations and the need to maintain balances for its nodes,
which has prevented AVL trees from becoming the standard structure for implementing
dictionaries. The advantage of AVL trees are that of rebalancing a binary search tree via rotations
has led to discoveries of other interesting variations of the classical binary search tree.

Review Questions:
1. How to construct an AVL Tree?
2. What is the drawback of AVL trees?
3. What is the advantage of AVL trees?

SOWMYA K, DEPARTMENT OF CSE,CEC 6


LECTURE 25:
2-3 TREES
The simplest implementation of balancing a search tree to allow more than one key in the same
node of such a tree is 2-3 trees, introduced by the U.S. computer scientist John Hopcroft in 1970.
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.
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, and the rightmost child serves as the root of a subtree with
keys greater than K2

Two kinds of nodes of 2-3 Tree


The last requirement of the 2-3 tree is that all its leaves must be on the same level. In other words,
a 2-3 tree is always perfectly height-balanced: the length of a path from the root to a leaf is the
same for every leaf.
Searching for a given key K in a 2-3 tree is quite straightforward. Start at the root. If the root is a
2-node, either stop if K is equal to the root’s key or continue the search in the left or right subtree
if K is, respectively, smaller or larger than the root’s key. If the root is a 3- node, we know after no
more than two key comparisons whether the search can be stopped (if K is equal to one of the
root’s keys) or in which of the root’s three subtrees it needs to be continued.
Inserting a new key in a 2-3 tree is done as follows. First of all, always insert a new key K in a
leaf, except for the empty tree. The appropriate leaf is found by performing a search for K. If the
leaf in question is a 2-node, insert K there as either the first or the second key, depending on
whether K is smaller or larger than the node’s old key. If the leaf is a 3-node, split the leaf in two:
the smallest of the three keys (two old ones and the new key) is put in the first leaf, the largest key
is put in the second leaf, and the middle key is promoted to the old leaf’s parent. (If the leaf
happens to be the tree’s root, a new root is created to accept the middle key.) Note that promotion

SOWMYA K, DEPARTMENT OF CSE,CEC 7


of a middle key to its parent can cause the parent’s overflow (if it was a 3-node) and hence can
lead to several node splits along the chain of the leaf’s ancestors.

Construction of 2-3 Tree for the List 9,5,8,3,2,4,7


As for any search tree, the efficiency of the dictionary operations depends on the tree’s height. A
2-3 tree of height h with the smallest number of keys is a full tree of 2-nodes. Therefore, for any
2-3 tree of height h with n nodes, we get the inequality
n ≥ 1 + 2 + . . . + 2h = 2h+1 − 1,
and hence
h ≤ log 2(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. 3h = 2(1 + 3 + . . . + 3h) = 3h+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,
imply that the time efficiencies of searching, insertion, and deletion are all in θ(log n) in both the
worst and average case.

Review Questions:
1. What is 2-node ?
2. Define 3-node.
3. What is the upper bound of 2-3 tree?
4. What is the lower bound of 2-3 tree?
5. What is the efficiency for insertion in 2-3 tree?

SOWMYA K, DEPARTMENT OF CSE,CEC 8


LECTURE 26:

1.2 .HEAPS and HEAPSORT

The data structure called the “heap” is, partially ordered data structure that is especially suitable for
implementing priority queues. Priority queue is a multiset of items with an orderable characteristic
called an item’s priority, with the following operations:

 finding an item with the highest (i.e., largest) priority


 deleting an item with the highest priority
 adding a new item to the multiset
The heap is also the data structure that serves as a cornerstone of a theoretically important sorting
algorithm called heapsort.

NOTION OF THE HEAP


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.

2. The parental dominance or heap property—the key in each node is greater than or equal to the
keys in its children.

Illustration if heap. Only the leftmost is heap

For example, consider the trees of above figure. The first tree is a heap. The second one is not a
heap, because the tree’s shape property is violated. And the third one is not a heap, because the
parental dominance fails for the node with key 5.

Key values in a heap are ordered top down; i.e., a sequence of values on any path from the root to a
leaf is decreasing. However, there is no left-to-right order in key values; i.e., there is no relationship

SOWMYA K, DEPARTMENT OF CSE,CEC 9


among key values for nodes either on the same level of the tree or, more generally, in the left and
right subtrees of the same node.

Properties of heaps are:

Heap and its array representation

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]) 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].

A heap can be defined 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].

Review Questions:
1. What is priority queue?
2. Define Heap.
3. What element does the root of heap contain?
4. How is heap represented using an array?

SOWMYA K, DEPARTMENT OF CSE,CEC 10


LECTURE 27:
HEAP TREE CONSTRUCTION

There are two principal alternatives for constructing a heap for a given list of keys.
The first is the bottom-up heap construction algorithm as illustrated in figure

Bottom construction of heap for the list 2,9,7,6,5,8


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 predecessor. The algorithm
stops after this is done for the root of the tree.

How efficient is this algorithm in the worst case? Assume, for simplicity, that n = 2 k − 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 in the list at the beginning of the

SOWMYA K, DEPARTMENT OF CSE,CEC 11


section, h = [log2 n] or just [log 2 (n + 1)] − 1 = k − 1 for the specific values of n we are
considering. 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

The alternative algorithm constructs a heap by successive insertions of a new key into previously
constructed heap; 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. Then shift 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 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
Since the height of a heap with n nodes is about log 2 n, the time efficiency of insertion is in O(log
n).

Inserting a key

Deleting the root’s key from a heap can be done with the following algorithm, illustrated in figure

Deleting the roots key from heap


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.

SOWMYA K, DEPARTMENT OF CSE,CEC 12


Step 3 “Heapify” the smaller tree by shifting K down the tree exactly in the same as 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

Review Questions
1. What is bottom up heap construction?
2. What is the total number of key comparisons in the worst case?
3. What is top down heap construction?
4. What is the time efficiency for insertion?
5. What is the time efficiency for deletion?

SOWMYA K, DEPARTMENT OF CSE,CEC 13


LECTURE 28:

HEAPSORT
Heapsort 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. Heapsort is traced on a specific input in the figure

Sorting the array 2,9,7,6,5,8 by heapsort


The heap construction stage of the algorithm is in O(n), we need to investigate just the time
efficiency of the second stage. For the number of key comparisons, C(n), needed for eliminating
the root keys from the heaps of diminishing sizes from n to 2, we get the following inequality:

This means that C(n) ∈ O(n log n) for the second stage of heapsort. For both stages, we get O(n)
+ O(n log n) = O(n log n). The time efficiency of heapsort is, in fact, in θ(n log n) in both the
worst and average cases. Thus, heapsort’s time efficiency falls in the same class as that of
mergesort.

SOWMYA K, DEPARTMENT OF CSE,CEC 14


Review Questions

1. What is heap construction.


2. Define maximum deletions.
3. What is the time efficiency of Heapsort?
4.

SOWMYA K, DEPARTMENT OF CSE,CEC 15

You might also like