Introduction to Algorithms Massachusetts Institute of Technology Singapore-MIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson
Day 20 6.046J/18.410J SMA5503 Handout 22
Problem Set 5
MIT students: This problem set is due in l
Introduction to Algorithms Massachusetts Institute of Technology Singapore-MIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson
Day 8 6.046J/18.410J SMA5503 Handout 9
Problem Set 3
MIT students: This problem set is due in lec
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 1
Prof. Charles E. Leiserson
Welcome to Introduction to Algorithms, Fall 2001
Handouts
1. 2. 3. 4. 5. 6. Course Information Calendar Registration (MIT students only) References Objectives an
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 15
Prof. Charles E. Leiserson
Dynamic programming
Design technique, like divide-and-conquer. Example: Longest Common Subsequence (LCS) Given two sequences x[1 . . m] and y[1 . . n], find a
Introduction to Algorithms
6.046J/18.401J LECTURE 15
Shortest Paths II Bellman-Ford algorithm DAG shortest paths Linear programming and difference constraints VLSI layout compaction Prof. Charles E. Leiserson
Negative-weight cycles
Recall: If a
Introduction to Algorithms Massachusetts Institute of Technology Singapore-MIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson
Day 10 6.046J/18.410J SMA5503 Handout 11
Problem Set 1 Solutions
Exercise 1-1. Do Exercise 2.3-5
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 2
Prof. Erik Demaine
Solving recurrences
The analysis of merge sort from
Lecture 1 required us to solve a
recurrence.
Recurrences are like solving integrals,
differential equations, etc.
o Learn
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 22
Prof. Charles E. Leiserson
Flow networks
Definition. A flow network is a directed graph G = (V, E) with two distinguished vertices: a source s and a sink t. Each edge (u, v) E has a nonn
Introduction to Algorithms Massachusetts Institute of Technology Singapore-MIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson
Day 6 6.046J/18.410J SMA5503 Handout 8
Problem Set 2
MIT students: This problem set is due in lec
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 20
Prof. Erik Demaine
Disjoint-set data structure (Union-Find)
Problem: Maintain a dynamic collection of pairwise-disjoint sets S = {S1, S2, ., Sr}. Each set Si has one element distinguished
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 21
Prof. Charles E. Leiserson
Take-home quiz
No notes (except this one).
2001 by Charles E. Leiserson
Introduction to Algorithms
Day 35
L21.2
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 8
Prof. Charles E. Leiserson
A weakness of hashing
Problem: For any hash function h, a set of keys exists that can cause the average access time of a hash table to skyrocket. An adversary c
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 19
Prof. Erik Demaine
Shortest paths
Single-source shortest paths Nonnegative edge weights General DAG
Bellman-Ford: O(VE) One pass of Bellman-Ford: O(V + E) Dijkstra's algorithm: O(E + V
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 9
Prof. Charles E. Leiserson
Binary-search-tree sort
T Create an empty BST for i = 1 to n do TREE-INSERT(T, A[i]) Perform an inorder tree walk of T. Example: A = [3 1 8 2 6 7 5] 3 3 1 1 2 2
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 10
Prof. Erik Demaine
Balanced search trees
Balanced search tree: A search-tree data structure for which a height of O(lg n) is guaranteed when implementing a dynamic set of n items. AVL tr
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 11
Prof. Erik Demaine
Dynamic order statistics
OS-SELECT(i, S): returns the i th smallest element in the dynamic set S. OS-RANK(x, S): returns the rank of x S in the sorted order of S's ele
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 12
Prof. Erik Demaine
Computational geometry
Algorithms for solving "geometric problems" in 2D and higher. Fundamental objects: Basic structures:
point line segment line
point set
2001 by
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 13
Prof. Erik Demaine
Fixed-universe successor problem
Goal: Maintain a dynamic subset S of size n of the universe U = {0, 1, ., u 1} of size u subject to these operations: INSERT(x U \ S
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 14
Prof. Charles E. Leiserson
How large should a hash table be?
Goal: Make the table as small as possible, but large enough so that it won't overflow (or otherwise become inefficient). Probl
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 16
Prof. Charles E. Leiserson
Graphs (review)
Definition. A directed graph (digraph) G = (V, E) is an ordered pair consisting of a set V of vertices (singular: vertex), a set E V V of ed
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 17
Prof. Erik Demaine
Paths in graphs
Consider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v1 v2 L vk is defined to be
w( p ) = w(vi , vi +1 ) .
i =1
Introduction to Algorithms
6.046J/18.401J/SMA5503
Lecture 18
Prof. Erik Demaine
Negative-weight cycles
Recall: If a graph G = (V, E) contains a negativeweight cycle, then some shortest paths may not exist. . Example: <0 u u v v
Bellman-Ford algori
Introduction to Algorithms Massachusetts Institute of Technology Singapore-MIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson
Day 10 6.046J/18.410J SMA5503 Handout 12
Problem Set 2 Solutions
MIT students: This problem set i
Introduction to Algorithms Massachusetts Institute of Technology Singapore-MIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson
Day 14 6.046J/18.410J SMA5503 Handout 17
Problem Set 3 Solutions
MIT students: This problem set i
Introduction to Algorithms Massachusetts Institute of Technology Singapore-MIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson
Day 11 6.046J/18.410J SMA5503 Handout 14
Problem Set 4
MIT students: This problem set is due in r
Example: Matrix Chain Multiplication
n
n
What if we want to multiply a string of more than two
matrices together?
Matrix multiplication is associative, that is A(BC) =
(AB)C, so the result is the same no matter in what
order we multiply the matrices, but
Introduction to Algorithms
Chapter 15: Dynamic Programming
Dynamic Programming
Well known algorithm design techniques:
n
q
q
n
Another strategy for designing algorithms is dynamic
programming.
q
n
Brute-Force (iterative) algorithms
Divide-and-conquer algo
Using The Master Method
Case 1
n
T(n) = 9T(n/3) + n
q
q
q
q
q
a=9, b=3, f(n) = n
nlog a = nlog 9 = (n2)
Since f(n) = O(nlog 9 - ) = O(n2-0.5) = O(n1.5)
where =0.5
case 1 applies:
b
3
3
(
)
(
T (n) = nlogb a when f (n) = O nlogb a
q
)
Thus the solution is
Red-Black Trees
n
The red-black properties:
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
n
Note: this means every real node has 2 children
3. If a node is red, both children are black
n
Note: cant have 2 consecutive reds on a
Knapsack problem
There are two versions of the problem:
(1) 0-1 knapsack problem and
(2) Fractional knapsack problem
n
(1) Items are indivisible; you either take an item or
not. Solved with dynamic programming
(2) Items are divisible: you can take any fra