Divide-and-conquer A fundamental
posted on 23 Jun 2008 10:47 by vicsviper in AlgorithmOn sunday i have a class at Thammasart University about Algorithm. The lesson told about how to solve a problemand sorting data by division,the 1 easy way to sorting data in computer system.
The divide-and-conquer strategy consists in breaking a problem into simpler subproblems of the same type, next to solve these subproblems, finally to amalgamate the obtained results into a solution to the problem. Thus it is primarily a recursive method. The algorithms of this type display two parts; the first one breaks the problem into subproblems, the second one merges the partial results into the total result.
Sorting Mergesort provides a clear example of this strategy. The data is a list of items (one also uses the word key in place of item) taken in a set endowed with a total order; the aim is to obtain a list made up of these same elements but arranged according to the order. The algorithm reads as follows:
- the list is partitioned into two half-lists;
- each half-list is sorted;
- the two sorted half-lists are merged.
The basic case of the algorithm is when the list contains only one key.
The implementation of the algorithm depends on the data-structure that is selected to represent the list of items; we suppose that the lists are represented by tables, one could just as easily use linked lists. In a first version, which is an pseudo-algorithm, one merges two sorted lists into a third one. This haves the disadvantage of create a lot of lists. By a program transformation one creates the merged list in place, with the help of an auxiliary list. In practice it is sufficient to merge two sorted lists represented by subintervals of a table, these subintervals being contiguous and of the form [i,j) and [j,k). In the Pascal procedure which follows these integers are thus the parameters, and the table is a global variable.
Multiplication. The multiplication of structured objects provides another application of the divide-and-conquer strategy. A positive integer has an expansion
with respect to the radix B, if it satisfies
.
This integer is seen as a table of digits. In the multiplication of two integers n and m according to Karatsuba's method the tables which represent them are broken into two parts of approximatively equal lengths. This gives the equalities
.
The following products are computed
.
Finally the product is obtained
nm=y B2k+ (x-y-z)Bk+z.
Of course products x, y and z are computed using the same strategy. Notice that the algorithm carries out three multiplications where the naive algorithm needs four. This is here the interest of the divide-and-conquer strategy lies.
The multiplication of polynomials is treated in the same way; the implementation is even simpler because there is no carry to take into account . In the same spirit the algorithm of Strassen computes the product C=AB of two matrices A and B broken up into four blocks,
.
The evaluation of the seven products
yelds the matrix C,
Again it is noticed that the algorithm uses seven multiplications where the naive algorithm needs eight. This leads us to expect a better behavior on large data; the phenomenon will be quantified in the last section.
Geometry. Computational geometry uses the divide-and-conquer strategy too. Let us quote for example the search of the convex hull of a finite set of points or the construction of its Voronoï diagram . In the search for the convex hull of a family of n points in a plane, one proceeds as follows. First, the points are sorted according to their abscissae, and this arranges them in a list p1,
, pn. Next, the list is broken into two sublists of the same length p1,
, pk and pk+1,
, pn. The convex hulls of the two sublists are recursively built. The stopping case is the case where a list comprises at most three points. Finally the two convex hulls are merged. The difficulty lies in this last operation. To achieve it the algorithm proceeds as follows. The higher bridge and the lower bridge between the two convex hulls are built. The higher bridge is obtained in the following way. The point pk is the rightmost point of the hull on the left-hand side, and pk+1 is the leftmost point of the hull on the right-hand side. Two variables l and r are introduced. They are initialized respectively with the values pk and pk+1. The line which join l and r is denoted by L. If the point of the convex hull on the left-hand side which follows l counterclockwise is above L, then l is replaced by its successor. Likewise, if the point of the convex hull on the right-hand side which precedes r counterclockwise is above L, then r is replaced by its predecessor. Both actions are repeated as long as possible. At the end the line L provides the higher bridge. The lower bridge is obtained in the same way.
edit @ 23 Jun 2008 10:57:53 by vicviper