算法设计与实现 Algorithm Design and Implementation
Overview
In this course, we mainly learned algorithm analysis and some classic algorithm ideas:
- Algorithm analysis: time complexity and space complexity
- Ideas: Brute Force, Divide and Conquer, Dynamic Programming
一、Algorithm Analysis
For an algorithm, we need to use some indicators to measure its performance, such as time complexity and space complexity.
Time complexity refers to the time required to execute the algorithm, and space complexity refers to the memory space required by the algorithm.
1) Time Complexity
We usually use the progressive symbol \(O\) to represent time complexity.
Symbol \(O\) indicates the upper limit of the function.
In order to estimate time complexity, we need to find out the basic operation of the algorithm and estimate how many times it has been executed.
For example, the program:
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
num++;
}
}
The basic operation is num++. And how many times it has been exexuted?
Therefore, the time complexity of the algorithm is \(O(n^2)\)
2) Space Complexity
It represents the size of the storage space temporarily occupied by the algorithm during operation.
If you used an single-dimensional array, then the space compexity is \(O(n)\).
If you don't need auxillary space, then the space compexity if \(O(1)\).
二、Classic Algorithm Ideas
1) Brute Force
The brute force method is a direct problem-solving algorithm.
Usually the brute force method traverses all the solutions of the problem and finds the one that meets the requirements.
For example, Selection Sort.
The brute force method is obviously not the best algorithm, but it is always an effective method.
2) Divide and Conquer
The steps of the divide-and-conquer method is :
- Decompose the problem into smaller sub-problems
- Solvie these sub-problems
- Merge the solutions of the sub-problems
Classic algorithm using divide and conquer strategy is Merge Sort.
3) Dynamic Programming
Sometimes we can't get the solution of the main problems by simply combining solutions of sub-problems.
Then we can use the dynamic programming method.
The overall idea of the dynamic programming method is: save the answers to the sub-questions that have been solved, and get the answers directly when needed, thereby avoiding a lot of double calculations.
For example:
A Fibonacci sequence can be recursively defined as:
The programm is:
int fibonacci(int n){
if(n == 0 || n == 1){
return 1;
}
return fibonacci(n-1)+fibonacci(n-2)
}
But, when \(n = 5\), its calculation process is:
We can see that \(F(3)\) and \(F(2)\) are repeatedly calculated many times, which wastes a lot of time.
To this end, we can define a dp table with length of \(n\) to store the result of each calculation.
It is worth mentioning that the dynamic programming method requires that the problem has an optimal substructure property, that is, the optimal solution of the main problem contains the optimal solution of the sub-problem.
原文地址:https://www.cnblogs.com/danielwong2021/p/15271268.html
- JavaWeb三大组件之Servlet学习
- Android App瘦身实战
- [先行者周末课程] 日历组件的开发思路讲解&&日历组件在实际工作中的使用方式
- Batik渲染png图片异常的bug修复
- SpringMVC支持跨域的两种姿势
- SpringMVC返回图片的几种方式
- Redis实现分布式锁相关注意事项
- React Native调用Android相机图库
- SpringMVC之请求参数的获取方式
- 糖大夫--测量流程性能监控自动化方案设计
- ReactNative调用Android原生模块
- jvm调优的工具介绍
- Python时间序列预测案例研究:巴尔的摩年度用水量
- [一对一课程] 之 设计并实现第一个JS模块?
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Spring第三天:Spring的AOP的注解开发、Spring的声明式事务、JdbcTemplate
- Spring Boot中集成Slf4j 与Logback
- 一文搞定 Linux 常用高频命令
- 推荐一款科研必备的Python数据可视化神器——PyQtGraph
- 机器学习基础:可视化方式理解决策树剪枝
- 神级代码注释-这次是来搞笑的
- Gremlin 图查询概述
- JS,PHP,Python,Java对JSON数据的处理
- 基于Canal与Flink实现数据实时增量同步(二)
- Spring第四天:SSH的整合、HibernateTemplate的使用、OpenSessionInViewFilter的使用
- IDEA 下单程序多端口不同配置独立运行
- 基于Canal与Flink实现数据实时增量同步(一)
- 8848钛金手机之nacos的注册发现
- 让你git 时不再输入账号和密码
- JS 实现点击按钮复制一段文字