Introduction to Algorithms
π‘ Introduction to Algorithms & Peak Finding
This course, 6.006: Introduction to Algorithms, focuses on efficient procedures for solving problems on large inputs. The emphasis is on understanding and analyzing algorithms that can handle massive datasets and scale effectively.
π Course Overview & Philosophy
The core philosophy revolves around efficiency and scalability.
Large Inputs: The definition of "large" has evolved significantly. While a few decades ago, 1,000 was considered large, today's "large" inputs can be trillions (e.g., US highway system, human genome, social networks like Facebook).
Paramount Concern: Even with increasing computational power, efficiency remains critical. An algorithm with cubic complexity (e.g., N3) on an input of 109 elements would become 1027, which is unmanageable even for modern computers.
Scalability: Algorithms must be designed to scale well, anticipating future increases in input size. What is large today might be small tomorrow, requiring algorithms that perform optimally as inputs grow exponentially.
π Key Topics & Data Structures
The course will cover a variety of classic data structures and algorithms:
Data Structures:
Binary Search Trees: Fundamental tree-based structures for efficient searching and sorting.
Hash Tables: Often referred to as dictionaries in Python, used for fast key-value lookups.
Balanced Binary Search Trees: More efficient variants of binary search trees that maintain balance to ensure logarithmic time complexity for operations.
Algorithms:
Sorting: Algorithms for arranging data in a specific order.
Matching: Techniques for finding relationships between data elements.
Graph Algorithms: For problems involving networks and relationships (e.g., shortest paths, Rubik's Cube solutions).
Dynamic Programming: A powerful technique for solving complex problems by breaking them into simpler subproblems.
π» Practical Implementation & Problem Sets
A significant aspect of the course involves real implementations in Python.
Problem Sets: Each problem set will have two parts:
Theory Part: Directly connected to lecture and recitation content, focusing on analysis and proofs.
Programming Part: Implementing or augmenting algorithms discussed in lectures, testing their performance on large inputs.
Learning by Doing: The problem sets are designed to be challenging and worthwhile, ensuring students gain a deep understanding through practical application.
πΊοΈ Course Modules
The course is structured into eight modules, each with an associated problem set:
Algorithmic Thinking: Introduction to problem-solving approaches and complexity analysis (starts today with peak finding).
Sorting & Trees: Exploring sorting algorithms and various tree data structures for tasks like scheduling.
Hashing: Techniques for efficient data storage and retrieval, with applications like genome comparison.
Numerics: Handling extremely large numbers (thousands of bits) for applications like RSA encryption.
Graphs: Fundamental data structure for modeling relationships, used in problems like Rubik's Cube or shortest path.
Shortest Paths: Algorithms for finding the most efficient routes in networks.
Dynamic Programming: An important algorithm design technique for optimization problems, such as image compression.
Advanced Topics: An overview of complexity theory and current research in algorithms.
β°οΈ Peak Finding Problem
The peak finding problem is a toy problem that effectively illustrates the challenges and solutions in designing efficient algorithms.
π 1D Peak Finding
π― Problem Definition
Given a one-dimensional array of numbers, find a peak.
Peak Condition: An element at position 2 (value
b) is a peak ifb >= a(its left neighbor) ANDb >= c(its right neighbor).Edge Cases:
Position 1 (
a):a >= b(only compare to the right).Position
n(i):i >= h(only compare to the left).
Existence: If the definition uses
>=(greater than or equal to), a peak is guaranteed to exist in any array. If it used>(strictly greater than), a peak might not exist.
πΆ Straightforward Algorithm: Linear Scan
Approach: Start from the left and traverse the array one element at a time, checking the peak condition.
Worst-Case Scenario: The peak is at the very right end of the array, requiring a scan of all
nelements.Complexity: Ξ(n) (theta of n). This means the runtime grows linearly with the input size
n, providing both a lower and upper bound on the complexity.
π³ Divide and Conquer Algorithm: Binary Search Approach
This algorithm aims to reduce the asymptotic complexity by using a divide and conquer strategy.
Examine Middle Position: Look at the element at position n/2.
Compare with Neighbors:
If A[n/2]<A[n/2β1]: The peak must be in the left half of the array (positions 1 to n/2β1). Recursively search the left half.
If A[n/2]<A[n/2+1]: The peak must be in the right half of the array (positions n/2+1 to n). Recursively search the right half.
Otherwise: If A[n/2] is greater than or equal to both its neighbors, then A[n/2] is a peak. You are done.
Correctness: This algorithm is correct because if A[n/2] is not a peak, and it's less than one of its neighbors, say A[n/2β1], then a peak must exist in the direction of A[n/2β1] (the left half). This is due to the "greater than or equal to" definition of a peak and the array boundaries acting as "negative infinity".
Recurrence Relation:
T(n)=T(n/2)+Ξ(1)
T(n/2): The recursive call on half the input size.
Ξ(1): Constant time for comparing A[n/2] with its two neighbors.
Base Case: T(1)=Ξ(1) (a single element array is always a peak).
Complexity: Ξ(log2βn) (theta of log n). This is a significant improvement over Ξ(n).
Example: For n=107:
Ξ(n) algorithm: ~13 seconds
Ξ(logn) algorithm: ~0.001 seconds
The difference is exponential, highlighting the importance of asymptotic complexity for large inputs.
π 2D Peak Finding
π― Problem Definition
Given a two-dimensional array (matrix) with n rows and m columns, find a 2D peak.
Peak Condition: An element
Ais a 2D peak ifA >= B,A >= C,A >= D, andA >= E(where B, C, D, E are its immediate neighbors: up, down, left, right).Existence: Similar to the 1D case, if the definition uses
>=(greater than or equal to), a peak is guaranteed to exist.
π§ Straightforward Algorithm: Greedy Ascent
Approach: Start at an arbitrary position (e.g., the middle). Continuously move to an adjacent element that is greater than the current one. If no adjacent element is greater, the current position is a peak.
Worst-Case Scenario: The algorithm might traverse a large fraction, or even all, of the elements in the matrix before finding a peak.
Complexity: Ξ(nm) (theta of n times m). If n=m, this becomes Ξ(n2).
β Incorrect Divide and Conquer Attempt
An initial thought might be to apply the 1D binary search approach directly:
Pick the middle column (j=m/2).
Find a 1D peak within this column (using the efficient Ξ(logn) 1D algorithm, which would be Ξ(logn) here for the column). Let this be at position (i,j).
Return (i,j) as the 2D peak.
Why it's Incorrect: A 1D peak in a column is not necessarily a 2D peak. An element might be the maximum in its column but smaller than its left or right neighbor in the matrix.
Example: An element
12might be a 1D peak in its column (surrounded by10and11), but a neighbor19to its right makes12not a 2D peak.
Lesson: Correctness is paramount; efficiency is secondary. An efficient but incorrect algorithm is useless.
β Correct Divide and Conquer Algorithm
This algorithm correctly finds a 2D peak using a divide and conquer strategy:
Pick a Middle Column: Select the column j=m/2.
Find Global Maximum in Column: Find the global maximum element within this middle column. Let this element be at position (i,j).
This step takes Ξ(n) time (linear scan of the column).
Compare with Left/Right Neighbors:
If A[i,jβ1]>A[i,j]: The peak must be in the left half of the matrix (columns 1 to jβ1). Recursively solve the problem on the left half.
If A[i,j+1]>A[i,j]: The peak must be in the right half of the matrix (columns j+1 to m). Recursively solve the problem on the right half.
Otherwise: If A[i,j]β₯A[i,jβ1] AND A[i,j]β₯A[i,j+1]: Then A[i,j] is a 2D peak.
Reasoning: Since A[i,j] is the global maximum in its column, it's greater than or equal to all elements above and below it. By checking its left and right neighbors, if it's also greater than or equal to them, it satisfies the 2D peak definition.
Base Case: When only a single column remains (m=1), find the global maximum in that column; it will be a 2D peak.
Recurrence Relation:
T(n,m)=T(n,m/2)+Ξ(n)
T(n,m/2): The recursive call on a matrix with half the number of columns.
Ξ(n): Time taken to find the global maximum in a column (which has
nrows).
Base Case: T(n,1)=Ξ(n) (finding the maximum in a single column).
Complexity: Ξ(nlog2βm) (theta of n log m).
This is significantly better than Ξ(nm) or Ξ(n2) (if n=m). For example, if n=m, it becomes Ξ(nlogn).
π Problem Set 1: Algorithmic Thinking
The first problem set will involve analyzing four different algorithms for peak finding, coded in Python.
Students will need to:
Analyze Complexity: Determine the asymptotic complexity of each algorithm.
Prove Correctness: Formally argue why one of the algorithms is correct.
Find Counterexamples: Identify specific inputs where incorrect algorithms fail.
Identify Bugs: Some provided code will have bugs that need to be found.
This problem set will reinforce the concepts of algorithm correctness, efficiency, and formal analysis.

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