Below is a clear, complete, exam-oriented explanation of Algorithms, written exactly like a Data Structures & Algorithms expert.
π Introduction to Algorithms
An Algorithm is a finite, step-by-step, logical procedure used to solve a problem or perform a computation.
In simple words:
π Algorithm = A set of instructions to achieve a specific task efficiently.
Examples of algorithms in daily life:
- Steps to prepare tea
- Steps to search a contact in your phone
- Steps to arrange books in order
In computer science, algorithms operate on data structures.
β¨ Characteristics of a Good Algorithm
A good algorithm must have:
1. Input
Should take zero or more inputs.
2. Output
Should produce at least one output.
3. Definiteness
Each step must be clear and unambiguous.
4. Finiteness
Algorithm must terminate after a finite number of steps.
5. Effectiveness
All operations must be basic and feasible.
6. Efficiency
Should use minimum time and memory.
π Classification / Types of Algorithms
There are various ways to classify algorithms. Below is the most important classification for exams and interviews:
1. Brute Force Algorithms
- Check all possible solutions
- Simple but inefficient
Example: Linear search, checking all permutations
2. Divide and Conquer Algorithms
Split β Solve β Combine
Used for problems where the solution can be broken into subproblems.
Examples:
- Merge Sort
- Quick Sort
- Binary Search
3. Greedy Algorithms
Make the locally optimal choice at every step hoping to reach a global optimum.
Examples:
- Kruskalβs algorithm (MST)
- Primβs algorithm (MST)
- Dijkstraβs shortest path
- Fractional knapsack
4. Dynamic Programming Algorithms (DP)
Solve complex problems by breaking them into overlapping subproblems and using memoization or tabulation.
Examples:
- Fibonacci (DP version)
- 0/1 Knapsack
- Longest Common Subsequence (LCS)
5. Backtracking Algorithms
Try all solutions but backtrack when a solution is not valid.
Examples:
- N-Queens problem
- Sudoku solving
- Graph coloring
6. Branch and Bound Algorithms
Used to find optimal solutions by pruning branches that cannot produce a better solution.
Examples:
- Traveling Salesman Problem (TSP)
- 0/1 Knapsack (optimal)
7. Randomized Algorithms
Use randomness to make decisions during execution.
Examples:
- Randomized Quick Sort
- Monte Carlo algorithms
8. Recursive Algorithms
Functions that call themselves until the base condition is met.
Examples:
- Factorial
- Tower of Hanoi
- Tree traversal
9. Iterative Algorithms
Use loops instead of recursion.
Examples:
- Iterative Fibonacci
- Iterative factorial
π Time and Space Complexity
Algorithms are analyzed based on:
1. Time Complexity
How much time an algorithm takes.
Common notations:
- O(1) β constant time
- O(log n) β logarithmic
- O(n) β linear
- O(n log n) β log linear
- O(nΒ²) β quadratic
- O(2βΏ) β exponential
- O(n!) β factorial
2. Space Complexity
How much memory an algorithm uses.
Includes:
- Input space
- Auxiliary space (extra memory)
π Steps to Design an Algorithm
- Understand the problem
- Identify input/output
- Write logical steps
- Choose best data structures
- Analyze time & space
- Optimize if required
- Correctness proof & testing
π Example of an Algorithm
Problem: Find the largest element in an array
Algorithm:
- Start
- Assume the first element is max
- Repeat for each element
- If current element > max
- Update max
- Print max
- Stop
Time Complexity: O(n)
Space Complexity: O(1)
π Difference Between Data Structures and Algorithms
| Data Structure | Algorithm |
|---|---|
| How data is stored | How data is processed |
| Structure-oriented | Procedure-oriented |
| Example: Array, Stack, Tree | Example: sorting, searching |
| Focus on memory | Focus on computation |
They are complementary:
π Algorithm uses data structure to work efficiently.
π― Summary (for exams)
An Algorithm is a finite sequence of unambiguous instructions designed to solve a problem effectively.
Types include:
- Brute Force
- Divide & Conquer
- Greedy
- Dynamic Programming
- Backtracking
- Branch & Bound
- Randomized
- Recursive & Iterative
Algorithms are analyzed using time and space complexity.
