# CLRS ## How to prove the algorithm's correctness [連結](https://hackmd.io/xe4bpXZ_R7SP-owqNZs7Kw#) ## Chapter 2 ### Insertion sort ![](https://i.imgur.com/dmzxMdo.png) ### Loop invariant ![](https://i.imgur.com/K6pO9yf.png) ### Merge sort ![](https://i.imgur.com/mBvljY1.png)<br> ![](https://i.imgur.com/TTnTbEU.png) ![](https://i.imgur.com/HDPT8Kz.png)<br> ![](https://i.imgur.com/9qsqWYv.png) ## Chapter 3 ### Comparing function ![](https://i.imgur.com/GnCSJee.png) ![](https://i.imgur.com/4eUoN2p.png)<br> * polylogarithmetically bounded <br> ![](https://i.imgur.com/uZNTu3o.png) ## Chapter 4 ### Methods for solving recurrence function ![](https://i.imgur.com/wv1JpDp.png) ### The maximum-subarray problem * Brute force ![](https://i.imgur.com/uxBdKj6.png) * Divide & conquer ![](https://i.imgur.com/i3KmhzN.png) ![](https://i.imgur.com/oSXnvmn.png) ![](https://i.imgur.com/E5dmUYl.png) * Greedy (best) time complexity : O(n) ![](https://i.imgur.com/5lfS3lo.png) ### Strassen's algorithm * Brute force, time complexity : Θ($n^{3}$) ![](https://i.imgur.com/2ISmKCm.png) ![](https://i.imgur.com/qtspSLG.png) * Strassen ![](https://i.imgur.com/5fwD56K.png) ![](https://i.imgur.com/QnKH9lz.png) ### Substitution method ![](https://i.imgur.com/Pk1RCSn.png) ### Recursion-tree method ![](https://i.imgur.com/nud4D54.png) ![](https://i.imgur.com/KZ5Rn2t.png) ![](https://i.imgur.com/F1fQ8Dl.png) ![](https://i.imgur.com/ly2YzF1.png) ### Master theorem method ![](https://i.imgur.com/JN3gerD.png) ## Chapter 5 ### The hiring problem ![](https://i.imgur.com/8NL6yub.png) ## Chapter 6 ### Heap sort * Heapify ![](https://i.imgur.com/TW0j5qS.png) * Build heap ![](https://i.imgur.com/SwvjVNn.png) ### Priority queue ![](https://i.imgur.com/IULkLpZ.png) ![](https://i.imgur.com/4thFTki.png) ## Chapter 7 ### Partition ![](https://i.imgur.com/4Xs50C9.png) ### Quick sort ![](https://i.imgur.com/5TVf2Uo.png) ![](https://i.imgur.com/kWOeeTR.png) ![](https://i.imgur.com/XZOeXI8.png) ### Randomized Quick sort ![](https://i.imgur.com/p9bGJE1.png) ![](https://i.imgur.com/wzO9qtz.png) ## Chapter 8 ### Counting sort ![](https://i.imgur.com/a3JQ3qf.png) ![](https://i.imgur.com/t1VTtof.png) ![](https://i.imgur.com/M7Exayh.png) ![](https://i.imgur.com/LZxQv2o.png) ### Radix sort ![](https://i.imgur.com/AmPhP09.png) ### Bucket sort ![](https://i.imgur.com/p1m9h9R.png) * Proof that bucket sort's time complexity is linear time ![](https://i.imgur.com/ZayToxH.png) ![](https://i.imgur.com/mNCKf6u.png) ![](https://i.imgur.com/NaUKg9V.png) ## Chapter 9 ### Selection problem ![](https://i.imgur.com/13kiJ0P.png) ![](https://i.imgur.com/NXTVi7U.png) ![](https://i.imgur.com/uyNPUAP.png) * worst case ![](https://i.imgur.com/LSjqZHF.png) ![](https://i.imgur.com/dpARCGy.png) ![](https://i.imgur.com/6OdEUGA.png) ## Chapter 10 ### Sentinels ![](https://i.imgur.com/yFKcTQS.png) ![](https://i.imgur.com/facq9md.png) ![](https://i.imgur.com/FMHP64P.png) ### Comparsions among list ![](https://i.imgur.com/FbJipXh.png) ## Chapter 11 ### Analysis of hashing using chaining ![](https://i.imgur.com/K2Wmx1r.png) * unsuccessful search ![](https://i.imgur.com/2mk5qXe.png) * successful search ![](https://i.imgur.com/mZmVkxC.png) ![](https://i.imgur.com/Ezu0W0r.png) ![](https://i.imgur.com/4mCf3sI.png) ![](https://i.imgur.com/sSD2rEl.png) ### Open addressing ![](https://i.imgur.com/aubAcWg.png) ![](https://i.imgur.com/BFCOYlK.png) ![](https://i.imgur.com/sHU3aR7.png) ### Linear probing ![](https://i.imgur.com/DoIOkKY.png) ### Quadratic probing ![](https://i.imgur.com/HmWnBSE.png) ### Double hashing ![](https://i.imgur.com/YdTkg85.png) ### Perfect hashing ![](https://i.imgur.com/y8skkc1.png) ![](https://i.imgur.com/gLaoW7K.png) ## Chapter 12 ### The number of bst with n node ![](https://i.imgur.com/N1cF21U.png) ![](https://i.imgur.com/lgp4miK.png) ![](https://i.imgur.com/ywC8qWe.png) ![](https://i.imgur.com/nDEHsK1.png) ## Chapter 13 ### [Insertion](http://alrightchiu.github.io/SecondRound/red-black-tree-insertxin-zeng-zi-liao-yu-fixupxiu-zheng.html) * Uncle is red → color change * Uncle is black (insert node is right child) → left rotation * Uncle is black (insert node is left child) → right rotation ### [Deletion](http://alrightchiu.github.io/SecondRound/red-black-tree-deleteshan-chu-zi-liao-yu-fixupxiu-zheng.html) ## Chapter 14 ### Interval trees ![](https://i.imgur.com/kdqJFQU.png) ## Chapter 15 ### Dynamic programming definition ![](https://i.imgur.com/gzkmx0h.png) ### Rod cutting ![](https://i.imgur.com/51unLYu.png) * Brute Force : time complexity O($2^n$) * DP Equation : time complexity O($n^2$) 1. Top-down with memorization ![](https://i.imgur.com/BBnHFzS.png) 2. Bottom method ![](https://i.imgur.com/ic2vFmM.png) 3. Reconstructing a solution (not only value but an optimal solution) ![](https://i.imgur.com/sUcYJul.png) ### Matrix-chain multiplication * Brute force : time complexity O($n^3$) * Counting the number ![](https://i.imgur.com/RzbIQUI.png) * DP Equation : time complexity O($n^2$) ![](https://i.imgur.com/wE9Vgur.png) ![](https://i.imgur.com/yjLzEMJ.png) ![](https://i.imgur.com/QCkstUu.png) ![](https://i.imgur.com/Y5yzxtZ.png) ### LCS * time complexity : O($m * n$) * space complexity : O($m * n$) ![](https://i.imgur.com/OZL1i1p.png) ![](https://i.imgur.com/basgxdn.png) ![](https://i.imgur.com/DUmOWI4.png) ### OBST * Naive recursive approach time complexity : O($2^n$) * DP time complexity : O($n^3$) ![](https://i.imgur.com/zrsNhZc.png) ![](https://i.imgur.com/OGoHeYl.png) ![](https://i.imgur.com/guBqiJI.png) ![](https://i.imgur.com/LC6Ro3d.png) ### Longest simple path in a directed acyclic graph * time complexity : O(n) * space complexity : O(n) ![](https://i.imgur.com/9uRRvL9.png) ### Edit distance s is the maximum edit distance * time complexity : O($s×min(m,n)$), where m and n are the lengths of the strings. * Space complexity is O($s^2$) or O(s) ![](https://i.imgur.com/wOwzPpJ.png) ![](https://i.imgur.com/fUpfIFC.png) ![](https://i.imgur.com/1GQk0PE.png) ## Chapter 16 ### Greedy definition ![](https://i.imgur.com/zrAnjaw.png) ### An activity-selection problem ![](https://i.imgur.com/QuP0xD4.png) ![](https://i.imgur.com/L42gxyF.png) ![](https://i.imgur.com/bYcyzM2.png) ### Greedy strategy elements 1. A candidate set : 可構成解的子集 2. A selection function : 用來選擇最佳解的 function 3. A feasibility function : 用來決定誰可成為 candidate 的 function 4. A objective function : 此問題的部分解 5. A solution function : 用來決定完整解是否已被找到 ![](https://i.imgur.com/dfMADIU.jpg) ### Huffman codes ![](https://i.imgur.com/lDMo6jF.png) ![](https://i.imgur.com/wtPsKmR.png) ![](https://i.imgur.com/cwt8fB0.png) * Correctness about constructing a Huffman tree ![](https://i.imgur.com/J8hgY5r.png) ## Chapter 17 [講解影片](https://youtu.be/kShuBNlXQ8M) [台大ppt](https://www.csie.ntu.edu.tw/~hsinmu/courses/_media/ada_11fall/amortized_analysis.pdf) ## Chapter 18 ### B-tree definition ![](https://i.imgur.com/bGsUuHX.png) ![](https://i.imgur.com/p3AC09v.png) ### B-tree height ![](https://i.imgur.com/53yM9Ft.png) ![](https://i.imgur.com/eqTL5e6.png) ### B-tree search ![](https://i.imgur.com/Ss4eZJF.png) ### B-tree create tree ![](https://i.imgur.com/3Pa9HJo.png) ### B-tree vs B + tree ![](https://i.imgur.com/zbJDVJa.png) ### B tree vs B + tree time complexity ![](https://i.imgur.com/mIBGoiE.png) ![](https://i.imgur.com/DEEdGyB.png) ### B-tree insert / deletion [教學網站](https://medium.com/hallblazzar-%E9%96%8B%E7%99%BC%E8%80%85%E6%97%A5%E8%AA%8C/%E5%AD%B8%E7%BF%92%E6%89%8B%E8%A8%98-2018%E6%B8%85%E8%8F%AF%E5%A4%A7%E5%AD%B8db-ai-bootcamp-ii-b-tree-indexing-648a096e1598) ## Chapter 19 ### time complexity * Prim's algorithm & Dijkstra's algorithm 因 decrease key O(1) 時間複雜度降低 ![](https://i.imgur.com/cJTdaQH.png) [教學影片](https://youtu.be/y2v6SwOvB_M) ### [Fibonacci number analysis](https://stackoverflow.com/questions/14333314/why-is-a-fibonacci-heap-called-a-fibonacci-heap) ![](https://i.imgur.com/Ytri7nA.png) ![](https://i.imgur.com/Y9nlw76.png) ### Difference between Binomial heap and Fibonacci heap [Greeksfoegreeks](https://www.geeksforgeeks.org/difference-between-binary-heap-binomial-heap-and-fibonacci-heap/) ![](https://i.imgur.com/I7tSCGU.png) ## Chapter 20 ### Definition [MIT教學影片](https://youtu.be/hmReJCupbNU) [Wiki](https://en.wikipedia.org/wiki/Van_Emde_Boas_tree) * Use bit vector to represent the universe integer set * And buid tree on the bit vector * Using binary search to search the value, in each level * Time complexity : $O(lglgn)$ * Recursion function : $T(n) = T(\sqrt n) + O(1)$ ## Chapter 21 ### Definition [Disjoint set](https://cihcih.medium.com/%E5%9C%96%E8%AB%96%E6%BC%94%E7%AE%97%E6%B3%95ch21-ds-for-disjoint-sets-de61210d671a) [NTU ppt](https://www.csie.ntu.edu.tw/~hsinmu/courses/_media/dsa_17spring/disjoint_set.pdf) Weight Union * Union by size 以點數較多者為合併對象,點數少者合併至點數多者,將 pointer 指向多者 set 之 root * Union by height 以樹高較高者為合併對象,樹高低者合併至樹高高者,將 pointer 指向高者 set 之 root * Path compression (路徑壓縮) 路徑壓縮是一種在樹上使用 Find 時平展樹結構的方法。由於在到達根的途中訪問的每個元素都是同一集合的一部分,因此所有這些訪問的元素都可以直接重新附加到根。生成的樹要平坦得多,不僅加快了對這些元素的未來操作,而且也加快了對引用它們的元素的操作。 簡而言之,在 Find-Set () 過程中尋訪之元素將其原指向 parent pointer 改指向 root (降低樹高) Weight Union + Path compression → time complexity O(α(n)*m) = O($lg*n$) 其中,α 為 [Ackermann inverse function](https://en.wikipedia.org/wiki/Ackermann_function#Inverse),趨近於 constant which $< 5$ m 為 operation 次數 (Make-Set / Find-Set / Union),n 為 Make-Set 個數 ### Analysis ![](https://i.imgur.com/OQqXMC1.png) ### Amortized analysis ![](https://i.imgur.com/cr8km1Y.png) ### Potential function ![](https://i.imgur.com/Kh9Nfet.png) ![](https://i.imgur.com/23JR0OR.png) ### Why Link operation cost α(n) ![](https://i.imgur.com/i68AAuj.png) ## Chapter 22 ### Incidence matrix (directed graph) ![](https://i.imgur.com/GMSb3Yw.png) ### BFS ![](https://i.imgur.com/GMzjyfP.png) ### Diameter * 對經過每一點之一邊做一次 BFS,time complexity O(|E|) = O(|V| - 1) = O(V) ![](https://i.imgur.com/U9NYRwk.png) ![](https://i.imgur.com/S4MuKBA.png) ### DFS ![](https://i.imgur.com/4MI9ALP.png) * Edge type ![](https://i.imgur.com/yU5Ginc.png) ![](https://i.imgur.com/z8qw9SX.png) * White path theorem ![](https://i.imgur.com/1nRwbsr.png) ### Topological sort ![](https://i.imgur.com/g71svzP.png) ### Euler tour ![](https://i.imgur.com/vsW6cQr.png) [Solution](https://walkccc.me/CLRS/Chap22/Problems/22-3/) ### Find strongly connected component Strongly connected : 每點 vertex 至圖中任一點皆有路徑經過 演算法將會用到Transpose of Graph,edge的方向顛倒,就得到$G^{T}$, 例如,原本的 edge(0,1) 改為 edge(1,0),edge(5,6) 改為 edge(6,5)。 $G$與$G^{T}$的SCC完全相同 ![](https://i.imgur.com/I5Bldsv.png) [Method](http://alrightchiu.github.io/SecondRound/graph-li-yong-dfsxun-zhao-strongly-connected-componentscc.html) ## Chapter 23 ### Kruskal's algorithm ![](https://i.imgur.com/8GBhdRu.png) ### Prim's algorithm <img src="https://i.imgur.com/KDWtKPx.png"> ### Sollin's algorithm ![](https://i.imgur.com/qpj98Ts.png) ### Second best MST 找不在集合邊且加入後與原先MST差相距最小 [Algorithm](https://www.geeksforgeeks.org/second-best-minimum-spanning-tree/) ## Chapter 24 ### Dijsktra's algorithm ![](https://i.imgur.com/fwcGTY8.png) ![](https://i.imgur.com/dLdo52F.png) ![](https://i.imgur.com/Ow2gaqA.png) ![](https://i.imgur.com/9Ip9XWc.png) * Prove the correctness ![](https://i.imgur.com/N6Govc5.png) ![](https://i.imgur.com/nW3WWwG.png) ### Bellamn-Ford ![](https://i.imgur.com/gno8e2K.png) * Prove the correctness ![](https://i.imgur.com/BcwV9Cj.png) ![](https://i.imgur.com/NTaOqTS.png) ### Floyd-Warshall ![](https://i.imgur.com/AfxIz1h.png) ### Johnson's algorithm ![](https://i.imgur.com/yKPrCiy.png) ![](https://i.imgur.com/7IX5E2M.png) ![](https://i.imgur.com/wbm9q37.png) * time complexity : $O(V(VlogV + E)) = O(V^{2}logV + VE)$ with Fibonacci heap * if use binary heap : $O(V((V+E)logV + E)) = O(VElogV)$ * But all the above are better than Floyd-Warshall with $O(V^{3})$ ## Chapter 25 ### Ford-Flukerson * Given a flow network G = (V, E) and a flow f , an ==augmenting path== p is a simple path from s to t in the residual network $G_{f}$ * We call the maximum amount by which we can increase the flow on each edge in an augmenting path p the ==residual capacity== of p ![](https://i.imgur.com/Oh2XJJF.png) ### The Edmonds-Karp algorithm ==We can improve the bound on FORD-FULKERSON by finding the augmenting path p in line 3 with a breadth-first search.== That is, we choose the augmenting path as a shortest path from s to t in the residual network, where each edge has unit distance (weight). We call the Ford-Fulkerson method so implemented the Edmonds-Karp algorithm. We now prove that the Edmonds-Karp algorithm runs in ==$O(VE^{2})$== time. ![](https://i.imgur.com/MMmlx7V.png) ![](https://i.imgur.com/ZmfFB6n.png)