問題的分解及分而治之算法

給我們一個問題應該怎麼觀察:

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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章