算法分析(二)

算法分析

前言:开始对自己的学习做一个记录,看看自己的知识图谱是什么样子。如果可以帮助到其他人是也是一件非常开心的事,更多的是对学习的东西的一个整理。

学习地址:网易公开课的算法导论。链接:
http://open.163.com/special/opencourse/algorithms.html


Divde the Conquer(分治法)

1.Divide the problem into the one more subproblems
2.Conquer:each subproblem recursion
3.combine:solutions

注:分治法基本思路是把大问题分解成小问题,通过递归方式把小问题再度分解到可以解决程度,通过合并来完成问题的求解。递归是分治法中解决问题的方式并不等同于分治法。递归是 通过 for 改写。


Some problems are solved by Divde the Conquer.

Binary search(二分查找)

1.Divide:compore x with middle
2.Conquer:recurse x on sub array
3.combin : trival
T(n) = T(n/2) + θ(1) = θ(lgn)

Marge Sort(归并)

1.Divide:divided your array into two halves
2.Conquer:recursively sort each subarray
3.combin : linear time merge
T(n) = 2T(n/2) + θ(n) = θ(nlgn)

Powering number(乘方问题)

Give you a number x(int,double,float…) and give you a integer n (n>0),comput x^n

solve 1 :
x·x·x····x = x^n

solve 2 :

xn={xn/2xn/2x(n1)/2x(n1)/2xn ,n .

T(n) = T(n/2) + θ(1) = θ(lgn)

Fibonaci numbers(斐波那契数)

solve 1 : navie recursive squaring(朴素平方递归式)

Fn=01Fn1+Fn2n=0,n=1,n>=2.

T(n) = Ω(Φ^n) Φ =(1+√5)/2

solve2:bottom-up algorithms(自下而上计算)

注:通过自下而上计算来减少F(n-2)重复的部分

Compute F0,F1,F2,……,Fn

T(n) = θ(n)

solve3:not allowed
Fn = (Φ^n)/√5 round it to the nearest integer that is the nth Fibonacci number Φ =(1+√5)/2
注:这个方法是通过 Fibonaci numbers 的数学特性获取,当n取特别大时候,浮点数精度会影响结果。

solve4:squaring algorithmn(平方递归算法)

{Fn+1FnFnFn1}={1110}n

注:斐波那契数可由上述矩阵函数表示

证明如下:
假设 n=1 ,即

{F2F1F1F0}={1110}1={1110}

设 当 n>2 时,即
{FnFn1Fn1Fn2}={1110}n1{FnFn1Fn1Fn2}{1110}={1110}n1{1110}{Fn+Fn1Fn1+Fn2FnFn1}={1110}n{FnFn1Fn1Fn2}={1110}n

Matrix multiplications(矩阵乘法)
Input A[aij],B[bij] i,j = 1,2,3,4….mn
Output C=[cij]=AB

Cij=k=1naikbkj=0

//pseudocode伪代码
    for i<-1 to n
        do for j<-1 to n
            c_{ij}<-0
            do for k<- to n
                Cij<-Cij +aik·bki

可以通过矩阵切割实现
Idea : n*n matrix = 2*2 block matrix of n/2 * n/2 of matrix

{rtsv}={acbd}{egfh}

r = ae + bg
s = af + bh
t = ce + gd
v = cf + dh

8 recvrsive multiplication of n/2-by-n/2 matrixex
4 addtions -> θ(n^2)
T(n) = 8T(n/x) + θ(n^2) = θ(n^3)

Strassen’s algorithm
Strassen算法可以相比矩阵运算,减少一次乘法运算
P1=a(f-h)
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 compcte terms for producfs θ(n^2)
2.Confuer recursively computing all the P1….P7
3.Combine r,a,t,v
T(n)=7T(n/2)+θ(n^2)=θ(nlg7)=O(n^2.81)

VLSI Layout(集成电路问题)

Problem Embed complete binary tree on n leaves in a gird with minmum area

Naive embedding
完全二插树

H(n)=H(n/2)+1=θ(lgn)
W(n)=2W(n/2)+1=θ(n)
Area=θ(nlgn)

Gool:想要实现Area=θ(n) 即猜想:
H(n)=θ(√n)
W(n)=θ(√n)
则T(n)=2T(n/4)+θ(n^(1/2-e))
图为:
这里写图片描述
L(n)=2L(n/4)+θ(1)=θ(√n)

注:

aT(n/b)=θ(lognb)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值