Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
算法初步
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Thomas
February 02, 2017
Programming
210
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
算法初步
该 PPT 来自,七月在线的算法课程,如有侵权请通知。
Thomas
February 02, 2017
More Decks by Thomas
See All by Thomas
Self Confidence
zhgqthomas
0
63
Android Interprocess Commuincation
zhgqthomas
0
110
Other Decks in Programming
See All in Programming
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
3k
cdk deploy JawsSonic #MARATHONしながらAWSリソースをデプロイしてみよう
akihisaikeda
2
140
Building an Out-of-Order CPU
latte72
1
790
AGENTS.md Is Not Enough:Build Skills, Don't Download Them
lx_t
0
120
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
730
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
1
340
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
4
2.6k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
450
App Intentsのビルドプロセスを支える技術
kntkymt
0
390
AgentCore CLI で進化した AWS での AI エージェントの作り方 : 必要な機能を必要な時に
icoxfog417
PRO
3
350
What We Talk About When We Talk About XP
m_seki
2
650
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
310
Featured
See All Featured
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
430
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
65k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
240
Ruling the World: When Life Gets Gamed
codingconduct
0
340
Color Theory Basics | Prateek | Gurzu
gurzu
1
470
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
500
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Technical Leadership for Architectural Decision Making
baasie
3
570
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
520
Transcript
算法初步 Ben
Why? • 应用:机器学习、数据挖掘、自然语言处理、密码学、计算 机图形学等 • 研究:时空复杂度等 • 找工作:贪心、分治、动态规划、树、图等
What? • 大象放进冰箱 • 有穷性 必须在人类毁灭前结束 • 确定性 老板,便宜一点(康盲,一点是多少) •
可行性 造个飞碟(你这么厉害你家里人造么) • 输入&输出
How? • 穷举(万能算法) 求N个数的全排列 8皇后问题 • 分而治之(减而治之) 二分查找——减而治之 归并排序——分而治之
How? • 贪心 最小生成树 Prim, Kruskal 单源最短路 Dijkstra • 动态规划
背包 士兵路径
复杂度 • 谈算法不谈复杂度=耍流氓 • 硬件发展,速度提升、内存提升(常数级) • 在实现之前,我们要预估算法所需要的资源 • 时间 •
空间
复杂度 • 时空复杂度 使用大O记号 时间:基本操作次数(汇编指令条数) 空间:占用内存字节数 区别:空间可以再利用 时空互换(Hash表)
复杂度 • 常见时间复杂度分析方法 数循环次数 均摊分析 递归式——主定理
复杂度 • O(1) 基本运算,+,-,*,/,%,寻址 • O(logn) 二分查找 • O(n1/2) 枚举约数
复杂度 • O(n) 线性查找 • O(n2) 朴素最近点对 • O(n3) Floyd最短路
普通矩阵乘法
复杂度 • O(nlogn) 归并排序 快速排序的期望复杂度 基于比较排序的算法下界 • O(2n) 枚举全部的子集
复杂度 • O(n!) 枚举全排列 • 总结: 优秀 O(1) < O(logn)
< O(n1/2) < O(n) < O(nlogn) 可能可以优化 O(n2) < O(n3) < O(2n) < O(n!)
均摊分析 • 多个操作,一起算时间复杂度 MULTIPOP的队列,可以一次性出队k个元素 每个元素只出入队列一次 动态数组尾部插入操作(vector) 一旦元素超过容量限制,则扩大一倍,再复制
例题:最大子数组和 • 给定数组a[1…n],求最大子数组和,即找出1<=i<=j<=n, 使a[i]+a[i+1]+…+a[j]最大 • 介绍三个算法 暴力枚举 O(n3) 优化枚举 O(n2)
贪心法 O(n)
暴力枚举 • 暴力枚举:三重循环 for i 1 to n for
j i to n sum a[i]+..+a[j] ans max(ans, sum) 时间复杂度 O(n3) 附加空间复杂度 O(1)
优化枚举 • 优化枚举:两重循环 for i 1 to n sum
0 for j i to n sum sum + a[i] ans max(ans, sum) 时间复杂度 O(n2) 附加空间复杂度 O(1)
贪心法 • 贪心法:一重循环 sum 0 ans 0 for
i 1 to n sum sum + a[i] ans max(sum, ans) if (sum < 0) sum 0 时间复杂度 O(n) 附加空间复杂度 O(1)
作业题1 • 设计一个队列 • 支持:出队,入队,求最大元素 • 要求O(1) • 均摊分析
作业题2 • 给定一个正整数组a,是否能以3个数为边长构成三角形? • 即是否存在不同的i,j,k, • 满足 a[i] < a[j]
+ a[k] • 并且 a[j] < a[i] + a[k] • 并且 a[k] < a[i] + a[j]