D&C examples

Quicksort

Strassen's algorithm for matrix multiplication

(AHU p.230-231, Baase p.526-528, CLR p.75-82)

To multiply two n×n matrices normally takes n scalar multiplications and n-1 scalar additions for each of n2 elements, for a total of Θ(n3) scalar multiplications and Θ(n3) scalar additions.

Strassen, using divide-and-conquer, was able to do better!   He found a way of multiplying two n×n matrices using 7 scalar multiplications and 18 scalar additions instead of the normal 8 scalar multiplications and 4 scalar additions.

This might not seem like great progress, but look:

T(n) = 7T(n/2) + 18(n/2)2

It is easy to show that, for some constant k, T(n) ≤ knlg 7 = O(n2.81).

The skyline problem

Given the location and height of rectangular buildings, we wish to draw the 2-d skyline, eliminating hidden lines.

We express the outline of a single building as a triple {L,H,R}, where L,R are the left and right extents of the building and H is its height.

Example:   We are given 4 buildings with outlines

{1,11,5}
{2,6,7}
{3,13,9}
{12,7,16}

The outline of the collection of these 4 buildings is

{1,11,3,13,9,0,12,7,16}

How to solve this problem?

One solution is to add building one at a time.   It takes O(n) time to find where the left and right ends of the new building are located relative to the buildings already in-place and to adjust the heights in between to become the maximum of {what was there before, the height of the new building}.   The total time complexity to handle n buildings will be O(n2).

A faster solution uses divide-and-conquer:

T(n) = 2T(n/2) + O(n)

→ T(n) = O(n lg n)


Dan Hirschberg
Last modified: Oct 28, 2003