Skip to content
Home ยป Mathematical analysis of Recursive and Non-recursive algorithms

Mathematical analysis of Recursive and Non-recursive algorithms


๐Ÿ“˜ Mathematical Analysis of Recursive and Non-Recursive Algorithms

1๏ธโƒฃ Introduction

Mathematical analysis of algorithms involves expressing the time and space requirements of an algorithm using mathematical functions based on the input size (n).

Algorithms are broadly classified as:

  • Non-recursive (Iterative) algorithms
  • Recursive algorithms

Each requires a different approach for mathematical analysis.


2๏ธโƒฃ Mathematical Analysis of Non-Recursive Algorithms

๐Ÿ”น Definition

A non-recursive algorithm uses loops (for, while) instead of recursive function calls.

The time complexity is analyzed by:

  • Counting number of loop iterations
  • Identifying the basic operation
  • Expressing the count as a function of n

๐Ÿ”น Steps for Analysis

  1. Identify the basic operation
  2. Count how many times it executes
  3. Write a mathematical expression
  4. Simplify using asymptotic notation

๐Ÿ”น Example 1: Single Loop

for i = 1 to n
   print i

๐Ÿ”ธ Mathematical Analysis

  • Loop runs n times
  • Basic operation executes n times

[
T(n) = n
]

๐Ÿ‘‰ Time Complexity = O(n)


๐Ÿ”น Example 2: Nested Loops

for i = 1 to n
   for j = 1 to n
      print i, j

๐Ÿ”ธ Mathematical Analysis

  • Inner loop runs n times
  • Outer loop runs n times

[
T(n) = n \times n = n^2
]

๐Ÿ‘‰ Time Complexity = O(nยฒ)


๐Ÿ”น Example 3: Loop with Variable Range

for i = 1 to n
   for j = 1 to i
      print j

๐Ÿ”ธ Mathematical Analysis

[
T(n) = 1 + 2 + 3 + \dots + n = \frac{n(n+1)}{2}
]

๐Ÿ‘‰ Time Complexity = O(nยฒ)


3๏ธโƒฃ Mathematical Analysis of Recursive Algorithms

๐Ÿ”น Definition

A recursive algorithm solves a problem by calling itself with smaller input sizes.

Its analysis is done using recurrence relations.


4๏ธโƒฃ Recurrence Relation

๐Ÿ”น Definition

A recurrence relation expresses the time complexity of a recursive algorithm in terms of its complexity for smaller inputs.

๐Ÿ“Œ General Form:
[
T(n) = aT(n/b) + f(n)
]

Where:

  • a = number of recursive calls
  • n/b = size of subproblem
  • f(n) = work done outside recursion

5๏ธโƒฃ Example 1: Linear Recursion

Factorial Algorithm

factorial(n)
   if n == 0 return 1
   return n * factorial(n-1)

๐Ÿ”ธ Recurrence Relation

[
T(n) = T(n-1) + c
]

๐Ÿ”ธ Solution

[
T(n) = T(0) + nc = O(n)
]


6๏ธโƒฃ Example 2: Binary Recursion

Fibonacci Algorithm (Naive)

fib(n)
   if n โ‰ค 1 return n
   return fib(n-1) + fib(n-2)

๐Ÿ”ธ Recurrence Relation

[
T(n) = T(n-1) + T(n-2) + c
]

๐Ÿ”ธ Solution

[
T(n) = O(2^n)
]


7๏ธโƒฃ Example 3: Divide and Conquer

Binary Search

[
T(n) = T(n/2) + c
]

๐Ÿ”ธ Solution

[
T(n) = O(\log n)
]


Merge Sort

[
T(n) = 2T(n/2) + cn
]

๐Ÿ”ธ Solution (Master Theorem)

[
T(n) = O(n \log n)
]


8๏ธโƒฃ Comparison: Recursive vs Non-Recursive Analysis

AspectNon-RecursiveRecursive
MethodLoop countingRecurrence relations
Complexity ExpressionDirect formulaRecursive formula
Space UsageLessMore (stack)
Analysis DifficultyEasierMore complex

9๏ธโƒฃ Space Complexity Consideration

๐Ÿ”น Non-Recursive

  • Usually O(1) auxiliary space

๐Ÿ”น Recursive

  • Stack space depends on recursion depth
  • Example: Factorial โ†’ O(n) space

๐Ÿ”Ÿ Exam-Oriented Key Points

  • Loop-based algorithms โ†’ count iterations
  • Recursive algorithms โ†’ write recurrence relation
  • Solve recurrence using substitution or Master Theorem
  • Worst case is generally preferred

๐Ÿ”š Conclusion

Mathematical analysis provides a systematic method to evaluate both recursive and non-recursive algorithms.

Iterative algorithms are easier to analyze, while recursive algorithms require recurrence relations for accurate complexity estimation.