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

CS3401-UNIT1

The document covers various topics related to algorithm analysis, including definitions of algorithms, pseudocode, time and space complexity, and types of searching and sorting algorithms. It discusses key concepts such as asymptotic notations, recurrence relations, and the complexities of linear search, binary search, insertion sort, and heap sort. Additionally, it outlines specific algorithms like Rabin-Karp, Knuth-Morris-Pratt, and naive string matching, along with their complexities.
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)
1 views

CS3401-UNIT1

The document covers various topics related to algorithm analysis, including definitions of algorithms, pseudocode, time and space complexity, and types of searching and sorting algorithms. It discusses key concepts such as asymptotic notations, recurrence relations, and the complexities of linear search, binary search, insertion sort, and heap sort. Additionally, it outlines specific algorithms like Rabin-Karp, Knuth-Morris-Pratt, and naive string matching, along with their complexities.
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/ 10

SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

UNIT 1

INTRODUCTION

Algorithm analysis: Time and space complexity - Asymptotic Notations and its
properties Best case, Worst case and average case analysis – Recurrence
relation: substitution method - Lower bounds – searching: linear search,
binary search and Interpolation Search, Pattern search: The naïve string-
matching algorithm - Rabin-Karp algorithm - Knuth-Morris-Pratt algorithm.
Sorting: Insertion sort – heap sort
Part-A

1. Define Algorithm (AU-N/D23)


An algorithm is a sequence of unambiguous instructions for solving
a problem i.e., for obtaining a required output for any legitimate
input in a finite amount of time

2. What is Pseudocode?

A pseudo code is a mixture of a natural language and


programming language constructs to specify an
algorithm. A pseudo code is more precise than a natural
language and its usage often yields more concise
algorithm descriptions.

3. What are the characteristics of algorithm?


 Finiteness: An algorithm must be completed after a finite
number of instructions that have been executed.
 Definiteness: Each set of algorithm should be clear and
unambiguous
 Input: Any number of inputs can be given(0 or 1)
 Output: Number of outputs produced
 Effectiveness: The effective ness and operations of an

PREPARED BY D.SUJATHA, HoD/AI&DS 1


SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

algorithm must be simple and can be performed manually in a


finite amount of time.

4. Define Recurrence Relation (AU-N/D23)

A recurrence relation is a mathematical expression that defines


a sequence in terms of its previous terms. In the context of
algorithmic analysis, it is often used to model the time complexity
of recursive algorithms.
General form of a
Recurrence Relation:
an=f(an−1,an−2,....,an−k)
where f is a function that defines the relationship between the
current term and the previous terms

5. What are the types of recurrence relation?


 Homogenous recurrence relation
 Non homogenous recurrence relation
6. Discuss the time and space complexity for insertion sort(AU-
N/D23)
The insertion sort algorithm encompasses a time complexity of O
(n2) and a space complexity of O (1) because it necessitates some
extra memory space for a key variable to perform swaps.

7. Define time complexity and space complexity of an


algorithm.(AU-A/M23)

Time Complexity:
Time Complexity is defined as the number of times a particular
instruction set is executed rather than the total time taken. It is
PREPARED BY D.SUJATHA, HoD/AI&DS 2
SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

because the total time taken also depends on some external


factors like the compiler used, the processor’s speed, etc.
Space Complexity:
Space Complexity is the total memory space required by the
program for its execution.
Both are calculated as the function of input size(n). One important
thing here is that despite these parameters, the efficiency of an
algorithm also depends upon the nature and size of the input.

8. List the types of asymptotic notation in analysing complexity


of algorithm(AU-A/M23)
1. Big-O Notation (O-notation)
2. Omega Notation (Ω-notation)
3. Theta Notation (Θ-notation)

9. What is pattern searching?(AU-N/D22)


Pattern searching algorithms are essential tools in computer
science and data processing. These algorithms are designed to
efficiently find a particular pattern within a larger set of data.

10. State how the running time of an algorithm is


measured.(AU-A/M24)
The running time of an algorithm is typically measured in terms of
its time complexity, which quantifies the amount of time an
algorithm takes to complete as a function of the size of its input.
This is often expressed using Big O notation.

PREPARED BY D.SUJATHA, HoD/AI&DS 3


SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

11. Outline the significance of performing worst case analysis


of an algorithm.(AU-A/M24)
Performing a worst-case analysis of an algorithm is crucial for
several reasons:
 Predictable Performance: It helps in understanding the
maximum time or space an algorithm can take, which is essential
for scenarios requiring guaranteed performance.
 Resource Allocation: It aids in determining the necessary
resources, such as memory and processing power, ensuring the
system can handle the worst-case scenario.
 Comparison: It allows for fair comparison between different
algorithms by evaluating their efficiency in the most unfavorable
conditions.
 Reliability: It ensures the reliability and robustness of an
algorithm, highlighting potential inefficiencies or areas for
improvement.
 Worst-Case Scenarios: Real-world applications often face
unpredictable data; worst-case analysis prepares the algorithm
to handle such situations efficiently.
12. Compare between worst case, best case and average
efficiencies of an algorithm.(AU-A/M22)
Scenario Definition Purpose Example
Worst Case Maximum Ensures Element at the
time/resources performance end or not
needed under present
challenging
situations

PREPARED BY D.SUJATHA, HoD/AI&DS 4


SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

Best Case Minimum Shows optimal Element is the


time/resources efficiency first in the list
needed
Average Case Expected Provides a Element
time/resources realistic position is
on average performance randomly
expectation distributed

13. Define Asymptotic notation.

The efficiency of an algorithm depends on the amount


of time, storage and other resources required to
execute the algorithm. The efficiency is measured with
the help of asymptotic notations. Asymptotic notation
is shorthand way to represent the time complexity
The various notations are Big “Oh”, Big Omega, and Theta.

14. What is binary search?


Binary Search Algorithm is a searching algorithm used in a sorted
array by repeatedly dividing the search interval in half. The idea of
binary search is to use the information that the array is sorted and
reduce the time complexity to O(log N).
15. What is linear search?
Linear search is defined as the searching algorithm where the list
or data set is traversed from one end to find the desired value.
16. List the method for solving recurrence equation.
 Substitution Method
 Recurrence Tree Method
 Master Method

PREPARED BY D.SUJATHA, HoD/AI&DS 5


SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

17. What are the best case, average case & worst case
complexity of linear search?
 Best Case: In the best case, the key might be present at the
first index. So the best case complexity is O(1)
 Worst Case: In the worst case, the key might be present at the
last index i.e., opposite to the end from which the search has
started in the list. So the worst-case complexity is O(N) where
N is the size of the list.
 Average Case: O(N)

18. What are the best case, average case & worst case
complexity of binary search?
 Best Case: O(1) This occurs when the middle element is the
target element.
 Average Case: O(log n). This is the expected complexity when
the target element is not necessarily at the middle.
 Worst Case: O(log n). This happens when the search has to go
all the way down the tree, but it's still logarithmic due to the
halving nature of binary search.

19. What do you mean by interpolation search?

Interpolation search finds a particular item by


computing the probe position. Initially, the probe
position is the position of the middle most item of the
collection.

PREPARED BY D.SUJATHA, HoD/AI&DS 6


SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

If a match occurs, then the index of the item is


returned. To split the list into two parts, we use the
following method −

mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo])


where −
A= list
Lo = Lowest index of the list
Hi = Highest index of the list

20. Define Rabin karp algorith


The Rabin-Karp string matching algorithm calculates a hash value
for the pattern, as well as for each M-character subsequences of text
to be compared. If the hash values are unequal, the algorithm will
determine the hash value for next M-character sequence. If the hash
values are equal, the algorithm will analyze the pattern and the M-
character sequence. In this way, there is only one comparison per
text subsequence, and character matching is only required when
the hash values match.

RABIN-KARP-MATCHER (T, P, d, q)

1. n ← length [T]

2. m ← length [P]

3. h ← dm-1 mod q

PREPARED BY D.SUJATHA, HoD/AI&DS 7


SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

4. p← 0

5. t0 ← 0

6. for i ← 1 to m

7. do p ← (dp + P[i]) mod q

8. t0 ← (dt0+T [i]) mod q

9. for s ← 0 to n-m

10. do if p = ts

11. ............................. then if P [1.....m] = T [s+1 s + m]

12. then "Pattern occurs with shift" s

13. If s < n-m

14. then ts+1 ← (d (ts-T [s+1]h)+T [s+m+1])mod q

21. What is meant by naïve string matching algorithm?

The naïve approach tests all the possible placement of


Pattern P [1…m] relative to text
T [1......n]. We try shift s = 0, 1.......n-m,
successively and for each shift s. Compare T
[s+1.......s+m] to P [1…m].

The naïve algorithm finds all valid


shifts using a loop that checks the
condition P [1.......m] = T [s+1 s+m]
for each of the n - m +1 possible value
of s.

NAIVE-STRING-MATCHER (T, P)

PREPARED BY D.SUJATHA, HoD/AI&DS 8


SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

1. n ← length [T]

2. m ← length [P]

3. for s ← 0 to n -m

4. do if P [1.....m] = T [s + 1....s + m]

5. then print "Pattern occurs with shift" s

22. Define Knuth-Morris-Pratt algorithm.


Knuth-Morris and Pratt introduce a linear time algorithm for the
string matching problem. A matching time of O (n) is achieved by
avoiding comparison with an element of 'S' that have previously
been involved in comparison with some element of the pattern 'p'
to be matched. i.e., backtracking on the string 'S' never occurs

23. What is insertion sort?


Insertion sort is a simple sorting algorithm that works by iteratively
inserting each element of an unsorted list into its correct position
in a sorted portion of the list. It is like sorting playing cards in your
hands.
24. What is heap sort?
Heap sort is a comparison-based sorting algorithm that divides the
input into a sorted and an unsorted region and iteratively shrinks
the unsorted region by extracting the largest (or smallest) element
from it and inserting it into the sorted region. It involves two main
steps:
 Build a Heap: Convert the input array into a max heap or
min heap. In a max heap, each parent node is greater than

PREPARED BY D.SUJATHA, HoD/AI&DS 9


SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM

or equal to its child nodes. In a min heap, each parent node


is less than or equal to its child nodes.
 Heapify: Repeatedly extract the maximum element from
the heap (for a max heap) or the minimum element (for a
min heap) and move it to the end of the array, reducing the
heap size each time.

25. What is the complexity analysis of heap sort?


 Worst case time complexity: O(n*logn)
 Best case time complexity: O(n*logn)
 Average case complexity: O(n*logn)
 Space complexity: O(1)

PREPARED BY D.SUJATHA, HoD/AI&DS 10

You might also like