算法设计与分析课程
Chapter 1 Algorithm Analysis
- Complexity
- time/space
- Best/Worst/Average
Searching
- Linear
- Best: Ω ( 1 ) \Omega(1) Ω(1)
- Worst: O ( n ) O(n) O(n)
- Space: O ( 1 ) O(1) O(1)
- Average: E ( x ) = ∑ P r ( x ) ⋅ c o s t ( x ) = ∑ i = 1 n 1 n ⋅ i = 1 n ∑ i = 1 n ⋅ n ( n + 1 ) 2 = n + 1 2 = O ( n ) E(x)=\sum Pr(x)\cdot cost(x)=\sum_{i=1}^n \frac{1}{n}\cdot i=\frac{1}{n}\sum i=\frac{1}{n}\cdot \frac{n(n+1)}{2}=\frac{n+1}{2}=O(n) E(x)=∑Pr(x)⋅cost(x)=∑i=1nn1⋅i=n1∑i=n1⋅2n(n+1)=2n+1=O(n)
- Binary(sorted array as input)
- Best: Ω ( 1 ) \Omega(1) Ω(1)
- Worst: O ( log n ) O(\log n) O(logn)
- Space: O ( 1 ) O(1) O(1)
- Average: E ( x ) = ∑ P r ( x ) ⋅ c o s t ( x ) = ∑ i = 1 log n 2 i − 1 n ⋅ i = 1 n ( ∑ i = 1 log n 2 i − 1 ⋅ i ) = 1 n ( 2 log n ⋅ log n − 2 log n + 1 ) = O ( log n ) E(x)=\sum Pr(x)\cdot cost(x)=\sum_{i=1}^{\log n} \frac{2^{i-1}}{n}\cdot i=\frac{1}{n}(\sum_{i=1}^{\log n}2^{i-1}\cdot i)=\frac{1}{n}(2^{\log n}\cdot \log n-2^{\log n}+1)=O(\log n) E(x)=∑Pr(x)⋅cost(x)=∑i=1lognn2i−1⋅i=n1(∑i=1logn2i−1⋅i)=n1(2logn⋅logn−2logn+1)=O(logn)
Sorting
- Linear
- Selection:every iteration, select the i i ith smallest number and locates x x x at i i ith slot.
- Best=Worst=Average= ∑ i = 1 n − 1 i = n ( n − 1 ) 2 = Θ ( n 2 ) \sum_{i=1}^{n-1} i=\frac{n(n-1)}{2}=\Theta(n^2) ∑i=1n−1i=2n(n−1)=Θ(n2)
- Space: O ( 1 ) O(1) O(1)
- Bubble:repeated swap the adjacent elements if they are in wrong order.
- Best=Worst=Average= ∑ i = 1 n − 1 i = n ( n − 1 ) 2 = Θ ( n 2 ) \sum_{i=1}^{n-1} i=\frac{n(n-1)}{2}=\Theta(n^2) ∑i=1n−1i=2n(n−1)=Θ(n2)
- Space: O ( 1 ) O(1) O(1)
- Insertion:each time takes first element in the unsorted part and inserts it to the right place of the sorted one( ∀ A [ j ] > x , A [ j + 1 ] ← A [ j ] , j ← j − 1 \forall A[j]>x, A[j+1]\leftarrow A[j],j\leftarrow j-1 ∀A[j]>x,A[j+1]←A[j],j←j−1)
- Best: Ω ( n ) \Omega(n) Ω(n)
- Worst: ∑ i = 2 n = n ( n − 1 ) 2 = O ( n 2 ) \sum_{i=2}^n=\frac{n(n-1)}{2}=O(n^2) ∑i=2n=2n(n−1)=O(n2)
- Space: O ( 1 ) O(1) O(1)
- Average:
- suppose A [ i ] A[i] A[i] should be inserted at position j j j, E ( x ) = ∑ P r ( x ) ⋅ c o s t ( x ) = 1 i ⋅ ( i − 1 ) + ∑ j = 2 i 1 i ⋅ ( i − j + 1 ) = i 2 − 1 i + 1 2 E(x)=\sum Pr(x)\cdot cost(x)=\frac{1}{i}\cdot (i-1)+\sum_{j=2}^{i}\frac{1}{i}\cdot (i-j+1)=\frac{i}{2}-\frac{1}{i}+\frac{1}{2} E(x)=∑Pr(x)⋅cost(x)=i1⋅(i−1)+∑j=2ii1⋅(i−j+1)=2i−i1+21,
- ∑ i = 2 n ( i 2 − 1 i + 1 2 ) = n 2 4 + 3 n 4 − ∑ i = 1 n 1 i = O ( n 2 ) \sum_{i=2}^n(\frac{i}{2}-\frac{1}{i}+\frac{1}{2})=\frac{n^2}{4}+\frac{3n}{4}-\sum_{i=1}^n\frac{1}{i}=O(n^2) ∑i=2n(2i−i1+21)=4n2+43n−∑i=1ni1=O(n2)
- Selection:every iteration, select the i i ith smallest number and locates x x x at i i ith slot.
- Recursive
- MergeSort:select the mid element, MergeSort(A[left, mid]), MergeSort(A[mid+1, right]), Merge(A, left, mid, right)
- Best=Worst=Average= Θ ( n log n ) \Theta(n\log n) Θ(nlogn)
- Space: O ( n ) O(n) O(n)
- QuickSort: Randomly Choose a p i v o t pivot pivot and partition the array by smaller and larger halves, QucikSort(A[1, i-1]), QuickSort(A[i+1, n])
- Best: Ω ( n log n ) \Omega(n\log n) Ω(nlogn)
- Worst: O ( n 2 ) O(n^2) O(n2),退化到linear
- Space: O ( log n ) O(\log n) O(logn)
- Average:
T ( n ) = E m [ T ( m − 1 ) + T ( n − m ) ] + c n T(n)=E_m[T(m-1)+T(n-m)]+cn T(n)=Em[T(m−1)+T(n−m)]+cn
= E m [ T ( m − 1 ) ] + E m [ T ( n − m ) ] + c n =E_m[T(m-1)]+E_m[T(n-m)]+cn =Em[T(m−1)]+Em[T(n−m)]+cn
= ∑ m = 1 n P r ( m ) ⋅ T ( m − 1 ) + ∑ m = 1 n P r ( m ) ⋅ T ( n − m ) + c n =\sum_{m=1}^n Pr(m)\cdot T(m-1)+\sum_{m=1}^n Pr(m)\cdot T(n-m)+cn =
- MergeSort:select the mid element, MergeSort(A[left, mid]), MergeSort(A[mid+1, right]), Merge(A, left, mid, right)