Running notes for a Data Structures and Algorithms course. Updated as topics progress.
Basic Concepts
A Computational Problem asks for a solution in terms of an Algorithm.
The Algorithm maps a set of instances or cases to a possibly empty set of solutions — so a computational problem is a relation that maps instances to solutions.
The behavior of algorithms is described using model of computations, which specify how an algorithm handles input/output and what operations it may perform. This course uses the RAM Model of Computation.
Formally, an algorithm solves a computational problem if:
- It takes inputs
- It produces outputs
- The produced pair
A Data Structure stores and organizes data for efficient algorithm access.
Analysis of Algorithms
While analyzing an algorithm, we evaluate its efficiency and correctness. The algorithm should:
- Solve the problem correctly for all valid inputs
- Utilize resources efficiently — primarily time, secondarily space
Primitive Operations
To analyze at an abstract level, we count primitive operations — each defined to run in time on the RAM model:
- Arithmetic operations (+, −, ×, ÷, mod)
- Comparison and assignment of variables
- Array indexing
- Logical operations
- Function calls and pointer dereferencing
Growth of Functions
We study running time as a function of input size . Three asymptotic notations bound growth:
Big-O (upper bound)
if there exist constants and such that:
We say grows no faster than .
Big-Omega (lower bound)
if there exist constants and such that:
We say grows at least as fast as .
Big-Theta (tight bound)
if and .
and grow at the same rate up to constant factors.
Common Complexity Classes
| Class | Name | Typical algorithm |
|---|---|---|
| Constant | Array access, hash lookup | |
| Logarithmic | Binary search, balanced BST ops | |
| Linear | Linear scan, BFS | |
| Linearithmic | Merge sort, heap sort | |
| Quadratic | Insertion sort, bubble sort | |
| Cubic | Matrix multiplication (naive) | |
| Exponential | Subset enumeration | |
| Factorial | Permutation enumeration |
Sorting Algorithms
Insertion Sort
- Best case: — already sorted
- Worst case: — reverse sorted
- Space: in-place
- Stable: yes
Works well for small or nearly-sorted data. Inner loop is tight and cache-friendly.
Merge Sort
- All cases:
- Space: — requires auxiliary array
- Stable: yes
Divide-and-conquer: split in half, sort each half, merge. The merge step takes ; the recurrence is , which solves to by the Master Theorem.
Heap Sort
- All cases:
- Space: in-place
- Stable: no
Build a max-heap in , then extract-max times in each.
Data Structures Covered
| Structure | Access | Search | Insert | Delete |
|---|---|---|---|---|
| Array | ||||
| Linked List | ||||
| Stack / Queue | ||||
| Hash Table | — | avg | avg | avg |
| AVL Tree | ||||
| Heap | — |
Related
- Algorithm — formal definition
- Computational Problem — what we’re solving
- RAM Model of Computation — the machine model
- Data Structure — how data is organized