Algorithm
This article is a concept explanation about algorithms - including exam questions, key points, and tags.
In a Nutshell
An algorithm is a finite, unambiguously described sequence of steps to solve a problem. It’s evaluated by criteria such as correctness, runtime, memory usage, stability, and robustness.
Compact Technical Description
An algorithm processes well-defined inputs and delivers deterministic or probabilistic outputs. It’s often described in pseudocode with pre- and post-conditions.
For analysis, asymptotic notations are used:
- O (upper bound)
- Ω (lower bound)
- Θ (tight bound)
Often separated by best/average/worst case and supplemented with amortized analysis.
Typical design paradigms:
- Divide and Conquer
- Greedy
- Dynamic Programming
- Backtracking
- Randomization
Data structures (Array, List, Heap, Hash Table, Tree, Graph) strongly determine real costs.
Security aspect: Worst-case inputs can trigger Algorithmic Complexity Attacks, therefore input validation and resource limits are relevant.
Exam-Relevant Key Points
- Exact vs heuristic/approximate; deterministic vs randomized
- O/Ω/Θ; Best/Average/Worst; amortized
- Paradigms: D&C, Greedy, DP, Backtracking
- Vocational training: Pre/post-conditions, termination, loop invariants
- Practice: Choose appropriate data structure, measure first then optimize
- Security: Worst-case hardening, limits
- Documentation: Problem definition, pseudocode, complexity, test protocols
Core Components
- Specification (Input/Output/Constraints)
- Cost model (Time/Memory/I/O)
- Pseudocode (Sequence, Selection, Loop)
- Data structure selection
- Correctness (Invariants/Induction/Termination)
- Complexity analysis
- Design approach
- Implementation details (Recursion/Iteration)
- Robustness/Security
- Tests (Edge cases, Fuzzing, Regression)
Practical Example: Insertion Sort (Pseudocode)
function insertionSort(a)
for i from 1 to length(a)-1
key <- a[i]
j <- i-1
while j >= 0 and a[j] > key
a[j+1] <- a[j]
j <- j-1
end
a[j+1] <- key
end
return a
Explanation: Stable, in-place, Worst-Case O(n^2), Best-Case O(n) for nearly sorted data.
Typical Exam Questions (with Brief Answers)
- O vs Ω vs Θ? O upper bound, Ω lower bound, Θ tight bound.
- When is Greedy correct? When optimal substructure and greedy-choice property hold.
- How to recognize DP? Overlapping subproblems + optimal substructure.
- How to prove termination? Variation function that strictly decreases and is bounded below.
Free Response
For vocational training tasks: clearly define the problem, write clean pseudocode, justify complexity, and test edge cases. In real systems, cache/I/O effects and robustness against worst-case inputs are crucial.
Learning Strategy
- Write pseudocode + invariants for old questions.
- Measure runtimes (linear vs binary, different sorts).
- Model DP example (knapsack) as table.
- Always test edge cases and termination conditions.