Design and Analysis of Algorithms

106502
Back to Design and Analysis of Algorithms

Module 1: Introduction

  1. Q1a. Which of the following best describes space complexity? (i) Time taken by the algorithm (ii) Memory required to run an algorithm (iii) Number of recursive calls (iv) Number of loops used20242m

    Module 1: Introduction

    Which of the following best describes space complexity?
    (i) Time taken by the algorithm
    (ii) Memory required to run an algorithm
    (iii) Number of recursive calls
    (iv) Number of loops used

    View this question on its own page →
    Worked Solution

    Answer: (ii) Memory required to run an algorithm

    Space complexity = total memory (input + auxiliary + program). Time is execution steps.

  2. Q1a. Which property of an algorithm ensures that every step is clear and has exactly one meaning? (i) Finiteness (ii) Effectiveness (iii) Definiteness (iv) Generality20252m

    Module 1: Introduction

    Which property of an algorithm ensures that every step is clear and has exactly one meaning?

    (i) Finiteness
    (ii) Effectiveness
    (iii) Definiteness
    (iv) Generality

    View this question on its own page →
    Worked Solution

    Answer: (iii) Definiteness

    • Finiteness: terminates after finite steps
    • Definiteness: each step unambiguous
    • Effectiveness: each step basic feasible
    • Generality: works for all inputs of type
  3. Q1a. Which of the following notations is used to represent the worst-case time complexity of an algorithm? (i) O-notation (ii) \Omega-notation (iii) \Theta-notation (iv) \delta-notation20232m

    Module 1: Introduction

    Which of the following notations is used to represent the worst-case time complexity of an algorithm?
    (i) O-notation
    (ii) Ω\Omega-notation
    (iii) Θ\Theta-notation
    (iv) δ\delta-notation

    View this question on its own page →
    Worked Solution

    Answer: (i) O-notation

    • O = upper bound (worst-case)
    • Ω = lower bound (best-case)
    • Θ = tight bound (average-case when upper=lower)
      Worst-case expressed with Big-O.
  4. Q1b. An algorithm is made up of two independent time complexities f(n) and g(n). Then the complexities of the algorithm is in the order of (i) f(n) \times g(n) (ii) \max \{ f(n), g(n) \} (iii) \min \{ f(n), g(n) \} (iv) f(n) + g(n)20222m

    Module 1: Introduction

    An algorithm is made up of two independent time complexities f(n)f(n) and g(n)g(n). Then the complexities of the algorithm is in the order of
    (i) f(n)×g(n)f(n) \times g(n)
    (ii) max⁡{f(n),g(n)}\max \{ f(n), g(n) \}
    (iii) min⁡{f(n),g(n)}\min \{ f(n), g(n) \}
    (iv) f(n)+g(n)f(n) + g(n)

    View this question on its own page →
    Worked Solution

    Answer: (ii) max{ f(n), g(n)} (which equals (iv) f+g asymptotically)

    If algorithm has two independent parts sequentially, T = f+g = Θ(max). Sum asymptotically dominated by max. Both (ii) and (iv) are correct; exam expects (ii) max as simplified.

  5. Q1b. If an algorithm has time complexity T(n) = 5n^2 + 3n + 7, its asymptotic upper bound is: (i) O(n) (ii) O(n^2) (iii) O(n \log n) (iv) O(n^3)20242m

    Module 1: Introduction

    If an algorithm has time complexity T(n)=5n2+3n+7T(n) = 5n^2 + 3n + 7, its asymptotic upper bound is:
    (i) O(n)O(n)
    (ii) O(n2)O(n^2)
    (iii) O(nlog⁡n)O(n \log n)
    (iv) O(n3)O(n^3)

    View this question on its own page →
    Worked Solution

    Answer: (ii) O(n²)

    5n² dominates 3n+7; tight upper bound is O(n²). O(n³) also upper but not minimal.

  6. Q1b. In Asymptotic notation, Big- \Omega (Omega) is used to represent (i) The upper bound (Worst-case) (ii) The lower bound (Best-case) (iii) The tight bound (Average-case) (iv) None of the above20252m

    Module 1: Introduction

    In Asymptotic notation, Big- Ω\Omega (Omega) is used to represent

    (i) The upper bound (Worst-case)
    (ii) The lower bound (Best-case)
    (iii) The tight bound (Average-case)
    (iv) None of the above

    View this question on its own page →
    Worked Solution

    Correct option: (ii) The lower bound (as the intended distinction in this question).

    Big-Ω (Omega) notation describes an asymptotic lower bound on a function's growth. Formally, (f(n) \in \Omega(g(n))) if there are constants (c>0) and (n_0) such that (f(n) \ge c,g(n)) for every (n \ge n_0).

    Important clarification: Big-Ω does not inherently mean best-case complexity. Upper bound, lower bound and tight bound are properties of asymptotic notation; best-case, worst-case and average-case describe which case of an algorithm's input behaviour is being analysed. A lower bound can be applied to any of those functions.

    • Big-O: asymptotic upper bound.
    • Big-Ω: asymptotic lower bound.
    • Big-Θ: asymptotically tight bound.

    Therefore, choose option (ii) for the lower-bound part, but do not memorize “Ω = best case” as a general rule.

  7. Q1c. The recurrence T(n) = T(n/2) + 1 has a time complexity of: (i) O(n) (ii) O(\log n) (iii) O(n \log n) (iv) O(n^2)20242m

    Module 1: Introduction

    The recurrence T(n)=T(n/2)+1T(n) = T(n/2) + 1 has a time complexity of:
    (i) O(n)O(n)
    (ii) O(log⁡n)O(\log n)
    (iii) O(nlog⁡n)O(n \log n)
    (iv) O(n2)O(n^2)

    View this question on its own page →
    Worked Solution

    Answer: (ii) O(log n)

    Master theorem: a=1,b=2,f=1,n^{log_b a}=1 → case 2 with f=Θ(n^{0}) → Θ(log n).

  8. Q1d. Which sorting algorithm is considered stable and has a time complexity of O(n^2)? (i) Quick sort (ii) Merge sort (iii) Insertion sort (iv) Selection sort20232m

    Module 1: Introduction

    Which sorting algorithm is considered stable and has a time complexity of O(n2)O(n^2)?
    (i) Quick sort
    (ii) Merge sort
    (iii) Insertion sort
    (iv) Selection sort

    View this question on its own page →
    Worked Solution

    Answer: (iii) Insertion sort

    Insertion sort stable O(n²) worst, adaptive O(n) nearly sorted. Merge stable O(n log n), Quick unstable, Selection unstable. Question expects insertion.

  9. Q1e. What is the complexity of T(n) = 2T(n/4) + n^2 \log n? (i) \Theta(n^2 \log(\log n)) (ii) \Theta(n^3 \log n) (iii) \Theta(n^2 \log n) (iv) \Theta(n \log n)20232m

    Module 1: Introduction

    What is the complexity of T(n)=2T(n/4)+n2log⁡nT(n) = 2T(n/4) + n^2 \log n?
    (i) Θ(n2log⁡(log⁡n))\Theta(n^2 \log(\log n))
    (ii) Θ(n3log⁡n)\Theta(n^3 \log n)
    (iii) Θ(n2log⁡n)\Theta(n^2 \log n)
    (iv) Θ(nlog⁡n)\Theta(n \log n)

    View this question on its own page →
    Worked Solution

    Answer: (iii) Θ(n² log n)

    Master: a=2,b=4, n^{log_b a}=n^{0.5}. f=n² log n dominates → case 3 → Θ(f)=Θ(n² log n). Regularity holds.

  10. Q1f. Which one of the following functions is asymptotically smallest? (i) 2^n (ii) n^{\log n} (iii) n^{\sqrt{n}} (iv) (100)^{(\log n)^{1/3}} + (\log \log n)^{2/3}20222m

    Module 1: Introduction

    Which one of the following functions is asymptotically smallest?
    (i) 2n2^n
    (ii) nlog⁡nn^{\log n}
    (iii) nnn^{\sqrt{n}}
    (iv) (100)(log⁡n)1/3+(log⁡log⁡n)2/3(100)^{(\log n)^{1/3}} + (\log \log n)^{2/3}

    View this question on its own page →
    Worked Solution

    Answer: (iv) (100)(log⁡n)1/3+(log⁡log⁡n)2/3(100)^{(\log n)^{1/3}} + (\log \log n)^{2/3} is asymptotically smallest

    Explanation:

    Take logs or compare growth orders:

    • (i) 2n=enln⁡2=eΘ(n)2^n = e^{n \ln 2} = e^{\Theta(n)} → exponential
    • (ii) nlog⁡n=e(log⁡n)(ln⁡n)=eΘ((log⁡n)2)n^{\log n}= e^{(\log n)(\ln n)} = e^{\Theta((\log n)^2)}
    • (iii) nn=en⋅ln⁡n=eΘ(nlog⁡n)n^{\sqrt n}= e^{\sqrt n \cdot \ln n}= e^{\Theta(\sqrt n \log n)}
    • (iv) (100)(log⁡n)1/3=e(ln⁡100)(log⁡n)1/3=eΘ((log⁡n)1/3)(100)^{(\log n)^{1/3}} = e^{(\ln 100)(\log n)^{1/3}} = e^{\Theta((\log n)^{1/3})} plus (log⁡log⁡n)2/3=eΘ(log⁡log⁡log⁡n)(\log\log n)^{2/3}= e^{\Theta(\log\log\log n)} which is doubly logarithmic.

    For large nn: (log⁡n)1/3≪(log⁡n)2≪nlog⁡n≪n(\log n)^{1/3} \ll (\log n)^2 \ll \sqrt n\log n \ll n.

    Hence ordering from smallest to largest: (iv) ≪\ll (ii) ≪\ll (iii) ≪\ll (i).

    Formally f4=no(1)f_4 = n^{o(1)} (sub-polynomial, quasi-polylog), f2=nΘ(log⁡n)f_2 = n^{\Theta(\log n)} (super-polynomial but sub-exponential), f3=2Θ(nlog⁡n)f_3 = 2^{\Theta(\sqrt n \log n)}, f1=2Θ(n)f_1=2^{\Theta(n)}.

    Correct choice: (iv)

    Note: Options (ii) and (iii) are often swapped in ranking; but both dominate (iv) asymptotically.

  11. Budget App•POCKETLY

    Pocketly on Android

    Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

    Install Pocketly
  12. Q1f. In algorithm analysis, what does "space complexity" refer to? (i) The number of input elements (ii) The amount of physical memory used (iii) The number of recursive calls (iv) The number of lines of code in the algorithm20232m

    Module 1: Introduction

    In algorithm analysis, what does "space complexity" refer to?
    (i) The number of input elements
    (ii) The amount of physical memory used
    (iii) The number of recursive calls
    (iv) The number of lines of code in the algorithm

    View this question on its own page →
    Worked Solution

    Answer: (ii) The amount of physical memory used

    Explanation:

    • Space complexity is the total amount of memory (in terms of asymptotic function of input size nn) required by an algorithm to run to completion, including:

      • Input space / auxiliary space for variables, data structures, recursion stack, dynamically allocated memory.
      • Often reported as auxiliary space (extra space excluding input itself).
    • (i) number of input elements is nn itself, not space complexity.

    • (iii) number of recursive calls contributes to stack space but is only one component.

    • (iv) lines of code is program size / software metric, unrelated to asymptotic memory usage.

    Hence space complexity = memory consumed as function of nn, e.g., iterative linear search O(1)O(1), merge sort O(n)O(n), recursive DFS O(h)O(h) stack.

    Correct choice: (ii)

  13. Q1h. The average number of comparisons performed by merge sort algorithm in merging two sorted lists of 2 elements is (i) 8/5 (ii) 11/7 (iii) 11/6 (iv) 8/320222m

    Module 1: Introduction

    The average number of comparisons performed by merge sort algorithm in merging two sorted lists of 2 elements is
    (i) 8/5
    (ii) 11/7
    (iii) 11/6
    (iv) 8/3

    View this question on its own page →
    Worked Solution

    Answer: (iv) 8/3≈2.678/3 \approx 2.67

    Explanation:

    Merge of two sorted lists A=[a1<a2]A=[a_1<a_2], B=[b1<b2]B=[b_1<b_2] with all 4! / (2!2!) = 6 interleavings equally likely (distinct elements, random order). Standard merge:

     i=j=0, comps=0
     while i<2 and j<2: comps++; if A[i]<B[j] i++ else j++
    

    Best case = 2 comps (one list entirely smaller: a1<a2<b1<b2a_1<a_2<b_1<b_2 or b1<b2<a1<a2b_1<b_2<a_1<a_2)
    Worst case = 3 comps (interleaved).

    Enumerate:

    1. a1<a2<b1<b2a_1<a_2<b_1<b_2: a1 ⁣< ⁣b1a_1\!<\!b_1, a2 ⁣< ⁣b1a_2\!<\!b_1 → 2
    2. a1<b1<a2<b2a_1<b_1<a_2<b_2: a1 ⁣< ⁣b1a_1\!<\!b_1, b1 ⁣< ⁣a2b_1\!<\!a_2, a2 ⁣< ⁣b2a_2\!<\!b_2 → 3
    3. a1<b1<b2<a2a_1<b_1<b_2<a_2: a1 ⁣< ⁣b1a_1\!<\!b_1, b1 ⁣< ⁣a2b_1\!<\!a_2, b2 ⁣< ⁣a2b_2\!<\!a_2 → 3
    4. b1<b2<a1<a2b_1<b_2<a_1<a_2: 2 (symmetric to 1)
    5. b1<a1<b2<a2b_1<a_1<b_2<a_2: 3
    6. b1<a1<a2<b2b_1<a_1<a_2<b_2: 3

    Average = (2+3+3+2+3+3)/6=16/6=8/3(2+3+3+2+3+3)/6 = 16/6 = 8/3.

    Worst-case merging m+n−1=3m+n-1 =3, best min⁡(m,n)=2\min(m,n)=2, average 8/38/3.

    Correct choice: (iv) 8/38/3

  14. Q2a. Explain the concept of asymptotic notation (Big O, Big Omega, and Big Theta) in algorithm analysis.20237m

    Module 1: Introduction

    Explain the concept of asymptotic notation (Big O, Big Omega, and Big Theta) in algorithm analysis.

    View this question on its own page →
    Worked Solution

    Asymptotic Notations — OO, Ω\Omega, Θ\Theta

    Asymptotic notation describes growth of T(n)T(n) for large nn, ignoring constants and lower-order terms.

    1. Big O — Upper bound (≤\le)

    O(g(n))={f(n):∃c>0,n0>0,0≤f(n)≤c g(n)  ∀n≥n0}O(g(n)) = \{ f(n): \exists c>0, n_0>0, 0 \le f(n) \le c\,g(n) \;\forall n \ge n_0 \}
    Example: 3n2+5n+2=O(n2)3n^2+5n+2 = O(n^2) (choose c=4c=4, n0=10n_0=10). Means algorithm at most that fast-growing.

    2. Big Omega — Lower bound (≥\ge)

    Ω(g(n))={f(n):∃c>0,n0>0,0≤c g(n)≤f(n)  ∀n≥n0}\Omega(g(n)) = \{ f(n): \exists c>0, n_0>0, 0 \le c\,g(n) \le f(n) \;\forall n \ge n_0 \}
    Example: n2/2−n=Ω(n2)n^2/2 - n = \Omega(n^2). Means algorithm at least that slow.

    3. Big Theta — Tight bound (==)

    Θ(g(n))={f(n):∃c1,c2>0,n0>0,c1g(n)≤f(n)≤c2g(n)  ∀n≥n0}\Theta(g(n)) = \{ f(n): \exists c_1,c_2>0, n_0>0, c_1 g(n) \le f(n) \le c_2 g(n) \;\forall n \ge n_0 \}
    Equivalently f(n)=Θ(g(n))  ⟺  f(n)=O(g(n))f(n)=\Theta(g(n)) \iff f(n)=O(g(n)) and f(n)=Ω(g(n))f(n)=\Omega(g(n)).
    Example: 3n2+5n=Θ(n2)3n^2+5n = \Theta(n^2).

    Small o / small omega (strict)

    • o(g)o(g): ∀c>0\forall c>0 eventually f(n)<cg(n)f(n) < c g(n) (non-tight upper).
    • ω(g)\omega(g): strict lower.

    Properties & Use

    • Transitivity, reflexivity, symmetry (Θ\Theta symmetric).
    • Transpose: f=O(g)  ⟺  g=Ω(f)f=O(g) \iff g=\Omega(f).
    • Used to classify algorithms: Linear search O(n)O(n) worst, Ω(1)\Omega(1) best, Θ(n)\Theta(n) average; Merge sort Θ(nlog⁡n)\Theta(n\log n); Binary search Θ(log⁡n)\Theta(\log n).
    • Hierarchy: O(1)⊂O(log⁡n)⊂O(n)⊂O(nlog⁡n)⊂O(n2)⊂O(2n)O(1) \subset O(\log n) \subset O(n) \subset O(n\log n) \subset O(n^2) \subset O(2^n).

    Graphical intuition

    Plot c⋅g(n)c\cdot g(n) bounding f(n)f(n) above (OO), below (Ω\Omega), or sandwiching (Θ\Theta) beyond n0n_0.

  15. Q2a. Solve the following recurrence by successive substitution method: f(1) = 1 \quad \text{if } n=1 f(n) = 3f(n/2) + 6 \quad \text{if } n > 120227m

    Module 1: Introduction

    Solve the following recurrence by successive substitution method:
    f(1)=1if n=1f(1) = 1 \quad \text{if } n=1
    f(n)=3f(n/2)+6if n>1f(n) = 3f(n/2) + 6 \quad \text{if } n > 1

    View this question on its own page →
    Worked Solution

    Solution – Recurrence f(n)=3f(n/2)+6f(n)=3f(n/2)+6, f(1)=1f(1)=1 by Successive Substitution

    Assume n=2kn=2^k (k=log⁡2nk=\log_2 n).

    Unfold:
    f(n)=3f(n/2)+6=3[3f(n/4)+6]+6=32f(n/22)+6⋅3+6=32[3f(n/23)+6]+6⋅3+6=33f(n/23)+6⋅32+6⋅3+6  ⋮f(n)=3kf(n/2k)+6∑i=0k−13i \begin{aligned} f(n) &= 3f(n/2)+6 \\ &= 3[3f(n/4)+6]+6 = 3^2 f(n/2^2)+6\cdot3+6 \\ &= 3^2[3f(n/2^3)+6]+6\cdot3+6 = 3^3 f(n/2^3)+6\cdot3^2+6\cdot3+6\\ &\;\vdots \\ f(n) &= 3^k f(n/2^k)+6\sum_{i=0}^{k-1}3^i \end{aligned}

    Since n/2k=1n/2^k=1, f(n/2k)=f(1)=1f(n/2^k)=f(1)=1 and ∑i=0k−13i=(3k−1)/(3−1)=(3k−1)/2\sum_{i=0}^{k-1}3^i = (3^k-1)/(3-1)=(3^k-1)/2.

    f(n)=3k⋅1+6⋅3k−12=3k+3(3k−1)=4⋅3k−3 f(n)=3^k \cdot 1 +6\cdot\frac{3^k-1}{2}=3^k+3(3^k-1)=4\cdot3^k-3

    With k=log⁡2nk=\log_2 n:
    f(n)=4⋅3log⁡2n−3=4⋅nlog⁡23−3\boxed{f(n)=4\cdot 3^{\log_2 n}-3 = 4\cdot n^{\log_2 3}-3} for nn power of 2.

    Asymptotics:
    f(n)=Θ(nlog⁡23)=Θ(n1.58496…)f(n)=\Theta(n^{\log_2 3}) = \Theta(n^{1.58496\dots})

    Check by Master Theorem: a=3a=3, b=2b=2, nlog⁡ba=nlog⁡23n^{\log_b a}=n^{\log_2 3} dominates f(n)=Θ(1)f(n)=\Theta(1) case 1 → Θ(nlog⁡23)\Theta(n^{\log_2 3}) consistent.

    For general nn (not power of 2), embed between powers of 2 → same Θ\Theta.

  16. Q2a. Answer the following: (i) Differentiate between best-case, worst-case, and average-case complexity with suitable examples. (ii) Use examples like linear search to demonstrate the differences. (iii) Discuss why worst-case analysis is often preferred in practice.20247m

    Module 1: Introduction

    Answer the following:
    (i) Differentiate between best-case, worst-case, and average-case complexity with suitable examples.
    (ii) Use examples like linear search to demonstrate the differences.
    (iii) Discuss why worst-case analysis is often preferred in practice.

    View this question on its own page →
    Worked Solution

    (i) Best / Worst / Average Case Complexity

    • Best case Tbest(n)T_{best}(n): minimum time over all inputs of size nn. Often overly optimistic.
    • Worst case Tworst(n)T_{worst}(n): maximum time — guarantee that algorithm never exceeds this.
    • Average case Tavg(n)T_{avg}(n): expected time over distribution (usually uniform) of inputs — needs probabilistic analysis.

    (ii) Example: Linear Search for key xx in array A[1..n]A[1..n]

    for i=1..n: if A[i]==x return i
    return -1
    
    • Best: x=A[1]x=A[1] → 1 comparison → Θ(1)\Theta(1) → O(1)O(1) / Ω(1)\Omega(1).
    • Worst: $x
      otin Aor or x=A[n]→ → ncomparisons→ comparisons → \Theta(n)$.
    • Average (uniform, xx always present): expected position (n+1)/2(n+1)/2 → Θ(n)\Theta(n) comparisons; if xx absent with probability pp, average = p⋅n+(1−p)(n+1)/2=Θ(n)p\cdot n + (1-p)(n+1)/2 =\Theta(n).

    Similarly Binary Search: best O(1)O(1), worst/avg Θ(log⁡n)\Theta(\log n); Bubble Sort: best O(n)O(n) (with early exit), worst/avg Θ(n2)\Theta(n^2).

    (iii) Why Worst-Case Preferred

    1. Guarantee: bounds response time for real-time / safety-critical systems.
    2. Simplicity: does not require input distribution knowledge (average case needs distribution).
    3. Dominant for many algorithms: average = worst order-wise (e.g., linear search, quicksort worst O(n2)O(n^2) but average O(nlog⁡n)O(n\log n) — here worst still informs; randomized analysis gives expected worst over random choices).
    4. Adversarial inputs: worst-case protects against malicious data.
    5. Standard in complexity theory: PP, NPNP defined via worst-case polytime.

    Note: Average-case is important when worst-case is rare (e.g., QuickSort, Hashing expected O(1)O(1)), but worst-case remains the default metric published.

  17. Q2b. Define time complexity and space complexity. Write an algorithm for adding n natural numbers and find the space required by that algorithm.20257m

    Module 1: Introduction

    Define time complexity and space complexity. Write an algorithm for adding n natural numbers and find the space required by that algorithm.

    View this question on its own page →
    Worked Solution

    Time and Space Complexity + Algorithm to Add nn Naturals

    Definitions

    • Time Complexity: asymptotic number of basic operations (steps) as function of input size nn, T(n)T(n).
    • Space Complexity: asymptotic total memory (input + auxiliary + recursion stack) S(n)S(n); often reported as auxiliary space (extra beyond input).

    Algorithm — Sum of First nn Natural Numbers

    Iterative O(n)O(n) time, O(1)O(1) aux space:

    Algorithm SumNatural(n):
      sum ← 0
      for i ← 1 to n:
        sum ← sum + i
      return sum
    

    Time: nn iterations × O(1)O(1) → T(n)=Θ(n)T(n)=\Theta(n) (nn additions). Space: variables sumsum, ii, nn → 3 words → S(n)=Θ(1)S(n)=\Theta(1) auxiliary (Θ(n)\Theta(n) if counting input array? Here input is just nn → O(1)O(1)).

    Formula O(1)O(1) time (mathematical):

    Algorithm SumFormula(n):
      return n*(n+1)//2
    

    Time Θ(1)\Theta(1) arithmetic operations, space Θ(1)\Theta(1). Uses closed form ∑i=1ni=n(n+1)/2\sum_{i=1}^n i = n(n+1)/2.

    Recursive (for illustration):

    RecSum(n): if n==1 return 1 else return n + RecSum(n-1)
    

    Time Θ(n)\Theta(n), stack space Θ(n)\Theta(n) due to nn frames — worse.

    Analysis Summary

    Variant Time Auxiliary Space Total Space
    Iterative loop Θ(n)\Theta(n) O(1)O(1) O(1)O(1)
    Formula Θ(1)\Theta(1) O(1)O(1) O(1)O(1)
    Recursive Θ(n)\Theta(n) Θ(n)\Theta(n) stack Θ(n)\Theta(n)

    Best practical: formula O(1)O(1). If required to demonstrate space complexity concept, loop shows O(1)O(1) auxiliary despite O(n)O(n) time.

  18. Q2b. Consider the following recurrence relation: T(n) = 2T(n/2) + n Use the substitution method to find an asymptotic upper bound for the function T(n).20237m

    Module 1: Introduction

    Consider the following recurrence relation:
    T(n)=2T(n/2)+nT(n) = 2T(n/2) + n
    Use the substitution method to find an asymptotic upper bound for the function T(n)T(n).

    View this question on its own page →
    Worked Solution

    T(n)=2T(n/2)+nT(n)=2T(n/2)+n — Substitution Method, Upper Bound O(nlog⁡n)O(n \log n)

    Guess: T(n)≤c nlog⁡nT(n) \le c\, n \log n for some c>0c>0, n≥n0n\ge n_0 (with T(1)=Θ(1)T(1)=\Theta(1)).

    Inductive Hypothesis: Assume for n/2n/2: T(n/2)≤c(n/2)log⁡(n/2)T(n/2) \le c (n/2) \log(n/2).

    Inductive Step:
    T(n)=2T(n/2)+n≤2[cn2log⁡n2]+n=cn(log⁡n−log⁡2)+n=cnlog⁡n−cnlog⁡2+n \begin{aligned} T(n) &= 2T(n/2)+n \\ &\le 2\left[ c\frac{n}{2}\log\frac{n}{2}\right]+n \\ &= c n (\log n - \log 2)+n \\ &= c n \log n - c n \log 2 + n \end{aligned}
    With log⁡\log base 2, log⁡2=1\log 2=1:
    T(n)≤cnlog⁡n−cn+n=cnlog⁡n−(c−1)nT(n) \le c n \log n -c n + n = c n \log n -(c-1)n
    Choose c≥1c \ge 1 → −(c−1)n≤0-(c-1)n \le 0 → T(n)≤cnlog⁡nT(n) \le c n \log n.

    Base case: Need T(1)≤c⋅1⋅log⁡1=0T(1) \le c\cdot1\cdot\log1 =0 fails for log base2. Use T(2)T(2) or define T(1)=1T(1)=1, choose n0=2n_0=2, pick cc large enough: c⋅2log⁡2≥T(2)=2T(1)+2=4c\cdot2\log2 \ge T(2)=2T(1)+2=4 → c≥2c\ge2 works. More formally prove for n≥2n\ge2, or use nlog⁡nn\log n with T(1)=Θ(1)T(1)=\Theta(1) and show T(n)≤cnlog⁡n+bT(n)\le c n\log n +b variant.

    Hence T(n)=O(nlog⁡n)T(n)=O(n\log n).

    Tight bound: Similarly show T(n)≥c′nlog⁡nT(n) \ge c' n\log n → Ω(nlog⁡n)\Omega(n\log n) → Θ(nlog⁡n)\Theta(n\log n).
    By Master Theorem case 2 (a=2a=2, b=2b=2, nlog⁡ba=n1n^{\log_b a}=n^1, f(n)=n=Θ(nlog⁡ba)f(n)=n=\Theta(n^{\log_b a})) → T(n)=Θ(nlog⁡n)T(n)=\Theta(n\log n).

    Application: This is the Merge Sort recurrence.

  19. Q2b. Suppose the number of basic operations in an algorithm is defined as: T(n) = T(n-1) + n, with T(1) = 1. Solve this recurrence using the substitution method and determine the time complexity.20247m

    Module 1: Introduction

    Suppose the number of basic operations in an algorithm is defined as: T(n)=T(n−1)+nT(n) = T(n-1) + n, with T(1)=1T(1) = 1.
    Solve this recurrence using the substitution method and determine the time complexity.

    View this question on its own page →
    Worked Solution

    T(n)=T(n−1)+nT(n)=T(n-1)+n, T(1)=1T(1)=1 — Solve by Iteration / Substitution

    Iterative Unfolding:
    T(n)=T(n−1)+n=[T(n−2)+(n−1)]+n=T(n−2)+(n−1)+n=[T(n−3)+(n−2)]+(n−1)+n  ⋮=T(1)+2+3+⋯+n=1+∑k=2nk=∑k=1nk=n(n+1)2 \begin{aligned} T(n) &= T(n-1)+n \\ &= [T(n-2)+(n-1)]+n = T(n-2)+(n-1)+n \\ &= [T(n-3)+(n-2)]+(n-1)+n \\ &\;\vdots \\ &= T(1) + 2+3+\dots+n \\ &= 1 + \sum_{k=2}^{n} k = \sum_{k=1}^{n} k = \frac{n(n+1)}{2} \end{aligned}

    Substitution Proof: Guess T(n)=n(n+1)/2T(n)=n(n+1)/2.

    • Base n=1n=1: 1⋅2/2=11\cdot2/2=1 ✓
    • Step: assume T(n−1)=(n−1)n/2T(n-1)=(n-1)n/2 → T(n)=(n−1)n/2+n=n(n+1)/2T(n)=(n-1)n/2 + n = n(n+1)/2 ✓

    Asymptotics:
    T(n)=n22+n2=Θ(n2)=O(n2),Ω(n2)T(n)=\frac{n^2}{2}+\frac{n}{2}=\Theta(n^2) = O(n^2), \Omega(n^2)

    Application: Sum of first nn naturals; time of naive incremental algorithms (e.g., insertion sort worst-case without divide).

  20. Q3a. Discuss the concepts of asymptotic notations and its properties.20257m

    Module 1: Introduction

    Discuss the concepts of asymptotic notations and its properties.

    View this question on its own page →
    Worked Solution

    Asymptotic Notations & Properties

    Definitions (as in Q36)

    • O(g)O(g) upper: ∃c,n0:0≤f(n)≤cg(n)\exists c,n_0: 0\le f(n)\le c g(n) ∀n≥n0\forall n\ge n_0.
    • Ω(g)\Omega(g) lower: ∃c,n0:0≤cg(n)≤f(n)\exists c,n_0: 0\le c g(n)\le f(n).
    • Θ(g)\Theta(g) tight: c1g(n)≤f(n)≤c2g(n)c_1 g(n)\le f(n)\le c_2 g(n) → O∩ΩO \cap \Omega.
    • o(g)o(g) strict upper: ∀c>0\forall c>0 f(n)<cg(n)f(n)<c g(n) eventually (f/g→0f/g\to0).
    • ω(g)\omega(g) strict lower (f/g→∞f/g\to\infty).

    Properties

    1. Transitivity: f=O(g),g=O(h)⇒f=O(h)f=O(g), g=O(h)\Rightarrow f=O(h) (similar for Ω,Θ\Omega,\Theta).
    2. Reflexivity: f=Θ(f)f=\Theta(f).
    3. Symmetry: f=Θ(g)  ⟺  g=Θ(f)f=\Theta(g)\iff g=\Theta(f); transpose symmetry f=O(g)  ⟺  g=Ω(f)f=O(g)\iff g=\Omega(f).
    4. Additivity/Max rule: If f1=O(g1),f2=O(g2)f_1=O(g_1), f_2=O(g_2) then f1+f2=O(max⁡(g1,g2))f_1+f_2=O(\max(g_1,g_2)). Hence polynomial aknk+⋯=O(nk)a_k n^k+\dots=O(n^k).
    5. Scaling: cf(n)=Θ(f(n))c f(n)=\Theta(f(n)) for constant c>0c>0; lower-order terms absorbed.
    6. Ordering: O(1)⊂O(log⁡n)⊂O(n)⊂O(nlog⁡n)⊂O(n2)⊂O(n3)⊂O(2n)⊂O(n!)O(1)\subset O(\log n)\subset O(n)\subset O(n\log n)\subset O(n^2)\subset O(n^3)\subset O(2^n)\subset O(n!).
    7. Polynomial gap: na=o(nb)n^{a}=o(n^{b}) if a<ba<b; any poly is o(2n)o(2^n).
    8. Master/recurrence use: notations hide constants allowing comparison Unabhängig von Hardware.

    Example Hierarchy

    • Linear search worst Θ(n)\Theta(n), binary Θ(log⁡n)\Theta(\log n), merge Θ(nlog⁡n)\Theta(n\log n), matrix mult naive Θ(n3)\Theta(n^3), Strassen Θ(n2.81)\Theta(n^{2.81}), TSP brute Θ(n!)\Theta(n!).

    Pitfalls

    • OO is not tight — use Θ\Theta when both upper and lower known.
    • Not all functions comparable (nsin⁡nn^{\sin n} incomparable).
    • Amortized vs worst-case: OO still worst over sequence.
  21. Q3a. What is the Master's Theorem? State the general form and the three cases of the theorem. Use it to solve the following recurrence relations: (i) T(n) = 2T(n/2) + n (ii) T(n) = 3T(n/2) + n^2 (iii) T(n) = 2T(n/2) + n/\log n Explain which case applies in each situation.20247m

    Module 1: Introduction

    What is the Master's Theorem? State the general form and the three cases of the theorem. Use it to solve the following recurrence relations:
    (i) T(n)=2T(n/2)+nT(n) = 2T(n/2) + n
    (ii) T(n)=3T(n/2)+n2T(n) = 3T(n/2) + n^2
    (iii) T(n)=2T(n/2)+n/log⁡nT(n) = 2T(n/2) + n/\log n
    Explain which case applies in each situation.

    View this question on its own page →
    Worked Solution

    Master's Theorem — Statement & Applications

    Theorem

    For T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n), a≥1a\ge1, b>1b>1:
    Let c=log⁡bac=\log_b a.

    • Case 1: f(n)=O(nc−ϵ)f(n)=O(n^{c-\epsilon}) → T(n)=Θ(nc)T(n)=\Theta(n^{c}) (leaf heavy).
    • Case 2: f(n)=Θ(nclog⁡kn)f(n)=\Theta(n^{c}\log^{k} n) → T(n)=Θ(nclog⁡k+1n)T(n)=\Theta(n^{c}\log^{k+1} n); with k=0k=0 → Θ(nclog⁡n)\Theta(n^{c}\log n) (balanced).
    • Case 3: f(n)=Ω(nc+ϵ)f(n)=\Omega(n^{c+\epsilon}) + regularity af(n/b)≤c′f(n)a f(n/b)\le c' f(n) → T(n)=Θ(f(n))T(n)=\Theta(f(n)) (root heavy).

    (i) T(n)=2T(n/2)+nT(n)=2T(n/2)+n

    • c=log⁡22=1c=\log_2 2=1 → nc=nn^{c}=n, f(n)=n=Θ(nc)f(n)=n=\Theta(n^{c}) → Case 2 (k=0) → T(n)=Θ(nlog⁡n)T(n)=\Theta(n\log n) (Merge Sort).

    (ii) T(n)=3T(n/2)+n2T(n)=3T(n/2)+n^2

    • c=log⁡23≈1.585c=\log_2 3 \approx1.585, nc=n1.585n^{c}=n^{1.585}, f(n)=n2=Ω(nc+ϵ)f(n)=n^2=\Omega(n^{c+\epsilon}) with ϵ≈0.415\epsilon\approx0.415.
    • Regularity: 3(n/2)2=3n2/4≤cf(n)3(n/2)^2=3n^2/4 \le c f(n) with c=3/4<1c=3/4<1 holds → Case 3 → T(n)=Θ(n2)T(n)=\Theta(n^2)
      Alternatively leaf vs root: root dominates.

    (iii) T(n)=2T(n/2)+n/log⁡nT(n)=2T(n/2)+n/\log n

    • c=1c=1 → nc=nn^{c}=n, f(n)=n/log⁡n=Θ(nc/log⁡n)f(n)=n/\log n = \Theta(n^{c}/\log n).
    • This is not polynomially smaller (nc−ϵn^{c-\epsilon}) nor Θ(nc)\Theta(n^{c}); it is n/polylogn/\text{polylog} — gap between cases 1 and 2.
    • Master Theorem does NOT apply (fails polynomial gap condition). By recursion tree / Akra-Bazzi:
      T(n)=2T(n/2)+n/log⁡n  ⇒  T(n)=Θ(nlog⁡log⁡n) T(n)=2T(n/2)+n/\log n\;\Rightarrow\; T(n)=\Theta(n\log\log n)
      Proof sketch: T(n)=n∑i=0log⁡n−11/(log⁡n−i)=nHlog⁡n=n⋅Θ(log⁡log⁡n)T(n)=n\sum_{i=0}^{\log n -1} 1/(\log n -i)=n H_{\log n}=n\cdot\Theta(\log\log n).
    • If forced into extended Case 2 (k=−1k=-1), some extensions give Θ(nlog⁡log⁡n)\Theta(n\log\log n).

    Summary: (i) Θ(nlog⁡n)\Theta(n\log n) Case2, (ii) Θ(n2)\Theta(n^2) Case3, (iii) Gap case — Θ(nlog⁡log⁡n)\Theta(n\log\log n), master not applicable in strict form.

  22. Budget App•POCKETLY

    Pocketly on Android

    Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

    Install Pocketly
  23. Q3a. Apply the Master Theorem to determine the time complexity of the following recurrence relations. (i) T(n) = 4T(n/2) + n^3 (ii) T(n) = T(n/2) + 2^n20235m

    Module 1: Introduction

    Apply the Master Theorem to determine the time complexity of the following recurrence relations.
    (i) T(n)=4T(n/2)+n3T(n) = 4T(n/2) + n^3
    (ii) T(n)=T(n/2)+2nT(n) = T(n/2) + 2^n

    View this question on its own page →
    Worked Solution

    Master Theorem Applications

    General Form: T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n), a≥1a\ge1, b>1b>1, f(n)f(n) asymptotically positive.
    Let nlog⁡ban^{\log_b a} be the watershed.

    • Case 1: f(n)=O(nlog⁡ba−ϵ)f(n)=O(n^{\log_b a -\epsilon}) for ϵ>0\epsilon>0 → T(n)=Θ(nlog⁡ba)T(n)=\Theta(n^{\log_b a}).
    • Case 2: f(n)=Θ(nlog⁡balog⁡kn)f(n)=\Theta(n^{\log_b a}\log^k n) → T(n)=Θ(nlog⁡balog⁡k+1n)T(n)=\Theta(n^{\log_b a}\log^{k+1} n); special k=0k=0: Θ(nlog⁡balog⁡n)\Theta(n^{\log_b a}\log n).
    • Case 3: f(n)=Ω(nlog⁡ba+ϵ)f(n)=\Omega(n^{\log_b a+\epsilon}) and regularity af(n/b)≤cf(n)a f(n/b)\le c f(n) for c<1c<1 → T(n)=Θ(f(n))T(n)=\Theta(f(n)).

    (i) T(n)=4T(n/2)+n3T(n)=4T(n/2)+n^3

    • a=4a=4, b=2b=2 → nlog⁡ba=nlog⁡24=n2n^{\log_b a}=n^{\log_2 4}=n^2.
    • f(n)=n3=Ω(n2+1)f(n)=n^3=\Omega(n^{2+1}) with ϵ=1\epsilon=1.
    • Regularity: 4(n/2)3=4n3/8=n3/2≤cn34(n/2)^3 =4 n^3/8 = n^3/2 \le c n^3 with c=1/2<1c=1/2<1 ✓
    • Case 3 → T(n)=Θ(f(n))=Θ(n3)T(n)=\Theta(f(n))=\Theta(n^3)

    (ii) T(n)=T(n/2)+2nT(n)=T(n/2)+2^n

    • a=1a=1, b=2b=2 → nlog⁡ba=n0=1n^{\log_b a}=n^0=1.
    • f(n)=2nf(n)=2^n is exponential, not polynomial; 2n=Ω(nϵ)2^n = \Omega(n^{\epsilon}) for any ϵ\epsilon, dominated by ff.
    • Although f(n)=2n=Ω(n0+ϵ)f(n)=2^n = \Omega(n^{0+\epsilon}), regularity holds: 1⋅2n/2≤c2n1\cdot2^{n/2} \le c 2^n for large nn (c=1/2c=1/2) as 2n/2/2n=2−n/2→02^{n/2}/2^n=2^{-n/2}\to0.
    • This fits Case 3 (generalized interpretation) where ff dominates polynomially. Hence T(n)=Θ(f(n))=Θ(2n)T(n)=\Theta(f(n))=\Theta(2^n)

    Note: Strict Master theorem in many textbooks requires ff polynomial; for exponential ff, the theorem still yields T(n)=Θ(2n)T(n)=\Theta(2^n) because nlog⁡ba=O(f(n))n^{\log_b a}=O(f(n)) and regularity holds; formally the answer is exponential dominated, not Θ(1)\Theta(1). Some state "Master theorem not applicable (f not polynomial)" but via extension the bound is Θ(2n)\Theta(2^n); if insisting on polynomial ff condition, declare not applicable, recurrence solves to Θ(2n)\Theta(2^n) by recursion tree: T(n)=2n+2n/2+2n/4+⋯+1=Θ(2n)T(n)=2^n+2^{n/2}+2^{n/4}+\dots+1 =\Theta(2^n).

  24. Q4a. Apply the Linear Search algorithm to find the position of the target element 7 in the following array: A=[3, 5, 2, 8, 7, 1, 4]. Also, analyze the time and space complexity.20237m

    Module 1: Introduction

    Apply the Linear Search algorithm to find the position of the target element 7 in the following array: A=[3,5,2,8,7,1,4]A=[3, 5, 2, 8, 7, 1, 4]. Also, analyze the time and space complexity.

    View this question on its own page →
    Worked Solution

    Linear Search for 7 in A=[3,5,2,8,7,1,4]A=[3,5,2,8,7,1,4]

    Algorithm

    LinearSearch(A, n, key):
      for i=0 to n-1:
        if A[i]==key: return i  // 0-based index, +1 for 1-based position
      return -1 // not found
    

    Trace for key=7

    i (0-based) A[i] Compare A[i]==7? Action
    0 3 3==7? No continue
    1 5 5==7? No continue
    2 2 2==7? No continue
    3 8 8==7? No continue
    4 7 7==7? Yes return 4 (0-based) / 5 (1-based)
    Search stops early; elements after index4 not examined.

    Result: Found at index 4 (0-based) → position 5 (1-based). Comparisons = 5 best/worst?

    Complexity Analysis (n=7n=7)

    • Time:
      • Best O(1)O(1) (key at first position)
      • Worst O(n)O(n) (key absent or at last) → here worst would be 7 comparisons
      • Average O(n)O(n) → expected (n+1)/2(n+1)/2 if key present uniformly
      • In this instance 5 comparisons = Θ(n)\Theta(n) worst-like.
    • Space: O(1)O(1) auxiliary (loop variable + key), O(n)O(n) total if counting input array; in-place.
    • No sorting needed; works on unsorted arrays; optimal for unsorted (any algorithm must examine Ω(n)\Omega(n) in worst case).
  25. Q8b. State master's theorem and find the time complexity for the following recurrence: T(n) = 2T(n^{1/2}) + \log n20227m

    Module 1: Introduction

    State master's theorem and find the time complexity for the following recurrence:
    T(n)=2T(n1/2)+log⁡nT(n) = 2T(n^{1/2}) + \log n

    View this question on its own page →
    Worked Solution

    Master's Theorem & T(n)=2T(n1/2)+log⁡nT(n)=2T(n^{1/2})+\log n

    Master's Theorem Statement (standard)

    For T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n), a≥1a≥1, b>1b>1, c=log⁡bac=\log_b a:

    • Case1 f(n)=O(nc−ε)→Θ(nc)f(n)=O(n^{c-ε}) → Θ(n^c)
    • Case2 f(n)=Θ(nclog⁡kn)→Θ(nclog⁡k+1n)f(n)=Θ(n^c\log^k n)→Θ(n^c\log^{k+1}n)
    • Case3 f(n)=Ω(nc+ε)f(n)=Ω(n^{c+ε}) + regularity → Θ(f)
      (Requires n/bn/b form, not n1/2n^{1/2}.)

    Can Master Apply to T(n)=2T(n)+log⁡nT(n)=2T(\sqrt n)+\log n?

    Not directly — argument is n1/2n^{1/2}, not n/bn/b with constant bb. Standard master not applicable. Use substitution / change of variable.

    Solution via Domain Transformation

    Let n=2mn=2^m (assuming nn is power of 2 for asymptotics), define S(m)=T(2m)S(m)=T(2^m).
    Then:
    S(m)=T(2m)=2T((2m)1/2)+log⁡(2m)=2T(2m/2)+m=2S(m/2)+m S(m)=T(2^m)=2T((2^m)^{1/2})+\log(2^m)=2T(2^{m/2})+m =2S(m/2)+m
    Now S(m)=2S(m/2)+mS(m)=2S(m/2)+m → This is master form with a=2a=2, b=2b=2, f(m)=mf(m)=m.

    • c=log⁡22=1c=\log_2 2=1, f(m)=m=Θ(m1)=Θ(mc)f(m)=m=Θ(m^1)=Θ(m^c) → Case2 → S(m)=Θ(mlog⁡m)S(m)=Θ(m\log m).

    Convert back: m=log⁡nm=\log n →
    T(n)=S(log⁡n)=Θ(log⁡n⋅log⁡log⁡n)T(n)=S(\log n)=Θ(\log n · \log \log n)

    Result: T(n)=Θ(log⁡n⋅log⁡log⁡n)T(n)=Θ(\log n \cdot \log\log n)
    Alternatively O(log⁡nlog⁡log⁡n)O(\log n \log\log n).

    Verification via Recursion Tree

    Depth: each level halves mm → log⁡m\log m levels = log⁡log⁡n\log\log n levels; work per level Θ(m)Θ(m) → total Θ(mlog⁡m)Θ(m\log m).

  26. Q9. Write short notes on any two of the following: (i) Asymptotic Notations (ii) Clique decision problem (iii) Max-flow-min-cut theorem (iv) Union-Find Algorithm202214m

    Module 1: Introduction

    Write short notes on any two of the following:
    (i) Asymptotic Notations
    (ii) Clique decision problem
    (iii) Max-flow-min-cut theorem
    (iv) Union-Find Algorithm

    View this question on its own page →
    Worked Solution

    Short Notes — Any Two (Covering All Four for Choice)

    (i) Asymptotic Notations (see Q36/Q88)

    OO upper, Ω\Omega lower, Θ\Theta tight, oo/ω\omega strict. Hierarchy O(1)<O(log⁡n)<O(n)<O(nlog⁡n)<O(n2)<O(2n)O(1)<O(\log n)<O(n)<O(n\log n)<O(n^2)<O(2^n). Used to compare algorithms independent of hardware; max rule for polynomials etc.

    (ii) Clique Decision Problem

    Given undirected G(V,E)G(V,E) and integer kk, does GG contain a clique (complete subgraph) of size ≥ kk?

    • NP-complete: In NP (certificate = kk vertices, check all (k2)\binom{k}{2} edges in O(k2)O(k^2)) + NP-hard via reduction from 3-SAT or Vertex Cover (complement: GG has clique kk ⇔ complement Gˉ\bar G has vertex cover V−kV-k). Brute O(Vk)O(V^k) exponential; no known polytime.
    • Applications: clustering, social networks.

    (iii) Max-Flow Min-Cut Theorem (Ford-Fulkerson)

    For network with source ss, sink tt, capacities cc, value of max flow = capacity of min s−ts-t cut (set S∋sS∋s, T∋tT∋t partition, capacity sum of edges S→TS→T). Proof via Ford-Fulkerson: flow maximal iff no augmenting path in residual graph → reachable set from ss defines min cut with equal capacity. Consequences: integrality (if capacities integer, max flow integer), duality for bipartite matching, etc. Used to prove correctness of Ford-Fulkerson O(VE2)O(VE^2) (Edmonds-Karp) etc.

    (iv) Union-Find (Disjoint Set Union, DSU) Algorithm

    Maintains partition of elements into disjoint sets with operations:

    • MakeSet(x): parent[x]=xparent[x]=x, rank/size 0
    • Find(x): with path compression → O(α(n))O(α(n))
    • Union(x,y): by rank/size → O(α(n))O(α(n))
      Sequence of mm ops on nn elements → O(mα(n))O(mα(n)) almost constant (αα inverse Ackermann <5 for practical nn).
      Uses: Kruskal MST cycle check, connected components, offline LCA. Pseudo:
    Find(x): if parent[x]!=x: parent[x]=Find(parent[x]); return parent[x]
    Union(a,b): ra=Find(a), rb=Find(b); if ra==rb return; if rank[ra]<rank[rb] swap; parent[rb]=ra; if rank equal rank[ra]++
    
  27. Q9b. Write a short note on: Recursion tree method20257m

    Module 1: Introduction

    Write a short note on:

    Recursion tree method

    View this question on its own page →
    Worked Solution

    Recursion Tree Method

    Idea

    Visualize recurrence as tree where node = subproblem cost, level = total cost of that recursion depth, sum over levels gives total. Complements Master Theorem and substitution; handles forms master cannot (e.g., n/log⁡nn/\log n, uneven splits).

    General Form T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n)

    • Root cost f(n)f(n)
    • aa children cost f(n/b)f(n/b) each → level1 af(n/b)a f(n/b)
    • a2a^2 nodes cost f(n/b2)f(n/b^2) → level2 a2f(n/b2)a^2 f(n/b^2)
    • Depth log⁡bn\log_b n leaves, leaves count alog⁡bn=nlog⁡baa^{\log_b n}=n^{\log_b a} cost Θ(1)Θ(1) each → leaf level Θ(nlog⁡ba)Θ(n^{\log_b a}).
    • Total T(n)=∑i=0log⁡bn−1aif(n/bi)+Θ(nlog⁡ba)T(n)=\sum_{i=0}^{\log_b n-1} a^i f(n/b^i) + Θ(n^{\log_b a}).

    Example T(n)=3T(n/4)+Θ(n2)T(n)=3T(n/4)+Θ(n^2)

    Level0 cn2cn^2, Level1 3c(n/4)2=3cn2/163c(n/4)^2=3cn^2/16, Level2 9c(n/16)2=9cn2/2569c(n/16)^2=9cn^2/256 → geometric ratio 3/16<13/16<1 → sum dominated by root Θ(n2)Θ(n^2) → T(n)=Θ(n2)T(n)=Θ(n^2) (Case3).

    Example T(n)=2T(n/2)+nT(n)=2T(n/2)+n

    Tree: root nn, level1 2⋅n/2=n2·n/2=n, level2 4⋅n/4=n4·n/4=n → each level nn, depth log⁡n\log n → total nlog⁡n+Θ(n)=Θ(nlog⁡n)n\log n+Θ(n)=Θ(n\log n).

    Steps

    1. Draw tree, label cost per node/level.
    2. Determine depth (until n=1n=1 or constant).
    3. Sum levels; identify geometric vs constant series.
    4. Verify via substitution (induction).

    Advantages

    • Intuitive for guessing bound for substitution proof.
    • Handles non-uniform: T(n)=T(n/3)+T(2n/3)+O(n)T(n)=T(n/3)+T(2n/3)+O(n) → recursion tree shows still O(nlog⁡n)O(n\log n) worst but more complex.

    Use: Guess T(n)T(n) then prove via substitution method for rigor.

  28. Q9d. Write a short note on: Asymptotic Notations20257m

    Module 1: Introduction

    Write a short note on:

    Asymptotic Notations

    View this question on its own page →
    Worked Solution

    Asymptotic Notations — Short Note

    Purpose

    Describe algorithm growth for large nn, abstracting constants/hardware, classifying efficiency (as in Q36/Q88).

    Definitions

    • O(g(n))O(g(n)): ∃c,n0:0≤f(n)≤c⋅g(n)∃c,n_0: 0≤f(n)≤c·g(n) ∀n≥n0∀n≥n_0 — at most gg (upper bound).
    • Ω(g(n))Ω(g(n)): ∃c,n0:0≤c⋅g(n)≤f(n)∃c,n_0: 0≤c·g(n)≤f(n) — at least gg (lower bound).
    • Θ(g(n))Θ(g(n)): ∃c1,c2,n0:c1g≤f≤c2g∃c_1,c_2,n_0: c_1g≤f≤c_2g — tight bound (O∩ΩO∩Ω).
    • o(g)o(g): ∀c>0∀c>0 eventually f<c⋅gf<c·g — strict upper (f/g→0f/g→0).
    • ω(g)ω(g): strict lower (f/g→∞f/g→∞).

    Examples

    • 3n2+2n+1=Θ(n2)=O(n2)=O(n3)=Ω(n2)=Ω(n)3n^2+2n+1 = Θ(n^2)=O(n^2)=O(n^3)=Ω(n^2)=Ω(n).
    • nlog⁡n=o(n2)n\log n = o(n^2), 2n=ω(n100)2^n = ω(n^{100}).

    Hierarchy

    O(1)⊂O(log⁡n)⊂O(n)⊂O(nlog⁡n)⊂O(n2)⊂O(2n)⊂O(n!)O(1)⊂O(\log n)⊂O(n)⊂O(n\log n)⊂O(n^2)⊂O(2^n)⊂O(n!) — helps choose algorithm (e.g., merge nlog⁡nn\log n vs insertion n2n^2).

    Properties

    Transitivity, reflexivity, symmetry for ΘΘ, transpose f=O(g)⇔g=Ω(f)f=O(g)⇔g=Ω(f), sum rule f1+f2=O(max(g1,g2))f_1+f_2=O(max(g_1,g_2)).

    Use: Analyze worst/best/average cases; e.g., linear search worst Θ(n)Θ(n), binary Θ(log⁡n)Θ(\log n), quicksort avg Θ(nlog⁡n)Θ(n\log n).