Divide-and-Conquer

Algorithm Algorithm Divide and Conquer

if sizeof(P)\text{sizeof}(P) is small then

return sol(P)\text{sol}(P)

else

Split PP into P1,P2,P3,P_1, P_2, P_3, \ldots

for i1i \gets 1 to nn do

siDC(Pi)s_i \gets \text{DC}(P_i)

end for

return combine(s1,s2,)\text{combine}(s_1, s_2, \ldots)

end if

Example By 2











Example By 2 and Solve it With n









Maximum Contiguous Subarray Problem

  • Input: R = [1 ... n] is an array of n integers
  • Output: R = R [i...j] is a subarray of R, s.t.
    • A pair of two integer
    • sum (R)=Sum(R[i...j]) is maximized
  • Respect to
    • sum(R[i...j])=
  • Maximum
  • Subarray of R

Algorithm ReuseDataMCS ( R )

  • Input: An array R of n integers
  • Output: The value of MCS
VMAX = R[1],mi=0,mj=0
for i = 1 to N do 
	V = 0 
	for j = i to N do 
		// calculate V ( i, j )
		V = V + R [ j ] 
		if V > VMAX then 
			VMAX = V
			mi=i
			mj=j 
		end if
	end for
end for 
return VMAX

Algorithm Maximum Contiguous Subarray Problem {Brute Force Reuse Data}

VMAX=R[1]VMAX = R[1]

mi=0m_i = 0

mj=0m_j = 0

for i=1i = 1 to NN do

V=0V = 0

for j=ij = i to NN do

//calculate V(i,j)V(i, j)

V=V+R[j]V = V + R[j]

if V>VMAXV > VMAX then

VMAX=VVMAX = V

mi=im_i = i

mj=jm_j = j

end if

end for

end for

return VMAXVMAX

MCS(R, i, j)

  • Input: R[i...j]
  • Output: MCS of R[i...j]

Algorithm Maximum Contiguous Subarray Problem

if i=ji = j then

return R[i]R[i]

else

s1MCS(R,i,(i+j)/2)s1 \gets \text{MCS}(R, i, \lfloor(i+j)/2\rfloor)//Using floor function for lower rounding

s2MCS(R,(i+j)/2+1,j)s2 \gets \text{MCS}(R, \lfloor(i+j)/2\rfloor + 1, j)

AMaxSuffix(R,i,(i+j)/2)+MaxPrefix(R,(i+j)/2+1,j)A \gets \text{MaxSuffix}(R, i, \lfloor(i+j)/2\rfloor) + \text{MaxPrefix}(R, \lfloor(i+j)/2\rfloor + 1, j)

return MAX(s1,s2,A)\text{MAX}(s1, s2, A)

end if

  • Cpp 解法
class Solution {
public:
	int maxPreffix(vector<int>&nums,int i ,int j){
		int ans=nums[i];
		int tmp=0;
		for(;i<=j;i++){
			tmp+=nums[i];
			if(tmp>ans){
				ans=tmp;
			}
		}
		return ans;
	}
	int maxSuffix(vector<int>&nums,int i ,int j){
		int ans=nums[j];
		int tmp=0;
		for(;i<=j;j--){
			tmp+=nums[j];
			if(tmp>ans){
			ans=tmp;
			}
		}
		return ans;
	}
	int MSA(vector<int>&nums,int i ,int j){
		if(i==j){
			return nums[i];
		}
		else{
			int S1=MSA(nums,i,(i+j)/2);
			int S2=MSA(nums,(i+j)/2+1,j);
			int A = maxSuffix(nums,i,(i+j)/2)+maxPreffix(nums,(i+j)/2+1,j);
			return max({S1,S2,A});
		}
	}
	int maxSubArray(vector<int>& nums) {
		return MSA(nums,0,nums.size()-1);
	}
};

Polynomial Multiplication

Definition: A x B
is the multiplication of A x B s.t
(i+j=k)0\leq k\leq n+m$$

  • Brute Force

Divide and Conquer

Algorithm Polynomial Multi1(A(x),B(x)A(x),B(x))

if n=0n=0 then

return a0×b0a_0 \times b_0

else

A0(x)=a0+a1x++an21xn21A_0(x) = a_0 + a_1x + \dots + a_{\lfloor \frac{n}{2} \rfloor - 1}x^{\lfloor \frac{n}{2} \rfloor -1}

A1(x)=an2+an2+1x++anxnn2A_1(x) = a_{\lfloor \frac{n}{2} \rfloor} + a_{\lfloor \frac{n}{2} \rfloor +1}x + \dots + a_nx^{n-\lfloor \frac{n}{2} \rfloor}

B0(x)=b0+b1x++bn21xn21B_0(x) = b_0 + b_1x + \dots + b_{\lfloor \frac{n}{2} \rfloor - 1}x^{\lfloor \frac{n}{2} \rfloor -1}

B1(x)=bn2+bn2+1x++bnxnn2B_1(x) = b_{\lfloor \frac{n}{2} \rfloor} + b_{\lfloor \frac{n}{2} \rfloor +1}x + \dots + b_nx^{n-\lfloor \frac{n}{2} \rfloor}

U(x)=PolyMult1(A0(x),B0(x))U(x) = \text{PolyMult1}(A_0(x), B_0(x))

V(x)=PolyMult1(A0(x),B1(x))V(x) = \text{PolyMult1}(A_0(x), B_1(x))

W(x)=PolyMult1(A1(x),B0(x))W(x) = \text{PolyMult1}(A_1(x), B_0(x))

Z(x)=PolyMult1(A1(x),B1(x))Z(x) = \text{PolyMult1}(A_1(x), B_1(x))

return U(x)+(V(x)+W(x))xn2+Z(x)x2n2U(x) + (V(x) + W(x))x^{\lfloor \frac{n}{2} \rfloor} + Z(x)x^{2\lfloor \frac{n}{2} \rfloor}

end if

  • Time Complexity
    Example By 2

Algorithm Polynomial Multi2(A(x),B(x)A(x),B(x))

if n=0n=0 then

return a0×b0a_0 \times b_0

else

A0(x)=a0+a1x++an21xn21A_0(x) = a_0 + a_1x + \dots + a_{\lfloor \frac{n}{2} \rfloor - 1}x^{\lfloor \frac{n}{2} \rfloor -1}

A1(x)=an2+an2+1x++anxnn2A_1(x) = a_{\lfloor \frac{n}{2} \rfloor} + a_{\lfloor \frac{n}{2} \rfloor +1}x + \dots + a_nx^{n-\lfloor \frac{n}{2} \rfloor}

B0(x)=b0+b1x++bn21xn21B_0(x) = b_0 + b_1x + \dots + b_{\lfloor \frac{n}{2} \rfloor - 1}x^{\lfloor \frac{n}{2} \rfloor -1}

B1(x)=bn2+bn2+1x++bnxnn2B_1(x) = b_{\lfloor \frac{n}{2} \rfloor} + b_{\lfloor \frac{n}{2} \rfloor +1}x + \dots + b_nx^{n-\lfloor \frac{n}{2} \rfloor}

Y(x)=PolyMult2(A0(x)+A1(x),B0(x)+B1(x))Y(x) = \text{PolyMult2}(A_0(x)+A_1(x), B_0(x)+B_1(x))

U(x)=PolyMult2(A0(x),B0(x))U(x) = \text{PolyMult2}(A_0(x), B_0(x))

Z(x)=PolyMult2(A1(x),B1(x))Z(x) = \text{PolyMult2}(A_1(x), B_1(x))

return U(x)+(Y(x)U(x)Z(x))xn2+Z(x)x2n2U(x) + (Y(x) - U(x) - Z(x))x^{\lfloor \frac{n}{2} \rfloor} + Z(x)x^{2\lfloor \frac{n}{2} \rfloor}

end if