XJTLU————CSE102 Algorithm总结:

CSE102 Algorithm总结:

一、 什么是算法? 为什么需要算法?

(What is algorithm? And why we need algorithm?)

1、  What is algorithm?

Algorithm design is a foundation forefficient and effective program.

2、  Why we need algorithm?

Algorithm is a sequence of precise andconcise instructions that guide you or computer to solve a class of specificproblem.

二、 渐近分析(asymptotic analysis)

1、  时间复杂度分析(Time complexity analysis)

时间复杂度分析的核心就是计数,即计算算法每个语句执行的数量。

2、  空间复杂度分析(Space complexity analysis)

空间复杂度中的空间指的是内存(包括物理内存、虚拟内存但不包括磁盘),一个程序占用的内存越少程序更优。

3、  量级与渐近线(magnitude and asymptotic)

如果一个算法比另外一个算法多一个步骤,我们不会认为这两个算法的效率有什么不同,多两个步骤也是如此,但是如果多100个,1000个效率就有区别了,我们怎么来区别这些效率呢,这时我们就需要在某个地方画一条线,此线范围内的算法的效率我们认为差不多,而超过这个范围的就是效率有很大的不同。因此,我们就引出了量级的概念(量级包括:常数级、对数级、多项式级、和指数级)。

注: 这里的渐进分析我们只观察当输入尺寸n趋向无穷时的算法表现。

Example:

Show that 2n2+4n is O(n2)

Solution:

Since n≤n2for all n>1

We have 2n2+4n ≤ 2n2+4n2≤6n2for all n>1

Therefore, by definition, 2n2+4nis O(n)

4、  量级效率大小:

O(1) < O(logn)<O(n)<O(n2)<….<O(nk)<O(2n)

三、 搜索与排序算法(Searching and sort)

1、  二分搜索(binary search):

注意二分搜索的输入数组必须是按从小到大排序的。

(1)     pseudo code:

binarySearch(Array[0…n-1], first, last, X)

for i=0 to n-1 do

begin

                  midß (first+last)/2

                  iffirst>=last then

                                    returnnot found

                  ifa[mid]=X then

                                    returnmid

                  ifa[mid]>X then

                                    returnbinarySearch(Array, first, mid-1, X)

                  ifa[mid]<X then

                                    returnbinarySearch(Array, mid+1, last, X)

end

(2)时间复杂度:

Best case: O(1)

Worst Case: O(logn)

2、  冒泡排序(Bubble Sort):

(1)   Pseudo code:

bubbleSort(Array[0…n-1])

for iß0 ton-1 do

begin

                  forjßI to n-1 do

                  begin

if Array[j]>Array[j+1] then

                                                      tempßArray[j]

                                                      Array[j]ßArray[j+1]

                                                      Array[j+1]ßtemp

                  end

end

(2)   Time complexity:

O(n2)

3、  插入排序(Insertion Sort):

(1)     pseudo code:

insertionSort(array(0…n-1))

for iß1 ton-1 do

begin

                  kßarray[i]

                  pßi

while p>0 andarray[p-1]>k do

               array[p]ßarray[p-1]

               pßp-1

array[p]ßkey

end

return array[0…n-1]

(2)     Time complexity:

O(n2)

4. 选择排序(SelectionSort):

(1)Pseudo code:

selectSort(Array[0…n-1])

for iß0 ton-1 do

begin

                  minßi

                  forjßi+1 to n-1 do

                                    ifa[j]<a[min] then

                                                      minßj

                  swapa[min] and a[i]

end

return array

(2)Time complexity:

O(n2)

四、 分治法(Divide and conquer)

1.    What is divide and conquer?

A problem instance is divided into severalsmaller instances of the same problem. And the smaller instances are solved. Atlast, the solutions for the smaller instances are combined to get a solution tothe original problem.

2.    (1) Calculate the timecomplexity of binary search with recursive formula:

1                   if n = 1

T(n) =

       T(n/2)+1   otherwise

Solution:

                  T(n/2)+1

  =(T(n/4)+1)+1

 =T(n/4)+2

     .

    .

    .

 =T(n/2k)+k

Since T(1) = 1

n/2k = 1

2k=n

K = logn

So when k = log n

T(n) = T(1) + logn = 1 + logn

Thus, the time complexity is O(logn)

(2)Show that T(n) = O(logn) by the substitutionmethod. (Hint: show that T(n)≤2logn for n≥2 by Mathematical induction)

Solution:

When n=1,

L.H.S = T(1) = 1;  R.H.S = clogn=clog1=0

Because o<1 then the statement is false

Yet, when n=2

L.H.S = T(2) = T(1)+1 = 2;  R.H.S = clogn = clog2 = c

when c = 2, R.H.S = 2

L.H.S≤R.H.S

Make a guess, T(n) ≤2logn

Assume true for all n’<n T(n/2)≤2log(n/2)

So T(n)=T(n/2)+1

                  ≤ 2log(n/2)+1

=2(logn-log2)+1

=2(logn-1)+1

=2logn-1

<2logn

Thus, guess istrue T(n)≤2logn

3.    合并排序(Merge Sort)

合并排序用到了归并思想。

(1)   Pseudo code:

mergeSort(array[0…n-1],p,q)

if p<q then

                  midß (p+q)/2

                  mergeSort(array,p,m)

                  mergerSort(array,m+1,q)

                  conquer(array,p,m,q)

else

                  returnarray

 

conquer(array[0…n-1],p,m,q)

p1ßp

p2ßm+1

cntß0

make b[0…100] be a new array

while p1<=m and p2<=q do

begin

                  ifa[p1]<a[p2]

                                    b[cnt]ßa[p1]

                                    p1ßp1+1

                                    cntßcmt+1

                  else

                                    b[cnt]ßa[p2]

                                    p2ßp2+1

                                    cntßcnt+1

end

if p1>m then

                  forißp2 to q do

                  begin

                                    b[cnt]ßa[i]

                                    cntßcnt+1

                  end

if p2>q then

                  forißp1 to m do

                  begin

                                    b[cnt]ßa[i]

                                    cntßcnt+1

                  end

for iß0 tocnt do

begin

                  a[i+p]ßb[i]

end

return a

(2)   Time complexity:

O(nlogn)

The recurrent formula is:

1               if n=1

T(n) =      

       2T(n/2) + n   otherwise

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码下载:完整代码,可直接运行 ;运行版本:2022a或2019b或2014a;若运行有问题,可私信博主; **仿真咨询 1 各类智能优化算法改进及应用** 生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化 **2 机器学习和深度学习方面** 卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断 **3 图像处理方面** 图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知 **4 路径规划方面** 旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化 **5 无人机应用方面** 无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配 **6 无线传感器定位及布局方面** 传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化 **7 信号处理方面** 信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化 **8 电力系统方面** 微电网优化、无功优化、配电网重构、储能配置 **9 元胞自动机方面** 交通流 人群疏散 病毒扩散 晶体生长 **10 雷达方面** 卡尔曼滤波跟踪、航迹关联、航迹融合

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值