Module 3 Ada New

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

PES Institute of Technology and Management

NH-206, Sagar Road,Shivamogga-577204

Department of Computer Science and Engineering


Affiliated to

VISVESVARAYA TECHNOLOGICAL UNIVERSITY


Jnana Sangama, Belagavi, Karnataka –590018c

Lecture Notes
on

Module 3-Analysis and Design of Algorithms


(BCS401)
2022 Scheme

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.

Heap Sort Algorithm

Step 1: Build a heap from the given input array.


Step 2: Repeat the following steps until the heap contains only one element:
o Swap the root element of the heap (which is the largest element) with the
last element of the heap.
o Remove the last element of the heap (which is now in the correct
position).
o Heapify the remaining elements of the heap.
Step 3: The sorted array is obtained by reversing the order of the elements in the
input array.

Example: Sort the list {4,10,3,5,1} by Heap sort

PESITM, Dept of CSE Prepared By, Prathibha S Page 2


Module 1-ADA (BCS401)

PESITM, Dept of CSE Prepared By, Prathibha S Page 3


Module 1-ADA (BCS401)

PESITM, Dept of CSE Prepared By, Prathibha S Page 4


Module 1-ADA (BCS401)
Time-Space Trade-Off in Algorithms
Space-Time tradeoff in computer science is basically a problem solving technique in
which we solve the problem:

 Either in less time and using more space, or


 In very little space by spending more time.

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.

Comparison Counting Sorting


 For each element of the list, count the total number of elements smaller than
the element.
 These numbers indicates the positions (0-based) of the elements in the sorted
list.

Example: 62 31 84 96 19 47

PESITM, Dept of CSE Prepared By, Prathibha S Page 5


Module 1-ADA (BCS401)

Time Complexity of Comparison Counting Sort

Belongs to O(n2)

Distribution Counting (Counting Sort Algorithm)

Counting Sort is a non-comparison-based sorting algorithm that works well when


there is limited range of input values. It is particularly efficient when the range of input
values is small compared to the number of elements to be sorted. The basic idea
behind Counting Sort is to count the frequency of each distinct element in the input
array and use that information to place the elements in their correct sorted positions.

Example: Apply Distribution count on 2,5,3,0,2,3,0,3

Step1 :
 Find out the maximum element from the given array

PESITM, Dept of CSE Prepared By, Prathibha S Page 6


Module 1-ADA (BCS401)

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

PESITM, Dept of CSE Prepared By, Prathibha S Page 7


Module 1-ADA (BCS401)

PESITM, Dept of CSE Prepared By, Prathibha S Page 8


Module 1-ADA (BCS401)

PESITM, Dept of CSE Prepared By, Prathibha S Page 9


Module 1-ADA (BCS401)

Example: Sort by Distribution Counitng 13 11 12 13 12 12

Input Array

[0] [1] [2] [3] [4] [5]


13 11 12 13 12 12
Frequency or Count array (Take lower =11, m upper =13 in given set)

Freequency of 11 -1, 12-3 , 13-2

1 3 2
Distribution Vector

1 3 2

1 4 6
Sorting

13 11 12 13 12 12

1 4 6

1-1=[0] 4-1=[1] 4-1=[2] 4-1=[3] 6-5=[4] 6-5=[5]

[0] [1] [2] [3] [4] [5]


11 12 12 12 13 13

PESITM, Dept of CSE Prepared By, Prathibha S Page 10


Module 1-ADA (BCS401)

Horspool’s algorithm

Input Enhancement in String Matching –String matching involves finding an


occurrence of a given string of m characters called the pattern in a longer string of n
characters called the text.

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.

EXAMPLE : Apply Horspool’s algorithm, consider searching for the pattern


string BARBER in a text JIM_SAW_ME_IN_A_BARBERSHOP

Prepare shift table

Character B A R E
No of shifts 2 4 3 1

PESITM, Dept of CSE Prepared By, Prathibha S Page 11

You might also like