Knowt
Knowt
2. Asymptotic Notations
Definition: A method to solve recurrence relations by guessing the form of the solution
and proving it via substitution and induction.
Concept:
A recurrence relation defines a function in terms of its value on smaller inputs
(e.g., T(n) = 2T(n/2) + n).
Steps:
1. Guess the solution: Hypothesize a form, e.g., T(n) = O(n log n).
2. Substitute: Plug the guessed solution into the recurrence to verify.
3. Prove by induction: Show the solution holds for all cases.
Example: For T(n) = 2T(n/2) + n (merge sort), the solution is T(n) = O(n log n).
Useful for divide-and-conquer algorithms.
5. Greedy Approach
Definition: An algorithmic paradigm that makes the locally optimal choice at each step to
find a global optimum.
Concept:
Builds a solution incrementally by selecting the best option at each step without
reconsidering previous choices.
Works for optimization problems where local optimality leads to global
optimality.
Examples:
Kruskal’s Algorithm (minimum spanning tree): Picks the smallest edge
that doesn’t form a cycle.
Huffman Coding: Builds a tree by repeatedly choosing the lowest-
frequency nodes.
Not always optimal (e.g., doesn’t work for the 0/1 knapsack problem).
6. Dynamic Programming
Definition: A method for solving complex problems by breaking them into overlapping
subproblems, solving each subproblem once, and storing results for reuse.
Concept:
Key features:
Optimal Substructure: The optimal solution to the problem contains
optimal solutions to subproblems.
Overlapping Subproblems: Subproblems are solved multiple times
in a recursive approach.
Uses a table (memoization or tabulation) to store intermediate results.
Examples:
Fibonacci: Store previously computed values to avoid redundant
calculations.
0/1 Knapsack: Build a table to compute the maximum value.
More computationally efficient than naive recursion for problems with
overlapping subproblems.
7. Backtracking
Definition: A systematic method for solving problems by exploring all possible solutions
incrementally and abandoning partial solutions (backtracking) when they cannot lead to a
valid solution.
Concept:
Builds a solution incrementally, exploring a search tree of possibilities.
If a partial solution violates constraints, it prunes the branch and backtracks to
try another option.
Examples:
N-Queens Problem: Place queens on a chessboard, backtrack if a
placement leads to conflicts.
Sudoku Solver: Fill cells and backtrack if a number violates rules.
Often used for combinatorial problems but can be computationally expensive.
Summary
These concepts form the foundation of algorithm design and analysis: