IT 238

Data Structure and Algorithms

TU BIM · Semester 3 · BIM curriculum effective from 2021

Requirement
required
Credits
3
Study Guide
9 units available

Chapter notes

Authoritative study guide across 9 syllabus units

Open a unit to read its study notes.

Unit 1: Introduction to Data Structure and Algorithms· 4 LHs· ~5 marks

Pass path: Define ADT, define Big-O, Big-Omega, and Big-Theta notations, and identify best vs worst case complexity. Top path: Formally prove asymptotic bounds using limit definitions, compute closed-form operation counts from nested code loops, and evaluate space-time tradeoffs.

Part 1: Core Concepts (2 Marks)

  • ★ Abstract Data Type (ADT): A mathematical specification of a data collection and the set of operations that can be performed on it, defined purely by its behavior and interface without specifying implementation details.
  • ★ Asymptotic Notations:
    • Big-O (OO): Formal mathematical upper bound. f(n)=O(g(n))f(n) = O(g(n)) if there exist positive constants cc and n0n_0 such that 0≤f(n)≤c⋅g(n)0 \le f(n) \le c \cdot g(n) for all n≥n0n \ge n_0.
    • Big-Omega (Ω\Omega): Formal mathematical lower bound. f(n)=Ω(g(n))f(n) = \Omega(g(n)) if 0≤c⋅g(n)≤f(n)0 \le c \cdot g(n) \le f(n) for all n≥n0n \ge n_0.
    • Big-Theta (Θ\Theta): Asymptotically tight bound. f(n)=Θ(g(n))f(n) = \Theta(g(n)) if c1g(n)≤f(n)≤c2g(n)c_1 g(n) \le f(n) \le c_2 g(n) for all n≥n0n \ge n_0.
  • ★ Space-Time Tradeoff: The principle that computational execution time can often be reduced by utilizing additional auxiliary memory storage (e.g., hash tables), or memory consumption reduced at the cost of slower execution.

Part 2: Structural Frameworks (10 Marks)

1. Mathematical Derivation of Asymptotic Bounds

When prompted to analyze the computational complexity of code snippets:

  1. Loop Complexity Analysis Rules:
    • Single loop running 11 to nn: O(n)O(n) linear time.
    • Nested loops running 11 to nn: ∑i=1n∑j=1n1=n2  ⟹  O(n2)\sum_{i=1}^n \sum_{j=1}^n 1 = n^2 \implies O(n^2) quadratic time.
    • Loop variable multiplying or dividing by 2 (i=i×2i = i \times 2): log⁡2n\log_2 n iterations   ⟹  O(log⁡n)\implies O(\log n) logarithmic time.
  2. Formal Asymptotic Proof Example: Prove that f(n)=3n2+5n+2=O(n2)f(n) = 3n^2 + 5n + 2 = O(n^2).
    • We must find c>0c > 0 and n0≥1n_0 \ge 1 such that 3n2+5n+2≤c⋅n23n^2 + 5n + 2 \le c \cdot n^2 for all n≥n0n \ge n_0.
    • For all n≥1n \ge 1:
      5n≤5n2and2≤2n25n \le 5n^2 \quad \text{and} \quad 2 \le 2n^2
    • Therefore:
      3n2+5n+2≤3n2+5n2+2n2=10n23n^2 + 5n + 2 \le 3n^2 + 5n^2 + 2n^2 = 10n^2
    • Choosing c=10c = 10 and n0=1n_0 = 1, the inequality holds strictly:
      3n2+5n+2≤10n2∀n≥13n^2 + 5n + 2 \le 10n^2 \quad \forall n \ge 1
    • Hence, 3n2+5n+2=O(n2)3n^2 + 5n + 2 = O(n^2). Q.E.D.

Part 3: Distinction Tables

Asymptotic Notations Comparison

Dimension Big-O (OO) Big-Omega (Ω\Omega) Big-Theta (Θ\Theta)
Bound Type Asymptotic Upper Bound (Ceiling) Asymptotic Lower Bound (Floor) Asymptotically Tight Bound
Growth Meaning Algorithm will run at most this slow Algorithm will take at least this much work Exact growth rate matches function
Mathematical Relation f(n)≤c⋅g(n)f(n) \le c \cdot g(n) f(n)≥c⋅g(n)f(n) \ge c \cdot g(n) c1g(n)≤f(n)≤c2g(n)c_1 g(n) \le f(n) \le c_2 g(n)
Practical Use Worst-case algorithm guarantee Best-case lower bound proof Standard benchmark description

Part 4: Worked Problems & Exam Traps

Problem 1 (Standard): Complexity from Nested Loop Code

Analyze the exact step count and Big-O time complexity:

int sum = 0;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
        sum++;
    }
}
  • Step Count Derivation:
    Total steps=∑i=0n−1i=0+1+2+⋯+(n−1)=(n−1)n2=n2−n2\text{Total steps} = \sum_{i=0}^{n-1} i = 0 + 1 + 2 + \dots + (n-1) = \frac{(n-1)n}{2} = \frac{n^2 - n}{2}
  • Asymptotic Classification:
    12n2−12n=O(n2)\frac{1}{2}n^2 - \frac{1}{2}n = O(n^2)

Students lose marks here because they assert that Big-O represents “worst case” and Big-Omega represents “best case”. Big-O, Ω\Omega, and Θ\Theta are pure mathematical bounding notations applicable to any case (e.g., worst-case can have a Θ\Theta bound).