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

BCS401-Module-3

The document discusses the Transform-and-Conquer technique, focusing on balanced search trees, heaps, and heapsort. It explains the concepts of AVL trees and 2-3 trees, detailing their properties, construction methods, and efficiency in operations like searching and insertion. Additionally, it covers the definition and characteristics of heaps, including their use in priority queues and heapsort algorithms.

Uploaded by

aishwarya.k
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)
143 views

BCS401-Module-3

The document discusses the Transform-and-Conquer technique, focusing on balanced search trees, heaps, and heapsort. It explains the concepts of AVL trees and 2-3 trees, detailing their properties, construction methods, and efficiency in operations like searching and insertion. Additionally, it covers the definition and characteristics of heaps, including their use in priority queues and heapsort algorithms.

Uploaded by

aishwarya.k
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/ 23

MODULE-III

TRANSFORM-AND-CONQUER: Balanced Search Trees, Heaps and Heapsort. SPACE-


TIME TRADEOFFS: Sorting by Counting: Comparison counting sort, Input Enhancement
in String Matching: Horspool’s Algorithm. Chapter 6 (Sections 6.3,6.4), Chapter 7
(Sections 7.1,7.2)

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

SOWMYA K, DEPARTMENT OF CSE,CEC 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.
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

SOWMYA K, DEPARTMENT OF CSE,CEC 2


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

SOWMYA K, DEPARTMENT OF CSE,CEC 7


promotion 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

The first tree is a heap. The second one is not a heap, because the tree’s shape property is violated.
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 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:

SOWMYA K, DEPARTMENT OF CSE,CEC 9


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

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.

SOWMYA K, DEPARTMENT OF CSE,CEC 12


Step 2 Decrease the heap’s size by 1.
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

SOWMYA K, DEPARTMENT OF CSE,CEC 14


worst and average cases. Thus, heapsort’s time efficiency falls in the same class as that of
mergesort.

Review Questions

1. What is heap construction.


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

SOWMYA K, DEPARTMENT OF CSE,CEC 15


LECTURE 29:

SPACE AND TIME TRADEOFFS


Consider, the problem of computing values of a function at many points in its domain. If it is
time that is at a premium, we can precompute the function’s values and store them in a table.
This is exactly what human computers had to do before the advent of electronic computers, the
underlying idea has proven to be quite useful in the development of several important algorithms
for other problems. The idea is to preprocess the problem’s input, in whole or in part, and store
the additional information obtained to accelerate solving the problem afterward. This approach
is called input enhancement

The other type of technique that exploits space-for-time trade-offs simply uses extra space to
facilitate faster and/or more flexible access to the data. This approach is called prestructuring.
This name highlights two facets of this variation of the space-for-time trade-off: some processing
is done before a problem in question is actually solved but, unlike the input-enhancement variety,
it deals with access structuring

There is one more algorithm design technique related to the space-for-time trade-off idea:
dynamic programming. This strategy is based on recording solutions to overlapping subproblems
of a given problem in a table from which a solution to the problem in question is then obtained.

Two final comments about the interplay between time and space in algorithm design need to be
made. First, the two resources—time and space—do not have to compete with each other in all
design situations. In fact, they can align to bring an algorithmic solution that minimizes both the
running time and the space consumed. Such a situation arises, in particular, when an algorithm
uses a space-efficient data structure to represent a problem’s input, which leads, in turn, to a
faster algorithm. Second, one cannot discuss space-time trade-offs without mentioning the
hugely important area of data compression. Note, however, that in data compression, size
reduction is the goal rather than a technique for solving another problem.

SORTING BY COUNTING -
As a first example of applying the input-enhancement technique, we discuss its application to
the sorting problem. For each element of a list to be sorted, count the total number of elements
smaller than this element and record the results in a table. These numbers will indicate the
positions of the elements in the sorted list: e.g., if the count is 10 for some element, it should be

SOWMYA K, DEPARTMENT OF CSE,CEC 16


in the 11th position (with index 10, if we start counting with 0) in the sorted array. Thus, we will
be able to sort the list by simply copying its elements to their appropriate positions in a new,
sorted list. This algorithm is called comparison-counting sort

The time efficiency of this algorithm should be quadratic because the algorithm considers all the
different pairs of an n-element array. More formally, the number of times its basic operation, the
comparison A[i] < A[j ], is executed is equal to the sum encountered several times already:

Thus, the algorithm makes the same number of key comparisons as selection sort and in addition
uses a linear amount of extra space. On the positive side, the algorithm makes the minimum
number of key moves possible, placing each of them directly in their final position in a sorted
array.

Review Questions

1. What is dynamic programming?


2. What is comparison counting sort.

SOWMYA K, DEPARTMENT OF CSE,CEC 17


LECTURE 30:

INPUT ENHANCEMENT IN STRING MATCHING


The technique of input enhancement can be applied to the problem of string matching. The
problem of string matching requires finding an occurrence of a given string of m characters
called the pattern in a longer string of n characters called the text. Since the maximum number
of such trials is n − m + 1 and, in the worst case, m comparisons need to be made on each of
them, the worst-case efficiency of the brute-force algorithm is in the O(nm) class. The average-
case efficiency is O(n + m).

The input-enhancement idea: preprocess the pattern to get some information about it, store this
information in a table, and then use this information during an actual search for the pattern in a
given text. This is exactly the idea behind the two best- known algorithms of this type: the Knuth-
Morris-Pratt algorithm and the Boyer-Moore algorithm

The principal difference between these two algorithms lies in the way they compare characters
of a pattern with their counterparts in a text: the Knuth- Morris-Pratt algorithm does it left to
right, whereas the Boyer-Moore algorithm does it right to left.

HORSPOOL’S ALGORITHM - BASICS


Consider, as an example, searching for the pattern BARBER in some text:

s0 . . . c . . . sn−1

BARBER

Starting with the last R of the pattern and moving right to left, compare the corresponding pairs
of characters in the pattern and the text. If all the pattern’s characters match successfully, a
matching substring is found. Then the search can be either stopped altogether or continued if
another occurrence of the same pattern is desired. If a mismatch occurs, shift the pattern to the
right. Horspool’s algorithm determines the size of such a shift by looking at the character c of
the text that is aligned against the last character of the pattern. This is the case even if character
c itself matches its counterpart in the pattern.

In general, the following four possibilities can occur.

SOWMYA K, DEPARTMENT OF CSE,CEC 18


Case 1 If there are no c’s in the pattern—e.g., c is letter S in the example—safely shift the pattern
by its entire length (if shifting is less, some character of the pattern would be aligned against the
text’s character c that is known not to be in the pattern):

Case 2 If there are occurrences of character c in the pattern but it is not the last one there—e.g.,
c is letter B in the example—the shift should align the rightmost occurrence of c in the pattern
with the c in the text:

Case 3 If c happens to be the last character in the pattern but there are no c’s among its other
m−1 characters—e.g., c is letter R in the example—the situation is similar to that of Case 1 and
the pattern should be shifted by the entire pattern’s length m:

Case 4 Finally, if c happens to be the last character in the pattern and there are other c’s among
its first m−1 characters—e.g., c is letter R in the example - the situation is similar to that of Case
2 and the rightmost occurrence of c among the first m − 1 characters in the pattern should be
aligned with the text’s c

These examples demonstrate that right-to-left character comparisons can lead to farther shifts of
the pattern than the shifts by only one position always made by the brute-force algorithm.
However, if such an algorithm had to check all the characters of the pattern on every trial, it
would lose much of this superiority. Fortunately, the idea of input enhancement makes repetitive
comparisons unnecessary. Shift sizes can be precomputed and stored in a table.

SOWMYA K, DEPARTMENT OF CSE,CEC 19


The table will be indexed by all possible characters that can be encountered in a text, including,
for natural language texts, the space, punctuation symbols, and other special characters.The
table’s entries will indicate the shift sizes computed by the formula

For example, for the pattern BARBER, all the table’s entries will be equal to 6, except for the
entries for E, B, R, and A, which will be 1, 2, 3, and 4, respectively.

Review Questions

1. What is string matching?


2. Which are the familiar string matching algorithm?

SOWMYA K, DEPARTMENT OF CSE,CEC 20


LECTURE 31:

HORSPOOL’S ALGORITHM AND ANALYSIS


Here is a simple algorithm for computing the shift table entries. Initialize all the entries to the
pattern’s length m and scan the pattern left to right repeating the following step m − 1 times: for
the j th character of the pattern (0 ≤ j ≤ m − 2), overwrite its entry in the table with m − 1 − j ,
which is the character’s distance to the last character of the pattern. Note that since the algorithm
scans the pattern from left to right, the last overwrite will happen for the character’s rightmost
occurrence—exactly as we would like it to be.

Horspool’s algorithm:

Step 1: For a given pattern of length m and the alphabet used in both the pattern and text,
construct the shift table

Step 2: Align the pattern against the beginning of the text

Step 3: repeat the following until either a matching substring is found or the pattern reaches
beyond the last character of the text. Starting with the last character in the pattern, compare the
corresponding characters in the pattern and text until either all m characters are matched(then
stop) or a mismatching pair is encountered. Retrieve entry t(c) from the c’s column of the shift
table where c is the text’s character currently aligned against the last character of the pattern, and
shift the pattern by t(c) characters to the right along the text.

SOWMYA K, DEPARTMENT OF CSE,CEC 21


Consider searching for the pattern BARBER in a text that comprises English letters and spaces
(denoted by underscores). The shift table, is filled as follows:

The actual search in a particular text proceeds as follows:

Worst-case efficiency of Horspool’s algorithm is in O(nm). But for random texts, it is O(n),
Horspool’s algorithm is obviously faster on average than the brute-force algorithm.

Review Questions

1. What is the case when there is no character c in the given Pattern?


2. What is the case when character c is the last occurrence in the pattern and there are no
other c in the pattern?
3. What is the case when character c is the last occurrence in the pattern and there are other
c in the pattern.?
4. What is the time efficiency of Horspool’s algorithm?

SOWMYA K, DEPARTMENT OF CSE,CEC 22


QUESTION BANK- MODULE III

1. Explain transform and conquer in detail, with an example.


2. What is an AVL Tree. Explain its four types of rotation to construct AVL tree. Insert 1,25,28 and 12
in the following tree.

3. Explain AVL Tree and 2-3 trees.


4. Construct an AVL tree and 2-3 tree for the input sequence 1,2,3,4,5,6 with neat tree diagram.
5. Explain bottom up heap construction algorithm with an example. Give the worst case efficiency of
this algorithm.
6. Construct bottom up heap for the list 2,9,7,6,5,8. Obtain its time complexity
7. Explain transform and conquer technique. Sort the given list using Heap sort 3, 2,4,1, 6, 5
8. Define heap. Explain the properties of heap along with its representation.
9. Explain sorting by counting. Write algorithm comparison counting sort. Sort the list
62,31,84,96,19,47
10. Design Horspools algorithm for string matching. Apply Horspools algorithm to find the pattern
BARBER in the text: JIM_SAW_ME_IN_A_BARBERSHOP
11. Explain all the cases of Horspool algorithm and give its efficiency
12. Write the Horspools algorihm to count the number of occurences of a pattern in the given
string. Find the shift table for the pattern ‘ BARBER’

SOWMYA K, DEPARTMENT OF CSE,CEC 23

You might also like