session: "5101-design-analysis-of-algo/2026-08-29 0000 DAA 29 Aug"
title: "DAA 29 Aug"
date: "2026-08-29 00:00"
duration: "1h 07m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 2
transcript_chars: 38503
generated: "2026-11-23T23:29:39+05:30"
DAA 29 Aug
Overview
This class bridges the recap of mathematical preliminaries (monotonicity, factorial, modulo, Fibonacci, logarithms, exponentials, floor/ceiling) and algorithm control logic (sequential, selection, iterative) into the formal study of algorithmic analysis. It defines algorithmic analysis as evaluating efficiency (time) and scalability, motivates a theoretical, platform-independent model, and introduces running time as a function of input size by counting key/dominating operations. The class derives f(n) for linear search, distinguishes best, average and worst cases, and argues why worst case is usually the focus. It closes by introducing growth of functions and announcing asymptotic analysis (big O, big omega, big theta, little O, little omega) for the next class.
Learning outcomes
- Define algorithmic analysis in terms of efficiency and scalability, and explain why a theoretical, platform-independent model is preferred over rerunning code.
- Identify the key/dominating operation for a given problem (e.g., comparisons in searching/sorting; multiplication over addition).
- Derive f(n) for a simple algorithm by counting operations, including the failed loop-condition check.
- Distinguish best, average and worst case for linear search, and state the probability assumption behind average-case analysis.
- Explain why worst-case complexity is usually reported, and how average case relates to worst case as n → ∞.
- Compare growth of log n, n, n log n, n², n³, 2ⁿ and match an estimated f(n) to a known g(n).
Core concepts
Algorithmic analysis
The process of evaluating an algorithm's performance in terms of efficiency (time taken to run) and scalability (behavior as input size changes — an algorithm made for 10 numbers should also apply to 100, 1000, and so on). It matters because multiple correct codes can solve the same problem, and we need a principled way to compare them. It connects to everything that follows: f(n), case analysis, and asymptotic notation are all tools of this analysis.
Why theoretical, platform-independent analysis
Two people running the same algorithm on a low-configuration vs. high-configuration device see different speeds; likewise HDD vs. SSD for fetching values. Hence we want a theoretical model so we do not have to rerun the code. Theoretical time complexity is analyzed by counting the number of operations as a function of input size (dry run, no execution needed). Other factors mentioned but not covered in detail: memory use (e.g., merge sort uses extra space), parallelism, heuristics and other optimization techniques.
Input size n
For sorting 10 numbers, n = 10; for 20 numbers, n = 20. The input size is always a positive integer; floating/fractional n is not considered. With small n (e.g., n = 10) it is hard to distinguish two algorithms; at large n (e.g., 10,000) the difference becomes clear (compare factorial 5 with factorial 25 — the clock difference will be evident).
Key / dominating operation
The operation that consumes most of the time, defined so that time for other operations is much less than, or proportional to, the time of the key operation. In searching and sorting, the number of comparisons is the dominating operation (assignment is just an address redirection, whereas comparison requires converting both values to binary and comparing bit by bit). Multiplication is more costly than addition: a code with few multiplications and more additions/subtractions is more efficient than one using more multiplications. This is the basis for counting operations to get f(n).
f(n) and complexity
f(n) is a function of input size n showing how the algorithm behaves with respect to key operations over input n. Directly, computational complexity cannot be determined for an algorithm; first f(n) must be determined. Complexity convention: unless otherwise stated or implied, "complexity" refers to runtime (time complexity), not space. Space is measured by counting the maximum memory needed; space required is a multiple of data size (e.g., merge sort has space complexity 2n).
Best, worst and average case
Best case: minimum possible value of f(n) — the element is encountered at the very beginning, so the algorithm stops early. Worst case: maximum value of f(n) for any possible input — all comparisons performed, element at the last position or absent. Average case: the expected value of f(n), lying between best and worst; there is no clear boundary, and the exact middle position cannot simply be declared the average case. Average-case analysis is tricky and assumes a probability distribution; one assumption is that all possible permutations of input data are equally likely (each position probability 1/n). Average case is usually much more complicated to analyze than worst case ("average case is moving here and there in between"). Worst case is the maximum time the algorithm will take; if you know the worst case, performance will be better or equivalent to it — therefore in most cases computational complexity refers to the worst case.
Growth of functions and asymptotic analysis
Once f(n) (e.g., 3n + 3) is determined, it is specific to the algorithm; the task is to identify a well-known function g(n) (log n, n, n log n, n², n³, 2ⁿ, n!) that resembles f(n) for large n. If they are equivalent for higher values of n, "our task is done." Asymptotic analysis (dictionary meaning): "asymptotic approach gives the value as an expression containing the variable tending to infinity" — concerned with behavior as n tends to infinity, with no bound on n. Five notations were announced: big O, big omega, big theta, little O, little omega.
Formulas and maths
f(n) = n + 1 — for a loop for i = 1; i <= 10; i++ printing i: 10 successful iterations plus 1 failed case (i becomes 11); n = number of iterations. Used to count loop executions including the failing condition check.- Expected value (average case) = first position × its probability + second position × its probability + … + last position × its probability — positions weighted by their probabilities. Used when deriving average-case f(n) under a probability assumption (e.g., all probabilities 1/n).
f(n) = 3n + 3 — linear search step count: step 1 runs 1 time; repeat condition n + 1; two internal statements n + n; if runs 1 time and else runs 1 time; overall n + n + n = 3n + 3. Used as the derived f(n) for linear search.f(n) = 1 + (n + 1) + n + n + 1 + 1 = 3n + 4 — aggregate linear search count: initialization 1; while condition n + 1 (fails when k becomes n + 1, or when LOC becomes 1 / LOC > 0); if comparison n; increment n; if/else 1 each. Used as the detailed operation count (constants 3, 4, 5, 7, 8 are all constants).f(n) = 3n + 3 ≈ order of n (and 3n + 4 likewise) — as n tends to infinity the dominating part is n and the rest is eliminated. Used to state linear search's computational complexity.- Average case order
(n + 1)/2, equivalent to n/2 — as n → ∞, n/2 is also infinity, so average and worst case become equivalent for very large n. Used to relate average and worst case. - Growth table as stated: n = 5: log n = 3, n = 5, n log n = 15, n² = 25, n³ = 125, 2ⁿ = 32; n = 10: log n = 4, n = 10, n log n = 40, n² = 100, n³ = 10³; n = 1000: log n = 10, n = 10³, 2ⁿ = 10³⁰⁰ (as spoken). Used to compare growth rates and pick g(n).
Worked examples
- Four students example: problem P given to S1, S2, S3, S4, who write codes C1–C4. All four are correct, bug-free and running. To decide which is superior/inferior, analyze the algorithms theoretically (platform-independent) rather than rerunning.
- P1 and P2 with n = 10: with only 10 elements, the efficiency difference between algorithms a1 and a2 cannot be determined; at n = 10,000 the difference is easily seen.
- Factorial comparison: factorial 5 vs. factorial 25 shows a clear difference in execution time.
- Loop count example:
for i = 1; i <= 10; i++ with print i runs 11 times (10 successful + 1 failed); for 100 it becomes 100 plus 1; f(n) = n + 1. - Loop
i = 0; i < 3: body runs 3 times; after the last increment i = 4, condition 4 < 3 fails — one extra comparison, so counted as running one more time for complexity purposes. - Array search example: array of 7 numbers — 7, 9, 3, 2, 5, 21, 31 (positions 1 to 7). Search 9: only 2–3 comparisons → best situation. Search 21 or 5: some more comparisons → could be an average situation. Search 31: compare with all elements to reach it; search 41: compare with all elements and conclude it is not present → worst situation.
- TEXT story example: in an English story named TEXT, find the occurrence of the three-letter word "the" — found at a very initial stage (best case). Find "Jew" (as transcribed) — possibly not present, so search till the last (worst case). Same text, same algorithm, different behavior. If found somewhere in between, it is the average case.
- Linear search trace: data array with n elements and a key item; find location loc of the item, or set loc = 0 (item not available). Initialization k = 1 and LOC = 0 (one time). Repeat steps 3 and 4 while LOC = 0 and k ≤ n; if item = data[k] … otherwise the loop increments. Step counts: condition n + 1 times, internal statements n times each, increment n times, final check of loc = 0 and the else each one time → f(n) = 3n + 3 → order of n.
- Best case example: element identified at the fifth position → f(n) becomes a constant value like 3 or 2.
- Worst case: element at the tenth/last position → f(n) runs for the whole value or till n.
- Growth comparison: log n stays very small (10 even at n = 1000), showing log-order algorithms are very effective; tree algorithms largely follow log n order; linear search is acceptable (equivalent with the value); most sorting algorithms follow n log n; some algorithms offer n² and n³.
Questions asked in class
- What is loc? — loc is location, just a flag. loc = 0 means we have not obtained the key element / don't know its location; it is declared 0 initially. If the element is encountered, the flag becomes 1, and then we write that the element is present. You can eliminate it if confused; it is not an issue.
- In the for loop with i = 0 and i < 3, you said the loop runs four times — how? — Three times is clear; after the last increment i becomes 4, and the fail condition is also a comparison (
4 < 3). Since computational complexity counts all comparisons performed, the loop runs one extra time (the failed case). - Not getting how we get 3n + 3 / the algorithm — the instructor re-explained the whole linear search and the counting (see Formulas).
- Best case? — For best case f(n) is already there as a function; e.g., considering 3n + 3, but the execution of that function — how many times it runs (one, two, three times) — is lesser. In linear search, if we identify the element at the fifth position, f(n) becomes a constant value like 3 or 2; f(n) runs the whole value or till n when we reach the tenth or last position.
- Logistics: the instructor asked Siddhant to clear the lobby and reminded everyone to join via their ITP ID (avoids lobby waiting and attendance issues). Student queries would not be taken during the theoretical portion; queries would be taken after covering the theoretical material, before starting the mathematical/numerical aspects. A two-to-three minute break was announced; the instructor noted the class was "a little bit theoretical" today but that this is important for algorithmic analysis.
Key takeaways
- Algorithmic analysis = evaluating efficiency (time) and scalability; the primary goal is understanding behavior as input size increases.
- Analysis must be theoretical and platform-independent (machine, HDD vs. SSD must not matter); count operations via dry run instead of executing code.
- Input size n is always a positive integer; small n cannot distinguish algorithms, large n can.
- The key/dominating operation is the one consuming most time: comparisons in searching/sorting; multiplication costs more than addition.
- For
for i = 1; i <= 10; i++, the loop runs 11 times (10 successful + 1 failed); generally f(n) = n + 1. - Linear search f(n) = 3n + 3 (or 3n + 4 in the aggregate count) → order of n.
- Best case = minimum f(n) (element at the start); worst case = maximum f(n) (last position or absent); average case = expected value between them.
- Average-case analysis assumes a probability distribution (e.g., all permutations equally likely, each position 1/n); it is harder than worst-case analysis.
- Worst case is usually reported because it is the maximum time; actual performance is better or equivalent.
- Average case order (n + 1)/2 ≈ n/2 becomes equivalent to worst case as n → ∞.
- Growth order: log n < n < n log n < n² < n³ < 2ⁿ; log n = 10 even at n = 1000.
- The task after finding f(n) is to identify a known g(n) equivalent to f(n) for large n; asymptotic notations (big O, big omega, big theta, little O, little omega) formalize this.
Exam-style questions
1. Define algorithmic analysis and explain why a platform-independent theoretical model is preferred over measuring runtime by execution. Outline: efficiency + scalability; different machines/HDD vs. SSD give different speeds; dry-run operation counting avoids rerunning code.
2. Derive f(n) for linear search and state its computational complexity. Outline: initialization 1; while condition n + 1; if comparison n; increment n; if/else 1 each → 3n + 4 (or 3n + 3); constants vanish → order of n.
3. For the array 7, 9, 3, 2, 5, 21, 31, classify searching for 9, 21, 31 and 41 into best/average/worst case with reasons. Outline: 9 → 2–3 comparisons (best); 21 or 5 → some more comparisons (average); 31 → all elements (worst); 41 → all elements and conclude absent (worst).
4. Why is worst-case complexity usually reported, and how does average case relate to it for large n? Outline: worst case is the maximum time, actual performance better or equivalent; average case harder to analyze, needs probability assumptions; (n + 1)/2 ≈ n/2 → both infinity as n → ∞, so equivalent for very large n.
5. Order the functions log n, n, n log n, n², n³, 2ⁿ by growth and illustrate with values at n = 5, 10, 1000. Outline: log n < n < n log n < n² < n³ < 2ⁿ; n = 5: 3, 5, 15, 25, 125, 32; n = 10: 4, 10, 40, 100, 10³; n = 1000: 10, 10³, 2ⁿ = 10³⁰⁰ (as spoken).
Glossary
- Algorithmic analysis — evaluating an algorithm's performance in terms of efficiency and scalability.
- Efficiency — the time taken by the algorithm to run.
- Scalability — how the algorithm behaves as input size changes.
- Theoretical time complexity — operation count as a function of input size, obtained by dry run.
- Input size n — a positive integer representing the amount of data (e.g., 10 numbers → n = 10).
- Key / dominating operation — the operation consuming most of the time (comparisons in searching/sorting).
- f(n) — function of input size showing behavior with respect to key operations.
- Best case — minimum possible value of f(n).
- Worst case — maximum value of f(n) for any possible input.
- Average case — expected value of f(n) under a probability distribution of inputs.
- loc — a flag: 0 means the key element has not been found; 1 means it is present.
- Asymptotic analysis — analysis of algorithm behavior as n tends to infinity.
- g(n) — a well-known function (log n, n, n log n, n², n³, 2ⁿ, n!) matched against f(n) for large n.
Follow-ups
- Asymptotic notations (big O, big omega, big theta, little O, little omega) — class stopped before starting theta notation; to be continued in the next class, extended by some more time.
- The PPT will be uploaded; students are asked to read it, since the next 6–7 lectures will be difficult otherwise.
- A separate half-hour session in tomorrow's lab to clarify this material.
- Average case estimation (probability/expected-value example) was deferred: "we will discuss with this example."
- Merge sort (and its extra memory use, space complexity 2n) will be covered later; students not aware of merge sort need not worry now.
- Searching and sorting algorithms will be covered later, where comparison is the key operation.
- The instructor will explain at later stages how to design your own algorithm and what relevant problems/topics are (designing vs. analysis).
- [unclear] The statement "An algorithm that is asymptotically more effective will be the best choice for very very small input" — likely intended "very large input," but the transcript says "small"; verify with the PPT.
- [unclear