0% found this document useful (0 votes)
2 views16 pages

DAA L4-L5

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 16

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)

12/09/2024 DISCOVER . LEARN . EMPOWER


1
Content

Analysis of iterative algorithms.

Analysis of recursive algorithms.

2
Iterative Algorithm Analysis

 An iterative method is a method that uses a loop to repeat an action.


 Anything that can be done iteratively can be done recursively, and vice versa.
 Iterative algorithms and methods are generally more efficient than recursive
algorithms.
 It is faster than recursion.
 It uses less memory as compared to recursion.
 It is used when we have to balance the time complexity against a large code
size.
 An infinite loop occurs with iteration if the loop condition test never becomes
false and Infinite looping uses CPU cycles repeatedly.
 An iteration terminates when the loop condition fails.

3
Recursive Algorithm Analysis
 A recursive method is a method that calls itself.
 To build a recursive algorithm, you will break the given problem statement into
two parts.
 The first one is the base case, and the second one is the recursive step.
 The code size in recursion is smaller than the code size in iteration.

 Base Case: It is nothing more than the simplest instance of a problem,


consisting of a condition that terminates the recursive function. This base case
evaluates the result when a given condition is met.

 Recursive Step: It computes the result by making recursive calls to the same
function, but with the inputs decreased in size or complexity.

 Recursion is generally used where there is no issue of time complexity, and


code size requires being small.

4
:

Example

For example, consider this problem statement:


Print the sum of n natural numbers using recursion.

This statement clarifies that we need to formulate a function that will calculate the
summation of all natural numbers in the range 1 to n.

Hence, mathematically you can represent the function as:

F(n) = 1 + 2 + 3 + 4 + …………..+ (n-2) + (n-1) + n

It can further be simplified as:

5
Problem Breakdown
You can breakdown this function into two parts as follows:

6
Factorial

7
Fibonacci Series

8
Binary search

9
Insertion Sort

10
Recursive approach

In the case of recursion, we can calculate the time complexity by the use of a
recursive tree which is generated by recursive calls. The recurrence equation of
recursive tree is given as T(n) = T(n-1) + T(n-2) + c

On solving the above recurrence equation, we get the time complexity is O(2^n).

11
TASKS END OF LECTURE LEARNING (TELL):

12
TASKS END OF LECTURE LEARNING (TELL):

13
TASKS END OF LECTURE LEARNING (TELL):

14
References
• Fundamentals of Computer Algorithms 2nd Edition (2008) by Horowitz, Sahni
and Rajasekaran
• Introduction to Algorithms 3rd Edition (2012) by Thomas H Cormen, Charles E
Lieserson, Ronald

15
THANK YOU

For queries
Email: [email protected]

12/09/2024 16

You might also like