Introduction to Algorithms
💡 Introduction to Algorithms
This course focuses on teaching students how to solve computational problems effectively. Beyond merely finding solutions, it emphasizes the critical skills of communicating these solutions to others, proving their correctness, and demonstrating their efficiency. Unlike many other computer science courses, this class involves a significant amount of writing and theoretical argumentation rather than just coding. The core objective is to develop the ability to mathematically prove that solutions are sound and superior to alternatives.
🔍 Understanding Computational Problems & Algorithms
What is a Computational Problem?
A computational problem is formally defined as a binary relation between a set of inputs and a set of outputs.
For each specific input, the problem specifies which outputs are considered correct.
It's not always a one-to-one mapping; a single input might have multiple correct outputs.
Problems are typically defined by a predicate, a rule that allows us to check if a given output is correct for a given input, rather than enumerating all input-output pairs. This is essential because inputs can be arbitrarily large.
Example: Checking if any two students in a classroom share the same birth time.
Input: A list of student birth times.
Output: "Yes, a pair exists" or "No, no pair exists."
A predicate would be: "Given two birth times, are they identical?"
Generalization: We are interested in algorithms that work for arbitrarily sized inputs, not just small, fixed instances. A single, fixed-size algorithm should be able to handle inputs ranging from a few items to billions of items (e.g., checking for matches in a Facebook database).
What is an Algorithm?
An algorithm is a fixed-size procedure or function that takes an input and produces a single output.
The crucial requirement is that for every valid input, the algorithm's output must be correct according to the problem's definition.
It's a deterministic process: same input always yields the same output.
Think of an algorithm as a recipe or a plan – a sequence of steps to achieve a specific outcome.
Example Algorithm: Birthday Problem
To check if any two students have the same birth time:
Maintain a record: An empty list or data structure to store birth times.
Interview students in some order:
For each student, ask for their birth time.
Check if birthday is in record: Search the current record for a match.
If a match is found, return a pair (e.g., "Yes, a match exists") and stop.
Otherwise, add the new student's birth time to the record.
If all students are interviewed and no match was found, return "None" (e.g., "No match exists").
This description is a verbal statement of an algorithm, which is the level of detail expected for theoretical problem sets – clear enough for another person to understand and implement.
✅ Proving Correctness: The Power of Induction
Proving an algorithm's correctness is fundamental, especially for algorithms designed to handle arbitrarily large inputs. Since a fixed-size algorithm must loop or recurse to process large inputs, mathematical induction is the primary technique for proving correctness.
Inductive Proof Setup
Inductive Hypothesis (IH): A statement about the algorithm's behavior that holds true for some value
kand can be extended tok+1.For the birthday problem, a suitable IH could be: "If the first
kstudents contain a match, the algorithm returns a match before interviewing studentk+1."
Base Case: The simplest instance for which the IH is true.
For the birthday problem,
k=0is the base case. After interviewing 0 students, there are no matches, and the algorithm hasn't returned anything, so the IH holds trivially (the premise "firstkstudents contain a match" is false).
Inductive Step: Assume the IH is true for some
k'(the inductive assumption) and then prove it holds fork'+1.Case 1: The first
k'students already contain a match. By the inductive hypothesis, the algorithm would have already returned a pair, thus being correct.Case 2: The first
k'students do not contain a match. The algorithm proceeds to interview studentk'+1.If there's a match among the first
k'+1students, it must involve studentk'+1(because no match existed among the firstk'students).The algorithm then checks student
k'+1's birthday against all existing birthdays in the record. If a match is found, it returns a pair.Thus, the IH is re-established for
k'+1.
By induction, if the property holds for k=n (all students), then the algorithm correctly returns a match if one exists, or "None" otherwise.
⏱️ Arguing Efficiency: Asymptotic Analysis
Efficiency measures how well an algorithm performs, not just in absolute time, but relative to other approaches and the size of its input.
Why Not Measure Real Time?
Measuring the actual time an algorithm takes on a computer is problematic because:
It depends on the size of the dataset (which we want to analyze).
It heavily depends on the strength and speed of the specific computer hardware (CPU, memory, etc.).
Instead, we use an abstract measure: counting fundamental operations. We assume each basic operation (like addition, comparison, memory access) takes a fixed amount of time.
Asymptotic Analysis
Asymptotic analysis is used to describe an algorithm's performance (running time or space usage) as the input size (n) grows very large. It abstracts away constant factors and lower-order terms, focusing on the growth rate.
Notation:
Big-O Notation (O): Provides an upper bound on the growth rate. O(f(n)) means the algorithm's performance will not grow faster than f(n) beyond some input size.
Omega Notation (Ω): Provides a lower bound on the growth rate. Ω(f(n)) means the algorithm's performance will grow at least as fast as f(n) beyond some input size.
Theta Notation (Θ): Provides a tight bound (both upper and lower). Θ(f(n)) means the algorithm's performance grows at the same rate as f(n).
Common Growth Rates (from most efficient to least efficient for large n):
Notation | Name | Description |
|---|---|---|
Θ(1) | Constant Time | Performance does not depend on the input size |
Θ(logn) | Logarithmic Time | Performance grows very slowly with |
Θ(n) | Linear Time | Performance grows directly proportional to |
Θ(nlogn) | Log-linear Time | Performance is slightly worse than linear. Often seen in efficient sorting algorithms. |
Θ(n2) | Quadratic Time | Performance grows quadratically with |
Θ(nc) | Polynomial Time | For any constant c>1. This is generally considered efficient in this class. |
Θ(cn) | Exponential Time | For any constant c>1. Performance grows extremely rapidly with |
Visual Comparison of Growth Rates: Image showing the relative growth of common time complexities. Constant and logarithmic are nearly flat, linear and n log n grow steadily, quadratic grows faster, and exponential explodes.
Model of Computation: Word RAM
To standardize the "fundamental operations," we use a theoretical model called a Word RAM (Random Access Machine).
Key Features of Word RAM:
Memory: Modeled as a long string of bits (0s and 1s), addressable in fixed-size chunks called words.
Random Access Memory (RAM): The CPU can access any memory location (address) in constant time (Θ(1)).
Word Size: The number of bits a CPU can process or store as a single unit (e.g., 64 bits on modern computers). This also dictates the maximum addressable memory (2word size).
A 32-bit word could address 232 bytes (4 GB), which was a limitation in the past.
A 64-bit word can address 264 bytes (20 exabytes), far exceeding current data storage needs.
CPU Operations: The CPU performs operations on words in constant time:
Binary Operations: Integer arithmetic (addition, subtraction, multiplication, division), comparisons (equal, less than, greater than), logical and bitwise operations.
Memory Access: Reading a word from a given address or writing a word to a given address.
Important Implication: The CPU operates on a constant amount of data at a time. If an algorithm needs to process n items, it will take at least Θ(n) time just to read all the input.
🏗️ Data Structures and Algorithm Design
The first half of this course will heavily focus on data structures.
Data structures are ways of organizing and storing large amounts of data to support specific operations (e.g., searching, insertion, deletion) more efficiently than simply processing raw data.
Examples: arrays, linked lists, trees, hash tables. Python's
list,set, anddictionaryare high-level abstractions that hide complex underlying data structures and operations, making it crucial to understand their theoretical performance.
Algorithm Design Paradigms
Algorithms are often designed using a combination of strategies:
Reduction: Transforming a problem into an instance of another problem for which a known efficient solution already exists.
Recursive Design: Breaking down a problem into smaller, similar subproblems and solving those recursively. This often involves:
Data Structures: Using appropriate data structures (like arrays, heaps, graphs) to manage and organize data for efficient operations.
Sorting: Algorithms for arranging data in a specific order.
Graph Algorithms: Techniques for traversing and analyzing relationships in graphs (e.g., shortest paths).
Dynamic Programming: Solving problems by breaking them into subproblems and storing the results of subproblems to avoid redundant computations.
The course structure typically divides into these areas:
Quiz 1: Data Structures and Sorting
Quiz 2: Shortest Paths Algorithms and Graphs
Quiz 3: Dynamic Programming

Ask anything
We've read this whole study set, so questions about it get the best answers.