复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 01 Algorithm analysis and recurrences

Data Structures and Algorithm Xiaoqing Zheng Zhengxq@fudan.edu.cn
Data Structures and Algorithm Xiaoqing Zheng zhengxq@fudan.edu.cn

What are algorithms? d A sequence of computational steps that transform the input into the output Sorting problem: Input: A sequence of n numbers Oupu:
What are algorithms? A sequence of computational steps that transform the input into the output Sorting problem: Input: A sequence of n numbers Output: A permutation (reordering) such that a' 1 ≤ a' 2 ≤ … ≤ a' n Instance of sorting problem: Input: Output:

Example of insert sort 822222 8 433 44884 999986 333398 66666 done
Example of insert sort 8 2 493 6 2 8 493 6 24 8 93 6 24 89 3 6 23 489 6 23 468 9 done

Insertion sort INSERTION-SORT(A) I for j←2 to length[4] 2 do key←A 3 / Insert AG] into the sorted sequence All.j-1] 4 while i>0 and ai>key doA[i+11←A[ 7 8[i+1]←key A Sorted
Insertion sort INSERTION-SORT(A) 1 for j ← 2 to length[A] 2 do key ← A[j] 3 // Insert A[j] into the sorted sequence A[1 .. j − 1] 4 i ← j − 1 5 while i > 0 and A[i] > key 6 do A[i + 1] ← A[i] 7 i ← i − 1 8 A[i + 1] ← key A: 1 i j key n Sorted

Running time a The running time depends on the input: an already sorted sequence is easier to sort a Parameterize the running time by the size of the input, since short sequences are easier to sort than long ones a Generally, we seek upper bounds on the running time, because everybody likes a guarantee
Running time The running time depends on the input: an already sorted sequence is easier to sort. Parameterize the running time by the size of the input, since short sequences are easier to sort than long ones. Generally, we seek upper bounds on the running time, because everybody likes a guarantee

Kinds of analyses Worst-case:(usually) T(n)=maximum time of algorithm on any input of size n Average-case:(sometimes) T(n)=expected time of algorithm over all inputs of size n Need assumption of statistical distribution of inputs Best-case:(bogus) Cheat with a slow algorithm that works fast on some input
Kinds of analyses Worst-case: (usually) T( n) = maximum time of algorithm on any input of size n. Average-case: (sometimes) T( n) = expected time of algorithm over all inputs of size n. Need assumption of statistical distribution of inputs. Best-case: (bogus) Cheat with a slow algorithm that works fast on some input

Analysis of insertion sort INSERTION-SORT(A) times I for j←2 to length[4] do key←A[ C 2345678 // Insert AG] into the sorted sequence while i>0 and aG>key doA[i+1←A[i ∑2(1-1) A[i+11←key 7(n)=cm+2(n-1)+c1(n-1)+c∑m21+c∑=2(1-1)+c∑=2(1-1)+c(n-1
Analysis of insertion sort INSERTION-SORT(A) 1 for j ← 2 to length [A] 2 do key ← A [j] 3 // Insert A [j] into the sorted sequence A[1 .. j − 1] 4 i ← j − 1 5 while i > 0 and A [ i] > key 6 do A [ i + 1] ← A [ i] 7 i ← i − 1 8 A [ i + 1] ← key cost c1 c 2 0 c 4 c 5 c 6 c 7 c 8 times n n − 1 n − 1 n − 1 n − 1 2 n j j t ∑ = 2 ( 1) n j j t = ∑ − 2 ( 1) n j j t = ∑ − 12 4 5 6 7 8 22 2 ( ) ( 1) ( 1) ( 1) ( 1) ( 1) nn n jj j jj j T n cn c n c n c t c t c t c n == = = + −+ −+ + −+ −+ − ∑ ∑ ∑

Analvsis of insertion sort: best and worst T(m)=cn+c2(n-1)+c(n-1)+c∑21+c∑2(1-1)+c∑2(1-1)+c(n-1) Best case e The best case occurs if the array is already sorted (n)=c1n+c2(n-1) 1)+c3(n-1) (C+C,+C4+C+C8)n-(C,+C4+C+c) an+ b (inear function) Worst case The worst case occurs if the array is in reverse sorted order 7(n)=c1n+c2(n-1)+c4(n-1)+c5( n(n+1) -1)+c6( n(n-1)、,。n(n-1) )+c7( )+c3(n-1) ++-)n2+(c+c2+c +cs3)n-(c2+c4+c5+c3) an+ bn +c(quadratic function)
Analysis of insertion sort: best and worst 12 4 5 6 7 8 22 2 ( ) ( 1) ( 1) ( 1) ( 1) ( 1) nn n jj j jj j T n cn c n c n c t c t c t c n == = = + −+ −+ + −+ −+ − ∑ ∑ ∑ Best case: y The best case occurs if the array is already sorted 12 4 5 8 T n cn c n c n c n c n ( ) ( 1) ( 1) ( 1) ( 1) = + −+ −+ −+ − 12458 2458 = ( )( ) c c c c cn c c c c ++++ − +++ an + b (linear function) Worst case: y The worst case occurs if the array is in reverse sorted order 12 4 5 6 7 8 ( 1) ( 1) ( 1) ( ) ( 1) ( 1) ( 1) ( ) ( ) ( 1) 2 22 nn nn nn T n cn c n c n c c c c n + − − = + −+ −+ −+ + + − 567 567 2 124 8 2458 ( ) ( )( ) 222 222 ccc ccc = + + + +++ − − + − +++ n c c c cn c c c c an 2 + bn + c (quadratic function)

Machine-independent time a What is insertion sort's worst-case? It depends on the speed of our computer Relative speed (on the same machine), Absolute speed (on different machines) a BIG IDEA Ignore machine-dependent constants Look at growth of r(n)as n>00 "Asymptotic Analysis
Machine-independent time What is insertion sort's worst-case? It depends on the speed of our computer: y Relative speed (on the same machine), y Absolute speed (on different machines). "Asymptotic Analysis" "Asymptotic Analysis" BIG IDEA: y Ignore machine-dependent constants. y Look at growth of T( n ) as n → ∞

notation a Math o(8(n)=fn): there exist positive constants c1 C2,and no such that 0 sC1gn)sf(n)s c2g(n) for all n2 no 3 ENgineering Drop low-order terms; ignore leading constants Example: 3n3+90n2-5n +6046=o(n
Θ-notation Math: Θ(g(n)) = { f(n) : there exist positive constants c1, c2, and n0 such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 } Engineering: y Drop low-order terms; ignore leading constants. y Example: 3n3 + 90n2 – 5n + 6046 = Θ(n3)
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 复旦大学:《数据结构与算法设计》综合项目_Project3. All-pairs shortest path.pdf
- 复旦大学:《数据结构与算法设计》综合项目_Project2. English-Chinese dictionary based on binary search tree.pdf
- 复旦大学:《数据结构与算法设计》综合项目_Project1. Combining quicksort with insertion sort.pdf
- 复旦大学:《数据结构与算法设计》实验设计_Lab 8. String Matching.pdf
- 复旦大学:《数据结构与算法设计》实验设计_Lab 7. Single-Source Shortest Paths.pdf
- 复旦大学:《数据结构与算法设计》实验设计_Lab 6. Greedy Algorithms.pdf
- 复旦大学:《数据结构与算法设计》实验设计_Lab 5. Red-Black Tree.pdf
- 复旦大学:《数据结构与算法设计》实验设计_Lab 4. Binary Search Trees.pdf
- 复旦大学:《数据结构与算法设计》实验设计_Lab 3. Hash Tables.pdf
- 复旦大学:《数据结构与算法设计》实验设计_Lab 2. Divide and Conquer.pdf
- 复旦大学:《数据结构与算法设计》实验设计_Lab 1. Stack.pdf
- 复旦大学:《数据结构与算法设计》考试样卷_2009-2010年度A卷(答案).pdf
- 复旦大学:《数据结构与算法设计》考试样卷_2009-2010年度A卷(试卷).pdf
- 复旦大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)移动商务介绍(概念及其特点、移动商务与电子商务、价值链及商业模式).ppt
- 复旦大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)无线局域网补充删节版本.ppt
- 复旦大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)移动电话通信原理(补充资料).ppt
- 复旦大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)Wide Area Networks(WANs).ppt
- 复旦大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)Ethernet LANs.ppt
- 复旦大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)Topics Covered(胥正川).ppt
- 复旦大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)Networked Applications.ppt
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 02 Divide-and-conquer design paradigm.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 03 Basic data structure - Elementary data structures.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 04 Soring.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 05 Hash tables.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 06 Binary search trees.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 07 Amortized analysis.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 08 Dynamic programming.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 09 Greedy algorithms.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 10 Graph algorithms.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 11 String Matching.pdf
- 复旦大学:《数据结构与算法设计 Data Structures and Algorithm》课程英文讲义_Chapter 12 NP-complete problems.pdf
- 复旦大学:《计算机网络 Computer Networking》课程教学资源_考试样卷.doc
- 复旦大学:《计算机网络 Computer Networking》课程教学资源_各章节练习题.docx
- 复旦大学:《计算机网络 Computer Networking》课程教学资源_考试样卷.doc
- 《计算机网络》课程教学资源(参考文献)END-TO-END ARGUMENTS IN SYSTEM DESIGN.pdf
- 《计算机网络》课程教学资源(参考文献)The Design Philosophy of the DARPA Internet Protocols.pdf
- 《计算机网络》课程教学资源(参考文献)Interdomain Internet Routing.pdf
- 《计算机网络》课程教学资源(参考文献)Stable Internet Routing Without Global Coordination.pdf
- 《计算机网络》课程教学资源(参考文献)9e5f3e30-0aac-407f-b55f-4b04f28fcc05.pdf
- 《计算机网络》课程教学资源(参考文献)Congestion Avoidance and Control.pdf