**10**Comments August 25, 2008

# MIT's Introduction to Algorithms, Lectures 4 and 5: Sorting

This is the third post in an article series about MIT's lecture course "**Introduction to Algorithms**." In this post I will review lectures four and five, which are on the topic of sorting.

The previous post covered a lecture on "Divide and Conquer" algorithm design technique and its applications.

Lecture four is devoted entirely to a single sorting algorithm which uses this technique. The algorithm I am talking about is the "**Quicksort**" algorithm. The quicksort algorithm was invented by Charles Hoare in 1962 and it is the most widely used sorting algorithm in practice.

I wrote about quicksort before in "Three Beautiful Quicksorts" post, where its running time was analyzed experimentally. This lecture does it theoretically.

Lecture five talks about theoretical running time limits of sorting using comparisons and then discusses two linear time sorting algorithms -- Counting sort and Radix sort.

## Lecture 4: Quicksort

The lecture starts by giving the divide and conquer description of the algorithm:

**Divide**: Partition the array to be sorted into two subarrays around pivot x, such that elements in the lower subarray <= x, and elements in the upper subarray >= x.**Conquer**: Recursively sort lower and upper subarrays using quicksort.**Combine**: Since the subarrays are sorted in place, no work is needed to combine them. The entire array is now sorted!

The main algorithm can be written in a few lines of code:

Quicksort(A, p, r) // sorts list A[p..r] if p < r q = Partition(A, p, r) Quicksort(A, p, q-1) Quicksort(A, q+1, r)

The key in this algorithm is the Partition subroutine:

Partition(A, p, q) // partitions elements in array A[p..q] in-place around element x = A[p], // so that A[p..i] <= x and A[i] == x and A[i+1..q] >= x x = A[p] i = p for j = p+1 to q if A[j] <= x i = i+1 swap(A[i], A[j]) swap(A[p], A[i]) return i

The lecture then proceeds with the analysis of Quicksort's worst-case running time. It is concluded that if the array to be sorted is already sorted or already reverse sorted, the running time is O(n^{2}), which is no better than Insertion sort (seen in lecture one)! What happens in the worst case is that all the elements get partitioned to one side of the chosen pivot. For example, given an array (1, 2, 3, 4), the partition algorithm chooses 1 as the pivot, then all the elements stay where they were, and no partitioning actually happens. To overcome this problem **Randomized Quicksort** algorithm is introduced.

The main idea of randomized quicksort hides in the Partition subroutine. Instead of partitioning around the first element, it partition around a random element in the array!

Nice things about randomized quicksort are:

- Its running time is independent of initial element order.
- No assumptions need to be made about the statistical distribution of input.
- No specific input elicits the worst case behavior.
- The worst case is determined only by the output of a random-number generator.

Randomized-Partition(A, p, q) <strong>swap(A[p], A[rand(p,q)])</strong> // the only thing changed in the original Partition subroutine! x = A[p] i = p for j = p+1 to q if A[j] <= x i = i+1 swap(A[i], A[j]) swap(A[p], A[i]) return i

The rest of the lecture is very math-heavy and uses indicator random variables to conclude that the **expected running time** of randomized quicksort is O(n·lg(n)).

In practice quicksort is 3 or more times faster than merge sort.

Please see Three Beautiful Quicksorts post for more information about the version of **industrial-strength quicksort**.

You're welcome to watch lecture four:

Topics covered in lecture four:

- [00:35] Introduction to quicksort.
- [02:30] Divide and conquer approach to quicksort.
- [05:20] Key idea - linear time (Θ(n)) partitioning subroutine.
- [07:50] Structure of partitioning algorithm.
- [11:40] Example of partition algorithm run on array (6, 10, 13, 5, 8, 3, 2, 11).
- [16:00] Pseudocode of quicksort algorithm.
- [19:00] Analysis of quicksort algorithm.
- [20:25] Worst case analysis of quicksort.
- [24:15] Recursion tree for worst case.
- [28:55] Best case analysis of quicksort, if partition splits elements in half.
- [28:55] Analysis of quicksort, if partition splits elements in a proportion 1/10 : 9/10.
- [33:33] Recursion tree of this analysis.
- [04:30] Analysis of quicksort, if partition alternates between lucky, unlucky, lucky, unlucky, ...
- [46:50] Randomized quicksort.
- [51:10] Analysis of randomized quicksort.

Lecture four notes:

## Lecture 5: Lower Sorting Bounds and Linear Sorting

The lecture starts with a question -- **How fast can we sort?** Erik Demaine (professor) answers and says that it depends on the model of what you can do with the elements.

The previous lectures introduced several algorithms that can sort n numbers in O(n·lg(n)) time. Merge sort achieves this upper bound in the worst case; quicksort achieves it on average. These algorithms share an interesting property: the sorted order they determine is based only on comparisons between the input elements. Such sorting algorithms are called **Comparison Sorts**, which is a model for sorting.

It turns out that any comparison sort algorithm can be translated into something that is called a **Decision Tree**. A decision tree is a full binary tree that represents the comparisons between elements that are performed by a particular sorting algorithm operating on an input of a given size.

Erik uses decision trees to derive the lower bound for running time of comparison based sorting algorithms. The result is that no comparison-based sort can do better than O(n·lg(n)).

The lecture continues with bursting outside of comparison model and looks at sorting in linear time using no comparisons.

The first linear time algorithm covered in the lecture is **Counting Sort**. The basic idea of counting sort is to determine, for each input element x, the number of elements less than x. This information can be used to place element x directly into its position in the output array. For example, if there are 17 elements less than x, then x belongs in output position 18.

The second linear time algorithm is **Radix Sort**, which sorts a list of numbers by examining each digit at a given position separately.

Erik ends the lecture by analyzing correctness and running time of radix sort.

Video of lecture five:

Topics covered in lecture five:

- [00:30] How fast can we sort?
- [02:27] Review of running times of quicksort, heapsort, merge sort and insertion sort.
- [04:50] Comparison sorting (model for sorting).
- [06:50] Decision trees.
- [09:35] General description of decision trees.
- [14:25] Decision trees model comparison sorts.
- [20:00] Lower bound on decision tree sorting.
- [31:35] Sorting in linear time.
- [32:30] Counting sort.
- [38:05] Example of counting sort run on an array (4, 1, 3, 4, 3)
- [50:00] Radix sort.
- [56:10] Example of radix sort run on array (329, 457, 657, 839, 546, 720, 355).
- [01:00:30] Correctness of radix sort.
- [01:04:25] Analysis of radix sort.

Lecture five notes:

Have fun sorting! The next post will be about order statistics (given an array find n-th smallest element)!

PS. This course is taught from the CLRS book (also called "Introduction to Algorithms"):

This week on Musical Geek Friday a song about the lovely cryptographic couple **Alice and Bob**!

Alice and Bob song is written by a guy MC Plus+. His real name is Armand Navabi and he's computer science Ph.D. student at Purdue University. His moniker is a pun on the name C++.

The song is about three different topics in cryptography.

First, the song talks about various archetypes used in cryptography. They are **Alice**, **Bob**, **Trent**, **Mallory** and **Eve**. Alice and Bob are the usual persons trying to communicate securely. They both trust Trent, who helps them communicate. Meanwhile Eve tries to eavesdrop their communication and Mallory tries to modify their messages!

Then, it covers cryptography algorithms -- the insecure 56-bit Data Encryption Standard (DES) algorithm, secure Advanced Encryption Standard (AES) algorithm, and not-so-secure Blowfish algorithm.

Lastly, the song reaches the problem of factoring numbers for which no algorithm in polynomial time is known.

This song is similar to the first song I ever posted on Musical Geek Friday -- Crypto.

Here it is! The **Alice and Bob** song:

[audio:http://www.catonmat.net/download/mc_plus_plus-alice_and_bob.mp3]

Download this song: alice and bob.mp3 (musical geek friday #14)

Downloaded: 21691 times

Download lyrics: alice and bob lyrics (musical geek friday #14)

Downloaded: 3480 times

Alice and Bob lyrics:

Alice is sending her message to Bob

Protecting that transmission is Crypto's job

Without the help of our good friend Trent,

It's hard to get that secret message sent

Work tries to deposit the check of your salary

But with no crypto, it'll be changed by Mallory

You think no one will see what it is, you believe?

But you should never forget, there's always an Eve...[Chorus]

'Cause I'm encrypting s**t like every single day

Sending data across the network in a safe way

Protecting messages to make my pay

If you hack me, you're guilty under DMCADES is wrong if you listen to NIST

Double DES ain't no better man, that got dissed

Twofish for AES, that was Schneier's wish

Like a shot from the key, Rijndael made the swish

But Blowfish is still the fastest in the land

And Bruce used his fame to make a few grand

Use ECB, and I'll crack your ciphertext

Try CFB mode to keep everyone perplexed[Chorus]

'Cause I'm encrypting s**t like every single day

Sending data across the network in a safe way

Protecting messages to make my pay

If you hack me, you're guilty under DMCARandom numbers ain't easy to produce...

Do it wrong, and your key I'll deduce

RSA, only public cipher in the game

Creating it helped give Rivest his fame

If we could factor large composites in poly time,

We'd have enough money to not have to rhyme

Digesting messages with a hashing function

Using SHA1 or else it won't cause disfunction[Chorus]

'Cause I'm encrypting s**t like every single day

Sending data across the network in a safe way

Protecting messages to make my pay

If you hack me, you're guilty under DMCAPassword confirmed. Stand by...

Download "Alice and Bob" Song

Download this song: alice and bob.mp3 (musical geek friday #14)

Downloaded: 21691 times

Download lyrics: alice and bob lyrics (musical geek friday #14)

Downloaded: 3480 times

Click to listen:

[audio:http://www.catonmat.net/download/mc_plus_plus-alice_and_bob.mp3]

Have fun and until next geeky Friday! :)

**20**Comments August 21, 2008

# MIT's Introduction to Algorithms, Lecture 3: Divide and Conquer

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 x^{n} is to express x^{n} as:

- x
^{n/2}·x^{n/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(n^{3}) running time, and an improved **Strassen's Algorithm** which reduces the number of multiplications yielding running time of approximately O(n^{2.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"):

**106**Comments August 19, 2008

# MIT's Introduction to Algorithms, Lectures 1 and 2: Analysis of Algorithms

I just finished watching the last lecture of MIT's "**Introduction to Algorithms**" course. Having a great passion for all aspects of computing, I decided to share everything I learned with you, my dear readers! This is the first post in an article series about this course.

As I wrote earlier, I am very serious about watching video lectures. If they are math-intensive, I usually take notes as if I were in the classroom. Lectures in this course were exactly like that -- logarithms, big-o's, thetas, expectations, and all the other math guys fighting with each other on the blackboards.

There are totally 23 video lectures, each around 1 hour 20 minutes long. I will be posting about 2 - 3 lectures at a time which will result in approximately 10 blog posts. Each post will contain annotated lecture, along with embedded video of the lecture and a time-stamped list of topics covered in the lecture. I will also post the notes I took myself as I watched the lectures (actually, I just bought a scanner (Canon CanonScan 4400F) just for this purpose!)

Understanding and designing effective algorithms is a very important skill for a top-notch programmer. You can still do good without knowing much about algorithms, but knowing them makes you superior. There are two kinds of people, those who can design effective algorithms and those who don't. ;)

Let's start with Lecture 1 of this course.

## Lecture 1: Analysis of Algorithms

The first lecture is given by the famous professor Charles E. Leiserson. He is the **L** in CLRS. If that doesn't ring you a bell - it's one of the most popular books on algorithms!

He starts the lecture by explaining what this course and algorithms will be all about. He says that this course will be about "**Analysis of Algorithms**" and states: "Analysis of algorithms is the theoretical study of computer program performance and resource usage".

Designing great software is not just about performance. Charles presents a list of 12 things that can be more important than performance. Just for comparison with algorithms guru, what do you think can be more important than performance?

Here is the list of things more important than performance that Charles presented:

- modularity,
- correctness,
- maintainability,
- security,
- functionality,
- robustness,
- user-friendliness,
- programmer's time,
- simplicity,
- extensibility,
- reliability, and
- scalability.

He also asks "Why study algorithms and performance at all?". He and students answer:

- Sometimes performance is correlated with user-friendliness.
- Performance draws line between feasible and unfeasible.
- Algorithms give language for talking about program behavior.
- Performance can be used to "pay" for other things, such as security, features and user-friendliness.

The lecture continues with the definition of the **Sorting Problem** - given a sequence (a_{1}, a_{2}, ..., a_{n}) of numbers, permute them in such a way that a_{1} <= a_{2} <= ... <= a_{n}.

There are various algorithms to solve this problem. Two algorithms are presented to solve this problems - one of them is **Insertion Sort** and the other is **Merge Sort**.

Running time of these algorithms is analyzed by introducing **Asymptotic Analysis** and **Recursion Trees**.

Here is the whole lecture:

Topics covered in lecture 1:

- [17:15] Main topic of the course - Analysis of algorithms.
- [19:00] What's more important than performance?
- [22:03] Why study algorithms and performance?
- [27:45] The sorting problem.
- [29:30] Insertion sort algorithm
- [34:30] Example of Insertion sort.
- [36:25] Running time of algorithms.
- [39:39] Definition of worst-case, average-case and best-case types of analysis.
- [46:50] How to analyze the Insertion sort's worst-case running time?
- [49:28] BIG IDEA - Asymptotic analysis.
- [50:49] Asymptotic notation - theta notation.
- [57:14] Insertion sort analysis.
- [01:02:42] Is Insertion sort fast?
- [01:03:40] Merge sort algorithm.
- [01:05:25] Example of Merge subroutine of Merge sort.
- [01:08:15] Analysis of Merge sort's running time.
- [01:10:55] Recurrence equation for Merge sort.
- [01:13:15] Recursion tree solution of the Merge sort's recurrence equation.

Lecture 1 notes:

## Lecture 2: Asymptotic Notation

Lecture 2, on the other hand, is given by genius professor Erik Demaine. He is the **youngest professor in the history of the MIT**! **He became professor at MIT at 20**! Wow!

This lecture is all about mathematical notation (Asymptotic Notation) used in the analysis of algorithms. It's the big-o notation, big omega notation, theta notation, small-o and small-omega notation.

The second half of the lecture is devoted to **solving recurrence equations**. Three methods are presented:

- Substitution method,
- Recursion-tree method, and
- The Master method.

Here is the whole lecture:

Topics covered in lecture 2:

- [01:25] Big-o (upper bounds) notation.
- [03:58] Set definition of big-o notation.
- [05:25] The meaning of O(h(n)) in notation f(n) = g(n) + O(h(n)).
- [10:20] Big-omega (lower bounds) notation.
- [11:40] Analogies of O, Ω and Θ to comparison operations of real numbers.
- [12:28] Theta (tight bounds) notation.
- [13:40] Small-o and small-omega notation.
- [17:03] Solving recurrences: substitution method.
- [37:56] Recursion-tree method.
- [49:00] The Master method.
- [01:02:00] Proof sketch of the Master method.

Lecture 2 notes:

Have fun absorbing all this information! Until next post!

Ps. It turned out that the lectures were not available anywhere but from MIT's OCW website. I found that they were released under CC license, which allowed me to upload them to Google Video, so I can embed them in the posts!

Pps. the lectures are taught from the CLRS book (also called "Introduction to Algorithms"):

**6**Comments August 15, 2008

# Musical Geek Friday #13: Song For Hackers and Crackers

After a few weeks of silence, the Musical Geek Friday is back! This time it's a song dedicated to all the hackers and the crackers!

The song is written and performed by a guy calling himself Zearle. It's very interesting to read his autobiography, where, among other crazy things, he says that during his childhood FBI was closing in on his family and his first years were spent underground, going from one small town to another. It's very interesting reading.

The Song for Hackers and Crackers was recorded in 1999 and it is included in his "Class War" music album (downloadable for free).

He says that the song was written as a praise for warez groups. Thanks to them, he was able to refine his computer skills and get software for making music.

The music tracks he uses in his songs are mostly created by unsigned hiphop beatmakers with R&B/soul, reggae, folk, and acid jazz backgrounds.

This song is **NSFW -- Not Suitable for Work**, as it contains explicit language. Though, you can still listen to it on your headphones :)

Contextually, this song is similar to another song I posted on Musical Geek Friday #2: Leech Access is Coming At You.

Here it is! **Song for Hackers and Crackers**:

[audio:http://www.catonmat.net/download/zearle-hackers_and_crackers.mp3]

Download this song: song for hackers and crackers.mp3 (musical geek friday #13)

Downloaded: 59925 times

Download lyrics: song for hackers and crackers lyrics (musical geek friday #13)

Downloaded: 3642 times

Song lyrics (I censored the explicit language, see the 'download lyrics' link at above for uncensored version):

UH! This is dedicated to all the hackers and the crackers.

You know what i'm sayin'.

If you hear this, want you to.. bump this and do your dirt dog, do your dirt.You can fill a stadium with cats using s**t cracked by Radium.

Liquid Sky will never die.

Before the crackers and the hackers, life was wacker.

Software we could never stack for.

We were floored by the evil hoards.

All the best s**t we could never afford.

The internet and mp3, they set us free.

I'm downloading Windows ME right now off FTP.

You b*****s will never find me!

Because I'm everywhere, see.

Yo dog, you got something new?

Hit me off on ICQ.

I'll help you back in jive.

I got some top-notch s**t on i-drive.

You shut one site down, we multiply.

Like a phoenix from a different place we rise.

Then you got the squares;

"Yo dude, your stealing, don't you care?"

L-O-L, b***h. I got a terabyte of warez.

I like my files zip, sit, or rar.

I download seven hundred segments from a thousand sites.

I drink coffee, snort speed; I'll stay up all night.

I'll hack a companies site, and blank it white.

Ghost ISP's; Find me, please...Dedicated to the hackers and the crackers.

This is dedicated to the hackers and the crackers.

The ones that set us free.

Thanks G for setting us free.

This is dedicated to the hackers and the crackers.I see in binary, I speak source code.

Step on my toes, I'll post a million jpg's.

If you in a fag pose.

And your digital stance getting firewalled hoe.

Cause I'm ridin' the net and my six four (old school car sixty four is the year).

I was a dick with my 56(K).

Now with my cable -- I'm able to get that stable.

On the out my name is Ace and I'm a Leo.

On the digital highway, my name is Neo and I'm a hero.

In a flash I'll school you on burning dreamcasts.

You need some ISO's?

Let me through my hard drive rifle.

Our exchange, you could never stifle.

With a digital hug, you just caught the lovebug.

I've bootlegged your CD.

I caused the fight between UN and Jay Z.

You see G?

It's all gonna be free.

Whether we take it with force or we take it nicely.

You feel that rattle in you bones?

When I tell you we just hacked DOW Jones?

And NASDAQ leaves you fighting on your back?Cause I'm the he who loves to hack and crack.

Cause I'm the he who loves to hack and crack.

This is dedicated to the hackers and the crackers.

Serial codes, source codes, ISO's, rar's, zip's, sit's.

This is dedicated to the hackers and the crackers.Your encryption is primitive egyptian.

I'll do more with a 486 and a Plextor.

We've won when six billion got Athlons.

And we tell each other how to get it on.

Cyber-army's and the Pentagon were storming.

I just found out who killed JFK.

The smoking gun will have it within a day.

All the lies they've been faster.

Go check the name "truth" on Napster.

Digital-disaster.

The p****s will never find me in this matrix.

A million keyboard voices all named Morpheus.

This is dedicated to those who set me free.

Got a buzz? Your always my cuz.

Radium, if i could only say to them; Thanks.

Kalisto, you know!

Utopia, and all my digital dogs.

My netgangs, my cybergangsters, my I/O-warriors, my computercomrades.

This is for you, this is dedicated to the hackers and the crackers.

I wouldn't be able to do s**t without ya'll man.

I'll be sittin' in front of my f**king computer doing a goddamn thing, playing games.

You have all made it possible. This is dedicated to you!

Download "Song for Hackers and Crackers"

Download this song: song for hackers and crackers.mp3 (musical geek friday #13)

Downloaded: 59925 times

Download lyrics: song for hackers and crackers lyrics (musical geek friday #13)

Downloaded: 3642 times

Click to listen:

[audio:http://www.catonmat.net/download/zearle-hackers_and_crackers.mp3]

Have fun and until next geeky Friday! :)