高级算法日记2:第1次课笔记
来源:互联网 发布:软件验收报告模板 编辑:程序博客网 时间:2024/06/08 14:53
第一次课笔记
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:
Divide the problem with a large input size into a number of subproblems that are smaller instances of the same problem.
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.”
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
import mathdef BinarySearch(A,p,r,x): if p > r: return None midpoint = math.floor((p+r)/2) if A[midpoint] == x: return midpoint elif 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
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,Min if p == r-1: Max = max(A[p],A[r]) Min = min(A[p],A[r]) return Max,Min q = 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
We can prove by induction that for any
Basis. When
Induction step. When
By induction,
Obviously, in the worst case,
另一种方法:
每相邻两个为一组,得到每一组的max,和min,再和上一次得到的max,min做比较,一共要做3次比较。
摸底作业题第2/3题:dominating number
巴基斯坦老兄的解法(Boyer-Moore Algorithm):
用stack数据结构,算法思想同第3题
证明:因为配对一共
note:
another version Boyer-Moore Algorithm is used for string matching
- 高级算法日记2:第1次课笔记
- 高级算法日记4:第2次课笔记
- 高级算法日记1:摸底课
- J2EE 第2次课笔记--Servlet
- J2EE 第2次课笔记--Servlet
- 高级算法日记9:图(2)
- 高级算法日记8:图(1)
- 第二周第2次课 算法分析基础
- 算法第1次实验作业
- J2EE 第5次课笔记--Servlet
- 第1次笔记:github入门学习
- 高级算法日记7:专题
- 第2周第1次课 线性表
- Oracle高级培训 第2课 学习笔记
- Oracle高级培训 第1课 学习笔记
- Oracle高级培训 第1课 学习笔记 修正版
- 第1次&第n次
- 算法课第4次实验--验证RSA加密算法
- Android中TextView中的.setText和.append的区别
- codeblocks常用快捷键
- C++表达式易错知识点
- 常见html5语义化标签
- shell脚本
- 高级算法日记2:第1次课笔记
- LeetCode 63. Unique Paths II
- 关于前端传值的相关问题续
- yii2 使用gii自动生成代码怎么添加分页
- Kotlin之匿名内部类
- 深度图像与点云区别
- Android音频系统之AudioPolicyService
- weblogic部署问题,Caused by: weblogic.descriptor.BeanAlreadyExistsException: Bean already exists: "weblog
- 清除浮动