I am now on Twitter! Meet me on Twitter here (my nick is pkrumins.)
Or on Google Buzz and Facebook.
This is the second post in an article series about MIT’s lecture course “Introduction to Algorithms.”
I changed my mind a little on how I will be posting the reviews of lectures. In the first post I said that I will be posting reviews of two or three lectures at a time, but I decided to group them by topic instead. This is more logical. Some lectures stand out alone. Lecture 3 is like that.
Lecture three is all all about a powerful technique in algorithm design called “Divide and Conquer.” I won’t be able to describe it better than the CLRS book, which says: “Divide and Conquer algorithms break the problem into several subproblems that are similar to the original problem but smaller in size, solve the subproblems recursively, and then combine these solutions to create a solution to the original problem.”
There are three steps to applying Divide and Conquer algorithm in practice:
- Divide the problem into one ore more subproblems.
- Conquer subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve the subproblems in a straightforward manner.
- Combine the solutions to the subproblems into the solution for the original problem.
An example of Divide and Conquer is the Merge Sort algorithm covered in lecture one:
- Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each.
- Conquer: Sort the two subsequences recursively using Merge Sort.
- Combine: Merge the two sorted subsequences to produce the sorted answer.
The recursion stops when the sequence to be sorted has length 1, in which case there is nothing else to be done, since every sequence of length 1 is already in sorted order!
Lecture three presents various other algorithms which use the powerful Divide and Conquer technique.
It starts by the running time analysis of Merge Sort algorithms and shows the general structure of recurrence equations generated by Divide and Conquer algorithms.
Then it moves on to Binary Search algorithm which allows finding an element in a sorted array in time proportional to logarithm of array length, or speaking in asymptotic notation speech, it’s worst running time complexity is O(lg(n)).
A naive algorithm which just scanned through all elements of an array would have time complexity of O(n) which is much, much worse!
The next algorithm presented solves the problem stated as following: given a number x and given an integer n, compute x to the power n.
A naive algorithm would create a loop from 1 to n, and multiply the x with itself:
result = 1
Naive-Power(x, n):
for 1 to n:
result = x * result
return result
This leads to running time of O(n).
A much more clever approach is to use an algorithm called Recursive Powering. The idea to calculate xn is to express xn as:
- xn/2·xn/2 if n is even, or
- x(n-1)/2·x(n-1)/2·x, if n is odd.
Here is the pseudo-code of this algorithm:
Recursive-Power(x, n):
if n == 1
return x
if n is even
y = Recursive-Power(x, n/2)
return y*y
else
y = Recursive-Power(x, (n-1)/2)
return y*y*x
Doing asymptotic analysis of this algorithm leads to O(lg(n)) running time, which is a great improvement over O(n).
The lecture continues with four algorithms on how to compute Fibonacci Numbers! You should definitely watch that! Man, it’s four algorithms! It’s at 17:49 in the lecture!
I won’t give the pseudo code here for these ones, but they are naive recursive algorithm, bottom up algorithm, naive recursive squaring and recursive squaring!
The next two topics are Matrix Multiplication the naive way, giving O(n3) running time, and an improved Strassen’s Algorithm which reduces the number of multiplications yielding running time of approximately O(n2.81).
The last topic covered in this lecture is VLSI (Very large Scale Integration) layout problem: given a number of various electronic gates an chips, how to position them on the circuit board to minimize area they occupy.
Having described the lecture in great detail, you’re welcome to watch it:
Topics covered in lecture 3:
- [01:25] Divide and conquer method.
- [04:54] Example of Divide and conquer applied to Merge sort.
- [06:45] Running time analysis of Merge sort.
- [09:55] Binary search algorithm.
- [12:00] Analysis of binary search running time.
- [13:15] Algorithm for powering a number (recursive powering).
- [17:49] Algorithms for computing Fibonacci numbers (FBs).
- [19:04] Naive recursive algorithm (exponential time) for computing FBs.
- [22:45] Bottom-up algorithm for computing FBs.
- [24:25] Naive recursive squaring algorithm for FBs (doesn’t work because of floating point rounding errors).
- [27:00] Recursive squaring algorithm for FBs.
- [29:54] Proof by induction of recursive squaring FB algorithm
- [34:00] Matrix multiplication algorithms.
- [35:45] Naive (standard) algorithm for multiplying matrices.
- [37:35] Divide and conquer algorithm for multiplying matrices.
- [40:00] Running time analysis of divide and conquer matrix multiplication algorithm.
- [43:09] Strassen’s matrix multiplication algorithm.
- [50:00] Analysis of Strassen’s algorithm.
- [55:25] VLSI layout problem.
- [57:50] Naive embedding algorithm for VLSI.
- [01:05:10] Divide and conquer algorithm for laying out VLSI.
Lecture 3 notes:
Have fun dividing and conquering! The next post will be about two lectures on sorting algorithms!
Ps. the lectures are taught from the CLRS book (also called “Introduction to Algorithms”):
Did you like this post? Subscribe here:
If you really enjoyed the post, I'd appreciate a gift from my geeky Amazon book wishlist. Books would make me more educated and I could write even better posts. Thanks! :)

(11 votes, average: 4.55 out of 5)
|
|
|


August 21st, 2008 at 10:53 pm
Thanks! I’m looking forward to seeing more posts in this series :)
August 22nd, 2008 at 12:55 am
Shouldn’t you link to the original lectures/site as well?
August 22nd, 2008 at 1:31 am
Nice summary. Your pseudo-code for the recursive power algorithm is not quite right. As written, it is still O(n). You don’t get the benefit if you make each recursive call twice. For the O(log n) version, try:
August 22nd, 2008 at 1:45 am
John, I don’t know. Perhaps I should. I linked to the original lectures from part one.
Steve, oops! What was I thinking! Fixed now. Many thanks!
August 22nd, 2008 at 10:00 am
just wanted to say thanks for putting up these posts. i especially like the fact that you summarise the videos and put up scanned notes.
August 23rd, 2008 at 5:20 am
Peteris, have a read of the Attribution-Noncommercial-Share Alike license at http://ocw.mit.edu/OcwWeb/web/terms/terms/index.htm
I think you need to make clear the license on the videos (e.g. by linking to that page). I’m not sure about commercial use though. You clearly have advertising that’s designed to bring in money - I suppose it depends on whether the adverts and sponsorship on your site are for profit or not and more importantly whether they’re incidental to these articles that include MIT’s work or not
It’s definitely worth a read though and it also encourages you to contact MIT if you have any questions
August 24th, 2008 at 4:16 am
I like the posts,especially the way you learning algorithms.
August 25th, 2008 at 9:38 pm
[…] previous post covered a lecture on “Divide and Conquer” algorithm design technique and its […]
August 27th, 2008 at 1:06 am
[…] MIT’s Introduction to Algorithms, Lecture 3: Divide and Conquer Sphere: Related Content Ask a Question […]
August 27th, 2008 at 7:58 pm
[…] Demaine (professor) presents a randomized divide and conquer algorithm for this problem (my second post covers divide and conquer algorithms), which runs in expected linear […]
September 29th, 2008 at 10:54 pm
[…] BST-sort and quicksort make the same comparisons but in different order. [more info on quicksort in part two of article series and in “three beautiful quicksorts” […]
September 30th, 2008 at 6:24 am
[…] into the solution for the original problem. Please follow this link to read the whole article: http://www.catonmat.net/blog/mit-int…thms-part-two/ You’re welcome to engage in a discussion about it! __________________ Peteris Krumins loves […]
November 9th, 2008 at 3:20 am
Hi, i saw this question on the other post on Order Statistics, can someone help answer this question
Let X[1::n] and Y [1::n] be two arrays, each containing n numbers already in sorted order.
Give an O(lg n)[in other words an algorithm better than O(n) ] time algorithm to find the nth smallest element of the 2n elements in arrays X and Y, using divide & conquer
Thanks!!
December 5th, 2008 at 12:09 am
[…] programming is a design technique similar to divide and conquer. Divide-and-conquer algorithms partition the problem into independent subproblems, solve the […]
December 17th, 2008 at 10:08 am
[…] on Sun 14-12-2008 Forex Trading: Fibonacci Channels Saved by fridman on Tue 02-12-2008 MIT’s Introduction to Algorithms, Lecture 3: Divide and Conquer Saved by lyme on Tue 02-12-2008 The Fibonacci Sequence Saved by soopahviv on Sun 23-11-2008 […]
February 19th, 2009 at 10:04 am
I have a very important question… Anyone know the e-mail address of the blond chick sitting to the right of the chick in the red shirt whom asked for clarification of Fibonacci series? They don’t have hot CS chicks like that at my school..
Jacob
school with held for personal safety reasons
March 7th, 2009 at 7:57 pm
[…] x + y | thread C # this is a really bad algorithm, because it’s # exponential time. # # see lecture three for four different # classical algorithms that compute Fibonacci […]
March 15th, 2009 at 3:14 am
[…] Bài giảng 3 (Chia để trị) […]
July 13th, 2009 at 9:12 am
[…] it’s shown that the classical binary search (covered in lecture 3) is not cache efficient, but order statistics problem (covered in lecture 6) is cache […]
August 13th, 2009 at 10:26 am
i want to write a c++ code that implements strassen’s algorithm for an n*n matrix.My my is how to divide a matrix in to sub matrices and apply strassen’s algorithm on them.
November 12th, 2009 at 8:37 pm
[…] Lecture 3: Divide and Conquer […]
November 22nd, 2009 at 4:18 am
Very informative, thank you very much :)