註:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Analysis algorithm
Divide and conquer
merge sort
Max subarray problem
Matrix multiplication
Selection problem
The closet pair problem
- p53
- 大致如下
- 把pair依照y座標排列
- 畫線垂直x軸,把P中的點分成左右: L,R
- 求左右closet pair距離:dl, dr,最短距離d=min{dl,dr}
- 找跨越中線的closet pair, 找到某個點離中線距離<d,往後面看7個點是否在中線兩邊 (看課本講)
- return d
Dynamic programming
兩種情況能用DP
- Optimal substructure
- Overlapping subproblem
- A recursive algorithm revisits the same subproblem over and over again.
Rod cutting
Given a rod of length n inches and an array of prices that includes prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces.
給定一根長度為n的繩子和一個價格陣列,其中包括所有尺寸小於 n 的長度價格。
求切繩子最多能賣多少錢。
time complexity:
參考
- geeksforgeeks
Fractional (Continuous) knapsack
time complexity: –>sort
0-1 knapsack
兩種方法,一個用2D array做,一個用1D array做。
- 2D版:
可以還原拿到哪些物品,並知道最高價值。
時間複雜度,空間複雜度 。N是物品數量,W是背包重量限制。而W的數值會隨input size成指數成長關係,為NPC,(目前)不是ploynomial time solvable
- 1D版
只能知道最高價值
Time Complexity: . As redundant calculations of states are avoided.
Auxiliary Space: O(W) As we are using 1-D array instead of 2-D array.
參考:
- 表格怎麼畫
PS: 這個表格基本上跟code原理一樣,看著題目對照code寫一次就會了!
多畫幾次就會發現憑感覺畫會比快
- 表格怎麼畫2
- Code source(1-D array)
- Code source(2-D array)
- 陳縕農講knapsack變化題
Matrix Chain Multiplication
看印度人講解
- 遞迴式
- 畫表格(以考古14題為例,p97)
- 有n個矩陣,需要兩個表格,分別為和

- 記得先處理兩個的table的對角線,也注意index有無標錯
- (2,1), (3,2), (4,3), (5,4)可以直接填入A1A2A3, A2A3A4…相乘的次數
- 然後就照公式推
參考
- geeksforgeeks
Optimal binary search tree (OBST)

畫表格:
i |
0 |
1 |
2 |
3 |
4 |
pi |
X |
5 |
2 |
4 |
3 |
qi |
3 |
2 |
3 |
4 |
2 |
ps: if no dummy node, all qi become 0
- W[i][j] = W[i][j-1]+p[j]+q[j] –>先畫出W表
- S[i][j] = min(i≤r≤j) { s[i][r-1]+s[r+1][j]+w[i][j] } –>依照剛剛的W表和題目給的pi, qi表畫出來
- 邊畫邊紀錄r表(哪個r讓S[i][j]有min)
- 看r表的形狀畫出Optimal search tree
- 資結用的算法和algo教的不同,資結版把Cii全設為0(s table的對角線),而非algo版的對應qi的值。也就是資結版不算入failure node level的cost,考試時需要自行註明
參考:
- 表格怎麼畫(無dummy node)
- 有dummy node的題目跟著講義答案做一次就會了(林立宇p.76兩題都要寫!!!)
- 許建平ch15-3投影片
Longest Common sequence (LCS)
Time complexity:
Longest increasing sequence (LIS)
考試應該寫出就夠了,不然LIS的版本有點複雜
Longest common substring
Longest palindrome subsequence
目前只有台大105考過 先放掉
Minimum edit distance
看影片畫表格,這個滿簡單的
參考
geeksforgeeks
The KMP algorithm
目前看到的都是畫表格題型,code要不要背再斟酌
prefix func畫表格: 肉眼比對一下前綴(prefix)後綴(postfix)長度就好了
prefix funtion全部-1後就是failure function
下面為failure function
參考
🌟成大資工wiki: KMP
🌟畫表格
影片示範
初學者學 KMP 演算法
change-making (coin-changing)
Huffman
Time complexity:
subset sum
可以被reduce成01 knapsack,詳細怎麼做還要再查
參考
geeksforgeeks
Graph
- 圖的名詞補充:
- light edge:考慮所有cross edge中,「weight最小」的edge稱為light edge(可以不只一條)
- Cut: (S,V−S)是一種將Graph(G=(V,E))的V(vertex set)分成兩部分的partition(分割)
- Cross:若存在一條edge(X,Y),其中X⊆S,Y⊆V−S,則稱這條edge「crosses」 Cut (S,V−S)
- Respect: Set A中沒有任何一條edge是Cut的crossing edge,則稱這個Cut「respect」 Set A。(因為尊敬,所以不切開。)
- Safe edge: edge對Set A來說是安全的(safe),使得「該edge加入Set A後,Set A仍然滿足 A⊆E(MST)
- (109清大計科, 109台大資演)
- distance: 兩點間最短路徑的長度
- eccentricity: 從某點v到其他點的最長的distance (或指該條最長路徑)
- diameter: 某圖中最大的eccentricity
- radius: 某圖中最小的eccentricity
- center: 形成radius的vertex (只有1 or 2個)
Elementary Graph Algorithms
BFS
- Time complexity:
- (下面的algo,用adjacency list存G)
- (用adjacency matrix存G)
上面是algo課本CLRS的,下面是陳宜欣slides的版本
DFS
- Time complexity
- (下面的algo,用adjacency list存G)
- (用adjacency matrix存G)
上面是algo課本CLRS的,下面是陳宜欣slides的版本
- 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)
topological sort
- Time complexity
- topological sort存在 <–> G是acyclic(無環)



Strongly Connected Component (SCC)
- Time complexity: 同DFS
- Finding_all_SCC又稱作Kosaraju's algorithm
Minimum spanning trees (MST)
- MST上的path必為minimax path (p172. 30題)
-
- 用single source shortest path 找(Dijkstra, Bellman-Ford)
Kruskal
- Time complexity:
- (sorted, adjacency list)
- (Unsorted, adjacency list)
- (adjacency matrix with array)
- 不會形成loop的前提下,每次都挑最小的邊,直到所有v被包含
Prim's
- Time complexity:
- (binary heap)
- (Fibonacci heap)
- (adjacency matrix with array)
- 隨便選個root,不會形成loop的前提下,把他到鄰居最小的edge選出來,直到所有v被包含
- CLRS版
補充: 還有一種MST algo 叫Sollin's algorithm,只有在陳宜欣的slides裡面有,真的很閒再去學
Single source shortest paths
Dijkstra
- Time complexity:
- (binary heap)
- (Fibonacci heap)
- (array)
- 不能有負邊!!!
- 表格: p.131
- 下面的code很pseudo考試應該要自己再轉換
Bellman-Ford
- Time complexity:
- 可以有負邊,不能有負環(所有最短路徑演算法都不能有負環,會無解)
- 第5行不一定要做到V-1次,如果某次relax完,發現沒有被更新,就可以停了
- 因為做到V-1也不會改變!
- 但如果多做1次,也就是V次,還被更新,表示存在負環
- 表格: p.135,
- 注意每一個k代表要relax每個source以外的點一次,順序沒差
- 可以把到每個vertex的最短距離寫在圖的vertex上
- relax過程中需要一直改圖上和表格中的內容
- 做一次應該就知道了,記得熟悉演算法原理
- 負環: sum of edge weight in a cycle is negative
DAG shortest path
- Time complexity:
- DAG –- Directed Acyclic Graph
DAG longest path
把上面shortest path的程式改成:
- inf -> -inf
- ->
- (所有edge weight乘-1): 應該不用
All pairs shortest paths
Floyd-Warshall
- Time complexity:
- W是G的adjacency matrix, 允許負邊,沒負環
- 檢查負環是否存在: 做完看矩陣對角線有沒有負數

- Transitive closure: 檢查兩點間有無path

▲
- 第7行前都在initialize,相鄰的就寫1,其他寫0
Johnson
- Time complexity:
- (Fibonacci Heap)
- (Binary heap)
- E很小時,比Floyd-Warshall快
- 改寫negative weight成positive
- 建一個新的vertex: s
- 把他和每個圖中的點相接,edge皆設0
- 把每個vertex加上weight=δ(s,v) –>s到v最短距離
- 改寫edge weight: ŵ(u,v)=w(u,v)+δ(s,u)-δ(s,v)
- Pseudo code

- 上面翻譯:
- 加入點s,從他連接每個vertex的w都設0
- 用Bellman-Ford檢查是否存在負環,如果有就GG
- 沒負環,用bellman-Ford把每個vertex的weight h(v)設s到v最短距離(bellman-ford)
- 改寫wight:
- 對每個點跑Dijkstra,計算到彼此的最短距離
- 剛剛的距離是改寫過的,真正最短距離要改回來:
Maximum flow
- minimum cut: 值同Maximum flow
- 從source流到terminal的path上,第一個滿的edge,就是bottleneck(就是他害你不能流更多水),該edge就在Minimum cut中 (見p223, 103題)
- Residual network: 有逆向箭頭的那個圖 (往回流)
- augmenting path: residual network中,能從s流到t的path。
- 如果找不到augmenting path,代表找到maximum flow了
Ford-Fulkerson
Edmonds-Karp
- Time complexity:
- 和Ford-Fulkerson唯一區別: 找augmenting path用BFS(Ford-Fulkerson沒限制)
Maximum Bipartite matching

- Time complexity:
- 因為用Ford-Fulkerson找=
- 其中是maximum flow,不會超過vertex數量
- 目標: 找出最多pair
- 作法:
- 左邊加上source s連接L的每個vertex v
- 又邊加上terminate t連接R的每個vertex u
- 每條edge的capacity c(u,v)=1
- 找maximum flow
NP complete & approximation algo
NPC
- 經典NPC問題
- 3-CNF-SAT
- Clique
- vertex-cover
- k-coloring (determine whether a graph can be colored with k colors)
- Hamiltonian Cycle
- Hamiltonian path
- Traveling salesman problem
- complete graph中是否cost至少為k的hamiltonian cycle
- Longest Path
- graph中是否存在長度至少為k的path (不一定經過每個點!)
- 01 knapsack
- Min-Flow/Max-cut (不是max-flow/min cut !)
- 常被誤會的P問題
- 2-CNF-SAT
- Euler circuit
- Euler trail
- Shortest path (SSSP, APSP)
-
通常證明套路 (ex. 證明TSP是NPC)
- TSP NP
- 可以在poly-time驗證某個case是否為TSP
- HC TSP
- 看下面我懶得打

approx algo
vertex-cover
- 證明他是2-approximation algo:
- In line 5,every edge can only covered by its endpoint
- Therefore, in each iteration, one of the selected vertex must be in the optimal vertex cover
Euclidean traveling salesman problem (ETSP)
-
我們無法在一般的TSP中找到approximation algorithm
- 以下是在歐幾里得平面上的TSP問題的近似演算法
- 證明他是2-approximation algo:



Maximum cut
-
minimum cut (maximum flow) 的相反問題
-

-
證明他是2-approximation algo:
When a vertex v is fixed, we will add some edges into the cut, and discard some edges (u, v) if u is placed in the same set as v
But when each vertex is fixed :
#edges added #edges discarded total #edges added m/2
-
Time Complexity Table
class |
algorithm |
time complexity |
Basic |
|
|
|
BFS |
|
|
DFS |
|
|
TP sort |
|
|
SCC |
|
MST |
|
|
|
Kruskal |
|
|
|
|
|
|
|
|
Prim |
|
|
|
|
|
|
|
SSSP |
|
|
|
Dijkstra |
|
|
|
|
|
|
|
|
Bellman-Ford |
|
|
DAG-SP(LP) |
|
APSP |
|
|
|
Floyd-Warshall |
|
|
Johnson |
Fh |
|
|
bh |
MaxFlow |
|
|
|
Ford-Fulkerson |
|
|
Edmonds-karp |
|
|
|
|
- 上表用詞解釋
- MST: minimum spanning tree
- SSSP: single source shortest path
- APSP: all source shortest path
- SCC: strongly connected component
- mat: matrix
- fh: fibonacci heap
- bh: binary heap

