Data structure and Algorithm
Introduction
Data structure: memory for storing information
Data structure is the combination of the data which stores the value and the metadata which maintains the structure.
Algorithm: method for solving a problem
Tips
Algorithm + Data Structure = Program.
Not all problems can be solved, e.g. halting problem
Basic operations on data structures
- Accessing
- Insertion
- Deletion
- Searching
- Sorting
- Traversal

Important
There is no one magic data structure and algorithm that can solve any problems in different scenarios. All of them have trade-offs.
What is algorithm design?
Computer doesn't actually have any special tricks for solving problems; its only solution is exhaustive search—exploring all possibilities. Algorithm design is simply about first thinking about "how to exhaustively search", and then pursuing "how to exhaustively search intelligently".
Pseudo code
Describe an algorithm
Should be abstract
- Ignores unimportant details
- Allows clear presentation of key idea
- Simplifies analysis of a proposed solution
Templates
Algorithm Inputs Returns Variables (local) Begin End- Not rigid, different among various organizations
Big O
Analyze an algorithm
Need to measure performance of different algorithms to allow us to compare them
Interested in efficiency of an algorithm
- Time: how fast does an algorithm run
- Space: how much memory is required simultaneously
Experimental approach: limited
Theoretical approach: Complexity analysis
Big O Notation
Give an approximately upper bound on an algorithm complexity
: indicate the approximate number of operations required by an algorithm for input size
N
Big O is a classification system that groups together algorithms that have similar performance. Enable us to quickly compare different algorithms
Extra effort to calculate exact bounds is normally unnecessary (is sometimes)
Two algorithms with same Big O performance may not behave exactly the same in practice
Big Omega Ω: lower bound on performance
Big Theta θ: bounded above and below
Rules
Drop the non-dominant arithmetic terms
Drop the constants
- Adding, subtracting, multiplying, or dividing a Big O performance bound by a constant factor does not change it, doesn't matter how big the constant factor is
Different terms for multiple inputs
Functional call can cost more than
We can also approach the runtime by thinking about what the code is supposed to be doing
Algorithm analysis case
- Best case; Worst case; Average case (Don't constraint the input, still be infinity)
- Normally focus on worst case as this gives us an upper bound
- Average case can be useful but hard to calculate and assume random data
- Best case seen as least useful but can provide a good indication of when to use a particular algorithm
Amortized complexity
Averages Over a Sequence of operations: Unlike worst-case analysis (which looks at the single most expensive operation) or average-case (random inputs), amortized analysis considers the total cost over many operations.
Spreading Costs: Expensive operations (e.g., ) are "paid for" by the cheap operations (e.g., ), so the amortized cost per operation remains low.
Accounting/Aggregate Methods: Techniques like the "accounting method" assign "credits" to cheap operations to pay for future expensive ones, while the "aggregate method" sums total cost and divides by the number of operations.
Example
Dynamic array resizes cost being averaged out to per insertion.
Recursion Complexity Analysis
Recursion time complexity =
the number of recursions time complexity of the recursive function, i.e.,
the number of nodes in the recursion tree time complexity of operation on each node.Recursion space complexity =
the depth of the recursion stack other space allocated by the algorithm, i.e.,
the height of the recursion tree other space allocated by the algorithm.
Hint of Time Complexity by Input Size
Computer can do roughly operations per second. Therefore, we can estimate the possible passed time complexity based on the input size.
Input Size Allowed Time Complexity Possible Algorithms or Backtracking, Brute-force State Compression DP Halving Enumeration Triple-loop DP, Floyd Double-loop DP, Knapsack Problem Most questions fall into this class, suitable for various algorithms Linear DP, Sliding Window Is Prime Number or Binary Search, Quick Power, Math Tricks
Tips
In practical, notice the influence of constant factor. E.g., hash table is slower than array despite both operations are .
