Unit 1 of 4 · BCA Sem 4

Unit 1: Complexity analysis and recursion

Design and Analysis of Algorithm notes · PTU syllabus (UGCC2519)

3 min read5 topics9 exam questions
On this page
  1. Unit summary
  2. Design and performance analysis
  3. Asymptotic notations
  4. Analysis of basic algorithms
  5. Recursion and recurrence relations
  6. The Master theorem
  7. Key terms
  8. Quick revision
  9. Important questions

Unit summary

An algorithm is only useful if it is efficient. This unit teaches how to measure efficiency — time and space complexity with asymptotic notation — how to analyse common algorithms, and how to analyse recursive algorithms using recurrences and the Master theorem.

After this unit you can

  • Define time and space complexity and use O, Ω and Θ
  • Analyse sequential search, bubble, selection and insertion sort and matrix multiplication
  • Write recurrences for recursive algorithms
  • Solve recurrences using the Master theorem

PTU syllabus topics

  • Design and performance analysis of algorithms
  • time and space complexity
  • asymptotic notations (O, Ω, Θ)
  • analysis of sequential search/bubble sort/selection sort/insertion sort/matrix multiplication
  • recursion basics
  • analysis of recursive algorithms
  • Master's theorem
Key formulasMaster's theorem: T(n) = aT(n/b) + f(n)
  • Case 1

    f(n) smaller than n^(log_b a) → T(n) = Θ(n^(log_b a))

  • Case 2

    f(n) equal to n^(log_b a) → T(n) = Θ(n^(log_b a) log n)

  • Case 3

    f(n) larger → T(n) = Θ(f(n))

  • Merge sort example

    T(n) = 2T(n/2) + n → Θ(n log n)

1

Topic 1

Design and performance analysis

An algorithm is a finite sequence of unambiguous steps that solves a problem. Designing one involves understanding the problem, choosing a strategy (divide and conquer, greedy, dynamic programming …), proving correctness and analysing efficiency.

  • Time complexity: how the number of basic operations grows with input size n.
  • Space complexity: how much extra memory is needed.
  • Analysis is done for the best, average and worst cases.
2

Topic 2

Asymptotic notations

NotationMeaningRead as
O(g(n))f(n) ≤ c·g(n) for large nUpper bound (at most)
Ω(g(n))f(n) ≥ c·g(n) for large nLower bound (at least)
Θ(g(n))Both O and ΩTight bound (exactly)

Growth order: 1 < log n < n < n log n < n² < n³ < 2ⁿ < n!

Example

f(n) = 3n² + 5n + 2 is Θ(n²): for large n the n² term dominates and constants are ignored.

3

Topic 3

Analysis of basic algorithms

AlgorithmBestWorst
Sequential (linear) searchO(1)O(n)
Bubble sortO(n) with a swap flagO(n²)
Selection sortO(n²)O(n²)
Insertion sortO(n)O(n²)
Matrix multiplication (n × n)O(n³)O(n³)

Exam tip

Count the number of times the innermost statement runs. Two nested loops each running n times usually mean O(n²).

4

Topic 4

Recursion and recurrence relations

A recursive algorithm's time is described by a recurrence relation.

  • Factorial: T(n) = T(n − 1) + c → O(n)
  • Binary search: T(n) = T(n/2) + c → O(log n)
  • Merge sort: T(n) = 2T(n/2) + cn → O(n log n)

Recurrences can be solved by substitution, the recursion-tree method, or the Master theorem.

5

Topic 5

The Master theorem

For T(n) = a·T(n/b) + f(n), with a ≥ 1 and b > 1, compare f(n) with n^(log_b a):

Key formulasMaster theorem cases
  • Case 1

    If f(n) = O(n^(log_b a − ε)), then T(n) = Θ(n^(log_b a))

  • Case 2

    If f(n) = Θ(n^(log_b a)), then T(n) = Θ(n^(log_b a) log n)

  • Case 3

    If f(n) = Ω(n^(log_b a + ε)) and regularity holds, then T(n) = Θ(f(n))

Example

Merge sort: a = 2, b = 2, n^(log₂2) = n and f(n) = n → Case 2 → Θ(n log n). Binary search: a = 1, b = 2, n⁰ = 1 and f(n) = 1 → Case 2 → Θ(log n).

Key terms

Time complexity
Growth of running time with input size
Big-O
Asymptotic upper bound
Theta
Asymptotic tight bound
Recurrence relation
An equation defining T(n) in terms of smaller inputs
Master theorem
A formula for solving divide-and-conquer recurrences

Quick revision

  • O upper, Ω lower, Θ tight.
  • Nested n-loops → O(n²); matrix multiply → O(n³).
  • Merge sort T(n) = 2T(n/2) + n = Θ(n log n).
  • Master theorem: compare f(n) with n^(log_b a).

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.Define time and space complexity.
  2. Q2.Differentiate between Big-O and Theta notation.
  3. Q3.What is the worst-case complexity of insertion sort?
  4. Q4.Write the recurrence for binary search.
  5. Q5.State the Master theorem.

Long-answer questions

  1. Q1.Explain asymptotic notations with graphs and examples.
  2. Q2.Analyse bubble, selection and insertion sort in the best and worst cases.
  3. Q3.Explain how recursive algorithms are analysed using recurrence relations.
  4. Q4.Solve T(n) = 2T(n/2) + n, T(n) = 4T(n/2) + n and T(n) = T(n/2) + 1 using the Master theorem.

Stuck on this unit?

Message SBS on WhatsApp for help with Design and Analysis of Algorithm, or to ask about studying BCA at Synetic.

WhatsApp us