清單
- 資料結構與演算法 Using Python
- 目錄
- About Data Structure
- Analysis of Algorithm
- Running Time
- Experimental Studies
- Theoretical Analysis
- Pseudocode (虛擬碼、偽代碼)
- Pseudocode Details
- Important Functions
- Primitive Operations (演算法的基本操作)
- Counting Primitive Operatioins
- Growth Rate of Running Time (執行時間增長率)
- Constant Factors
- Big-Oh Notation
- Big-Oh Example
- Big-Oh and Grow Rate
- Big-Oh Rules
- Asymptotic Algorithm Analysis (漸進演算法分析)
- Computing Prefix Averages
- Prefix Averages
- Review Math Define & Big-Oh
- Maps/Dictionaries
- Array
- Queues
- Queue的應用
- Stacks
- Stack的應用
- Linked Lists (鏈結串列)
- Recursion
- Trees
- Graphs
- Searching
- Sorting
- Text Processing
- Dynamic Programming
- Artificial Intelligence Algorithms
在資料結構中,常以抽象概念定義名詞,不依賴某種程式語言已有的結構特性,如array是個有序容器,起始index為0,同個array中只存放相同type的item,然而每個程式語言實作array後的特性未必相同
執行時間增長幅度通常取決於輸入大小,由於平均執行時間難以估計,所以通常以最壞的情況評估
以程式實現演算法,以不同的輸入規模測試並記錄時間(如下),最後畫成圖表
from time import time
start_time = time()
'''
# run algorithm
'''
end_time = time()
elapsed = end_time - start_time
若採實驗的方式,需要確保硬體跟執行環境的統一,以及精準不受其他效能干擾因素的環境
Big-O (O) 代表worst case
Theta (θ) 代表average case
Omega (Ω) 代表best case
- if...then...[else...]
- while...do...
- repeat...until...
- for...do...
- Indentation replaces braces (以縮排取代大括號)
- Method declaration
- Method call
- Returnn value
- Expressions
Name | Time Complexity |
---|---|
Constant | 1 |
Logarithmic | log(n) |
Linear | n |
N-Log-N | nlog(n) |
Quadratic | n^2 |
Cubic | n^3 |
Exponential | 2^n |
Factorial | n! |
e.g, 評估表達式(即計算值)、將變數賦予值(即assignment)、取得陣列中的某個索引值、呼叫function/method、回傳值(return)
透過檢視pseudocode,計算原始操作的最大值作為輸入大小的函數
op:operation,即1個操作時間
n ops:n operations,即n個操作時間Line 1:2 ops (定義+取值)
Line 3:2 ops (取值+賦值)
Line 4:2n ops ((向data取值+賦值給val)*迴圈n次)
Line 5:2n ops (取biggest值,判斷val>biggest狀態)
Line 6:0~n ops (受Line 5判斷,有可能完全跑不到(0),或n次迴圈都跑到(n))
Line 7:1 ops (回傳)將以上加總:
best case:4n+5 = 4n+c ≈ 4n
worst case:5n+5 = 5n+c ≈ 5n
由於n很大,所以較少的+1、+2…是可以忽略的
例如
為linear,而 為
給定一個函數f(n)跟g(n),我們稱作f(n)是O(g(n)) (讀作Big-O g(n))
e.g,
2n + 10 is O(n)
2n + 10 <= cn
(c-2)n >= 10
n >= 10/(c-2)
得到c = 3, n0 = 10
c = 7, n0 = 1
c = 28, n0 = 1
c = 8, n0 = 2
若f(n)是d次多項式,則f(n)是O(
)
2n 為O(n),而不是O(
)
3n+5 為O(n),而不是O(3n)
Using python (少用)
arr1 = array((Data Type), list)
從中間插入element時,worst case為O(n)
刪除任意位置item時,worst case為O(n)
與Insert的差別在於,添加的位置在array的最後,因此不會需要搬移其他items位置
worst case為O(1)
FIFO
example
f為front
r為end
當不斷dequeue時,前面閒置的空間過多,當r已經用完後面空間時,會將指標指到前面的空間形成環狀
LIFO
example
t為top
+
-
*
/
等運算規則的優先順序判斷
e.g, 14 – 3 * 2 + 7 = (14 – (3 * 2) ) + 7
Data Structure and Algorithms
Python