Design and Analysis of Algorithms · Unit 1 · 4 hrs
Foundation of Algorithm Analysis
Exam-focused notes for Foundation of Algorithm Analysis (Design and Analysis of Algorithms, CSC325): what the TU syllabus asks and how it has actually been tested, with 13 solved past questions from this unit.
Foundation of Algorithm Analysis in short
This unit sets up the vocabulary and machinery the rest of the course depends on: what an algorithm is, the RAM model used to count its steps, the time and space measures built on that count, aggregate analysis for a sequence of operations, the asymptotic notations that describe growth, and the recurrence relations that recursive algorithms produce together with the three standard techniques for solving them.
What this unit covers
- Algorithm and its properties
- RAM model
- Time and Space Complexity
- detailed analysis of algorithms (Like factorial algorithm)
- Concept of Aggregate Analysis
- Asymptotic Notations: Big-O, Big-Ω and Big-Ө Notations their Geometrical Interpretation and Examples
- Recurrences: Recursive Algorithms and Recurrence Relations
- Solving Recurrences (Recursion Tree Method, Substitution Method, Application of Masters Theorem)
Solving Recurrences
2082 · 5 marks
Solve the recurrence relation T(n) = 2T(n/2) + n using recursion tree method. [5]
Show solved answerHide answer
Solving $T(n) = 2T(n/2) + n$ Using the Recursion Tree Method
Given
- Recurrence: $T(n) = 2T\left(\dfrac{n}{2}\right) + n$
- Branching factor: $a = 2$ (each node makes 2 recursive calls)
- Subproblem shrink factor: $b = 2$ (size divided by 2)
- Cost at a node of size $m$: $m$ (the "$+n$" term)
- Base case: $T(1) = \Theta(1)$
Solution
Building the recursion tree
Level 0: n cost = n
/ \
Level 1: n/2 n/2 cost = n/2 + n/2 = n
/ \ / \
Level 2: n/4 n/4 n/4 n/4 cost = 4·(n/4) = n
... ...
Level k: 2^k nodes each of size n/2^k cost = 2^k·(n/2^k) = n
...
Level log n: n leaves each of size 1 cost = n
Cost per level
| Level $i$ | Number of nodes | Size per node | Cost per level |
|---|---|---|---|
| 0 | $1$ | $n$ | $n$ |
| 1 | $2$ | $n/2$ | $n$ |
| 2 | $4$ | $n/4$ | $n$ |
| $i$ | $2^i$ | $n/2^i$ | $2^i \cdot \dfrac{n}{2^i} = n$ |
| $\log_2 n$ | $n$ | $1$ | $n$ |
Key observation: Every level contributes exactly $n$.
Number of levels
The recursion stops when the subproblem size reaches 1:
$$\frac{n}{2^k} = 1 \implies 2^k = n \implies k = \log_2 n$$
Number of levels $= \log_2 n + 1$ (levels $0$ through $\log_2 n$).
Summing all levels
$$T(n) = \sum_{i=0}^{\log_2 n} n = n ,(\log_2 n + 1) = n\log_2 n + n$$
Final result
$$\boxed{T(n) = \Theta(n \log n)}$$
This matches the complexity of Merge Sort, which obeys the same recurrence.
2081 · 5 marks
Solve the following recurrence relations using master's method.
-
$T(n) = 2T\left(\frac{n}{2}\right) + n^3,\ n > 1;\quad T(n) = 1,\ n = 1$
-
$T(n) = 2T\left(\frac{n}{4}\right) + 1,\ n > 1;\quad T(n) = 1,\ n = 1$
[5]
Show solved answerHide answer
Solving Recurrence Relations Using Master's Method
Master Method Framework
For $T(n) = aT\left(\frac{n}{b}\right) + f(n)$ with $a \geq 1$, $b > 1$:
Compute the critical exponent $\log_b a$ and compare $f(n)$ with $n^{\log_b a}$:
| Case | Condition | Result |
|---|---|---|
| 1 | $f(n) = O(n^{\log_b a - \epsilon})$, $\epsilon > 0$ | $T(n) = \Theta(n^{\log_b a})$ |
| 2 | $f(n) = \Theta(n^{\log_b a})$ | $T(n) = \Theta(n^{\log_b a}\log n)$ |
| 3 | $f(n) = \Omega(n^{\log_b a + \epsilon})$, $\epsilon > 0$ (+ regularity) | $T(n) = \Theta(f(n))$ |
Given Data
- Recurrence 1: $T(n) = 2T(n/2) + n^3$, $n>1$; $T(1)=1$
- Recurrence 2: $T(n) = 2T(n/4) + 1$, $n>1$; $T(1)=1$
Problem 1: $T(n) = 2T\left(\dfrac{n}{2}\right) + n^3$
Parameters: $a = 2,\ b = 2,\ f(n) = n^3$
Critical exponent: $$\log_b a = \log_2 2 = 1 \implies n^{\log_b a} = n$$
Compare: $f(n) = n^3$ vs $n^1$
Since $n^3 = \Omega(n^{1+\epsilon})$ with $\epsilon = 2 > 0$, this is Case 3.
Regularity check: $$a,f!\left(\frac{n}{b}\right) = 2\left(\frac{n}{2}\right)^3 = \frac{n^3}{4} \leq c,n^3,\quad c=\tfrac14 < 1 \ \checkmark$$
Result: $$\boxed{T(n) = \Theta(n^3)}$$
Problem 2: $T(n) = 2T\left(\dfrac{n}{4}\right) + 1$
Parameters: $a = 2,\ b = 4,\ f(n) = 1 = n^0$
Critical exponent: $$\log_b a = \log_4 2 = \frac{1}{2} \implies n^{\log_b a} = \sqrt{n}$$
Compare: $f(n) = n^0$ vs $n^{1/2}$
Since $n^0 = O(n^{1/2 - \epsilon})$ with $\epsilon = \tfrac12 > 0$, this is Case 1.
Result: $$\boxed{T(n) = \Theta(\sqrt{n})}$$
Summary
| Recurrence | $\log_b a$ | $f(n)$ | Case | Solution |
|---|---|---|---|---|
| $2T(n/2)+n^3$ | $1$ | $n^3$ | 3 | $\Theta(n^3)$ |
| $2T(n/4)+1$ | $1/2$ | $1$ | 1 | $\Theta(\sqrt{n})$ |
2079 · 5 marks
Solve the following recurrence relations using masters method.
a. $T(n) = 2T(n/4) + kn^2,\ n > 1;\ T(1) = 1$
b. $T(n) = 5T(n/4) + kn,\ n > 1;\ T(1) = 1$
[5]
2078 · 5 marks
Explain the recursion tree method for solving the recurrence relation. Solve following recurrence relation using this method. T(n)=2T(n/2) +1 for n>1, T(n)=1 for n=1 [5]
Show solved answerHide answer
Recursion Tree Method for Solving Recurrence Relations
Explanation of the Recursion Tree Method
The recursion tree method is a technique for solving recurrence relations by expanding the recurrence into a tree. Each node represents the cost incurred at a subproblem, and the children of a node represent the recursive subproblems generated. The total cost is found by:
- Expanding the recurrence into a tree, level by level.
- Computing the cost contributed at each level.
- Finding the height (number of levels) of the tree by determining when the subproblem size reaches the base case.
- Summing the per-level costs across all levels.
- Expressing the total as a closed form / Big-Oh bound.
Given Data
- Recurrence: $T(n) = 2T(n/2) + 1$ for $n > 1$
- Base case: $T(1) = 1$
- Non-recursive (combine) cost per call: $1$
- Branching factor: $2$, subproblem size scaling: $n/2$
Step 1: Draw the Recursion Tree
Level 0: T(n) cost = 1
/ \
Level 1: T(n/2) T(n/2) cost = 2×1 = 2
/ \ / \
Level 2: T(n/4) T(n/4) T(n/4) T(n/4) cost = 4×1 = 4
...
Level k: 2^k nodes, each of size n/2^k cost = 2^k
Step 2: Cost at Each Level
| Level $i$ | Number of nodes | Cost per node | Level cost |
|---|---|---|---|
| 0 | $1$ | $1$ | $1$ |
| 1 | $2$ | $1$ | $2$ |
| 2 | $4$ | $1$ | $4$ |
| $i$ | $2^i$ | $1$ | $2^i$ |
Step 3: Height of the Tree
Recursion stops when subproblem size reaches the base case $n/2^k = 1$:
$$2^k = n \implies k = \log_2 n$$
So there are levels $0, 1, \dots, \log_2 n$, i.e. $\log_2 n + 1$ levels.
Note: at the last level, the $2^{\log_2 n} = n$ leaves each have $T(1) = 1$, contributing $n$, which is consistent with the internal-node accounting below.
Step 4: Sum the Total Cost
$$T(n) = \sum_{i=0}^{\log_2 n} 2^i$$
This is a geometric series with ratio $2$:
$$T(n) = \frac{2^{\log_2 n + 1} - 1}{2 - 1} = 2^{\log_2 n + 1} - 1 = 2\cdot 2^{\log_2 n} - 1$$
Since $2^{\log_2 n} = n$:
$$T(n) = 2n - 1$$
Step 5: Big-Oh Notation
$$\boxed{T(n) = 2n - 1 = O(n)}$$
Verification (Master Theorem)
For $T(n)=aT(n/b)+f(n)$ with $a=2,\ b=2,\ f(n)=1=\Theta(n^0)$: $$n^{\log_b a} = n^{\log_2 2} = n^1 = n$$ Since $f(n) = O(n^{1-\varepsilon})$, Case 1 applies, giving $T(n) = \Theta(n^{\log_b a}) = \Theta(n)$. This confirms $O(n)$.
Conclusion
Using the recursion tree method, $T(n) = 2T(n/2) + 1$ solves to $T(n) = 2n - 1 = O(n)$, matching the Master Theorem result.
2076 · 5 marks
Solve the following recurrence relation using the master method.
a. $T(n) = 7 T(n/2) + n^2$
b. $T(n) = 4 T(n/4) + kn$
[5]
Asymptotic Notations
2081 · 5 marks
Write short notes on: a) Big Oh, Big Omega, Big theta b) Class P, Class NP and NP-Complete [5]
Show solved answerHide answer
Short Notes: Asymptotic Notations and Complexity Classes
a) Big Oh, Big Omega, and Big Theta
Big Oh O(g(n)) - Asymptotic Upper Bound
Big Oh notation gives the asymptotic upper bound of a function.
Definition: A function f(x) is said to be O(g(x)) if and only if there exist two positive constants c and x₀ such that:
For all x >= x₀ : f(x) <= c * g(x)
This means g(n) is the upper bound of f(n). The algorithm will never take more time than this bound.
Example: If f(n) = 3n² + 4n + 1, then:
- 3n² + 4n + 1 <= 14n² for all n >= 1
- Therefore, f(n) = O(n²) with c = 14, x₀ = 1
Big Omega Ω(g(n)) - Asymptotic Lower Bound
Big Omega notation gives the asymptotic lower bound of a function.
Definition: A function f(x) is said to be Ω(g(x)) if and only if there exist two positive constants c and x₀ such that:
For all x >= x₀ : f(x) >= c * g(x)
This means g(n) is the lower bound of f(n).
Example: If f(n) = 3n² + 4n + 1, then:
- 3n² + 4n + 1 >= 3n² for all n >= 1
- Therefore, f(n) = Ω(n²) with c = 3, x₀ = 1
Big Theta Θ(g(n)) - Asymptotic Tight Bound
Big Theta notation gives the asymptotically tight bound of a function.
Definition: A function f(x) is said to be Θ(g(x)) if and only if there exist three positive constants c₁, c₂ and x₀ such that:
For all x >= x₀ : c₁ * g(x) <= f(x) <= c₂ * g(x)
This means g(n) is both an upper and lower bound of f(n).
Example: If f(n) = 3n² + 4n + 1, g(n) = n², choose c₁ = 1, c₂ = 14, n₀ = 1:
- 1 * n² <= 3n² + 4n + 1 <= 14 * n² for all n >= 1
- Therefore, f(n) = Θ(n²)
b) Class P, Class NP, and NP-Complete
Class P (Polynomial Time)
- Class P is the set of all decision problems that can be solved by a deterministic algorithm in polynomial time O(n^k) for some constant k.
- These are considered tractable (efficiently solvable) problems.
- Examples: Sorting, searching, shortest path (Dijkstra's), GCD computation.
Class NP (Non-deterministic Polynomial Time)
- Class NP is the set of all decision problems for which a given solution (certificate) can be verified in polynomial time by a deterministic algorithm.
- NP does not necessarily mean the problem cannot be solved in polynomial time; it means verification is polynomial.
- Every problem in P is also in NP (P ⊆ NP), but whether P = NP is an unsolved problem in computer science.
- Examples: Boolean satisfiability (SAT), Hamiltonian cycle, Subset sum.
NP-Complete
-
A problem X is NP-Complete if:
- X belongs to NP (its solution can be verified in polynomial time), AND
- Every other problem in NP can be reduced to X in polynomial time (X is NP-Hard).
-
NP-Complete problems are the hardest problems in NP.
-
If any NP-Complete problem can be solved in polynomial time, then all NP problems can be solved in polynomial time (i.e., P = NP).
-
Examples: SAT problem, Travelling Salesman Problem (TSP), Knapsack problem, Graph Coloring.
Relationship Summary
P ⊆ NP
NP-Complete ⊆ NP
If P = NP, then all NP-Complete problems become P
| Class | Solvable in Poly Time? | Verifiable in Poly Time? |
|---|---|---|
| P | Yes | Yes |
| NP | Unknown (maybe) | Yes |
| NP-Complete | Unknown | Yes |
2076 · 10 marks
What do you mean by the complexity of an algorithm? Explain the asymptotic notations used to describe the time/space complexity of any algorithm with their geometrical interpretation and example.[10]
RAM model
2080 · 5 marks
Define RAM model. Write down iterative algorithm for finding factorial and provide its detailed analysis. [5]
2078 · 10 marks
What are the elementary properties of algorithm? Explain. Why do you need algorithm? Discuss about analysis of the RAM model for analysis of algorithm with suitable example.[10]
Concept of Aggregate Analysis
2080 · 5 marks
Write short notes on a) Aggregate Analysis b) Selection problems [5]
Time and Space Complexity
2079 · 5 marks
Write short notes on: a) Best, Worst and average case complexity b) Greedy Strategy [5]
detailed analysis of algorithms
2078 · 5 marks
Write an algorithm to find the maximum element of an array and analyze its time complexity. [5]
Make Unit 1 stick
Practice CSC325 with flashcards & quizzes