第一次课笔记

lecture:沈孝钧

record:孙相国

time: 2017/05/06

  • 排序算法比较
  • Divide and Conquer
    • 1 Principle of Divide and Conquer
    • 2 Examples
      • Example 1 Binary Search
      • Example 2 Find max and min
      • 摸底作业题第23题dominating number

复杂度分析;分治法;排序及下界

1.排序算法比较:

2. Divide and Conquer

2.1 Principle of Divide and Conquer

When the input size of a problem is small, one or two numbers for example, then this problem can be easily solved. However, when the input size is large, many problems become difficult to solve. Therefore, a basic methodology is to find the relationship between the solution for a large sized input and the solutions for small sized inputs. Divide and Conquer is a particular approach that follows this methodology. We will see two more approaches later, namely, the greedy approach and the dynamic programming which are also following this methodology but differ in the ways of implementation.
Briefly speaking, Divide and Conquer method follows the following steps:

  1. Divide the problem with a large input size into a number of subproblems that are smaller instances of the same problem.

  2. Conquer the subproblems by solving them recursively. If the size of a subproblem is small enough, then solve it in a straightforward manner which is called “bottom out.”

  3. Combine the solutions to the subproblems into the solution for the original problem.

2.2 Examples

Example 1: Binary Search

Suppose we have a sorted array of n n numbers, A[1]⩽A[2]⩽⋯⩽A[n]A[1] \leqslant A[2] \leqslant \cdots \leqslant A[n]. Now, we need to design an algorithm that searches the array to see if this array contains a particular number x x. If A[i]=xA[i] = x, then report index i i, otherwise report nilnil. The following algorithm called binary search uses the divide and conquer approach. Note that a smaller subproblem corresponds to a segment of the array A A, denoted by A[p],A[p+1],⋯,A[r]A[p], A[p+1], \cdots, A[r], where 1⩽p⩽r⩽n 1 \leqslant p \leqslant r \leqslant n. This notation allows us to represent any subproblem. When, p=1,r=n p = 1, r = n, this sequence represents the original problem.

import mathdef BinarySearch(A,p,r,x):if p > r:return Nonemidpoint = math.floor((p+r)/2)if A[midpoint] == x:return midpointelif x < A[midpoint]:return BinarySearch(A,p,midpoint-1,x)else:return BinarySearch(A,midpoint+1,r,x)A=[1,2,3,5,6,7]
print(BinarySearch(A,0,5,2))

Example 2: Find max and min

Given n numbers stored in A[1⋯n] A[1\cdots n], we wish to design a divide-and-conquer algorithm that finds both the maximum number and the minimum number in this array.

Solution:

We design a procedure that finds the maximum and minimum numbers in the range of A[p, r].

import mathdef Max_Min(A,p,r):if p == r:Max = A[p]Min = A[p]return Max,Minif p == r-1:Max = max(A[p],A[r])Min = min(A[p],A[r])return Max,Minq = math.floor((p+r)/2)Max1 ,Min1 = Max_Min(A,p,q)Max2,Min2 = Max_Min(A,p+1,r)Max = max(Max1,Max2)Min = min(Min1,Min2)return Max,MinA=[1,3,4,5,6,7,8]
print(Max_Min(A,0,6))

By calling Max-Min (A[1.. n], Max, Min) , we will get the maximum and minimum numbers in A[1.. n].

The complexity follows the recurrence relation T(n)=T(⌊n/2⌋)+T(⌈n/2⌉)+2 T(n) = T(\lfloor n/2\rfloor) + T(\lceil n/2\rceil) + 2.

We can prove by induction that for any n n,T(n)⩽2n–2 T(n) \leqslant 2n – 2.

Basis. When n=1 n = 1 or n=2 n = 2, T(n)⩽2n–2 T(n) \leqslant 2n – 2 is obviously true.
Induction step. When n>2 n > 2, we have
T(n)=T(⌊n/2⌋)+T(⌈n/2⌉)+2 T(n) = T(\lfloor n/2\rfloor) + T(\lceil n/2\rceil) + 2
By induction,

T(n)⩽(2⌊n/2⌋−2)+(2⌈n/2⌉−2)+2=2(⌊n/2⌋+⌈n/2⌉)−4+2=2n−2.

T(n) \leqslant (2\lfloor n/2\rfloor -2) + (2 \lceil n/2\rceil -2) + 2= 2(\lfloor n/2\rfloor + \lceil n/2 \rceil ) - 4 + 2= 2n -2.
Obviously, in the worst case, T(n)=2n–2 T(n) = 2n – 2.

另一种方法:

每相邻两个为一组,得到每一组的max,和min,再和上一次得到的max,min做比较,一共要做3次比较。

T(n)=3×n2−2 T(n)=3\times \frac{n}{2}-2

摸底作业题第2/3题:dominating number

巴基斯坦老兄的解法(Boyer-Moore Algorithm)

用stack数据结构,算法思想同第3题

证明:因为配对一共 ⩽n/2 \leqslant n/2,这意味着,任何配对的数字都不可能是dominating number.只有剩下的数字才是dominating number

note:

another version Boyer-Moore Algorithm is used for string matching

高级算法日记2:第1次课笔记相关推荐

  1. 算法日记-01-算法和数据结构概览

    算法日记-01-算法和数据结构概览 文章目录 算法日记-01-算法和数据结构概览 1.数据结构 2.算法 3.如何解题? 4.刷题方法 5.小结 1.数据结构 一维 基础:数组arry(string) ...

  2. MySQL高级-04-授课笔记

    MySQL高级-04-授课笔记 一.MySQL存储引擎 1.MySQL体系结构 体系结构的概念 任何一套系统当中,每个部件都能起到一定的作用! MySQL的体系结构 体系结构详解 客户端连接 支持接口 ...

  3. 一款没有排课算法的《陈老师排课软件》

    一.当前流行的排课软件都是采用排课算法进行排课 有排课算法的软件大致排课流程: 1.设置每天上午.下午的排课节数,一个周期的上课的天数,一般是分别是五.六.七天. 2.设置开课的班级号.班级数.学校名 ...

  4. 在阿里AI实验室做NLP高级算法工程师是一种什么样的体验?

    最近应届生的拼抢大战的号角已经吹响.正如昨天那篇雄文,年薪25万也只是个白菜价......这让营长真心羞愧啊.....同样敲键盘的...不说也罢 绝不将小小的忧桑带入工作. 这几天,群里一直有很多小伙 ...

  5. 月薪40~50K|波波生活信息技术公司招聘高级算法工程师

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 深圳市波波生活信息技术服务有限公司成立于2019年1月,是一家集研发.生产.销售为一体的高科技企业.公 ...

  6. awgn信道中的噪声功率谱密度_从OFC2020看高级算法在光通信中的应用

    ​ 各种神经网络算法(XNN)在大数据机器学习和人工智能领域有着十分广泛的应用,这些高级算法在分类.优化.自学习这些方面的突出能力和其在互联网及自动控制领域的优异表现是毋庸置疑的,自然这些年也是火得一 ...

  7. 七月算法--12月机器学习在线班-第七次课笔记—最大熵

    七月算法--12月机器学习在线班-第七次课笔记-最大熵 七月算法(julyedu.com)12月机器学习在线班学习笔记 http://www.julyedu.com 转载于:https://www.c ...

  8. 七月算法--12月机器学习在线班-第五次课笔记—回归

    七月算法--12月机器学习在线班-第五次课笔记-回归 七月算法(julyedu.com)12月机器学习在线班学习笔记 http://www.julyedu.com 转载于:https://www.cn ...

  9. 阿里、京东高级算法专家讲述数学在企业中的应用

    学数学到底学什么?如果只是为了刷题和考试,那就大错特错了.学习数学目的是为了掌握数学的思想方法和精神实质,并以此去解决实际问题.学术界和企业界高级专家齐聚一趟,共同探讨数学是如何在企业中得到应用. 为 ...

最新文章

  1. day03-正则表达式
  2. lzg_ad:rundll32.exe进程详述
  3. 火爆背后的挑战:直播平台的高并发架构设计
  4. Hibernate初探(二)
  5. TCP/IP协议与Http协议的区别详解
  6. Codeforces Round #675 (Div. 2)——F主席树待补?
  7. Dubbo与Zookeeper伪集群部署
  8. java正则学习笔记
  9. 动态水晶报表:任意表,任意列 之 动态格线实现
  10. matlab破解方法
  11. java 连接sybase数据库_Jdbc连Sybase数据库的几种方法_MySQL
  12. 北京大学计算机系 丁主任,北大“扫地僧”韦东奕,真的是正常人吗?北大丁教授说出了答案...
  13. GreenPlum学习笔记:split_part与string_to_array字符截取
  14. 搞懂这 9 个步骤,DNS 访问原理就明明白白了
  15. 编写高质量代码:改善Java程序的151个建议(第9章:多线程和并发___建议125~131)
  16. html网页打不开二级网页,遇到二级网页打不开怎么办 讲解二级页面打不开的处理方法...
  17. Nginx/PHP安装
  18. ShaderToy入门教程(1) - SDF 和 Raymarching 算法
  19. echarts 横坐标显示一个月_图表太丑怎么破,ECharts神器带你飞!
  20. MATLAB 基础知识 数据类型 时间表 按行时间和变量类型选择时间表数据

热门文章

  1. 人民艺术家梁永和丹青传情新时代
  2. matlab用i3怎么,i3处理器-matalb速度差不多10倍DSP-C语言
  3. HTML图片标签及相关超链接
  4. linux【网络】DNS 解析时快时慢,我该怎么办?
  5. 梦到娭毑_原水_新浪博客
  6. 数据库基础(数据库的由来 + 第一、二、三范式)
  7. 补码运算溢出判断方法
  8. mongodb java查询_java操作mongoDB查询的实例详解
  9. 图片格式修改--png改为jpg
  10. VBA—EXCEL操作集合—06