๐ Algorithms: Analyzing Algorithms
1๏ธโฃ Meaning of Algorithm Analysis
Analyzing an algorithm means evaluating its efficiency in terms of:
- Time (how fast it runs)
- Space (how much memory it uses)
The main goal is to compare algorithms and predict performance before actual implementation.
Algorithm analysis helps us choose the best algorithm for a given problem.
2๏ธโฃ Why Do We Analyze Algorithms?
Algorithm analysis is required to:
- Measure efficiency
- Compare two or more algorithms
- Optimize execution time
- Optimize memory usage
- Predict behavior for large inputs
๐ Example:
Both Bubble Sort and Merge Sort sort data, but Merge Sort performs better for large inputs.
3๏ธโฃ Types of Algorithm Analysis
๐น 1. A Priori Analysis
- Done before coding
- Uses theoretical concepts
- Independent of hardware and programming language
- Focuses on algorithm logic
๐ Example:
- Counting number of comparisons
- Estimating loop iterations
๐น 2. A Posteriori Analysis
- Done after coding
- Based on actual execution
- Depends on:
- Machine
- Compiler
- Input data
๐ Example:
- Measuring running time using a stopwatch
- Memory usage during execution
4๏ธโฃ Performance Criteria for Algorithm Analysis
๐น 1. Time Complexity
Time complexity indicates how execution time grows with input size (n).
It depends on:
- Number of statements
- Loops
- Nested loops
- Recursive calls
๐ Example:
for i = 1 to n
print i
Time Complexity = O(n)
๐น 2. Space Complexity
Space complexity refers to total memory used by an algorithm.
It includes:
- Input space
- Auxiliary space (extra variables, arrays, recursion stack)
๐ Example:
- Iterative algorithm โ less space
- Recursive algorithm โ more space (stack memory)
5๏ธโฃ Case Analysis of Algorithms
๐น Best Case
- Minimum time required
- Most favorable input
๐ Example:
- Linear Search: element found at first position
- Time Complexity โ O(1)
๐น Average Case
- Expected time for random input
- Difficult to calculate
๐ Example:
- Linear Search โ O(n)
๐น Worst Case
- Maximum time required
- Most unfavorable input
๐ Example:
- Linear Search: element at last position
- Time Complexity โ O(n)
๐ Worst-case analysis is most commonly used in exams.
6๏ธโฃ Asymptotic Analysis
Asymptotic analysis studies algorithm performance for large input sizes.
It ignores:
- Constants
- Lower-order terms
๐น Asymptotic Notations
1. Big-O Notation (O)
- Represents upper bound
- Worst-case complexity
๐ Example:
T(n) = 3nยฒ + 5n + 10
O(nยฒ)
2. Omega Notation (ฮฉ)
- Represents lower bound
- Best-case complexity
๐ Example:
ฮฉ(n)
3. Theta Notation (ฮ)
- Represents tight bound
- Best and worst case are same
๐ Example:
ฮ(n log n)
7๏ธโฃ Steps to Analyze an Algorithm
- Identify input size (n)
- Count basic operations
- Calculate total operations
- Express complexity using asymptotic notation
- Analyze time and space
8๏ธโฃ Example: Algorithm Analysis
Linear Search Algorithm
for i = 1 to n
if a[i] == key
return i
| Case | Time Complexity |
|---|---|
| Best | O(1) |
| Average | O(n) |
| Worst | O(n) |
๐ Conclusion
Analyzing algorithms helps in:
- Understanding performance
- Selecting optimal solutions
- Designing efficient programs
A good algorithm is not only correct but also efficient in time and space.
