session: "5101-design-analysis-of-algo/2026-09-06 0000 DAA 06 Sep"
title: "DAA 06 Sep"
date: "2026-09-06 00:00"
duration: "1h 24m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 2
transcript_chars: 49884
generated: "2026-11-23T23:37:27+05:30"
DAA 06 Sep
Overview
This class completes the asymptotic-analysis toolkit begun earlier: it recaps insertion sort's cost analysis and quadratic running time, formalizes loop invariants (initialization, maintenance, termination) and applies them to insertion sort and linear search, then defines big O (upper bound) and big omega (lower bound) and their equivalence with theta (Theorem 3.1 from CLRS). It closes by introducing little o and little omega as strict, non-asymptotic bounds used when f(n) and g(n) are not comparable. This sits at the end of the asymptotic-notation block, immediately before divide and conquer and the master theorem / recursion tree methods for recursive algorithms.
Learning outcomes
- Analyze insertion sort's nested loops and justify its f(n) = a n² + b n + c (order n²) cost model, including why C4 dominates.
- State the three loop-invariant conditions and use them to argue correctness of insertion sort and linear search.
- Define big O and big omega formally (including the Sedgewick/SOM-series phrasing) and prove membership for a given polynomial.
- State and use Theorem 3.1: f(n) = theta(g(n)) iff f(n) = O(g(n)) and f(n) = omega(g(n)).
- Distinguish big O/omega (equality allowed) from little o/little omega (strict, not asymptotic bounds).
- Explain why best, average and worst cases require separate notations, and pick the right notation for a stated case.
Core concepts
Insertion sort recap and cost analysis
Two nested loops: start from the second position, compare with the previous one; then third with second and first; fourth with third, second and first; and so on. A loop inside a loop means the complexity will most likely touch order n². Comments impose no cost (not executed at runtime). Among the step costs, the loop/comparison step imposes the high-order complexity: C1 and C4 are maximum, and C4 is much much greater than C1, which is further much greater than all other costs — because C4 has two comparisons (j greater than zero, and current element less than a of j) while the for loop performs only one comparison. This motivates modeling the algorithm by f(n) = a n² + b n + c.
f(n) / T(n) as a quadratic
The function best representing the algorithmic structure is f(n) = a n² + b n + c, a quadratic expression, which primarily imposes a computational complexity of order n² if the term a is positive. The highest-degree term with a positive constant plays the pivotal role in determining theta, big O and big omega, because the complexity shifts toward that term when n is very very large.
Loop invariant
A loop invariant holds if the order of the element / property of the algorithm / output of the algorithm is not affected by the loop — no impact on the actual behavior of the algorithm while different loops run. Three conditions:
- Initialization: the condition holds before the initial iteration of the loop.
- Maintenance: if the condition is true before a loop iteration, it persists before the subsequent iteration.
- Termination: upon conclusion of the loop, the invariant furnishes an important feature demonstrating correctness of the algorithm.
If an algorithm holds all three properties, we can say the algorithm is correct; loop invariant is a famous mechanism to check whether an algorithm is correct.
Loop invariant for insertion sort
Start with the second index and initialize the first index as key; a single element is always sorted, so initialization holds the sorting nature even before the algorithm starts. At step 4, the first, second, third, fourth entries are sorted irrespective of which elements are present; after step 3, 3 elements are sorted; after step 2, 2 elements are sorted; after step 6, 6 elements are sorted — the property is maintained even when the loop is not fully executed (maintenance). After full execution we get the sorted result (termination).
Loop invariant for linear search
Target is to identify a key. Initialization: just declaration of the key (we have not identified the element at index one). Maintenance: however far we have gone without achieving the key, there is no chance the element is present in the portion already traversed. Termination: we have achieved the key or not achieved it at all.
Big O notation (upper bound)
For a given known function g(n), we denote big O of g(n). It confirms the worst case — the maximum time the algorithm will require. Theta used two constants c1, c2 with c1 g(n) less than f(n) less than c2 g(n) greater than 0 for some n0; big O confines to the upper bound only: f(n) less than or equal to c g(n) (only one c). It is the half portion of what was done for theta notation. Using big O we can often describe running time merely by inspecting the algorithm's overall structure — e.g., insertion sort's double nested loop immediately gives an order n² upper bound on the worst-case running time.
Sedgewick (SOM series) definition of big O
Suppose f(n) and g(n) are functions defined on the positive integers (n is input size, so of course a positive integer) with the property that f(n) is bounded by some multiplier of g(n) — that multiplier is c — for almost all n (i.e., n beyond n0). That is, there exists a positive constant n0 and a positive number c such that for n greater than n0 we have f(n) less than c g(n).
Big omega notation (lower bound)
The remaining half of theta notation; it identifies the minimum time the algorithm will require. For some c g(n) we assure f(n) will always be greater than this. Definition (given in Corman): f(n) = omega of g(n) if and only if there exists a positive number n0 and a positive number c such that f(n) is greater than or equal to c g(n) for all n greater than n0.
Relation between the three notations (Theorem 3.1)
If f(n) = theta of g(n), then f(n) is big O of g(n) and f(n) is omega of g(n). Conversely, if f(n) is big O of g(n) and f(n) is omega of g(n), then f(n) = theta of g(n). Both directions are true. Theta imposes both bounds (upper and lower), concluding the complexities lying inside; big O is the upper bound; big omega is the lower bound. Theta notation is for average, big O for worst.
Why three separate notations
The same asymptotic bound (e.g., order n² for insertion sort) behaves differently depending on input behavior — least comparisons (best case), in-between comparisons (average), worst case — so best, average and worst cases must be identified to represent all those characteristics. Computational complexity depends on the characteristics of the input data, not only input size: whether the element is at the first position, second, middle or last gives the three cases.
Little o and little omega
The asymptotic upper bound provided by big O may or may not be asymptotically tight; little o is concerned with the tighter/stricter bound. When equality is removed the bound becomes tighter or strict: at no point of time should f(n) and g(n) be comparable (equal). Little o considers g(n) greater than f(n) (g(n) very large / just large). Little omega is the reverse — g(n) much less than f(n); same condition as omega but with no equality sign, so it is a stricter bound and at no point will f(n) equal g(n) or be comparable. Little o and little omega give strict bounds but are not asymptotic bounds, because asymptotic analysis is more keen on how the algorithm performs as n tends towards infinity.
Asymptotic analysis assumptions
Applicable for positive functions on integer input values; theta, big O and big omega are also called asymptotic bounds. In asymptotic analysis there is a restriction that n is very very large and f(n) by g(n) should be equal to 1 when n tends to infinity; if this is violated we go for little o and little omega, but that will not be an asymptotically tighter bound. Asymptotic analysis assumes monotonically increasing behavior: increasing input size cannot decrease computational complexity. n is the number of inputs supplied to the algorithm, so it is a positive integer — never fractional, never negative; c1 and c2 (the multipliers) are positive.
Theoretical vs execution; same-complexity tie-breaks
Asymptotic analysis comes from theoretical analysis of the algorithm, not from the code side; it bridges the theoretical gap and the execution gap, letting you identify computational complexity even before execution of the code. If a wrong choice of g(n) is made, you will never correctly conclude the computational complexity. The notations are just nomenclature — the central idea is the order of complexity (n², n log n, log n, n³, n⁴); big O, big omega and big theta are the ones that appear, little o and little omega are for completion and will very rarely appear. When two algorithms share the same big-O complexity, other factors decide: merge sort and quicksort are both order n log n, but merge sort uses extra space during execution while quicksort does not (hence "quick"); then space complexity and the ignored lower-order terms come into the picture.
Formulas and maths
- f(n) = a n² + b n + c — quadratic cost model of insertion sort; a, b, c are the step-cost coefficients, and the complexity is order n² if a is positive.
- Theta notation: c1 g(n) less than f(n) less than c2 g(n) greater than 0, for some n0; written f(n) = theta of g(n) — c1, c2 are positive multipliers bounding f between two multiples of g beyond n0; used for the average case.
- Big O: f(n) less than or equal to c g(n); written f(n) = big O of g(n) — c is a single positive multiplier and n0 the threshold; used for the worst case (upper bound).
- Big omega: f(n) greater than or equal to c g(n) for all n greater than n0 — c is a positive multiplier, n0 the threshold; used for the best case (lower bound).
- Theta ⇒ big O and big omega; big O and big omega ⇒ theta — Theorem 3.1, the equivalence of theta with the two one-sided bounds.
- For a quadratic expression a n² + b n + c: complexity is big O of n²; to estimate c and n0 use c = a + mod of b and n0 = max of 1, minus b by a (either 1 or minus b by a) — shortcut for proving the bound.
- Example expression: 8n³ − 576n² + 832n − 28; assume g(n) = n³ (high-order term); require 8n³ − 576n² + 832n − 28 less than c n³, giving c greater than or equal to 8 − 576/n + 832/n² − 28/n³; as limit n tends to infinity, c greater than 8; take c = 9 (or 65/8, or even 8) — worked big-O proof.
- Theorem 3.1: f(n) = theta(g(n)) if and only if f(n) = big O(g(n)) and f(n) = omega(g(n)) — the formal statement from the third chapter of Coorman (CLRS).
- Example: f(n) = 8n³ + 6, g(n) = n⁴ — as limit n tends to infinity, (8n³ + 6)/n⁴ → 1/infinity → 0, so f and g are not comparable; for larger n, f(n)/g(n) = 0, not an applicable condition in asymptotic notation.
- Example: f(n) = 723n² + 32n + 76, g(n) = n³ — holds for smaller n but never for larger values because complexity is dominated by the higher-degree term; thus f(n) = little o of g(n), a strict bound but not an asymptotic bound.
- Example: f(n) = 2n, g(n) = n² — f(n) = little o of g(n); as n tends to infinity, 2/n → 0.
- Little omega: when f(n) is larger than g(n), the ratio tends to infinity as n tends to infinity — the strict lower-bound condition.
- Example: f(n) = n², g(n) = n ⇒ f(n) = little omega of g(n).
- Complexity table: linear search order n; insertion sort order n²; binary search order log n; bubble sort order n²; merge sort n log n.
Worked examples
- Insertion sort cost analysis: two loops; C4 has two comparisons (j greater than zero; current element less than a of j), the for loop only one comparison; C4 much much greater than C1, which is much greater than all other costs.
- Loop invariant on array 2, 4, 3, 6, 8, 9: Initialization — subarray array 1 comprises only one element, ARR1, the original element in array 1, inherently sorted, affirming preservation of loop invariance. Maintenance — the for loop shifts elements one position right until the correct position is identified, then inserts the value (the ith element — fourth, third, sixth, seventh); the sub portion is sorted. Termination — after completion of entire execution we get sorted array ARR.
- Big O example: 8n³ − 576n² + 832n − 28 equal to 0; g(n) assumed n³; c greater than or equal to 8 − 576/n + 832/n² − 28/n³; limit gives 8; take c = 9 (or 65/8, or even 8) so f(n) less than or equal to c g(n) holds. For n0, use hit and trials (e.g., 20, 30, or 1, 2) to find when the expression becomes positive. General shortcut: for a polynomial of degree d with the highest-degree term having a positive constant, the complexity is order of that term.
- Omega example reference: "f(n) should be greater than 18n" — for that case f(n) = omega of n (as stated).
- Story example (from earlier class): searching for the word "text" versus the word "you" — finding "text" is very easy, finding "you" is difficult; illustrates dependence on input characteristics.
- 8n³ + 6 vs n⁴: ratio → 0 as n → infinity (not comparable).
- 723n² + 32n + 76 vs n³: holds only for smaller n; dominated by higher-degree term for large n ⇒ little o.
- 2n vs n²: 2/n → 0 ⇒ little o.
- n² vs n: ratio → infinity ⇒ little omega.
- Insertion sort (as stated in class): maximum time — big O of n²; least time — big omega of n²; average case — theta of n².
- Merge sort vs quicksort: both order n log n; merge sort uses extra space, quicksort does not.
- Bubble sort: its computational complexity order is n² (the transcript's "n cube" remark is a hypothetical illustration of a wrong claim).
Questions asked in class
- Q (Rithika): In theta notation we put the condition that limit n tends to zero [unclear — likely infinity] of f(n) by g(n) should be equal to one; will that condition be forwarded to big O notation as well? A: Yes, hundred percent; if something is theta it will obviously be big O. We try to identify the least value of c, but you can also take bigger because we target from a set of values; different c gives a different value from the same set.
- Q (same student): If f(n) has a higher-degree polynomial of order three, and we take g(n) of order four, will that be the right thing? A: We will discuss in little o and little omega what the condition could be if f(n) by g(n) is not comparable; that will not be the asymptotically tight bound. We will complete big O, then big omega, then little o and little omega, and discuss the situation of mismatched f(n), g(n).
- Q (same student): The condition f(n) by g(n) equal to one when n is very very large — it should hold in all three cases? A: Yes, f(n) by g(n) should be one.
- Q: About loop invariance for linear search — if the searched variable is not there in the array, how do the first/second/third conditions hold? A: First we declare a key and the loop has not started, so we are not comparing — at that stage we haven't identified the array, that is initialization. If we reached the fifth or sixth index, we are certain the element is not there before the sixth index; as we proceed there is a chance we get it or not. Invariance comes in the sense that initialization: element was not found; as we proceed we ensure the element will never appear in the left half, it will appear in the right half; at the last either we identified the element or did not identify it at all. Primary thing: the algorithm should be correct for the sub part or sub problem.
- Q (follow-up): If in best linear search the element is at the first or second location, the algorithm terminates at the second iteration — then maintenance and termination? A: That is the termination part; if we identified the value at first index, maintenance and termination will be the same because the loop was concluded in one iteration.
- Q (Shubhi): About assignment and mid-sem exam — when will the mid-sem be, will it be a Google form? A: You may have received the schedule; on that I cannot comment. The type of questions primarily will be objective type. Do the first assignment — probably starting in some days — then you will find what type of questions I will ask; primarily from the slides and from our discussion only, not beyond that. When the mid-sem happens you will get the notification; dates are already disclosed.
- Q: In theta notation we ensured two guard rails (upper and lower bounds) within which the function should sustain; here there is only an upper bound, no lower bound — how are we ensuring n0 always makes the function positive and it doesn't go negative, and what is the relevance of the upper bound when any value above f(n) can be c? A: c is required — I will take nine, you can take 8.5, someone take 20; that is c, because f(n) is not a single value, f(n) is obtained from a set of possible values. Theta notation is