IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

DAA 30 Aug

30 Aug 20261h 26m2 parts

Source: IITP-big-files\Sem-1\ECS5101-DAA\Lectures\2026-08-30 0000 DAA 30 Aug\summary\notes.md

Overview

This class completes the recap of algorithm-analysis principles (sequential, selection and iterative logic; analysis as a function of input size; dominating operation; search space and best/average/worst cases) and moves into growth of functions and asymptotic analysis. The main focus is Theta notation (sandwich notation): its formal condition, how to choose g(n), how to compute c₁, c₂ and n₀, and why f(n)/g(n) → 1 as n → ∞ is a mandate condition. Worked examples include 3n + 3, 18n + 9, 3n² − 9n + 17 and ½n² − 3n, plus a general quadratic formula for c₁, c₂, n₀. The class closes with the practical applicability of asymptotic analysis and course logistics; insertion sort and the remaining notations (big O, big Omega, little o, little omega) are deferred.

Learning outcomes

  • Distinguish sequential, selection and iterative logic, and identify all three within a single piece of code.
  • Explain why algorithm analysis is expressed as a function f(n) of input size, and identify the dominating operation and the search space.
  • State the formal Theta condition and explain the roles of c₁, c₂ and n₀.
  • Choose an appropriate g(n) for a given f(n) and justify it via the condition f(n)/g(n) → 1 as n → ∞.
  • Compute c₁, c₂ and n₀ for linear and quadratic expressions, including cases where f(n) is negative for small n.
  • Apply the quadratic formula for c₁, c₂, n₀ when the highest-degree coefficient is positive.
  • Explain why n₀ = 0 is not used and why a negative highest-degree term rules out asymptotic analysis.

Core concepts

Three types of logic

  • Sequential logic: the logic written is executed one after another, one after another.
  • Selection logic: conditional flow — instructions may jump or terminate the algorithm depending on whether a condition is met. Examples: if (single alternative), if with else (double alternative), switch, and if-else if-else if-else ... (multiple alternative).
  • Iterative logic: repetitive flow — the same behavior is repeated again and again unless a specific condition is met. A for loop uses an index variable; a repeat statement is followed by a module called the body of the loop.
  • Why it matters: all three can appear in the same code. Example: int i = 1; (sequential) followed by a for loop with i from 1 while i <= 10, i++, printing i in the body — the repeated body execution is iterative logic, and the condition i <= 10 is selection logic.

Algorithm analysis as a function of input size

Any algorithm analysis is performed by assuming the algorithm as a function of input size, f(n). Input size is always a positive integer; as input size increases, the computational complexity of the algorithm increases. The analysis is theoretical and platform independent — it does not depend on which machine the code is run on.

Dominating operation

To determine computational complexity we must think about the key/dominating operation in the algorithm, e.g. the number of comparisons in searching and sorting algorithms. Multiplication dominates over addition and subtraction. In this course, algorithms primarily using searching or sorting concepts will be seen.

Search space and best/average/worst cases

The search space is the set of elements for sorting, or the array where the key value has to be identified; it plays a crucial role in determining best, average and worst case complexity. - Best case: the element is found at the initial location. - Worst case: the element is found at the last location or not at all, so all comparisons must be made till the last. - Average case: in between; the expected value assuming all locations are equally probable. Estimation of the average case is a little bit challenging.

Well-known functions g(n)

Well-known functions are those everyone knows — n², n³, n log n, log n, 2ⁿ — represented by the symbol g(n). f(n) represents the algorithm in terms of input size n; the aim is to identify the computational complexity of an algorithm closer to g(n), i.e. to reach a situation where f(n) can be represented in the form of g(n). - 3n + 3 is not a well-known function, but everyone knows n; if 3n + 3 can be resembled as n for a very large n, then the behavior of linear search is of order n. - If the algorithm behavior is 5n² + 3n + 2, it will resemble n².

Efficiency ordering

If computational complexity goes towards log n, the algorithm is more efficient. For input size 100, the number of key operations is only 7 for log n, whereas for 2ⁿ it is 10³⁰. In the case of trees and graphs, the complexity touches log n — that is the beauty of these data structures and why they are very popular.

Asymptotic analysis

Identifying the behavior of the algorithm when n is very very large, i.e. when n tends to infinity. Motivation: an algorithm/website may tolerate a simultaneous hit of 5000 persons and work fine when tested for 10, 20, 30, 1000, 1500, 2000, but start failing or showing delays at 10,000 — we cannot check behavior for a varied set of inputs, so we determine behavior as n tends to infinity.

Asymptotic notations

Big O, big Omega, big Theta, little o, little omega. Big O, big Omega and theta are only for representation/nomenclature; the basic idea is to establish how the algorithm behaves. If we can identify what order the algorithm follows, we can declare it in theta, big O, or big Omega notation.

Theta notation (sandwich notation)

Theta notation is also known as the sandwich notation because the function is bounded by c₁ and c₂ on both sides (upper and lower bound). Theta notation best represents the average case. The mandate condition is f(n)/g(n) = 1 when n tends to infinity — i.e., f(n) and g(n) are "very similar to each other." If this fails (e.g., comparing 3n + 3 with n², which forces c₁ ≤ 0), the Theta condition is violated.

Why f(n) must be positive

There is no algorithm whose computational complexity decreases with increase in input size; any algorithm's complexity increases with input size. Hence the target is to identify a positive function that best represents the algorithm.

n₀

n₀ is a specific value beyond which the function f(n) is positive and we are certain the claimed behavior persists throughout (till infinity). Before n₀ we cannot claim the behavior will persist. The target is to make the function monotonically increasing so that the established computational complexity is valid till infinity. n₀ is not given; it must be determined. n₀ = 0 is not taken because n is the number of inputs to the algorithm and the algorithm accepts at least one input; analyzing behavior at input size zero has no significance.

Negative highest-degree term

If the highest-degree term is negative in any condition, asymptotic analysis cannot be performed, because ultimately the function is negative.

Applicability

The analysis is directly applicable to algorithms where the dominating operation is comparison (insertion sort, selection sort, merge sort, binary search, bucket sort). For operations like multiplication or tensor multiplication in ML/AI, the same ideas apply with modification — f(n) may be of order n⁴ etc. The general behavior is the same: complexity increases with input size. The analysis identifies the nature of the algorithm (quadratic, linear, logarithmic, cubic, etc.) and creates a base to determine computational complexity of any known algorithm; one must identify the segments/components imposing computational complexity.

Formulas and maths

Theta condition: $$c_1 g(n) \le f(n) \le c_2 g(n) \quad \text{for all } n \ge n_0$$ c₁, c₂ are positive constants and n₀ is the threshold beyond which the bound holds. Used to establish f(n) = Θ(g(n)).

Set membership: $$f(n) \in \Theta(g(n))$$ Read as "f of n is theta of g of n." Since many positive values of c₁ and c₂ can be taken, all such answers are correct — there is no fixed c₁, c₂.

Mandate condition for choosing g(n): $$\lim_{n \to \infty} \frac{f(n)}{g(n)} = 1$$ Used to check that g(n) resembles f(n) even for very large n; e.g. for 18n + 9, n² is never considered because f(n)/g(n) will never equal 1 as n → infinity.

Determining c₁, c₂ (example 3n + 3, g(n) = n): $$c_1 n \le 3n + 3 \le c_2 n \;\Rightarrow\; c_1 \le \frac{3n+3}{n}$$ Taking the limit n → infinity gives 3 (since something by infinity is 0), so c₁ ≤ 3; c₂ > 3. Used to pick concrete constants (chosen: c₁ = 2, c₂ = 4; fractional positive values are allowed).

Determining n₀: $$3n + 3 = 0 \Rightarrow n = -1; \qquad 3n - 3 = 0 \Rightarrow n = 1$$ Solving where the function becomes positive gives the threshold. If f(n) is positive, n₀ = 1. If f(n) = 3n − 3, at n = 1 it is 0 and at n = 2 it becomes positive, so n₀ = 2. For 18n − 72, n must be greater than 4 (n = 5) for the function to become positive.

Example 18n + 9 with g(n) = 3n: $$c_1 \cdot 3n \le 18n + 9 \le c_2 \cdot 3n \;\Rightarrow\; c_1 \le \frac{18n+9}{3n} = 6 + \frac{3}{n}$$ As n → infinity this tends to 6, so c₁ can be 6 or less; c₂ is one value higher. Used to conclude 18n + 9 = Θ(3n); if 3n were not given, it is equivalent to n.

Failure of g(n) = n² for f(n) = 3n + 3: $$c_1 < \frac{3n+3}{n^2} = \frac{3}{n} + \frac{3}{n^2}$$ As n → ∞, c₁ ≤ 0 — fails (c₁ must be positive). Used to show n² is not a valid g(n).

Upper bound for 3n + 3 with g(n) = n: $$3n + 3 \le c_2 n \;\Rightarrow\; c_2 \ge \frac{3n+3}{n} = 3 + \frac{3}{n}$$ As n → ∞, c₂ ≥ 3; any value ≥ 3 works (e.g. c₁ = 2 or anything, for computational ease). Used to fix the upper constant.

f(n) = 3n² − 9n + 17: $$c_1 < 3 - \frac{9}{n} + \frac{17}{n^2}$$ So c₁ should be less than 3; example choice c₂ = 2 [as transcribed]. Used to conclude 3n² − 9n + 17 = Θ(n²) for all n₀ ≥ 3, with c₁, c₂ any positive values estimated beyond n₀ > 3.

½n² − 3n = Θ(n²): $$c_1 < \tfrac{1}{2}, \quad c_2 > \tfrac{1}{2}, \quad n > 7$$ Used to establish the Theta bound for this expression.

Quadratic formula (for a quadratic expression with coefficients a, b, c), applicable only when a > 0 (the high-order term must have a positive constant): $$c_1 = \frac{a}{4}, \qquad c_2 = \frac{7a}{4}, \qquad n_0 = 2 \cdot \max\left(\frac{|b|}{a}, \frac{|c|}{a}\right)$$ Stated as "2 into max of mod b irrespective of the sign by a and ... mod c by a" (with a comma between the two terms). Used to read off constants directly for quadratics; do not cram the formula — the technique shown is more than enough.

Cost evaluation: $$f(n) = 3n + 3: \quad n = 100 \Rightarrow 300 + 3; \quad n = 10{,}000 \Rightarrow 3{,}000 + 3$$ Used to show the concrete cost at a given input size.

Worked examples

  • For loop example: for (i = 1; i <= 100; i++) in C — continues 100 times (iterative logic).
  • Loop structure (general): initialization (e.g. k initialized), then check a specific condition (e.g. k > s), then the body of the loop, then increment k by a factor t (usually 1); continues unless the condition is met — if k > s, we stop. Similarly a while loop with a condition (e.g. i < 10) executes some module.
  • Growth comparison at input size 100: log n gives only 7 key operations; 2ⁿ gives 10³⁰. If the algorithm is at log n it is acceptable; at the next positions acceptable; at 2ⁿ it is [unclear] required.
  • Theta example 1: f(n) = 3n + 3, g(n) = n → c₁ = 2, c₂ = 4, n₀ = 1; hence linear search f(n) = Θ(n).
  • Theta example 2: f(n) = 18n + 9, g(n) = 3n → c₁ ≤ 6 + 3/n, limit gives 6; c₂ greater than c₁; n₀ ≥ 1; hence 18n + 9 = Θ(3n).
  • Negative-function n₀ examples: 3n − 3 → n₀ = 2; 18n − 72 → n > 4 (n = 5).
  • Hypothetical: algorithm behavior 5n² + 3n + 2 resembles n²; 5n² + 39n + 9 tends toward order n².
  • f(n) = 3n + 3, g(n) = n²: n₀ = 1 (function is positive). c₁ < 3/n + 3/n² → c₁ ≤ 0 as n → ∞, violating the positive-constant requirement — so n² is not a valid g(n).
  • f(n) = 3n + 3, g(n) = n: c₂ ≥ 3 + 3/n → c₂ ≥ 3; c₁ can be 2 or anything (chosen for ease). Behavior of 3n + 3 is equivalent to n.
  • f(n) = 18n + 9 (example sheet, slide 29): n₀ = 1 (not 0, since input size must be at least 1; at n = 0, f(n) = 9 is positive but not meaningful). For c₂: 18n + 9 over n less than c₂ gives c₂ should be greater than 6 [as transcribed: "there is 3n ... C2 should be greater than 6"]; they take 9 (7 would also do — anything more than 6). Once the lower bound c₁ = 6 is found, c₂ can be anything more than six.
  • f(n) = 3n² − 9n + 17: hit-and-trial: n = 1 → negative; n = 2 → negative; n = 3 → 3·9 − 9·3 + 17 (positive, but "n component is missing" [unclear]); n = 4 → 3·16 − 36 + 17 = 48 − 36 + 17 = 12 + 17 (positive). So for any value ≥ 3 the function is positive; n₀ ≥ 3. Then c₁ < 3 − 9/n + 17/n², c₁ < 3; example c₂ = 2. Result: Θ(n²) for n₀ ≥ 3.
  • ½n² − 3n = Θ(n²): c₁ < 1/2, c₂ > 1/2, n > 7.
  • 5n² + 19n + 276: the 5n² term dominates (after some time), so it resembles the behavior of n².

Questions asked in class

  • Q (Krishna): In the last example, g(n) is given as 3n — is g(n) supposed to be a known function, and should it be explicitly given? A: If it is not given, you have to consider n; otherwise it will be explicitly given.
  • Q: What is the importance of n₀? Earlier you said n₀ should be 2 because for 3n − 3 at n = 1 the function becomes 0, so n₀ should be greater than 1. Where are we using n₀? A: After n₀, if f(n) is positive, we are sure the claim remains the same throughout. Before n₀ we cannot claim the behavior will persist. E.g. for 18n − 72, n must be greater than 4 (n = 5) for the function to become positive. We identify a value beyond which the function is positive and we are certain that the determined c₁, c₂ are correct; otherwise the condition may fail because we are dealing with a negative function.
  • Q: If there is an array with 12 elements, is n = 12? And if we take 10 or 13 dominant operations, is f(n) the number of elements or the dominant operation? A: n is the input size — the entire search space on which the operation is performed (e.g. 20 elements to search a key in; 100 numbers for searching or sorting). f(n) denotes the entire cost the algorithm consumes (e.g. 3n