Divide and Conquer
Last updated
Last updated
Divide and conquer refers to a class of algorithmic techniques in which one breaks the input into several parts, solves the problem in each part recursively, and then combines the solutions to these subproblems into an overall solution.
Mergesort sorts a given list of numbers by first dividing them into two equal halves, sorting each half separately by recursion, and then combining the results. The input has been reduced to size 2.
Let denote the worst-case running time on input instance of size .
The algorithm takes time to split the inputs into two halves.
The algorithm takes to solve each half.
The algorithm takes to combine the solutions.
Recurrence relation: for , for some constant .
Unrolling: Find the running time of the first few levels and identify a patten that can be continued as the recursion expands.
Substituting: Guess for a solution and substitute it into the recurrence relation to check whether it works with an argument by induction on $ n $.
Analyzing the first few levels: The third level has four problems each of size , each taking time at most , for a total of at most .
Identifying a pattern: The level contributes a total of at most to the total running time.
Summing over all levels: The number of levels is . Therefore, the function is bounded by when .
Suppose the relation satisfies the recurrence relation.
Base step: .
Inductive step: . Therefore, the function is bounded by when .
The general form of the divide-and-conquer algorithm is creating subproblems of size each, and combine the results in time. For some constant , when , and .
The first few levels: The first level takes at most time. The second level takes at most $ time. The third level takes at most time.
Identifying a pattern: At level , the total work performed is .
Summing over all levels: .
Therefore, any function satisfying the recurrence relation with is bounded by .
The first few levels: The first level takes at most time. The second level takes at most $ time. The third level takes at most time.
Identifying a pattern: At level , the total work performed is .
Summing over all levels: .
Therefore, any function satisfying the recurrence relation with is bounded by .
Consider a variant form of divide-and-conquer algorithm: Divide the input into two pieces of equal time, solve them by recursion, and combine the resutls in quadratic time. For some constant , when , and .
The first few levels: The first level takes at most time. The second level takes at most $ time. The third level takes at most time.
Identifying a pattern: At level , the total work performed is .
Summing over all levels: .
Given points in the plane, find the pair that is closest together.
Let the set of points be , where has coordinats . The Euclidan distance between two points is . The algorithm holds the assumption that no two points in have the same x-coordinate or the same y-coordinate.
Let be the input of each recursion. Let the list be all the points in that have been sorted by increasing x-coordinate, be all the points in that have been sorted by increasing y-coordinate.
For each level of recursion, is defined as the set of points in the first positions of and is defined as the set of points in the final positions of the list .
By a single pass through and , four lists could be produced: . , , . Suppose that the closest pairs of points in and are q^\*_0 and q^\*_1, r^\*_0 and r^\*_1.
Let be the minimum of d(q^\*_0, q^\*_1) and d(r^\*_0, r^\*_1). Let be the vertical line that crosses through the rightmost point of $ Q $.
If there exists and $ r \in R d(q, r) < \alpha q r \alpha L $$.
Let be the set of points in this narrow band and be the points in that sorted with their y-coordinates. Thus, there exist and for which if and only if there exists for which .
If for which , then and are within 15 positions of each other in the sorted list . Therefore, for each point , the algorithm computes its distance to each of the next 15 points in . If there's a pair of points for which , they are the closest pair of points in . Otherwise, the closet pair is the minimum of d(q^\*_0, q^\*_1) and d(r^\*_0, r^\*_1).
The algorithm correctly outputs a closest pair of points in P. Since the algorithm divides the input into left and right halves, and merges the solutions in linear time, it satisties the basic recurrence and the time complexity is .
Basic step: When , the closest pair could be computed by brute-force.
Induction step: Assume that the remainder of the algorithm correctly determines the closest pair of a given input, then the closest pair in either has both elements in one of or , or it has one element in each.