# MIT midterm/Final ## T/F ![](https://i.imgur.com/zTCO627.png) > 四維用人腦不好想像,先想二維:$T(n) = T(n/2) + O(n), T(n) = O(n)$ ![](https://i.imgur.com/8bH7L0r.png) > WTF ![](https://i.imgur.com/9MfhwBo.png) > WTF ![](https://i.imgur.com/OomGyMu.png) > WTF ![](https://i.imgur.com/k78UwYj.png) > 什麼的rotation? ![](https://i.imgur.com/zmrRvNR.png) > ![](https://i.imgur.com/khdSQsY.png) ![](https://i.imgur.com/v4wZr97.png) > substring是連在一起的,subsequence是可以分開的 ![](https://i.imgur.com/guAxJp7.png) > But 如果是問 insert "rotation"的cost,是 O(1) ![](https://i.imgur.com/7JOLh36.png) ![](https://i.imgur.com/Bl4C9Dd.png) > ![](https://i.imgur.com/5pMmPP7.png) ![](https://i.imgur.com/i89IEDh.png) ![](https://i.imgur.com/LEP7WjA.png) ![](https://i.imgur.com/EPYCqax.png) > 上面例子中weight都等於 1,所以在證明greedy choice property時,用greedy choice取代原本的optimal choice後,至少一樣好,但是在weighted interval當中,greedy choice的weight可能比optimal choice的weight還低,所以greedy choice正確性就不成立了 > ![](https://i.imgur.com/QiBvgOE.png) ![](https://i.imgur.com/hvKHJxe.png) > ![](https://i.imgur.com/vXLLp9A.png) ![](https://i.imgur.com/Al5tNVA.png) > 在Worst case $O(nlogn)$的情況下,$T(n) = 2T(n/2) + n$ (不一定要完美split成兩半,只要分成any constant fraction都可以是$O(nlogn)$) > 只需要$O(logn)$去存unsort array的start address ![](https://i.imgur.com/kXBsnED.png) > randomized quicksort 是對**任何**input都有 **expected** $O(nlgn)$ > 當然可以說**某些**input在**某些時候**會有$O(n^2)$的表現 > 但是不可能 always runs in $O(n^2)$ ![](https://i.imgur.com/4um7EpO.png) ![](https://i.imgur.com/nb4Swcj.png) ![](https://i.imgur.com/am8AUNx.png) ![](https://i.imgur.com/aanMfCD.png) > lightest edge 一定在 MST裡面是正確的,heaviset edge一定不在MST中嗎?不一定,例如heaviset edge為bridge ![](https://i.imgur.com/WqHPcLB.png) ![](https://i.imgur.com/T08eTyA.png) > 因為edge (u,v)只加了vertex 的weight各一半,所以最後還要再加上 $1/2 (w(u) + w(v))$ 才會是正確的shortest path weight ![](https://i.imgur.com/kzRz9Md.png) ![](https://i.imgur.com/dCX6mYM.png) ![](https://i.imgur.com/kt76rFD.png) ![](https://i.imgur.com/eZAA132.png) ![](https://i.imgur.com/CMZAYhI.png) ![](https://i.imgur.com/OFSt0IX.png) ![](https://i.imgur.com/NOpldzL.png) ![](https://i.imgur.com/XXK9qIC.png) > $T(n) = T(n/9) + T(8n/9) + \theta (n)$ > $T(n) = \theta (nlgn)$ ![](https://i.imgur.com/Qrz9yyZ.png) > True: > 令 potential function = $\sum_{i=0}^{n} 2w_i$, $w_i$為每個node在heap中的depth > so root 的$w_i = 0$,root的兩個child $w_i = 1$ > > For insert: > $\hat Ci = Ci + \Phi(Hi) - \Phi(H\text{i-1}) = Ci + 2h$, $h$為 heap depth = $O(logn)$ > $\hat Ci = O(logn) + O(logn) = O(logn)$ > For extract-min: > $\hat Ci = Ci + \Phi(Hi) - \Phi(H\text{i-1}) = Ci + (-2h)$ > $\hat Ci = O(logn) + (-O(logn)) = O(1)$ ![](https://i.imgur.com/yFplywM.png) ![](https://i.imgur.com/8RK7ukT.png) ![](https://i.imgur.com/m8ourTE.png) ![](https://i.imgur.com/SU8hASb.png) > DP 的 sub problem tree 不需要是 rooted tree,只需要是 DAG即可 ![](https://i.imgur.com/8hPhT2p.png) > 原本的min cut每一條edge都 + 1,可能不再是 min cut ![](https://i.imgur.com/z99r3WN.png) ![](https://i.imgur.com/wAK60KB.png) ![](https://i.imgur.com/RNpmf8Q.png) > 3SAT is NP-Complete, 所以若 P = NP,有polynomial time algorithm > 但是如果是 NP-hard but not in NP, 例如 halting problem,則就算 P = NP,還是沒有polynomial time algorithm > ![](https://i.imgur.com/aOyN701.png) ![](https://i.imgur.com/V4wjI3Q.png) > not "always" > 就像 random quicksort runs in expected $O(nlgn)$ for **all input**, 但是還是會有**某些**input在**某次**執行時的**worst case**是 $O(n^2)$ ![](https://i.imgur.com/02oRQMi.png) > Approximation factor 和 reduction沒有絕對關係 ![](https://i.imgur.com/baiwObv.png) ![](https://i.imgur.com/B7GQwcW.png) > edge weight unique, MST is unique, but second lowest ST is not. > 找second lowest ST就是移除 MST中最大edge weight,加入尚未在MST中,不會形成cycle且weight最小的edge > 反例:![](https://i.imgur.com/WMxogQX.jpg) ![](https://i.imgur.com/nlogNbs.png) > at most k edges是 Bellman-ford的觀念 > Floyd-Warshall的核心就是DP on k, where k is intermediate vertex from 1 to n ![](https://i.imgur.com/ZYTTdet.png) > 同樣道理,Accounting method的 bank 存款也不能為負 ![](https://i.imgur.com/HlAc7yn.png) ![](https://i.imgur.com/7a7z8eS.png) ![](https://i.imgur.com/UVGa64L.png) ![](https://i.imgur.com/Ix3IuC0.png) ![](https://i.imgur.com/okWPYs0.png) > Master theorem case 3: ![](https://i.imgur.com/C7beWel.png) ![](https://i.imgur.com/bXexDEN.png) > $m*1/m^{3}$ ![](https://i.imgur.com/iolpBXF.png) > 這是在說對於所有sort 最多就是$nlgn$個比較,但是像是insertion sort就有可能 $n^2$ (little o 是對於所有 n 都要成立) 等於說sorting得上限被壓到nlgn了(問題是it's not the case) -- ![](https://i.imgur.com/CqIecUO.png) ![](https://i.imgur.com/DDYrcIZ.png) > WLOG,先排好 ACD,接著用兩次comparison找出E的位置,E排在哪裡都沒差,因為先前假設A > B,所以B也一定只會需要和三個人比較,也就是兩次comparison ![](https://i.imgur.com/EuLmIix.png) ![](https://i.imgur.com/6H5uiN6.png) > 第一點:decision tree必須要是balanced(對任何input,建構出來的decision tree),才可以說因為 $2^h > n!$,所以必定可以sort好 第二點:$lg(n!)$ 和 $nlgn$ 只是 theta bound的關係,數字大小沒有絕對關係 ![](https://i.imgur.com/L2CMvtS.png) --- ![](https://i.imgur.com/smnnYGg.png) ![](https://i.imgur.com/WsQHg0X.png) > $source ->t ->... ->u->v$ $d[v] = d[u] + w(u, v)$, in which $w(u,v) > 0$ 我們可以從後面一個一個往前推,u在到v的s-t path中,再往前推,最後推到t, t是s到v的shortest path中最靠近s 的node,因此可以證明t也在shortest path中,因為edge weight 只有在從s出去的時候才是 < 0,但s為起點,一定在shortest path中(因為是求s到v的shortest path,所以不影響正確性),所以t也必定在shortest path中。 > 要注意:Dijkstra的**正確性**是建立在**沒有 negative weight edge**之上,而不是沒有negative weight cycle。 ![](https://i.imgur.com/q3fbHcx.png) ![](https://i.imgur.com/0HDPQ3X.png) ![](https://i.imgur.com/MVbFVQR.png) ![](https://i.imgur.com/HF9seWL.png) ![](https://i.imgur.com/oo2eE3a.png) ![](https://i.imgur.com/cbUEtFd.png) > $n(n+1) / 2*n = (n+1) / 2$ --> $O(n)$ ![](https://i.imgur.com/fuTC0qM.png) ![](https://i.imgur.com/7dxli6Q.png) ![](https://i.imgur.com/hjMquQ9.png) ![](https://i.imgur.com/3wtg4HZ.png) > johnson's algorithm allow negative weights because it will reweight those edges --> but no negative cycle allowed! --> graph may not have cycle --> DAG ![](https://i.imgur.com/P7LUFoW.png) ![](https://i.imgur.com/d4SBdv7.png) ![](https://i.imgur.com/5aXY7hx.png) > 也可用遞迴來想 存在n條線,再加上一條線與這n條都相交 total = $n + n +1$ 故從1開始,是$1 + 2 + 3 + 4 + ... + n = O(n^2)$ ![](https://i.imgur.com/BuOop2I.png) ![](https://i.imgur.com/kS6tkJf.png) ![](https://i.imgur.com/QHgQ19o.png) ![](https://i.imgur.com/TI6lyhS.png) ![](https://i.imgur.com/WJ0MGoK.png) > Johnson's algorithm的正確性: > 1. negative cycle若存在於原本的圖,則用source和weight 0 edge連接每個vertex後,negative cycle繼續存在 > 2. triangle inequality:從 s 到 u 和 s 到 v的shortest path距離,再加上w(u,v),即有不等式,用來reweight,保證reweight後的edge weight > 0 ![](https://i.imgur.com/4zEPJhH.png) ![](https://i.imgur.com/5LhZ8fO.png) > Diffie Hellman (DH) key exchange algorithm 三人以上不適用 ![](https://i.imgur.com/xI3xywN.png) > A van Emde Boas tree (Dutch pronunciation: [vɑn 'ɛmdə 'boːɑs]), also known as a vEB tree or van Emde Boas priority queue, is a tree data structure which implements an associative array with m-bit integer keys. It performs all operations in O(log m) time, or equivalently in O(log log M) time, where M = 2m is the maximum number of elements that can be stored in the tree. The M is not to be confused with the actual number of elements stored in the tree, by which the performance of other tree data-structures is often measured. The vEB tree has good space efficiency when it contains many elements, as discussed below. It was invented by a team led by Dutch computer scientist Peter van Emde Boas in 1975.[1] ![](https://i.imgur.com/i0V8Imo.png) ## others ![](https://i.imgur.com/raNLbKY.png) > 看到要求複雜度$O(1)$,通常就是要用hash > ![](https://i.imgur.com/gzglPb4.png) > ![](https://i.imgur.com/j258Ovd.png) ![](https://i.imgur.com/BzsSsfu.png)