Module 3 Ada New
Module 3 Ada New
Module 3 Ada New
Lecture Notes
on
Prepared By,
Mrs. Prathibha S ,
Assistant Professor,
Department of CSE,PESITM
Module 1-ADA (BCS401)
MODULE -3
Transform and Conquer
Transform and conquer technique is a way of solving problems by breaking them down
into smaller subproblems, solving the smaller subproblems, and then combining the
solutions to the subproblems to solve the original problem.
Transform and Conquer is similar to Divide and Conquer. The difference is that
transform and conquer completely changes the form of the input.
This technique is often used when the original problem is too difficult to solve directly,
or when it is easier to solve the smaller sub-problems.
Heap Sort
A heap is a special type of tree data structure; a heap tree is typically either a min-heap
tree, in which each parent node is smaller than its children; or a max-heap tree, in which
each parent node is larger than its children.
The best algorithm is the one which helps to solve a problem that requires less space in
memory as well as takes less time to generate the output. But in general, it is not always
possible to achieve both of these conditions at the same time.
If our problem is taking a long time but not much memory, a space-time trade-off would
let us use more memory and solve the problem more quickly. Or, if it could be solved
very quickly but requires more memory than we have, we can try to spend more time
solving the problem in the limited memory.
Input Enhancement
Preprocess the problem’s input, in whole or in part, and store the additional
information obtained to accelerate solving the problem afterward.
Eg: Counting methods of sorting, Horspool’s algorithm, Boyer-Moore’s
algorithm.
Example: 62 31 84 96 19 47
Belongs to O(n2)
Step1 :
Find out the maximum element from the given array
.
Step 2:
Initialize a countArray[] of length max+1 with all elements as 0. This array will be
used for storing the occurrences of the elements of the input array.
Take lower =0, upper=5
Step 3:
In the countArray[], store the count of each unique element of the input array at
their respective indices.
Step 4:
Store the cumulative sum or prefix sum of the elements of the countArray[] by
doing countArray[i] = countArray[i – 1] + countArray[i]. This will help in placing
the elements of the input array at the correct index in the output array.
Input Array
1 3 2
Distribution Vector
1 3 2
1 4 6
Sorting
13 11 12 13 12 12
1 4 6
Horspool’s algorithm
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 as described above.
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. In the latter
case, retrieve the 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.
Character B A R E
No of shifts 2 4 3 1