Divide and Conquer

来源:互联网 发布:哥们傲剑数据 编辑:程序博客网 时间:2024/06/10 13:13

Divide and Conquer

  1. Divide the problem (instance) into one or more sub-problems.
  2. Conquer each sub-problems recursively
  3. Combine solutions

Running Time:
T(n)=2T(n/2)+Θ(n)=Θ(nlog2n)
(n/2) is the size of sub-problems.
2 is the number of sub-problems.

find x in sorted array.
1. Divide : comparex with middle
2. Conquer : recurse in one sub-array
3. Combine :trivial
T(n)=T(n/2)+θ(1)=θ(log2n)

Powering a number

given number x and integer n0,computer xn

Naive algorithm:

xxxxxxxx...x=xn
θ(n) time

Divide-and-conquer algorithm(recursive squaring):

xn=xn/2xn/2 if n is even

T(n) =

{xn/2xn/2,x(n1)/2x(n1)/2x,if n is evenif n is odd

T(n)=T(n/2)+Θ(1)=Θ(log2n)

Fibonacci numbers

Fn =

1,1,Fn1Fn2,if n=1if n=2if n2

T(n)=Ω(ϕn) ϕ=(1+52)

Bottom-up algorithm

Compare F0 F1 F2Fn
Time:Θ(n)

Naive recursive squaring

Fn=ϕn/5 round it to the nearest integer.
So T(n)=Θ(log2n)
For some reasons, this algorithm can not be realized in practice.

Recursive squaring

Theorem
(Fn+1FnFnFn1)n=(1110)n

T(n)=log2n
Proof by induction

Matrix multiplication

Input: A=[aij] B=[bij]
i,j=1,2…,n
Output:C=[cij]=A*B

cij=nk=1aikbkj

Standard algorithm

Computing every element in the matrix takes Θ(n)
T(n)=Θ(n3)

Idea:
nn matrix=22 block matrix
of n/2*n/2 sub-matrixes
C=[rtsu]=[acbd]*[egfh]
A=[acbd] B=[egfh]

r=ae+bg
s=af+bh
t=ce+dg
u=cf+dh

8 recursive multiplications of n/2-by-n/2 matrixes
4 additions Θ(n2)
T(n)=8T(n/2)+Θ(n2)=Θ(n3)

Strassen’s algorithm

Idea:Reduce multiplication
p1=a(f-c)
p2=(a+b)h
p3=(c+d)e
p4=d(g-e)
p5=(a+d)(e+h)
p6=(b-d)(g+h)
p7=(a-c)(e+f)
r=p5+p4-p2+p6
s=p1+p2
t=p3+p4
u=p5+p1-p3-p7
“`

  1. Divide A B compute terms for products (a bunch of additions and subtractions) -Θ(n2)
  2. Conquer by recursively each pi
  3. Combine r,s,t,u -Θ(n2)

T(n)=7T(n/2)+Θ(n2)=Θ(nlog27)=O(n2.81)

VLSI layout

Problem: Embed a complete binary tree on n leaves in a grid with minimum area.
The vertices have to be embedded onto dots on the grid.
These edges have to be routed as sort of orthogonal paths between one dot and another.

complete binary tree

Naive embedding

naive embedding

H(n)=H(n/2)+Θ(1)=Θ(log2n)
W(n)=2W(n/2)+O(1)=Θ(n)
Area(n)=Θ(nlog2n)

H-layout

Goal:W(n)=Θ(n) H(n)=Θ(n) Area(n)=Θ(n)
这里写图片描述
Length(n)=2Length(n/4)+O(1)=Θ(n)
Area(n)=Θ(n)

0 0