问题的分解及分而治之算法

给我们一个问题应该怎么观察:

1,先从最简单的case入手
2:,观察INPUT的关键数据结构
3,我们还得看能不能合上,看的是OUTPUT,就是把子问题的output变成原问题的output

select problem:the K-th smallest items in an array

INPUT:
An array A = [A0, A1, …, An−1], and a number k < n;
OUTPUT:
The k-th smallest item in general case (or the median of A as a
specical case)
举例子,我们要求一个数组的中位数,我们可以先对他进行排序后再取中位数,这样我们用了O(nlogn)的时间,但实际上我们可以做的更快,不用排序就可以找到。用divide and conquer我们可以到达线性的时间。
我们开始从简单的例子入手:
当n=2时,就两个数很容易得到,进行一次比较
当n=3,4,5,看能不能分,关键是看他的input,input是一个数组,从中砍一刀还是数组
我们分完之后需要看能不能合起来,

伪代码

 
Select(A, k)
 Choose an element Ai from A as a pivot;
 S+ = {};
 S= {};
 for j = 1 to n do
 if Aj > Ai then
 S+ = S+{Aj};
 else
 S= S− ∪ {Aj};
 end if
 end for
 if |S| = k − 1 then
 return Ai;

在这里我们选择了一个pivot,大于pivot放在一边,小于pivot放在另外一边,这样,第k小的元素肯定在某一个里面。这个跟快排特别像,有一点不一样的是我们不需要再接下来的两个数组里在进行调用,我们要么在S+里面要么在S_里面调用。
我们如果想做的更快就需要看子问题的size,子问题的size越小,我们的时间就会越少。因此接下来我们来想一下如何把子问题的规模变小。
在这里插入图片描述
假设选了最小的或者最大的元素作为pivot,我们的子问题仅仅降了一个元素,这样就是最坏的情况,Worst choice: select the smallest element at each iteration.T(n) = T(n − 1) + O(n) = O(n2)
如果我们选择了中间的元素,那么Best choice: select the median at each iteration.T(n) = T(n2) + O(n) = O(n),这是指数级下降
如果我们不选最好的也不选最坏的,我们选靠近中间的数,看图。假设我选择了在四分之一位置的数字,那么比我大的是四分之三,比我小的是四分之一,虽然没有变成一半,但也是指数级下降
Good choice: select a nearly-central element Ai, i.e.,|S+| ≥ n, and |S−| ≥ n for a fixed > 0.T(n) ≤ T((1 − )n) + O(n)≤ cn + c(1 − )n + c(1 − )2n + …= O(n)
通过上面的讨论,我们选择pivot的时候只要靠近中心就可以变成O(n),那么 如何得到靠近中心的点呢?

Strategy 1: BFPRT algorithm uses median of medians as pivot

在这里插入图片描述
一共55个数字,5个一组,每组排序,找出每一组的中位数,32是从每一个组里面选择的中位数,从图上可以看出比32 大的数字是红色的那一块,比32小的是蓝色的那一块,也就是说有n/5个组,n/10的group在32的右边,3n/10的数字大于32,7n/10小于32

Select(A, k)
1: Line up elements in groups of 5 elements;
2: Find the median of each group; //Cost 6
5
n time
3: Find the median of medians (denoted as M) through recursively
running Select over the group medians

T(n) ≤ T(n/5) + T(7n/10 ) + O(n) = O(n)

Matrix Multiplication problem: to multiply two matrices

在这里插入图片描述

ClosestPair problem: given a set of points in a plane, to find the closest pair

INPUT: n points in a plane;
OUTPUT: the pair with the least Euclidean distance;

这种我们可以用两个for循环解这个问题,时间复杂度为O(n2),我们可不可以做的更快?

Divide into 2 halves
It is easy to achieve this through sorting by x coordinate first,
and then select the median as pivot.
我们很容易分成两部分,通过对坐标x排序,选出中间的点进行划分。
在这里插入图片描述
就这样一直分,到最后我们就要考虑怎么合并
Divide: dividing into two (roughly equal) subsets;
Conquer: finding closest pairs in each half;
在这里插入图片描述
假设我们在两边已经找到了最近的点对,一个是21,一个是12,左边子问题和右边子问题都已经求解完了,现在需要找整体的问题。现在我们就差最后一种情况,就是最近点对是一个在左,一个在右。我们看一下中间是否有最小点对
在这里插入图片描述
中间的最近点对是8,那么我们如何求出中间的最近点对呢
我们已经知道两边最小的点对是12,我们只需要知道中间的点对是否小于12就可以
在这里插入图片描述
因此我们就看着个灰色条带里的点就可以,现在我们开始比较,(Moreover, it is unnecessary to explore all point pairs in the2δ-strip.)发现如果一个一个的比较会有冗余,这样效率还是不高,我们可以 divide the 2δ-strip into grids (size: δ2 ×δ2).
在这里插入图片描述
If two points are 2 rows apart, the distance between them should be over δ and thus cannot construct closest-pair.
Example: For point i, it suffices to search within 2 rows for possible closest partners (< δ)
在这里插入图片描述
Green: point i;要比较的点
Red: the possible closest partner (distance < δ) of point i;可能跟目标点离得最近的点。
If all points within the strip were sorted by y-coordinates, it suffices to calculate distance between each point with its next 11 neighbors
我们将这些点进行按Y轴排序

ClosestPair(pi, ..., pj) /* pi, ..., pj have already been sorted according to x-			    coordinate; */
 if j − i == 1 then
 return d(pi, pj);
 end if
 Use the x-coordinate of p[i+j/2⌋to divide pi, ..., pjinto two halves;
 δ1 = ClosestPair(left half);                    **T(n2)**
 δ2 = ClosestPair(right half);                     **T(n2)**
 δ = min(δ1, δ2);
 Sort points within the 2δ wide strip by y-coordinate; **O(n log n)**
 Scan points in y-order and calculate distance between each point with its next 11 neighbors. Update δ if finding a distance less than δ; **O(n)**

Time-complexity: T(n) = 2T(n2) + O(n log n) = O(n log2 n)

ClosestPair: an example with 8 points

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
We calculated distances for only 9 pairs of points (see ‘blue’ line). The other 19 pairs are redundant due to: at least one of the two points lies out of 2δ-strip.
although two points appear in the same 2δ-strip, they are at
least 2 rows of grids (size: δ2 ×δ2) apart.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值