Divide and Conquer
Introduction
-
Recursively:
- Divide the problem into a number of sub-problems
- Conquer the sub-problems by solving them recursively
- Combine the solutions to the sub-problems into the solution for the original problem
-
General recurrence:
-
Cases solved by divide and conquer
- The maximum subsequence sum – the solution
- Tree traversals –
- Mergesort and quicksort – $O( N\log N ) $
Closest Points Problem
- Given points in a plane. Find the closest pair of points. (If two points have the same position, then that pair is the closest with distance 0.)
Simple Exhaustive Search
- Check pairs of points
Divide and Conquer
- Step1: Sort according to x-coordinates and divide
- Step2: Conquer by forming a solution from left, right, and cross
-strip
- is the smallest distance
-
If NumPointInStrip = , we have
1
2
3
4
5/* points are all in the strip */
for (i = 0; i < NumPointsInStrip; i++)
for (j = i+1; j < NumPointsInStrip; j++)
if (Dist(Pi, Pj) < delta)
delta = Dist(Pi, Pj);The worst case: NumPointInStrip = N
-
Consider both vertical and horizontal -strip
1
2
3
4
5
6
7
8/* points are all in the strip */
/* and sorted by y coordinates */
for (i = 0; i < NumPointsInStrip; i++)
for (j = i+1; j < NumPointsInStrip; j++)
if ( Dist_y(Pi, Pj) > delta)
break;
else if (Dist(Pi, Pj) < delta)
delta = Dist(Pi, Pj);The worst case: 8 points sitting all at corners
For any , at most 7 points are considered
-
Three methods for solving recurrences
- Details to be ignored:
- if is an integer or not
- always assume for small
Substitution Method 代入法
-
猜测解的形式,然后用数学归纳法求出解中的常数并证明
-
Example:
-
Guess:
-
Proof: Assume it is true for all , in particular for
Then there exists a constant so that
Substituting into the recurrence:
-
-
需要证出与归纳假设严格一致的形式
-
改变变量
- 令,
- 令,
Recursion-tree Method 递归树法
- 用来生成好的猜测
- Proof by substitution:
Master Method 主方法
[Master Theorem] Let and be constants, let be a function, and let be defined on the nonnegative integers by the recurrence , then:
- If for some constant , then
- If , then
- If for some constant , and if for some constant and all sufficiently large , then
- Master Method cannot cover all the cases of
[Master Theorem] The recurrence can be solved as follows:
- If for some constant , then
- If , then
- If for some constant , then
[Theorem] The solution to the equation where , and is
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 OE.Heart's Blog!