MIT 算法导论公开课笔记——分而治之——寻峰器(peak finder)

Efficient programmer

Scalability可伸缩性

Classic data structures

Modules  模块

Trees 树sorting排序一万亿个数字

复杂性

RSA加密 SLL

Graphs 图最基本的数据结构:魔方任务

使用特定算法编码

最短路径问题

Advanced topics

 

 

 

一个具体问题:

Peak finder:一维

a

b

c

d

e

f

g

h

i

1        2         3        4       5         6        7        8       9

Peak:高峰——position 2 is a peak if and only if b>=a and b>=c;position 9 is a peak iff i>=h;

Problem:find a peak if it is exists;

 

 

Straightforward algorithm 简单的算法

Start from left:

 

 

 

 

 

……………

 

…………

 

 

1        2       3      4        5              n/2                n-1       n

 

MIT算法导论笔记

Look at n/2 elements

Worst case complexity theta n

一维寻峰器,如何降低渐进复杂度?

二分法:分而治之  把一个一维数组分成几个小数组

 

 

 

 

 

 

 

 

 

 

 

 

1       2      ………      n/2-1    n/2     n/2+1    ……                    n

分而治之—Divide & Conquer

 

 

                                                      Look at n/2 position

If a[n/2] <a[n/2-1],then only look at left half;1………n/2-1 to look for a peak;

Else if a[n/2]<a[n/2+1],then only look at right half;n/2+1………n to look for a peak;

Else n/2 position is a peak;

(Argue that algorithm is correct)

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

Base case :T(1)=theta(1)

T(n)=theta(1)+………+theta(1)=theta(log2n)                               与上面的递归算法相对应的复杂度的递归定义

 

             Log2n times

 

 

 

2D version

 

c

 

 

 

 

 

 

 

 

b

a

d

 

 

 

 

 

 

 

 

e

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n行rows m列colomns

a is a 2D-peak iff a >=b,a>=d,a>=c,a>=e;

贪婪的上升算法:Greedy Ascent algorithm:本质上是pick a direction

Theat(nm)complexity

Theta(n^2)if m=n;

 

 

 

 

Binary search:

Pick middle coloumn j=m/2;

Find a 1D-peak at (i,j);

Use (I,j) as a start to find a 1D-peak on row i ;

Efficient but incorrect

 

 

 

Problem :2D-peak may not exist on row i ;

 

 

 

AHempt#2

Pick middle column j=m/2;

Find global max on column j at (i,j)

Compare (i,j-1),(i,j),(i,j+1)

Pick left cols if (i,j-1)>(i,j) similarly for the right ;

If(i,j)>=(i,j-1),(i,j+1)=>(i,j) is a 2D-peak

Solve the new problem with half the number of cols

When you have a single col,find the global max <—done

 

T(n,m)=T(n,m/2) +theta(n)

         分解     找到global max

T(n,1)=theta(n);

T(n,m)= theta(n)+………+theta(n)

        Log2m   times//theta(n)相加log2m次;

 

相关文章:

  • 2021-06-18
  • 2021-06-10
  • 2021-11-29
  • 2022-01-18
  • 2021-06-25
  • 2022-01-27
  • 2021-08-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-08-22
  • 2021-06-21
  • 2021-10-03
相关资源
相似解决方案