0x00 前言
作爲學術生涯的最後一門課,選了一門據說是最難的,上下來的感覺也確實是難得不行,不太懂……
決定照着ppt和上課的筆記整理一下,以此爭取達到複習的目的。
(意思是有些雖然寫出來了,但自己都不見得明白,有的部分存疑後續去詢問之後再做修改)
Introduction of Randomized Algorithm
0x01 Big O notation
f ( n ) = O ( g ( n ) ) : ≤ f(n)=O(g(n)):~ \le f ( n ) = O ( g ( n ) ) : ≤
which means ∃ c , l i m ‾ n → ∞ f ( n ) g ( n ) ≤ c \exists~c, \overline{lim}_{n \rightarrow \infty}\frac{f(n)}{g(n)}\le c ∃ c , l i m n → ∞ g ( n ) f ( n ) ≤ c , c is constant
f ( n ) = o ( g ( n ) ) : < f(n)=o(g(n)):~ \lt f ( n ) = o ( g ( n ) ) : <
which means l i m ‾ n → ∞ f ( n ) g ( n ) = 0 \overline{lim}_{n \rightarrow \infty}\frac{f(n)}{g(n)} = 0 l i m n → ∞ g ( n ) f ( n ) = 0
f ( n ) = Ω ( g ( n ) ) : ≥ f(n)=\Omega(g(n)):~ \ge f ( n ) = Ω ( g ( n ) ) : ≥
which means g ( n ) = O ( f ( n ) ) g(n)=O(f(n)) g ( n ) = O ( f ( n ) )
f ( n ) = ω ( g ( n ) ) : > f(n)=\omega(g(n)):~ \gt f ( n ) = ω ( g ( n ) ) : >
which means g ( n ) = o ( f ( n ) ) g(n)=o(f(n)) g ( n ) = o ( f ( n ) )
f ( n ) = Θ ( g ( n ) ) : = f(n)=\Theta(g(n)):~ = f ( n ) = Θ ( g ( n ) ) : =
which means f ( n ) = O ( g ( n ) ) f(n)=O(g(n)) f ( n ) = O ( g ( n ) ) and f ( n ) = Ω ( g ( n ) ) f(n)=\Omega(g(n)) f ( n ) = Ω ( g ( n ) )
0x02 QuickSort
Sorting Problem: Given a set S of n numbers, sort them into ascending order.
Find random y y y of set S S S ; (O(1))
Partition S ∖ { y } S \setminus \{y\} S ∖ { y } into two sets S 1 S_1 S 1 and S 2 S_2 S 2 ; (n)
Recursively sort S 1 S_1 S 1 and S 2 S_2 S 2
Time complexity: O ( n l o g n ) O(n log n) O ( n l o g n ) does not depend on input. It holds for every input.
E ( T ( n ) ) = 1 n ∑ k = 1 n ( E ( T ( n ) ) ∣ X n = k ) = 1 n ∑ k = 1 n ( E ( T ( k − 1 ) ) + E ( T ( n − k ) ) + n − 1 ∣ X n = k ) = 1 n ∑ k = 1 n ( E ( T ( k − 1 ) ) + E ( T ( n − k ) ) + n − 1 ) ∴ E ( T ( n ) ) = 1 n ∑ k = 1 n ( E ( T ( k − 1 ) ) + E ( T ( n − k ) ) + ( n − 1 ) )
\begin{aligned}
E(T(n))~&=\frac{1}{n}\sum_{k=1}^{n}\left( E(T(n)) ~|~ X_n=k \right) \\
&=\frac{1}{n}\sum_{k=1}^{n}\left( E(T(k-1)) + E(T(n-k)) + n-1 ~|~ X_n=k \right) \\
&=\frac{1}{n}\sum_{k=1}^{n}\left( E(T(k-1)) + E(T(n-k)) + n-1 \right) \\
\therefore E(T(n))~&=\frac{1}{n}\sum_{k=1}^{n}\left( E(T(k-1)) + E(T(n-k)) + (n-1) \right) \\
\end{aligned}
E ( T ( n ) ) ∴ E ( T ( n ) ) = n 1 k = 1 ∑ n ( E ( T ( n ) ) ∣ X n = k ) = n 1 k = 1 ∑ n ( E ( T ( k − 1 ) ) + E ( T ( n − k ) ) + n − 1 ∣ X n = k ) = n 1 k = 1 ∑ n ( E ( T ( k − 1 ) ) + E ( T ( n − k ) ) + n − 1 ) = n 1 k = 1 ∑ n ( E ( T ( k − 1 ) ) + E ( T ( n − k ) ) + ( n − 1 ) )
0x03 Comparison Probability
判斷序列中的兩個數在QuickSort中被比較過的概率
當第i i i 個數字和第j j j 個數字曾經被比較過,設 X i j = 1 X_{ij}=1 X i j = 1 ,反之爲0。
有 E ( X i j ) = P i j E(X_{ij})= P_{ij} E ( X i j ) = P i j ,從而有 T ( n ) = ∑ 1 ≤ i < j ≤ n X i j T(n)= \sum_{1\le i<j\le n} X_{ij} T ( n ) = ∑ 1 ≤ i < j ≤ n X i j 。
P 12 = 1 , P 1 n = 2 n , P i j = 2 j − i + 1 P_{12}~=1,~ P_{1n}=\frac{2}{n},~ P_{ij}=\frac{2}{j-i+1} P 1 2 = 1 , P 1 n = n 2 , P i j = j − i + 1 2
E ( T ( n ) ) = ∑ 1 ≤ i < j ≤ n P i j E(T(n))~=\sum_{1\le i<j\le n} P_{ij} E ( T ( n ) ) = 1 ≤ i < j ≤ n ∑ P i j
∑ 1 ≤ i < j ≤ n 1 j − i + 1 = 2 ∑ 1 ≤ i ≤ n − 1 ∑ j = 2 n − i + 1 1 j ≤ 2 ( n − 1 ) ∑ j = 2 n 1 j ≈ 2 ( n − 1 ) ⋅ ln n \sum_{1\le i < j \le n} \frac{1}{j-i+1} = 2\sum_{1\le i \le n-1} \sum_{j=2}^{n-i+1} \frac{1}{j} \le 2(n-1)\sum_{j=2}^{n}\frac{1}{j} \approx 2(n-1) \cdot \ln n 1 ≤ i < j ≤ n ∑ j − i + 1 1 = 2 1 ≤ i ≤ n − 1 ∑ j = 2 ∑ n − i + 1 j 1 ≤ 2 ( n − 1 ) j = 2 ∑ n j 1 ≈ 2 ( n − 1 ) ⋅ ln n
其中可通過洛必達準則(忘記是不是這個名字了),計算出調和級數實際和 y = 1 x y=\frac{1}{x} y = x 1 相同
1 2 + 1 3 + ⋯ + 1 n ≤ ∫ 1 n 1 x = ln n \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n} \le \int_{1}^{n}\frac{1}{x}=\ln n 2 1 + 3 1 + ⋯ + n 1 ≤ ∫ 1 n x 1 = ln n
1 + 1 2 + 1 3 + ⋯ + 1 n ≥ ∫ 1 n 1 x = ln n − ln 1 1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n} \ge \int_{1}^{n}\frac{1}{x} = \ln n - \ln 1 1 + 2 1 + 3 1 + ⋯ + n 1 ≥ ∫ 1 n x 1 = ln n − ln 1
於是我們可以得到 E ( T ( n ) ) = Θ ( n ln n ) E(T(n)) = \Theta(n \ln n) E ( T ( n ) ) = Θ ( n ln n ) ,
不過這個計算出來除了作爲一道題而言,還有沒有其它的用處呢?
0x04 Min-Cut
Min-Cut problem: Given a graph G = ( V , E ) G = (V, E) G = ( V , E ) which is a connected, un-directed multi-graph, find a cut with minimum cardinality.
Pick an edge uniformly at random;
Merge the endpoints of this edge;
Remove self-loops;
Repeat steps 1-3 until there are only two vertices remain.
The remaining edges form a candidate cut.
f i x m i n c u t C . ∣ C ∣ = k fix~min~cut~C.~|C|=k f i x m i n c u t C . ∣ C ∣ = k
P r ( E 1 ∧ E 2 ∧ ⋯ ∧ E n − 1 ) , E i : the i-th chosen edge is not in C Pr(E_1 \land E_2 \land \cdots \land E_{n-1}),~ E_i: \text{the i-th chosen edge is not in C} P r ( E 1 ∧ E 2 ∧ ⋯ ∧ E n − 1 ) , E i : the i-th chosen edge is not in C
P r ( E 1 ) = ∣ E ∣ − k ∣ E ∣ ≤ 1 − 2 k n k = 1 − 2 n ∣ E ∣ ≥ n k 2 Notes: P r ( E 2 ∣ E 1 ) ≥ 1 − 2 n − 1 , not P r ( E 2 ) ≥ 1 − 2 n − 1 P r ( E i ∣ E 1 , ⋯  , E i − 1 ) ≥ 1 − 2 n − i + 1 P r ( E 1 ∧ E 2 ∧ ⋯ ∧ E n − 2 ) = P r ( E 1 ) ⋅ P r ( E 2 ∣ E 1 ) ⋅ P r ( E 3 ∣ E 1 , E 2 ) ⋯ ≥ ( 1 − 2 n ) ( 1 − 2 n − 1 ) ⋯ ( 1 − 2 3 ) = ( n − 2 n ) ⋅ ( n − 3 n − 1 ) ⋯ 2 4 ⋅ 1 3 = Θ ( 1 n 2 )
\begin{aligned}
Pr(E_1) ~&= \frac{|E|-k}{|E|} \le 1- \frac{2k}{nk} = 1 - \frac{2}{n} \\
|E| ~&\ge \frac{nk}{2} \\
\textbf{Notes: } Pr(E_2~|~E_1) ~&\ge 1 - \frac{2}{n-1},~ \textbf{not}~Pr(E_2) \ge 1 - \frac{2}{n-1}\\
Pr(E_i~|~E_1,\cdots, E_{i-1}) ~&\ge 1-\frac{2}{n-i+1} \\
Pr(E_1 \land E_2 \land \cdots \land E_{n-2}) ~&= Pr(E_1) \cdot Pr(E_2~|~E_1) \cdot Pr(E_3~|~E_1,E_2) \cdots \\
&\ge (1-\frac{2}{n})(1-\frac{2}{n-1})\cdots(1-\frac{2}{3}) \\
&= (\frac{n-2}{n}) \cdot (\frac{n-3}{n-1})\cdots \frac{2}{4} \cdot \frac{1}{3} \\
&= \Theta(\frac{1}{n^2})\\
\end{aligned}
P r ( E 1 ) ∣ E ∣ Notes: P r ( E 2 ∣ E 1 ) P r ( E i ∣ E 1 , ⋯ , E i − 1 ) P r ( E 1 ∧ E 2 ∧ ⋯ ∧ E n − 2 ) = ∣ E ∣ ∣ E ∣ − k ≤ 1 − n k 2 k = 1 − n 2 ≥ 2 n k ≥ 1 − n − 1 2 , not P r ( E 2 ) ≥ 1 − n − 1 2 ≥ 1 − n − i + 1 2 = P r ( E 1 ) ⋅ P r ( E 2 ∣ E 1 ) ⋅ P r ( E 3 ∣ E 1 , E 2 ) ⋯ ≥ ( 1 − n 2 ) ( 1 − n − 1 2 ) ⋯ ( 1 − 3 2 ) = ( n n − 2 ) ⋅ ( n − 1 n − 3 ) ⋯ 4 2 ⋅ 3 1 = Θ ( n 2 1 )
可得該隨機算法的成功概率爲:P r ( E i ∣ E 1 , ⋯  , E i − 1 ) = Ω ( 1 n 2 ) Pr(E_i~|~E_1,\cdots, E_{i-1})= \Omega(\frac{1}{n^2}) P r ( E i ∣ E 1 , ⋯ , E i − 1 ) = Ω ( n 2 1 )
於是我們可以重複 n 2 n^2 n 2 次,從而使得這個隨機算法成功概率提升至:
1 − ( 1 − 1 n 2 ) n 2 ∼ 1 − 1 e 1-(1-\frac{1}{n^2})^{n^2} \sim 1-\frac{1}{e} 1 − ( 1 − n 2 1 ) n 2 ∼ 1 − e 1
0x05 FastCut
我們把FastCut的過程等效的看作一棵二叉樹,其中每個節點都以一定的概率染成黑色或白色,
每一次生成子節點的過程,是將當前圖的頂點數變爲原來的 c ( c = 2 ) c~(c=\sqrt{2}) c ( c = 2 ) 倍,
父節點到該節點過程中,縮圖未影響最小切時,染爲黑色,反之染爲白色。
那麼顯然地,當且僅當存在一條從根到葉子節點的路徑上,所有的點都爲黑色時,說明當前算法成功的找到了最小切。
而對於一棵深度爲 h h h 的二叉樹隨機二染色,尋找存在從根到葉的黑色路徑,成功概率爲:
ρ h = 1 − ( 1 − ρ h − 1 2 ) 2 = ρ h − 1 2 ( 2 − ρ h − 1 2 ) = ρ h − 1 − ρ h − 1 2 4 \rho_{h} = 1-(1- \frac{\rho_{h-1}}{2})^2 = \frac{\rho_{h-1}}{2}(2-\frac{\rho_{h-1}}{2}) = \rho_{h-1} - \frac{\rho_{h-1}^2}{4} ρ h = 1 − ( 1 − 2 ρ h − 1 ) 2 = 2 ρ h − 1 ( 2 − 2 ρ h − 1 ) = ρ h − 1 − 4 ρ h − 1 2
首先,設 f ( x ) = x − x 2 4 f(x) = x - \frac{x^2}{4} f ( x ) = x − 4 x 2 ,則有 f ′ ( x ) = 1 − x 2 f'(x)= 1 - \frac{x}{2} f ′ ( x ) = 1 − 2 x ,即在 [0,1] 中該函數單調遞增,
通過數學歸納法可證得 ρ h ≥ 1 h + 1 \rho_{h} \ge \frac{1}{h+1} ρ h ≥ h + 1 1 :
ρ 1 = 1 − ( 1 2 ) 2 = 3 4 > 1 1 + 1 ρ h = f ( ρ h − 1 ) ≥ f ( 1 ( h − 1 ) + 1 ) = 1 h − 1 4 h 2 ≥ 1 h + 1 , for h > 1
\begin{aligned}
\rho_{1} &= 1 - (\frac{1}{2})^2 = \frac{3}{4} > \frac{1}{1+1} \\
\rho_{h} &= f(\rho_{h-1}) \ge f(\frac{1}{(h-1)+1}) \\
&= \frac{1}{h} - \frac{1}{4h^2} \\
&\ge \frac{1}{h+1} \textbf{, for } h > 1 \\
\end{aligned}
ρ 1 ρ h = 1 − ( 2 1 ) 2 = 4 3 > 1 + 1 1 = f ( ρ h − 1 ) ≥ f ( ( h − 1 ) + 1 1 ) = h 1 − 4 h 2 1 ≥ h + 1 1 , for h > 1
於是有:
P r ( h ) = 1 h + 1 ≥ 1 3 + log 2 n = Ω ( 1 log n ) Pr(h) = \frac{1}{h+1} \ge \frac{1}{3+\log_{\sqrt{2}}n} = \Omega(\frac{1}{\log n}) P r ( h ) = h + 1 1 ≥ 3 + log 2 n 1 = Ω ( log n 1 )
0xFF Prove in detail
Prove the expected running time for 0x02
設 T ( n ) T(n) T ( n ) 表示快排 n n n 個亂序數的執行時間,快排的每一步分爲三個部分,
當第 m m m 次取的 p i v o t pivot p i v o t 爲第 k m k_{m} k m 大,以第一次爲例,當第一次取的 p i v o t pivot p i v o t 爲 k 1 = k k_1 = k k 1 = k 時:
將其它數分別與 p i v o t pivot p i v o t 比較大小: 共 n − 1 n-1 n − 1 次操作
將比 p i v o t pivot p i v o t 大的數都放到 p i v o t pivot p i v o t 左邊: 共 k − 1 k-1 k − 1 次操作
將比 p i v o t pivot p i v o t 小的數都放到 p i v o t pivot p i v o t 右邊: 共 n − k n-k n − k 次操作
則有:
E ( T ( n ) ) = 1 n ∑ k = 1 n ( E ( T ( k − 1 ) ) + E ( T ( n − k ) ) + ( n − 1 ) ∣ p i v o t = k ) E(T(n)) = \frac{1}{n} \sum_{k=1}^{n} \left( E(T(k-1)) + E(T(n-k)) + (n-1)~|~pivot = k \right) E ( T ( n ) ) = n 1 k = 1 ∑ n ( E ( T ( k − 1 ) ) + E ( T ( n − k ) ) + ( n − 1 ) ∣ p i v o t = k )
由於選取 p i v o t = k pivot=k p i v o t = k 與 T ( k ) T(k) T ( k ) 間具有獨立性,令 f ( n ) : = E ( T ( n ) ) f(n) := E(T(n)) f ( n ) : = E ( T ( n ) ) ,則有:
f ( n ) = 1 n ∑ k 1 = 1 n ( f ( k 1 − 1 ) + f ( n − k 1 ) + ( n − 1 ) ) = 1 n ∑ k 1 = 1 n ( 1 k 1 ∑ k 2 = 1 k 1 − 1 ( f ( k 2 − 1 ) + f ( k 1 − k 2 ) + ( k 1 − 1 ) ) ) + 1 n ∑ k 1 = 1 n ( 1 n − k 1 ∑ k 3 = k 1 n ( f ( k 3 − k 1 ) + f ( n − k 3 ) + ( n − k 1 − 1 ) ) ) + 1 n ∑ k 1 = 1 n ( n − 1 ) = 1 n ∑ k 1 = 1 n ( 1 k 1 ∑ k 2 = 1 k 1 − 1 ( f ( k 2 − 1 ) + f ( k 1 − k 2 ) ) + 1 n − k 1 ∑ k 3 = k 1 n ( f ( k 3 − k 1 ) + f ( n − k 3 ) ) ) + 1 n ∑ k 1 = 1 n ( 1 k 1 ∑ k 2 = 1 k 1 − 1 ( k 1 − 1 ) + 1 n − k 1 ∑ k 3 = k 1 n ( n − k 1 − 1 ) + ( n − 1 ) )
\begin{aligned}
f(n) &= \frac{1}{n} \sum_{k_1=1}^{n} \left( f(k_1-1) + f(n-k_1) + (n-1) \right) \\
&= \frac{1}{n} \sum_{k_1=1}^{n} \left( \frac{1}{k_1}\sum_{k_2=1}^{k_1-1}(f(k_2-1) + f(k_1-k_2) + (k_1-1)) \right) \\
&+ \frac{1}{n} \sum_{k_1=1}^{n} \left( \frac{1}{n-k_1}\sum_{k_3=k_1}^{n}(f(k_3-k_1) + f(n-k_3) + (n-k_1-1)) \right)
+ \frac{1}{n} \sum_{k_1=1}^{n} \left( n-1 \right) \\
&= \frac{1}{n} \sum_{k_1=1}^{n} \left( \frac{1}{k_1}\sum_{k_2=1}^{k_1-1}(f(k_2-1) + f(k_1-k_2))
+ \frac{1}{n-k_1}\sum_{k_3=k_1}^{n}(f(k_3-k_1) + f(n-k_3)) \right) \\
&+ \frac{1}{n} \sum_{k_1=1}^{n} \left( \frac{1}{k_1}\sum_{k_2=1}^{k_1-1}(k_1-1)
+ \frac{1}{n-k_1}\sum_{k_3=k_1}^{n}(n-k_1-1) + (n-1) \right)
\end{aligned}
f ( n ) = n 1 k 1 = 1 ∑ n ( f ( k 1 − 1 ) + f ( n − k 1 ) + ( n − 1 ) ) = n 1 k 1 = 1 ∑ n ( k 1 1 k 2 = 1 ∑ k 1 − 1 ( f ( k 2 − 1 ) + f ( k 1 − k 2 ) + ( k 1 − 1 ) ) ) + n 1 k 1 = 1 ∑ n ( n − k 1 1 k 3 = k 1 ∑ n ( f ( k 3 − k 1 ) + f ( n − k 3 ) + ( n − k 1 − 1 ) ) ) + n 1 k 1 = 1 ∑ n ( n − 1 ) = n 1 k 1 = 1 ∑ n ( k 1 1 k 2 = 1 ∑ k 1 − 1 ( f ( k 2 − 1 ) + f ( k 1 − k 2 ) ) + n − k 1 1 k 3 = k 1 ∑ n ( f ( k 3 − k 1 ) + f ( n − k 3 ) ) ) + n 1 k 1 = 1 ∑ n ( k 1 1 k 2 = 1 ∑ k 1 − 1 ( k 1 − 1 ) + n − k 1 1 k 3 = k 1 ∑ n ( n − k 1 − 1 ) + ( n − 1 ) )
其中:
1 n ∑ k 1 = 1 n ( 1 k 1 ∑ k 2 = 1 k 1 − 1 ( k 1 − 1 ) + 1 n − k 1 ∑ k 3 = k 1 n ( n − k 1 − 1 ) + ( n − 1 ) ) < 1 n ∑ k 1 = 1 n ( 1 k 1 ∑ k 2 = 1 k 1 − 1 k 1 + 1 n − k 1 ∑ k 3 = k 1 n ( n − k 1 ) + n ) = 1 n ∑ k 1 = 1 n ( k 1 + n − k 1 + n ) = 1 n ∑ k 1 = 1 n ( 2 n ) = 2 n
\begin{aligned}
& \frac{1}{n} \sum_{k_1=1}^{n} \left( \frac{1}{k_1}\sum_{k_2=1}^{k_1-1}(k_1-1)
+ \frac{1}{n-k_1}\sum_{k_3=k_1}^{n}(n-k_1-1) + (n-1) \right) \\
<& \frac{1}{n} \sum_{k_1=1}^{n} \left( \frac{1}{k_1}\sum_{k_2=1}^{k_1-1}k_1
+ \frac{1}{n-k_1}\sum_{k_3=k_1}^{n}(n-k_1) + n \right) \\
=& \frac{1}{n} \sum_{k_1=1}^{n} \left( k_1 + n - k_1 + n \right)
= \frac{1}{n} \sum_{k_1=1}^{n} \left( 2n \right) = 2n
\end{aligned}
< = n 1 k 1 = 1 ∑ n ( k 1 1 k 2 = 1 ∑ k 1 − 1 ( k 1 − 1 ) + n − k 1 1 k 3 = k 1 ∑ n ( n − k 1 − 1 ) + ( n − 1 ) ) n 1 k 1 = 1 ∑ n ( k 1 1 k 2 = 1 ∑ k 1 − 1 k 1 + n − k 1 1 k 3 = k 1 ∑ n ( n − k 1 ) + n ) n 1 k 1 = 1 ∑ n ( k 1 + n − k 1 + n ) = n 1 k 1 = 1 ∑ n ( 2 n ) = 2 n
則有:
第 1 1 1 輪取 p i v o t pivot p i v o t 時 (m ∈ { 1 } m \in \{1\} m ∈ { 1 } ):
f ( n ) < 1 n ∑ k 1 = 1 n ( f ( k 1 − 1 ) + f ( n − k 1 ) + n ) f(n) < \frac{1}{n} \sum_{k_1=1}^{n} \left( f(k_1-1) + f(n-k_1) + n \right) f ( n ) < n 1 k 1 = 1 ∑ n ( f ( k 1 − 1 ) + f ( n − k 1 ) + n )
第 2 2 2 輪取 p i v o t pivot p i v o t 時 (m ∈ { 2 , 3 } m \in \{2,3\} m ∈ { 2 , 3 } ):
f ( n ) < 1 n ∑ k 1 = 1 n ( 1 k 1 ∑ k 2 = 1 k 1 − 1 ( f ( k 2 − 1 ) + f ( k 1 − k 2 ) ) + 1 n − k 1 ∑ k 3 = k 1 n ( f ( k 3 − k 1 ) + f ( n − k 3 ) ) ) + 2 n f(n) < \frac{1}{n} \sum_{k_1=1}^{n} \left( \frac{1}{k_1}\sum_{k_2=1}^{k_1-1}(f(k_2-1) + f(k_1-k_2))
+ \frac{1}{n-k_1}\sum_{k_3=k_1}^{n}(f(k_3-k_1) + f(n-k_3)) \right) + 2n f ( n ) < n 1 k 1 = 1 ∑ n ( k 1 1 k 2 = 1 ∑ k 1 − 1 ( f ( k 2 − 1 ) + f ( k 1 − k 2 ) ) + n − k 1 1 k 3 = k 1 ∑ n ( f ( k 3 − k 1 ) + f ( n − k 3 ) ) ) + 2 n
第 e p ep e p 輪取 p i v o t pivot p i v o t 時 (m ∈ { m ∣ 2 e p − 1 ≤ m < 2 e p } m \in \{m | 2^{ep-1} \le m < 2^{ep}\} m ∈ { m ∣ 2 e p − 1 ≤ m < 2 e p } ):
f ( n ) < 1 n ∑ k 1 = 1 n ( 1 k 1 ∑ k 2 = 1 k 1 − 1 ( 1 k 2 ∑ k 4 = 1 k 2 − 1 ( ⋯ 1 k 2 e p − 1 ∑ k 2 e p = 1 2 e p − 1 − 1 ( f ( k 2 e p − 1 − 1 ) + f ( k 2 e p − 2 − k 2 e p − 1 ) ) + ⋯  ) ) ) + n ⋅ e p f(n) < \frac{1}{n} \sum_{k_1=1}^{n} \left(
\frac{1}{k_1}\sum_{k_2=1}^{k_1-1} \left(
\frac{1}{k_2}\sum_{k_4=1}^{k_2-1} \left(
\cdots \frac{1}{k_{2^{ep-1}}}\sum_{k_{2^{ep}}=1}^{2^{ep-1}-1}(f(k_{2^{ep-1}} - 1) + f(k_{2^{ep-2}} - k_{2^{ep-1}})) + \cdots
\right)\right)\right)
+ n \cdot ep f ( n ) < n 1 k 1 = 1 ∑ n ⎝ ⎛ k 1 1 k 2 = 1 ∑ k 1 − 1 ⎝ ⎛ k 2 1 k 4 = 1 ∑ k 2 − 1 ⎝ ⎛ ⋯ k 2 e p − 1 1 k 2 e p = 1 ∑ 2 e p − 1 − 1 ( f ( k 2 e p − 1 − 1 ) + f ( k 2 e p − 2 − k 2 e p − 1 ) ) + ⋯ ⎠ ⎞ ⎠ ⎞ ⎠ ⎞ + n ⋅ e p
顯然地,$ f(1)=1 $,在有限輪(不超過 n n n )之後,所有的 f ( ⋅ ) = f ( 1 ) = 1 f(\cdot)=f(1)=1 f ( ⋅ ) = f ( 1 ) = 1 ,
對於每一層的 ∑ i j − 1 \sum_{i}^{j-1} ∑ i j − 1 求和,其外側的 1 j − i \frac{1}{j-i} j − i 1 剛好與之抵消,此時:
f ( n ) < 1 n ∑ k 1 = 1 n ( 1 k 1 ∑ k 2 = 1 k 1 − 1 ( 1 k 2 ∑ k 4 = 1 k 2 − 1 ( ⋯ 1 k 2 p 1 − 1 ∑ k 2 p 1 = 1 2 p 1 − 1 − 1 f ( 1 ) + ⋯  ) ) ) + n ⋅ max ( p i ) = ∑ n 1 + n ⋅ max ( p i )
\begin{aligned}
f(n) ~&< \frac{1}{n} \sum_{k_1=1}^{n} \left(
\frac{1}{k_1}\sum_{k_2=1}^{k_1-1} \left(
\frac{1}{k_2}\sum_{k_4=1}^{k_2-1} \left(
\cdots \frac{1}{k_{2^{p_1-1}}}\sum_{k_{2^{p_1}}=1}^{2^{p_1-1}-1}f(1) + \cdots
\right)\right)\right)
+ n \cdot \max(p_i) \\
&= \sum^{n}1 + n \cdot \max(p_i)
\end{aligned}
f ( n ) < n 1 k 1 = 1 ∑ n ⎝ ⎛ k 1 1 k 2 = 1 ∑ k 1 − 1 ⎝ ⎛ k 2 1 k 4 = 1 ∑ k 2 − 1 ⎝ ⎛ ⋯ k 2 p 1 − 1 1 k 2 p 1 = 1 ∑ 2 p 1 − 1 − 1 f ( 1 ) + ⋯ ⎠ ⎞ ⎠ ⎞ ⎠ ⎞ + n ⋅ max ( p i ) = ∑ n 1 + n ⋅ max ( p i )
其中 p i p_i p i 指的是 對於第 i i i 大的數,取到其作爲 pivot 是在第 p i p_i p i 輪時,
並且當且僅當每次選取pivot都恰好取到當前數列的中位數時,構成層數最少的滿二叉樹,從而有:
log 2 n ≤ p i ≤ n \log_2 n \le p_i \le n log 2 n ≤ p i ≤ n
所以:
f ( n ) < n + n log 2 n f(n) < n + n\log_2 n f ( n ) < n + n log 2 n
E ( T ( n ) ) = O ( n + n log 2 n ) = O ( n log 2 n ) E(T(n)) = O(n + n\log_2 n) = O(n\log_2 n) E ( T ( n ) ) = O ( n + n log 2 n ) = O ( n log 2 n )
Expansion problem for 0x04
Suppose that at each step of our min-cut algorithm, instead of choosing a random edge for contraction we choose two vertices at random and coalesce them into a single vertex. Show that there are inputs on which the probability that this modified algorithm finds a min-cut is exponentially small.
設在我們的最小切算法的每一步中,不是隨機地選擇一條邊做收縮,而是隨機地選擇兩個頂點並將他們結合成一個頂點。說明存在輸入,使得這個改變後的算法找到最小切的概率是指數級小的。
令 k ′ k' k ′ 是最小切的規模:最小切的效果是將所有頂點分爲兩側,即如果被最小切分在不同側的兩個頂點被結合,則本次找不到最小切,反之當最終結合成左右各剩下一個點時,這兩個點之間即爲最小切。
於是假設最小切將所有頂點分成了 k k k 個點和 n − k n-k n − k 個點的兩側,由於兩側的等效性,不妨設 k > = n − k k >= n-k k > = n − k ,稱較多點 ( k k k 個點所在的一側) 稱作 A A A 側,另一側爲 B B B 側,則有:
1 ≤ n − k ≤ n 2 ≤ k < n , k < = k ′ 1 \le n-k \le \frac{n}{2} \le k < n, \quad k <= k' 1 ≤ n − k ≤ 2 n ≤ k < n , k < = k ′
定義 S i S_i S i 表示在第 i i i 次操作中沒有選中了影響最小切的點對,則第一次"縮點"操作中,選中影響最小切的點對的概率爲:
p ( ¬ S 1 ) = k ⋅ ( n − k ) C n 2 p(\lnot S_1) = \frac{k \cdot (n-k)}{C_{n}^{2}} p ( ¬ S 1 ) = C n 2 k ⋅ ( n − k )
則有:
p ( S 1 ) = 1 − k ⋅ ( n − k ) C n 2 = C n 2 − k ⋅ ( n − k ) C n 2 = C k 2 + C n − k 2 C n 2 < C n / 2 2 + C n / 2 2 C n 2 = 2 ⋅ n 2 4 ⋅ n 2 < 1 2
\begin{aligned}
p(S_1) &= 1 - \frac{k \cdot (n-k)}{C_{n}^{2}} = \frac{C_n^2 - k \cdot (n-k)}{C_n^2} = \frac{C_{k}^{2} + C_{n-k}^{2}}{C_{n}^{2}} \\
&< \frac{C_{n/2}^{2} + C_{n/2}^{2}}{C_{n}^{2}} = \frac{2 \cdot n^2}{4 \cdot n^2} < \frac{1}{2}
\end{aligned}
p ( S 1 ) = 1 − C n 2 k ⋅ ( n − k ) = C n 2 C n 2 − k ⋅ ( n − k ) = C n 2 C k 2 + C n − k 2 < C n 2 C n / 2 2 + C n / 2 2 = 4 ⋅ n 2 2 ⋅ n 2 < 2 1
第二次"縮點"操作中,選中影響最小切的點對的概率,會受前一次結合的頂點處於 A A A 側或 B B B 側的影響:
p ( S 2 ∣ S 1 ) = p ( S 2 ∣ S 1 , A ) ⋅ p ( S 1 , A ) + p ( S 2 ∣ S 1 , ¬ A ) ⋅ p ( S 1 , ¬ A ) = ( k − 1 ) ⋅ ( n − k ) C n − 1 2 ⋅ C k 2 C n 2 + k ⋅ ( n − k − 1 ) C n − 1 2 ⋅ C n − k 2 C n 2 = k ⋅ ( n − k ) ⋅ ( C k 2 + C n − k 2 ) − C n − k 1 ⋅ C k 2 − C k 1 ⋅ C n − k 2 C n − 1 2 ⋅ C n 2 = k ⋅ ( n − k ) ⋅ ( C k 2 + C n − k 2 ) − k ⋅ ( n − k ) ⋅ 1 2 ( ( k − 1 ) + ( n − k − 1 ) ) C n − 1 2 ⋅ C n 2 = k ⋅ ( n − k ) ⋅ ( C k 2 + C n − k 2 − n / 2 − 1 ) C n − 1 2 ⋅ C n 2 = k ⋅ ( n − k ) ⋅ ( C n 2 − k ⋅ ( n − k ) − n / 2 − 1 ) C n − 1 2 ⋅ C n 2
\begin{aligned}
p(S_2|S_1) &= p(S_2|S_1, A) \cdot p(S_1, A) + p(S_2|S_1,\lnot A) \cdot p(S_1, \lnot A)\\
&= \frac{(k-1) \cdot (n-k)}{C_{n-1}^{2}} \cdot \frac{C_{k}^{2}}{C_{n}^{2}}
+ \frac{k \cdot (n-k-1)}{C_{n-1}^{2}} \cdot \frac{C_{n-k}^{2}}{C_{n}^{2}} \\
&= \frac{k \cdot (n-k) \cdot (C_k^2 + C_{n-k}^2) - C_{n-k}^{1} \cdot C_k^2 - C_{k}^{1} \cdot C_{n-k}^2 }{C_{n-1}^2 \cdot C_n^2} \\
&= \frac{k \cdot (n-k) \cdot (C_k^2 + C_{n-k}^2) - k \cdot (n-k) \cdot \frac{1}{2} ((k-1)+(n-k-1))}{C_{n-1}^2 \cdot C_n^2} \\
&= \frac{k \cdot (n-k) \cdot (C_k^2 + C_{n-k}^2 - n/2 - 1)}{C_{n-1}^2 \cdot C_n^2} \\
&= \frac{k \cdot (n-k) \cdot (C_n^2 - k \cdot (n-k) - n/2 - 1)}{C_{n-1}^2 \cdot C_n^2}
\end{aligned}
p ( S 2 ∣ S 1 ) = p ( S 2 ∣ S 1 , A ) ⋅ p ( S 1 , A ) + p ( S 2 ∣ S 1 , ¬ A ) ⋅ p ( S 1 , ¬ A ) = C n − 1 2 ( k − 1 ) ⋅ ( n − k ) ⋅ C n 2 C k 2 + C n − 1 2 k ⋅ ( n − k − 1 ) ⋅ C n 2 C n − k 2 = C n − 1 2 ⋅ C n 2 k ⋅ ( n − k ) ⋅ ( C k 2 + C n − k 2 ) − C n − k 1 ⋅ C k 2 − C k 1 ⋅ C n − k 2 = C n − 1 2 ⋅ C n 2 k ⋅ ( n − k ) ⋅ ( C k 2 + C n − k 2 ) − k ⋅ ( n − k ) ⋅ 2 1 ( ( k − 1 ) + ( n − k − 1 ) ) = C n − 1 2 ⋅ C n 2 k ⋅ ( n − k ) ⋅ ( C k 2 + C n − k 2 − n / 2 − 1 ) = C n − 1 2 ⋅ C n 2 k ⋅ ( n − k ) ⋅ ( C n 2 − k ⋅ ( n − k ) − n / 2 − 1 )
又因爲:
1 ≤ n − k ≤ n 2 ≤ k < n 1 \le n-k \le \frac{n}{2} \le k < n 1 ≤ n − k ≤ 2 n ≤ k < n
C i 2 = C i − 1 2 ⋅ i − 2 i = C i − j 2 ⋅ ∏ k = 1 j i − k − 1 i − k + 1 = C i − j 2 ⋅ i − j − 1 i C_i^2 = C_{i-1}^2 \cdot \frac{i-2}{i} = C_{i-j}^2 \cdot \prod_{k=1}^{j}\frac{i-k-1}{i-k+1} = C_{i-j}^2 \cdot \frac{i-j-1}{i} C i 2 = C i − 1 2 ⋅ i i − 2 = C i − j 2 ⋅ k = 1 ∏ j i − k + 1 i − k − 1 = C i − j 2 ⋅ i i − j − 1
上式可得:
p ( S 2 ∣ S 1 ) < n 2 ⋅ n 2 ⋅ ( C n / 2 2 − n 2 ⋅ n 2 − n 2 ) C n 2 ⋅ n − 2 n ⋅ C n 2 = n 2 4 ⋅ ( n 2 2 − n 2 4 − n 2 ) C n 2 ⋅ n − 2 n ⋅ C n 2 = n 4 16 − n 3 8 n − 2 n ⋅ n 4 4 = ( n − 2 ) ⋅ n 3 4 ( n − 2 ) ⋅ n 3 = 1 4
\begin{aligned}
p(S_2|S_1) &< \frac{\frac{n}{2} \cdot \frac{n}{2} \cdot (C_{n/2}^2 - \frac{n}{2} \cdot \frac{n}{2} - \frac{n}{2})}{C_{n}^2 \cdot \frac{n-2}{n} \cdot C_n^2} \\
&= \frac{\frac{n^2}{4} \cdot (\frac{n^2}{2} - \frac{n^2}{4} - \frac{n}{2})}{C_{n}^2 \cdot \frac{n-2}{n} \cdot C_n^2} \\
&= \frac{\frac{n^4}{16} - \frac{n^3}{8}}{\frac{n-2}{n} \cdot \frac{n^4}{4}}\\
&= \frac{(n-2) \cdot n^3}{4(n-2) \cdot n^3} \\
&= \frac{1}{4}
\end{aligned}
p ( S 2 ∣ S 1 ) < C n 2 ⋅ n n − 2 ⋅ C n 2 2 n ⋅ 2 n ⋅ ( C n / 2 2 − 2 n ⋅ 2 n − 2 n ) = C n 2 ⋅ n n − 2 ⋅ C n 2 4 n 2 ⋅ ( 2 n 2 − 4 n 2 − 2 n ) = n n − 2 ⋅ 4 n 4 1 6 n 4 − 8 n 3 = 4 ( n − 2 ) ⋅ n 3 ( n − 2 ) ⋅ n 3 = 4 1
第 m m m 次"縮點"操作中,未選中影響最小切的點對的概率:
p ( S m ∣ ⋂ i < m S i ) < ∑ j = 1 m ( ( ∏ a = k − j k C a 2 ) ⋅ ( ∏ b = n − k − j n − k C b 2 ) ) ) ( C n 2 ) m ⋅ ∏ c = 1 m C n − c 2 C n 2 < ∑ j = 1 m ( ( C n / 2 2 ) j ⋅ ∏ a = 1 j C n / 2 − a 2 C n / 2 2 + ( C n / 2 2 ) m − j ⋅ ∏ b = 1 m − j C n / 2 − b 2 C n / 2 2 ) n 2 m 2 m ⋅ ∏ c = 1 m C n − c 2 C n 2 = O ( n m + 1 ) 2 m ⋅ O ( n m + 1 ) = 1 2 m
\begin{aligned}
p(S_m|\bigcap_{i<m} S_i) &< \frac{\sum_{j=1}^{m} ( (\prod_{a=k-j}^{k} C_{a}^2) \cdot (\prod_{b=n-k-j}^{n-k} C_{b}^2)))}{(C_{n}^2)^{m} \cdot \prod_{c=1}^{m}\frac{C_{n-c}^2}{C_{n}^2}}\\
&< \frac{\sum_{j=1}^{m} \left(
(C_{n/2}^2)^j \cdot \prod_{a=1}^{j}\frac{C_{n/2-a}^2}{C_{n/2}^2} + (C_{n/2}^2)^{m-j} \cdot \prod_{b=1}^{m-j}\frac{C_{n/2-b}^2}{C_{n/2}^2} \right)
}{\frac{n^{2m}}{2^m} \cdot \prod_{c=1}^{m} \frac{C_{n-c}^2}{C_{n}^2}} \\
&= \frac{O(n^{m+1})}{2^{m} \cdot O(n^{m+1})}\\
&= \frac{1}{2^{m}}
\end{aligned}
p ( S m ∣ i < m ⋂ S i ) < ( C n 2 ) m ⋅ ∏ c = 1 m C n 2 C n − c 2 ∑ j = 1 m ( ( ∏ a = k − j k C a 2 ) ⋅ ( ∏ b = n − k − j n − k C b 2 ) ) ) < 2 m n 2 m ⋅ ∏ c = 1 m C n 2 C n − c 2 ∑ j = 1 m ( ( C n / 2 2 ) j ⋅ ∏ a = 1 j C n / 2 2 C n / 2 − a 2 + ( C n / 2 2 ) m − j ⋅ ∏ b = 1 m − j C n / 2 2 C n / 2 − b 2 ) = 2 m ⋅ O ( n m + 1 ) O ( n m + 1 ) = 2 m 1
特別地,第 n − 2 n-2 n − 2 次縮點後,最終剩下兩個頂點,此時剩下的兩點之間即爲所求,若每次縮點都沒有破壞最小切,則此時的兩個頂點間即爲最小切,概率爲 O ( 2 − ( n − 2 ) ) O(2^{-(n-2)}) O ( 2 − ( n − 2 ) )
Expansion problem for 0x05
Consider running the contraction algorithm until the number of vertices is reduced to t t t and then using a cubic-time algorithm to find the min-cut in the contracted graph. Show that this process as many times as necessary to ensure a probability of success at least 1/2 leads to an algorithm with running time Ω ( n 8 / 3 ) \Omega (n^{8/3}) Ω ( n 8 / 3 )
考慮執行收縮算法直到頂點數目被降低到 t t t ,然後用立方時間的算法在被收縮後的圖中尋找最小切。證明執行上述過程足夠多次以保證至少 1/2 的成功概率,算法需要運行時間 Ω ( n 8 / 3 ) \Omega (n^{8/3}) Ω ( n 8 / 3 )
對於一次成功的收縮,頂點數被降低到 t t t 時,每條選中的邊都未選中涉及最小切的邊,此時的成功概率爲:
p ( n ) = ∏ i = t n ( 1 − 2 i ) = ( t − 1 ) ( t − 2 ) n ⋅ ( n − 1 ) p(n) = \prod_{i=t}^{n}(1-\frac{2}{i}) = \frac{(t-1)(t-2)}{n \cdot (n-1)} p ( n ) = i = t ∏ n ( 1 − i 2 ) = n ⋅ ( n − 1 ) ( t − 1 ) ( t − 2 )
重複運行 k k k 次的成功概率爲:
P ( n , k ) = 1 − ( 1 − p ( n ) ) k = 1 − ( 1 − t 2 n 2 ) k P(n, k) = 1 - (1 - p(n))^k = 1 - ( 1 - \frac{t^2}{n^2})^k P ( n , k ) = 1 − ( 1 − p ( n ) ) k = 1 − ( 1 − n 2 t 2 ) k
取 k = ln 2 ⋅ n 2 t 2 k = \ln 2 \cdot \frac{n^2}{t^2} k = ln 2 ⋅ t 2 n 2 ,此時:
P ( n , ln 2 ⋅ n 2 t 2 ) = 1 − ( 1 − t 2 n 2 ) ln 2 ⋅ n 2 t 2 = 1 − ( ( 1 − t 2 n 2 ) n 2 t 2 ) ln 2 ) ≥ 1 − ( 1 e ) ln 2 = 1 2
\begin{aligned}
P(n, \ln 2 \cdot \frac{n^2}{t^2}) &= 1 - ( 1 - \frac{t^2}{n^2})^{\ln 2 \cdot \frac{n^2}{t^2}} \\
&= 1 - ((1 - \frac{t^2}{n^2})^{\frac{n^2}{t^2}})^{\ln 2}) \\
&\ge 1 - (\frac{1}{e})^{\ln 2} \\
&= \frac{1}{2}
\end{aligned}
P ( n , ln 2 ⋅ t 2 n 2 ) = 1 − ( 1 − n 2 t 2 ) ln 2 ⋅ t 2 n 2 = 1 − ( ( 1 − n 2 t 2 ) t 2 n 2 ) ln 2 ) ≥ 1 − ( e 1 ) ln 2 = 2 1
單次運行的時間複雜度爲:
T ( n ) = C 1 ⋅ ( n + t + 1 ) ⋅ ( n − t ) 2 + C 2 ⋅ t 3 = C 1 2 ( n 2 + n − t 2 − t ) + C 2 t 3 = C 1 2 ( n 2 + n ) − C 1 t 2 2 − C 1 t 2 + C 2 t 3 k ⋅ T ( n ) = ln 2 t 2 ⋅ n 2 ⋅ ( C 1 2 ( n 2 + n ) − C 1 t 2 2 − C 1 t 2 + C 2 t 3 ) ∵ n 2 + n > n 2 > n 2 3 ∴ k ⋅ T ( n ) > ln 2 t 2 ⋅ n 2 ⋅ ( C 1 2 n 2 3 − C 1 t 2 2 − C 1 t 2 + C 2 t 3 ) = Ω ( n 2 ⋅ n 2 3 ) = Ω ( n 8 3 )
\begin{aligned}
T(n) &= \frac{C_1 \cdot (n+t+1) \cdot (n-t)}{2} + C_2 \cdot t^3 \\
&= \frac{C_1}{2} (n^2 + n - t^2 - t) + C_2t^3 \\
&= \frac{C_1}{2} (n^2 + n) - \frac{C_1t^2}{2} - \frac{C_1t}{2} + C_2t^3 \\
\\
k \cdot T(n) &= \frac{\ln 2}{t^2} \cdot n^2 \cdot (\frac{C_1}{2} (n^2 + n) - \frac{C_1t^2}{2} - \frac{C_1t}{2} + C_2t^3) \\
\because ~ n^2 + n ~ &> n^2 > n^{\frac{2}{3}} \\
\therefore k \cdot T(n) &> \frac{\ln 2}{t^2} \cdot n^2 \cdot (\frac{C_1}{2} n^{\frac{2}{3}} - \frac{C_1t^2}{2} - \frac{C_1t}{2} + C_2t^3) \\
&= \Omega (n^2 \cdot n^{\frac{2}{3}}) = \Omega (n^{\frac{8}{3}})
\end{aligned}
T ( n ) k ⋅ T ( n ) ∵ n 2 + n ∴ k ⋅ T ( n ) = 2 C 1 ⋅ ( n + t + 1 ) ⋅ ( n − t ) + C 2 ⋅ t 3 = 2 C 1 ( n 2 + n − t 2 − t ) + C 2 t 3 = 2 C 1 ( n 2 + n ) − 2 C 1 t 2 − 2 C 1 t + C 2 t 3 = t 2 ln 2 ⋅ n 2 ⋅ ( 2 C 1 ( n 2 + n ) − 2 C 1 t 2 − 2 C 1 t + C 2 t 3 ) > n 2 > n 3 2 > t 2 ln 2 ⋅ n 2 ⋅ ( 2 C 1 n 3 2 − 2 C 1 t 2 − 2 C 1 t + C 2 t 3 ) = Ω ( n 2 ⋅ n 3 2 ) = Ω ( n 3 8 )
Complexity Class
0x01 Las Vegas VS. Monte Carlo
Las Vegas algorithm
拉斯維加斯算法
具有隨機的執行時間
一旦計算出來,得到的必然是正確結果
舉例:快排 Quick Sort
Monte Carlo algorithm
蒙特卡羅算法
具有隨機的結果質量
對於決策問題而言,可能出現單邊或雙邊錯誤
有一定概率獲得最優答案,通過多次重複可以獲得一個可接受的獲得最優解的概率
舉例:隨機最小割算法 randomized Min-cut algorithm
0x02 Denition
We only consider decision problem in this class.
Language
A language L ⊆ Σ ∗ L \subseteq \Sigma^* L ⊆ Σ ∗ is any collection of strings over Σ \Sigma Σ .
Usually Σ = { 0 , 1 } \Sigma = \{0,1\} Σ = { 0 , 1 } , and Σ ∗ \Sigma^* Σ ∗ is the set of all possible strings over this alphabet.
P
L ∈ P ⇔ ∃ polynomial time algorithm A s . t . L \in P \Leftrightarrow \exists ~ \text{polynomial time algorithm } A ~s.t. L ∈ P ⇔ ∃ polynomial time algorithm A s . t .
∀ x ∈ Σ ∗ , { x ∈ L ⇒ A ( x ) accepts x ∉ L ⇒ A ( x ) rejects \forall x \in \Sigma^*,
\begin{cases}
x \in L \Rightarrow A(x) \text{ accepts}\\
x \notin L \Rightarrow A(x) \text{ rejects}
\end{cases} ∀ x ∈ Σ ∗ , { x ∈ L ⇒ A ( x ) accepts x ∈ / L ⇒ A ( x ) rejects
NP
L ∈ N P ⇔ ∃ polynomial time algorithm A s . t . L \in NP \Leftrightarrow \exists ~ \text{polynomial time algorithm } A ~s.t. L ∈ N P ⇔ ∃ polynomial time algorithm A s . t .
∀ x ∈ Σ ∗ , { x ∈ L ⇒ ∃ y , ∣ y ∣ = p o l y ( ∣ x ∣ ) , A ( x , y ) accepts x ∉ L ⇒ ∀ y , ∣ y ∣ = p o l y ( ∣ x ∣ ) , A ( x , y ) rejects \forall x \in \Sigma^*,
\begin{cases}
x \in L \Rightarrow \exists y,|y|=poly(|x|), A(x,y) \text{ accepts}\\
x \notin L \Rightarrow \forall y,|y|=poly(|x|), A(x,y) \text{ rejects}
\end{cases} ∀ x ∈ Σ ∗ , { x ∈ L ⇒ ∃ y , ∣ y ∣ = p o l y ( ∣ x ∣ ) , A ( x , y ) accepts x ∈ / L ⇒ ∀ y , ∣ y ∣ = p o l y ( ∣ x ∣ ) , A ( x , y ) rejects
co- N P = { L ‾ ∣ L ∈ N P } \text{co-}NP=\{\overline L ~|~ L \in NP \} co- N P = { L ∣ L ∈ N P }
NP-hard problem (informal denition): A is NP-hard
⇔ \Leftrightarrow ⇔ if A is polynomial time solvable, all problems in NP are polynomial time solvable.
NP-complete problem: if A is NP-hard and A 2NP.
Famous NP-complete problems
3-SAT
Vertex cover, Set cover
Clique, Independent set
Hamilton cycle, Traveling salesman problem
Integer programming
RP and co-RP
RP here is for R andomized P olynomial time
L ∈ R P ⇔ ∃ randomized algorithm A running in worst-case polynomial time s . t . L \in RP \Leftrightarrow \exists ~ \text{randomized algorithm } A \text{ running in worst-case polynomial time} ~s.t. L ∈ R P ⇔ ∃ randomized algorithm A running in worst-case polynomial time s . t .
∀ x ∈ Σ ∗ , { x ∈ L ⇒ P r ( A ( x ) accepts ) ≥ 1 / 2 x ∉ L ⇒ P r ( A ( x ) accepts ) = 0 \forall x \in \Sigma^*,
\begin{cases}
x \in L \Rightarrow Pr\left( A(x) \text{ accepts} \right) \ge 1/2\\
x \notin L \Rightarrow Pr\left( A(x) \text{ accepts} \right) = 0
\end{cases} ∀ x ∈ Σ ∗ , { x ∈ L ⇒ P r ( A ( x ) accepts ) ≥ 1 / 2 x ∈ / L ⇒ P r ( A ( x ) accepts ) = 0
L ∈ co- R P ⇔ ∃ randomized algorithm A running in worst-case polynomial time s . t . L \in \text{co-}RP \Leftrightarrow \exists ~ \text{randomized algorithm } A \text{ running in worst-case polynomial time} ~s.t. L ∈ co- R P ⇔ ∃ randomized algorithm A running in worst-case polynomial time s . t .
∀ x ∈ Σ ∗ , { x ∈ L ⇒ P r ( A ( x ) accepts ) = 1 x ∉ L ⇒ P r ( A ( x ) accepts ) ≤ 1 / 2 \forall x \in \Sigma^*,
\begin{cases}
x \in L \Rightarrow Pr\left( A(x) \text{ accepts} \right) = 1\\
x \notin L \Rightarrow Pr\left( A(x) \text{ accepts} \right) \le 1/2
\end{cases} ∀ x ∈ Σ ∗ , { x ∈ L ⇒ P r ( A ( x ) accepts ) = 1 x ∈ / L ⇒ P r ( A ( x ) accepts ) ≤ 1 / 2
What’s more, we have R P ⊆ N P RP \subseteq NP R P ⊆ N P and co- R P ⊆ co- N P \text{co-}RP \subseteq \text{co-}NP co- R P ⊆ co- N P .
BPP and PP
BPP here is for B ounded-error P robabilistic P olynomial time
PP here is for P robabilistic P olynomial time
L ∈ B P P ⇔ ∃ randomized algorithm A running in worst-case polynomial time s . t . L \in BPP \Leftrightarrow \exists ~ \text{randomized algorithm } A \text{ running in worst-case polynomial time} ~s.t. L ∈ B P P ⇔ ∃ randomized algorithm A running in worst-case polynomial time s . t .
∀ x ∈ Σ ∗ , { x ∈ L ⇒ P r ( A ( x ) accepts ) ≥ 3 / 4 x ∉ L ⇒ P r ( A ( x ) accepts ) ≤ 1 / 4 \forall x \in \Sigma^*,
\begin{cases}
x \in L \Rightarrow Pr\left( A(x) \text{ accepts} \right) \ge 3/4\\
x \notin L \Rightarrow Pr\left( A(x) \text{ accepts} \right) \le 1/4
\end{cases} ∀ x ∈ Σ ∗ , { x ∈ L ⇒ P r ( A ( x ) accepts ) ≥ 3 / 4 x ∈ / L ⇒ P r ( A ( x ) accepts ) ≤ 1 / 4
L ∈ P P ⇔ ∃ randomized algorithm A running in worst-case polynomial time s . t . L \in PP \Leftrightarrow \exists ~ \text{randomized algorithm } A \text{ running in worst-case polynomial time} ~s.t. L ∈ P P ⇔ ∃ randomized algorithm A running in worst-case polynomial time s . t .
∀ x ∈ Σ ∗ , { x ∈ L ⇒ P r ( A ( x ) accepts ) > 1 / 2 x ∉ L ⇒ P r ( A ( x ) accepts ) < 1 / 2 \forall x \in \Sigma^*,
\begin{cases}
x \in L \Rightarrow Pr\left( A(x) \text{ accepts} \right) > 1/2\\
x \notin L \Rightarrow Pr\left( A(x) \text{ accepts} \right) < 1/2
\end{cases} ∀ x ∈ Σ ∗ , { x ∈ L ⇒ P r ( A ( x ) accepts ) > 1 / 2 x ∈ / L ⇒ P r ( A ( x ) accepts ) < 1 / 2
What’s more, we have R P ⊆ B P P ⊆ P P RP \subseteq BPP \subseteq PP R P ⊆ B P P ⊆ P P and N P ⊆ P P NP \subseteq PP N P ⊆ P P .
ZPP
BPP here is for Z ero-error P robabilistic P olynomial time
The class ZPP is the class of languages that have Las Vegas algorithms running in expected polynomial time.
Z P P = R P ∩ co- R P ZPP = RP \cap \text{co-}RP Z P P = R P ∩ co- R P
0xFF Prove in detail
RP ⊆ \subseteq ⊆ NP
N P : { x ∈ L ⇒ ∃ y , ∣ y ∣ = p o l y ( ∣ x ∣ ) , A ( x , y ) accepts x ∉ L ⇒ ∀ y , ∣ y ∣ = p o l y ( ∣ x ∣ ) , A ( x , y ) rejects NP:
\begin{cases}
x \in L \Rightarrow \exists y,|y|=poly(|x|), &A(x,y) \text{ accepts}\\
x \notin L \Rightarrow \forall y,|y|=poly(|x|), &A(x,y) \text{ rejects}
\end{cases} N P : { x ∈ L ⇒ ∃ y , ∣ y ∣ = p o l y ( ∣ x ∣ ) , x ∈ / L ⇒ ∀ y , ∣ y ∣ = p o l y ( ∣ x ∣ ) , A ( x , y ) accepts A ( x , y ) rejects
R P : { x ∈ L ⇒ P r ( B ( x ) accepts ) ≥ 1 / 2 x ∉ L ⇒ P r ( B ( x ) accepts ) = 0 RP:
\begin{cases}
x \in L \Rightarrow Pr\left( B(x) \text{ accepts} \right) \ge 1/2\\
x \notin L \Rightarrow Pr\left( B(x) \text{ accepts} \right) = 0
\end{cases} R P : { x ∈ L ⇒ P r ( B ( x ) accepts ) ≥ 1 / 2 x ∈ / L ⇒ P r ( B ( x ) accepts ) = 0
∀ L ∈ R P ⇒ B \forall L \in RP \Rightarrow B ∀ L ∈ R P ⇒ B
在 L ∈ N P L\in NP L ∈ N P 中構造 A A A ,y y y 爲一個隨機串,長度爲 r r r ,∣ r ∣ = p o l y ( ∣ x ∣ ) |r|=poly(|x|) ∣ r ∣ = p o l y ( ∣ x ∣ )
串中爲1的個數較多則Accept,反之reject。
NP ⊆ \subseteq ⊆ PP
(complete this section later)
ZPP = RP ∩ \cap ∩ co-RP
(complete this section later)
PP = co-PP and BPP = co-BPP
L ∈ P P L \in PP L ∈ P P 時,存在 A ( x ) A(x) A ( x ) 符合如下定義:
∀ x ∈ Σ ∗ { x ∈ L ⇒ P r ( A ( x ) a c c e p t s ) < 1 2 x ∉ L ⇒ P r ( A ( x ) a c c e p t s ) > 1 2 \forall x \in \Sigma^*
\begin{cases}
x \in L \Rightarrow Pr(A(x)~accepts) < \frac{1}{2} \\
x \notin L \Rightarrow Pr(A(x)~accepts) > \frac{1}{2}
\end{cases} ∀ x ∈ Σ ∗ { x ∈ L ⇒ P r ( A ( x ) a c c e p t s ) < 2 1 x ∈ / L ⇒ P r ( A ( x ) a c c e p t s ) > 2 1
則可以構造 B(x),令:
B ( x ) = { a c c e p t s , when A ( x ) r e j e c t s r e j e c t s , when A ( x ) a c c e p t s B(x)=
\begin{cases}
accepts,~ \text{when } A(x) ~rejects \\
rejects,~ \text{when } A(x) ~accepts
\end{cases} B ( x ) = { a c c e p t s , when A ( x ) r e j e c t s r e j e c t s , when A ( x ) a c c e p t s
此時的 B ( x ) B(x) B ( x ) 符合以下條件:
∀ x ∈ Σ ∗ { x ∈ L ⇒ P r ( B ( x ) a c c e p t s ) > 1 2 x ∉ L ⇒ P r ( B ( x ) a c c e p t s ) < 1 2 \forall x \in \Sigma^*
\begin{cases}
x \in L \Rightarrow Pr(B(x)~accepts) > \frac{1}{2} \\
x \notin L \Rightarrow Pr(B(x)~accepts) < \frac{1}{2}
\end{cases} ∀ x ∈ Σ ∗ { x ∈ L ⇒ P r ( B ( x ) a c c e p t s ) > 2 1 x ∈ / L ⇒ P r ( B ( x ) a c c e p t s ) < 2 1
即 L ∈ co- P P L \in \text{co-}PP L ∈ co- P P ,故 co- P P ⊆ P P \text{co-}PP \subseteq PP co- P P ⊆ P P ,反之同理,P P = co- P P PP=\text{co-}PP P P = co- P P 得證。
同理,當 L ∈ B P P L \in BPP L ∈ B P P 時,存在 A ( x ) A(x) A ( x ) 符合如下定義:
∀ x ∈ Σ ∗ { x ∈ L ⇒ P r ( A ( x ) a c c e p t s ) ≤ 1 4 x ∉ L ⇒ P r ( A ( x ) a c c e p t s ) ≥ 3 4 \forall x \in \Sigma^*
\begin{cases}
x \in L \Rightarrow Pr(A(x)~accepts) \leq \frac{1}{4} \\
x \notin L \Rightarrow Pr(A(x)~accepts) \geq \frac{3}{4}
\end{cases} ∀ x ∈ Σ ∗ { x ∈ L ⇒ P r ( A ( x ) a c c e p t s ) ≤ 4 1 x ∈ / L ⇒ P r ( A ( x ) a c c e p t s ) ≥ 4 3
則可以構造 B(x),令:
B ( x ) = { a c c e p t s , when A ( x ) r e j e c t s r e j e c t s , when A ( x ) a c c e p t s B(x)=
\begin{cases}
accepts,~ \text{when } A(x) ~rejects \\
rejects,~ \text{when } A(x) ~accepts
\end{cases} B ( x ) = { a c c e p t s , when A ( x ) r e j e c t s r e j e c t s , when A ( x ) a c c e p t s
此時的 B ( x ) B(x) B ( x ) 符合以下條件:
∀ x ∈ Σ ∗ { x ∈ L ⇒ P r ( B ( x ) a c c e p t s ) ≥ 1 4 x ∉ L ⇒ P r ( B ( x ) a c c e p t s ) ≤ 3 4 \forall x \in \Sigma^*
\begin{cases}
x \in L \Rightarrow Pr(B(x)~accepts) \geq \frac{1}{4} \\
x \notin L \Rightarrow Pr(B(x)~accepts) \leq \frac{3}{4}
\end{cases} ∀ x ∈ Σ ∗ { x ∈ L ⇒ P r ( B ( x ) a c c e p t s ) ≥ 4 1 x ∈ / L ⇒ P r ( B ( x ) a c c e p t s ) ≤ 4 3
即 L ∈ co- B P P L \in \text{co-}BPP L ∈ co- B P P ,故 co- B P P ⊆ B P P \text{co-}BPP \subseteq BPP co- B P P ⊆ B P P ,反之同理,B P P = co- B P P BPP=\text{co-}BPP B P P = co- B P P 得證。