Basic concept
Array
- 算幾題array和位址的轉換(2d array很多陷阱)就差不多了
- row major 和 column major 算出來結果可能都可以,答案就會不只一個
Stack & Queue
- queue & stack的實作應該可以跳過 (算都會了吧?)
- 考過不只一次,不看根本不會
- postfix 用stack運算結果
Linked list
- Sparse matrix (p141)有點常考
- Generalized list (p149)
Tree & Binary Tree
- Define:
- 全部node個數
- deg(v)=0的node個數
- deg(v)=1的node個數
- deg(v)=2的node個數
- 重要定理
- proof:
- 三種node合=從deg記算的node數,+1為沒被算入的root(沒人存他)
- Complete Binary tree
- 簡單來說就是,由上到下由左到右長

- Full Binary tree
- 把BST用inorder輸出,即為排序好的結果
- Thread Binary Tree (p182)
- Leftmost child next right sibling (p187)
- 留左邊child的link,其他砍掉,同層連起來,旋轉45度 (很抽象)
- 森林轉成樹 (p188)
- Inorder, preorder, postorder熟到爛掉,skip
- 記得一定要有Inorder才能決定唯一tree (preorder, postorder無法唯一決定)
- n個node的binary tree有種 (catalan number)
- 計算某條件下(ex. deg=i的node有幾個)的internal node/leaf數量,把branch數量也拿來列式 (歷屆5,6)
- Heapify整棵是亂的樹 (歷屆38)
- 從root開始,廣度優先檢查node是否符合heap規則(可能是min or max當parent)
- Heap可以在O(n)時間內建好
Graph
- edge種類(DFS後,把edge分成4種)
- Tree: Edges in the DFS forest
- Back: From descendant to ancestor when explored (include self loop)
- Forward: From ancestor to descendant when explored (exclude tree edge)
- Cross: Others (no ancestor-descendant relation)
- 無向圖只有tree edge, back edge而已!

- articulation point (cut vertex)
- 移出該vertex後,會導致圖變為disconnected,稱作articulation point
- 尋找articulation point的演算法
- Remove v from graph
- See if the graph remains connected (BFS or DFS)
- Add v back to the graph
-
adjacency matrix vs transitive closure
- adjacency matrix對角線是0,transitive closure對角線是1
- adjacency matrix紀錄點之間的距離
- transitive closure紀錄的是"能到達為1,不能到達為0"
- AOE network (activity on edge network)
- 以p297 試題50為例
- 先算出每個node最早發生時間,反推可得最晚發生時間,兩個時間可以註記在node上
- critial path length即為最後一個node(event)的數字
- 用看的找出critical path (看一下length是不是跟上面一樣!)
- event最早完成時間就是activity的earlist start time
- event最晚完成時間減去活動長度即為activity的latest start time
- 我還是拍影片好了
Sorting
- 證明用comparison為基礎的排序至少都要nlgn的時間:



穩定排序: 插入、泡泡、融合、基數 (insertion, bubble, merge, radix)
不穩定排序(也可記排除上面那些): 選擇、快速、堆積 (selection, quick, heap)
Insertion sort
不斷把尚未sort的item插入前面已經sort好的部分
- Stable
- Space complexity:
- Time complexity:
參考
Comparison Sort: Insertion Sort(插入排序法)
Rust Algorithm Club
Selection sort
一直把最小的抓下來放
- Unstable
- Space complexity:
- Time complexity:
參考
Rust Algorithm Club
C/C++ selection sort 選擇排序法
Bubble sort(sinking sort)
比較兩個相鄰元素,若首個元素比次個元素大,置換兩者的位置。
依序對相鄰元素執行步驟一,直到抵達序列頂端,此時頂端元素排序完成。
重複步驟 1 - 2 的整個序列疊代,直到任何一次疊代沒有執行元素置換。
當然也可以如下程式,直接疊代n次後停止
- stable
- Space complexity:
- Time complexity:
參考
[C++] 氣泡排序法(Bubble sort)
Quick sort
畫sort過程必考,看講義316說明和範例很容易懂
- Unstable
- Space complexity:
- Time complexity:
參考
Comparison Sort: Quick Sort(快速排序法)
Merge sort
合併過程必考,講義322,324分別為兩種(Iterative/Recursive)合併方法
- stable
- Space complexity:
- Time complexity:
參考
Comparison Sort: Merge Sort(合併排序法)
Heap sort
最重要的是了解抽走max/min後怎麼把樹重新Heapify,下面連結講的很清楚
尚未建成max-heap前,到建成第一個max-heap的過程也要會(參考講義)
特別注意:在同一個subtree裡,leftchild(index(2i))與rightchild(index(2i+1))的「數值」大小順序不重要,只要和root(index(i))比較即可。
- Unstable
- Space complexity:
- Time complexity:
參考
Comparison Sort: Heap Sort(堆積排序法)
Bucket sort
- mega筆記 ch5.Tree說bucket sort為Radix sort的MSD版
- Rusk algorithm club說bucket sort是先裝入桶子在用insertion sort排序
- ex: 1~100, 101~200…901~1000 假設共r=10桶
- 數字如果平均分散在這個範圍內,每個桶只會有n/10筆資料
- 每個桶用insertion sort,這邊需假設資料小到能時間完成
- 把資料串回來,需要
- Space complexity:
- Time complexity:
- Worst: 當所有人擠同一個桶子
- Avg:
- d: max number digit / r: buckets
Radix sort
- 主要有MSD/LSD兩種,差別在從最大or最小的digit開始分類
- 講義p.330有分類過程,務必看過
桶子是FIFO
- MSD: unstable, LSD: stable
- Space complexity:
- Time complexity:
- Worst:
- Avg:
- d: max number digit / r: buckets
參考

Hashing
-
名詞解釋
- bucket
- 分成幾個單位(ex. mod 5就是5個bucket)
- slot
- load density (factor)
- n=放入hash的變數各數
- sb=hash table的容量大小
- ,可以視為平均一個bucket中有幾個人
- collision
- overflow
- collision且桶子滿了(no more slots)
- perfect hashing
- 保證不會有collision發生的hashing function
-
overflow handing
- Linear probing
- Quadratic probing
- 如果經過h(x)算出的位置滿了,重算公式如下
- ,如果還是滿的,i++
- double hashing
- 如果經過h(x)算出的位置滿了,重算公式如下
- ,如果還是滿的,把再算一次
- ex. f(i)=(7-x%7)
- h(41)+1*f(1)=h(41)+1*(7-41%7) –->滿的話繼續往下算
- h(41)+2*f(2)=h(41)+2*(7-41%7)
- 以此類推
高等樹
Mega大筆記
Double-Ended priority queue
- Time complexity
- Insertion:
- Delete min/max:
- find min/max:
- 皆為Complete (binary) tree
- 以下三種都是這種queue
- Min-max heap
- Deap (Double-Ended-Heap)
- Symmetric min-max heap (SMMH)
Min-Max heap
- 一種Double-Ended priority queue
- 定義
- Complete Binary Tree
- Tree level被分為min level和max level
- 若某節點位於min(max) level,則以該點為根的子樹中,他的權重為最小(大)值。
- 第一層(root)必須為min level
- 完整介紹 & Insert & Delete
- 看圖弄懂流程就好,文章廢話太多了
- delete 有分delete min和delete max,略有不同
- Mega大筆記中,insert的圖示有誤,正確請參考文章
Deap (Double-Ended-Heap)
SMMH (Symmetric min-max heap)
- 定義
- 是一個Complete Binary Tree,Root不存Data,滿足:
- 左兄弟 ≤ 右兄弟 (近兄弟)
- 節點x的祖父的左子點必須 ≤ x
- 節點x的祖父的右子點必須 ≥ x
對於節點i來說,i的左子點是i的子樹中的最小值(不含i)。
對於節點i來說,i的右子點是i的子樹中的最大值(不含i)。
- 刪除插入文字敘述
- 刪除
Extended Binary Tree
- External Node = Failure Node, 數量為internal node+1
- E: External path length
- I: Internal path length
- N: Internal Node
- 完整介紹
Minimal Weighted External path length
Optimal Binary search tree (OBST)
略,見algo筆記
AVL tree
- 高度平衡樹,
- AVL定理
- 證明在連結最下面
- Let 形成高度h的AVL Tree所需最少的節點數為
- ,其中為費式數列,這裡令h=0為root height
- insert & 完整介紹: 見陳宜欣Slides
- delete
B tree
2-3 tree
- B tree of order 3 = 3 way B tree
- 內部結點degree為2 or 3
- degree=child數目,一個key的node有兩個child,兩個key的node有三個child
- 所有外部結點位於同階層
- 令2-3 tree height = h,internal node = n
- Insertion
- Delete: 看課本(也有影片,但課本講得比較好)
2-3-4 tree
B+ tree
- 很類似B tree,差在B+ tree leaf層才有完整資料,上面都是index,為的是降低IO次數
- 不是每次insert都需在non-leaf node留下自己的index,只有split後,才需要留
- split可以想成依照B tree規則先把資料弄上去,再把資料往下放到右子樹的最小值
以下為交大104資演:

ans:

參考mega ch9 advance tree筆記
其他參考: hackmd
B+ tree visualization
red-black tree

Union set
以下code使用union by rank with path compression
- 說明
- 複雜度:
- Union:
- Find (with union by rank):
- 另外有union by height , union by size,兩者導致union & find會有
- 參考台大102資演題目,當作練習
leftist tree
左傾樹
說明
Binomial queue
影片講得不錯
圖解
Binomial heap, Fibonacci heap
別人的筆記
補充
- In-place algorithm (重要)
- Build heap只需要就能完成,而非 !!!

- 複雜度O(n)證明: 最後一個父點所在的layer最多有n/4 nodes,他們最多會往下移動1;再上一層最多有n/8 nodes,他們最多往下移動2;… Top node最多往下移動h (ref)

- merge two heap:
- merge two leftist tree:

- Binary search tree如果不平衡,每次插入/查詢可達
- Fibonacci search, ref2 (還沒看,但感覺很厲害)

- 以108清大計科為例: 對1,2,3…16的sequence分別搜尋2, 10, 15所需次數