Chapter 1: Introduction to Data Structures and Algorithms
1.1 Abstract Data Types (ADT) and Data Structures
- Abstract Data Type (ADT): A conceptual model composed of a collection of data and a set of operations performed on that data.
- It specifies what the operations do, not how to implement them.
- Think of it as a "black box" that hides the inner design (independent of any programming language).
- Data Structure: A specific, concrete scheme for organizing data in computer memory.
- Equation: Data Structure = Organized Data + Operations.
- Classification of Data Structures:
- Primitive: Basic structures directly operated upon by machine instructions (e.g., integers, floats, characters, pointers).
- Non-Primitive: More sophisticated structures for homogeneous/heterogeneous groups of data (e.g., Arrays, Lists, Files, Trees, Graphs).
1.2 Algorithms and Their Properties
- Algorithm Definition: A step-by-step, finite sequence of precise instructions for solving a well-defined computational problem.
- Note: All programs are algorithms, but not all algorithms are programs (e.g., a cooking recipe is an algorithm).
- Key Properties of Algorithms:
- Input: What it takes in.
- Output: What it produces.
- Definiteness: Steps are precise and unambiguous.
- Correctness: It produces the correct output.
- Finiteness: It must eventually terminate.
- Effectiveness: Steps can be performed in a finite amount of time.
- Generality: Applicable to a class of similar problems.
1.3 Expressing Algorithms: Flowcharts and Pseudocode
Algorithms are typically expressed via natural language, flowcharts, pseudocode, or programming languages.
- Flowcharts: Diagrams that depict the "flow" of a program.
- Symbols: Rounded rectangles (Terminals/Start/End), Parallelograms (Input/Output), Rectangles (Processes/Math), Diamonds (Decisions/Yes-No).
- Four Flowchart Structures:
- Sequence: Actions performed linearly.
- Decision: Branching based on conditions (if/else).
- Repetition: Loops that execute as long as a condition holds (e.g., pre-test
whileloops, post-testdo-whileloops). - Case: Multiple actions depending on variable contents (switch statements).
- Pseudocode: An informal, high-level description of an algorithm that hides low-level programming details but remains precise enough for a programmer to implement.
1.4 Algorithmic Performance and Complexity
Performance is analyzed independently of hardware by counting fundamental primitive operations (like assignments, simple I/O, arithmetic, or array indexing).
- Space Complexity: The amount of memory an algorithm needs to run to completion.
- Time Complexity: The amount of time an algorithm takes to run, typically expressed as a function of the input size (n).
- Time-Space Trade-off: Optimizing an algorithm often requires sacrificing one resource for the other (e.g., using more memory to make a program run faster).
- Types of Analysis:
- Worst-Case: Maximum time required (upper bound). Most commonly used.
- Best-Case: Minimum time required (lower bound).
- Average-Case: Average time required across all inputs.
1.5 Growth Rates and Asymptotic Notations
To classify how fast an algorithm's execution time grows as input size increases, we use specific growth-rate functions and asymptotic notations. We generally drop constants and lower-order terms (e.g., O(3n^2 + 4n + 5) simplifies to O(n^2)).
-
Common Growth Rates (Fastest to Slowest):
- O(1) — Constant
- O(\log n) — Logarithmic
- O(n) — Linear
- O(n \log n) — Linearithmic
- O(n^2) — Quadratic
- O(n^3) — Cubic
- O(2^n) — Exponential
-
Asymptotic Notations:
- Big-O Notation (O): Defines the upper bound (worst-case). The algorithm takes no more than this much time. (f(n) \le c \cdot g(n))
- Big-Omega Notation (\Omega): Defines the lower bound (best-case). The algorithm takes at least this much time. (f(n) \ge c \cdot g(n))
- Big-Theta Notation (\Theta): Defines an exact/tight bound. The function gets "sandwiched" consistently between a lower and an upper bound.