Overview
This class completes the mathematical foundations needed for algorithm analysis and then introduces algorithms and algorithmic analysis proper. It recaps monotonicity, then covers floor/ceiling, modular arithmetic (including negative numbers), the integer and absolute value functions, exponentials, factorials, Fibonacci, polynomial representation via summation, and logarithms, before giving a simplified definition of an algorithm. The second half covers modular programming, a first algorithm (finding the largest element in an array), the components of an algorithm (step, control, exit, variable, comment, input/output, assignment), the three flow types (sequential, selection, iterative), and a quadratic-root procedure. It closes with the motivation for algorithmic analysis: efficiency and scalability, running time as a function of input size, and the two critical factors — input size and the dominating operation. Asymptotic analysis (big O, little O, big omega, big theta, little omega) is announced for the next class.
Learning outcomes
- Distinguish monotonic from strictly monotonic functions and explain why monotonic functions matter in complexity analysis.
- Compute floor, ceiling,
int, and absolute value for positive and negative real numbers. - Evaluate
a mod ncorrectly for negativea, and verify congruencea ≡ b mod nviandividesb − a. - State and apply exponential, factorial, Fibonacci, polynomial-summation, and logarithm facts.
- Store a polynomial as a coefficient array plus highest degree and reconstruct it from the summation form.
- Define an algorithm, list its components, and identify sequential, selection, and iterative flow.
- Explain why theoretical analysis is needed, and name the two critical factors determining computational complexity and the two main measures of efficiency (time, space).
Core concepts
Monotonicity and strict monotonicity
A monotonic function behaves similarly throughout — if increasing, it keeps increasing; if decreasing, it keeps decreasing. Strict monotonicity removes the equality sign: at no point are the two values (e.g., m and n) equal. Monotonically increasing functions are popular in algorithmic complexity because as input size increases, the number of computational steps increases side by side, so complexity can be easily determined.
Floor and ceiling
Floor is the greatest integer less than or equal to x; ceiling is the least integer greater than or equal to x, where x is a real number. For positive numbers, floor is the integer part and ceiling is the next integer; for negative numbers, ceiling is the integer part and floor is the next lowest number on the number line. These underpin the int function and later asymptotic bounds.
Modular function and congruence
a mod n identifies the remainder when a is divided by n; in general form k = mq + r, where r is the remainder. Congruence: if a mod n = b mod n, we write a ≡ b mod n, which holds if and only if n divides b − a (the remainder of (b − a) divided by n is 0), with a, b, n integers; you may take a larger a and smaller b or vice versa. This is not the triangle congruence of 9th/10th; it appears indirectly in algorithm analysis.
Integer and absolute value functions
The int function fetches just the integer component: int x = floor of x when x is positive, int x = ceiling of x when x is negative. There is no rounding — only the integer component is identified. The absolute value function gives a positive integer as output whatever the input.
Exponential properties
a⁰ = 1; a¹ = a; reverse power a^(−1) = 1/a; (a^m)^n = a^(m·n); same denominator with different powers: multiply/add powers as m + n (as stated in class). These facts "will definitely appear multiple times while estimating the computational complexity via asymptotic analysis."
Factorial and Fibonacci
Factorial is defined recursively: n! = n × (n−1)!, with base case 0! = 1; the process recursively calls the step until n = 0. Fibonacci starts with f(0) = 0 and f(1) = 1; each subsequent term is the sum of the two preceding terms. Fibonacci-type series occasionally appear as special series in asymptotic analysis.
Polynomial representation via summation
A polynomial is not stored exactly as written; instead its coefficients are stored along with the highest degree, and the expression is recreated using a summation (essentially a for loop). This connects to how algorithms' running-time functions are represented.
Logarithm
By default, log is treated as base 2 (because systems use 0 and 1, i.e., binary); ln is base e; if a different base applies it will be stated explicitly. Log properties were mentioned but flagged for full explanation during asymptotic analysis.
Definition of an algorithm
Naïve/simplified definition: a finite step-by-step list of well-defined instructions for solving a particular problem — a sequence of instructions for solving a problem. Issue with this definition: algorithms should be applicable to a subset of input (already known).
Modular programming
The main module is divided into different sub-modules, which may or may not be interdependent — e.g., a calculator program whose main function calls separate functions for addition, subtraction, multiplication and division. Benefit: if there is a bug in a function, the main program execution is not hampered, and fixing/removing errors is easy. It is presented as the best mechanism for writing algorithms.
Largest element in an array (first algorithm)
Declare the first element as max (and its location as loc), then sequentially compare with the remaining elements; max changes only when a larger value is found. The same logic applies for 5, 50, or 1000 numbers.
Components of an algorithm
- Step: steps execute one after another, from step one to step n, unless control changes.
- Control: decides whether the algorithm goes sequentially, takes a jump, or continues; it changes the normal/sequential flow (if-else, switch, loops).
- Exit: terminates the algorithm (e.g.,
get ch, closing curly braces); without it the algorithm may run infinitely or get stuck in a loop. - Comment: helps in bug fixing / debugging.
- Variable name: there are policies, but commonly adopted names are used.
- Assignment: primarily done via equal to; some languages prefer other notation.
- Input/output: the algorithm should have a certain set of input and a certain set of output.
Flow types: sequential, selection, iterative
Sequential logic: statement 1, then 2, then 3, then 4 … executed one by one with no change in flow. Selection (conditional) logic: flow depends on meeting criteria — switch: if a condition holds, statement one executes, otherwise statement two; if nothing executes, default executes. Variants: single alternative (if-else), double alternative (if, else-if, else), multiple alternatives (switch). Iterative (repetitive) logic: the same thing appears again and again up to some condition; simple examples are loops (for, while).
Algorithmic analysis: efficiency and scalability
Algorithmic analysis evaluates performance in terms of efficiency and scalability — how efficient an algorithm is, which of multiple algorithms is more efficient, and what happens when it is scaled to a larger extent (input increases). Primary goal: understand how an algorithm behaves as input size increases. Common approach: measure running time as a function of input size; f(n) denotes the algorithm where n is the input. Theoretical analysis is needed because testing on random input sets is unreliable (an algorithm tested on 10,000 inputs may fail at one million; a website fine for 5,000 simultaneous users fails at 20,000). Two critical factors determine computational complexity: input size and the dominating operation. In searching and sorting, comparison is the dominating/key operation — it costs more because it converts both digits to binary, compares digit by digit, then declares the number, whereas assignment and addition cost lower; for larger n the other operations can be omitted. Other factors (not the primary focus): memory use, parallelism, heuristics, other optimization techniques. Time and space are the two main measures of efficiency: time is measured by counting key operations, space by the maximum memory needed. The complexity of an algorithm M is a function f giving the running time and storage requirement in terms of input size.
Formulas and maths
- Floor: $\lfloor x \rfloor$ = greatest integer ≤ x; Ceiling: $\lceil x \rceil$ = least integer ≥ x.
- Symbols: x is a real number. Used to extract integer bounds for positive and negative reals.
- $a = mn + b$; general form $k = mq + r$.
- Symbols: m is the divisor, q the quotient, r/b the remainder. Used to define the mod operation.
- $a \bmod n = b \bmod n \iff a \equiv b \bmod n \iff n \mid (b - a)$.
- Symbols: a, b, n integers. Used to test/establish congruence.
- $\text{int}(x) = \lfloor x \rfloor$ for positive x; $\text{int}(x) = \lceil x \rceil$ for negative x.
- Symbols: x a real number. Used to take the integer component with no rounding.
- $a^0 = 1$; $a^1 = a$; $a^{-1} = 1/a$; $(a^m)^n = a^{m \cdot n}$; same denominator with different powers: multiply/add powers as $m + n$ (as stated in class).
- Symbols: a is the base, m and n are powers. Used in complexity estimation via asymptotic analysis.
- $n! = n \times (n-1)!$, with $0! = 1$.
- Symbols: n a non-negative integer. Used as the standard recurrence-relation example.
- $f(0) = 0,\ f(1) = 1,\ f(n) = f(n-1) + f(n-2)$.
- Symbols: f(n) is the nth Fibonacci term. Used for series that appear in asymptotic analysis.
- $p(n) = \sum_{i=0}^{d} a_i \cdot n^i$.
- Symbols: d is the maximum degree, $a_i$ are the coefficients. Used to store a polynomial as coefficients plus highest degree.
- Logarithm properties (as mentioned): log base C of A·B can be split into two components; log base B of A can be split in a certain way; log base B of A = log base B of A^(−1) = −log base B of A (as stated in class — exact form [unclear]).
- Symbols: A, B, C are the arguments/bases. Used when required during asymptotic analysis.
- Quadratic: discriminant $d = b^2 - 4ac$; if d = 0 one solution, if d > 0 two solutions, if d < 0 no solution (in the procedure as written).
- Symbols: a, b, c are the quadratic's coefficients. Used in the quadratic-root procedure.
- Running time as a function of input: $f(n)$, where n is the input size.
- Symbols: f is the algorithm's running-time function. Used as the common approach of analysis.
Worked examples
Mod examples: - 27 mod 7: 7 goes 3 times (21), remainder 4. - 25 mod 5: 5 goes 5 times (25), remainder 0. - 35 mod 11: 11 goes 3 times (33), remainder 2. - 3 mod 8: 8 goes 0 times, remainder 3. - −18 mod 6: 6 goes −3 times (−18), remainder 0. - −17 mod 3: naive answer −2 is wrong; 3 goes −6 times (−18), and the existing minus makes it +18, so remainder is 1. - −29 mod 5: 5 goes −6 times (−30), remainder 1. - −7 mod 2: 2 goes −4 times (−8), remainder 1. - Division sanity check: 5 divided by 3 — 3 goes 1 time, remainder 2; never take 6 (3 goes 2 times) as that gives a wrong result. The chosen multiple must be equal to or less than the number being divided.
Congruence example: a = 11, b = 17, n = 2. 11 mod 2 = 1 and 17 mod 2 = 1, so 11 ≡ 17 mod 2. Check: b − a = 17 − 11 = 6, and 6 ÷ 2 = 3 with remainder 0, so n divides b − a.
Integer/absolute examples: int 3.14 = 3; int √5 = 2; int −8.5 = 8 (as stated in class).
Factorial example: 5! = 5 × 4! = 5 × 4 × 3! = 5 × 4 × 3 × 2! = 5 × 4 × 3 × 2 × 1! = 5 × 4 × 3 × 2 × 1 × 0! = 5 × 4 × 3 × 2 × 1 × 1 = 120.
Fibonacci example: f(0) = 0, f(1) = 1; f(2) = 0 + 1 = 1; f(3) = 1 + 1 = 2; f(4) = 2 + 1 = 3 (transcript garbled here — [unclear]); series continues 1, 1, 2, 3, 5, 8, 13, …
Polynomial example 1: a·n⁶ + b·n⁵ + c·n⁴ + d·n² + 39 (coefficients a, b, c, d; no n³ or n¹ terms). Written as Σ (i = 0 to 6) a_i · n^i. Expanding: a₀ = 39, a₁ = 0, a₂ = d, a₃ = 0, a₄ = c, a₅ = b, a₆ = a. Stored coefficients: a₆, a₅, a₄, a₂, and a₀ = 39.
Polynomial example 2 (quadratic): a·n² + b·n + c = 0: a₂ = a, a₁ = b, a₀ = c. As summation: Σ (i = 0 to 2) a_i · n^i = a₀n⁰ + a₁n¹ + a₂n² = c + bn + an², which rearranges to an² + bn + c.
Polynomial example 3: 3x² + x + 1: a₀ = 1, a₁ = 1, a₂ = 3. Coefficient array: 1, 1, 3. Recreation: a₀x⁰ + a₁x¹ + a₂x² = 1 + x + 3x².
Polynomial example 4: 6x⁴ + x + 2: coefficients a₀ = 2, a₁ = 1, a₂ = 0, a₃ = 0, a₄ = 6. Coefficient array: 2, 1, 0, 0, 6. Recreation: a₀x⁰ + a₁x¹ + a₂x² + a₃x³ + a₄x⁴ = 2 + x + 0 + 0 + 6x⁴.
Largest element example: array 31, 20, 21, 7, 47 (array name data). Compare 31 with 20 → highest 31; 31 with 21 → 31; 31 with 7 → 31; 31 with 47 → 47. Location = 5. (The transcript's comparison sequence is slightly garbled; the final result given is max 47 at location 5.) Algorithm as described: a non-empty array data with n numerical values is given; find the location loc and the value max of the largest element. Initialize location k = 1 and max = data at location 1. Set counter k = 2. Stopping criterion: if k > n, stop and output location, max, and exit. Otherwise, if max < data[k], then location = k and max = data[k].
Quadratic procedure steps: Step 10: take the values of a, b, c supplied/input by the user. Next step: estimate d (discriminant) = value under root of b² − 4ac. Third step: check if d == 0 (one unique solution), else if d > 0 (two solutions), else (d < 0) no solution.
Imaginary-root example: 2x² + 4x + 9: b² − 4ac = 16 − 4·2·9 = 16 − 72, negative inside the root → imaginary value; this lands in the third case (d < 0), declared "no solution" in the procedure, but an imaginary result can be obtained using the imaginary number concept. (The transcript also contains a garbled remark "16 minus 72 of course will be 50 minus 56" — [unclear].)
Website example: website declared fine for 5,000 simultaneous users; 20,000 users accessing it makes it difficult — illustrating why testing on finite input sets is not sufficient.
Sorting example: ~100 records with insertion sort; with a 30-second clock and 1 million records, insertion sort will not finish in time, while merge sort takes lower time.
Chunking example: 100 students' marks; chunk into 10, 10, 10, 10, sort each chunk, then merge (divide and conquer / merge sort) — lower time than picking one copy at a time and comparing with all.
Questions asked in class
- Q (Punit): If a is congruent to b mod n, is it also "covalent" (co-exist)? A: Yes, both properties co-exist.
- Q (Shriyad): For absolute/integer, e.g., 3.7 — is it 4 or 3? A: No rounding. Only the integer component is identified; rounding off is a different thing not studied here.
- Q: Please re-explain the summation representation. A: A polynomial p(n) = Σ (i = 0 to d) a_i · n^i can be denoted this way. Example: an² + bn + c = 0 has a₂ = a, a₁ = b, a₀ = c; expanding the summation gives c + bn +