è¨ç®éã«ã¤ãã¦ã®ã話ã§ãã対象ã¯ãããã°ã©ãã³ã°çµé¨ã¯ãããè¨ç®éã®ãã¨ãç¥ããªãåå¿è ãããè¨ç®éã®ãã¨ãç¥ã£ã¦ããã¤ããã«ãªã£ã¦ããä¸ç´è ãããã§ãã
æ°å¼ãè¦ãããªã人ã«ã¨ã£ã¦ã¯èªãã®ã大å¤ããã§ãããæ·±å¼å¸ãã¤ã¤è½ã¡ã¤ãã¦èªãã§ãããã¨ããããã§ãã
ããããããã®è¨äºãèªåã«ã¯åããªããªãã¨æã£ãã¨ãã¯ãï¼å¥ã®è¨äºã Qiita ã¨ãã§æ¤ç´¢ãããããï¼ãã®è¨äºã®ä¸çªä¸ã® åèæç® ã«ããæ¬ãèªããã¨ããããããã¾ããAmazon ã®è©¦ãèªã¿ã§ç¡æã§èªãã¾ã*1ã
TL; DR
- é¢æ°ã®å¢å 度åãã®ãã¨ããªã¼ãã¼ã¨å¼ã¶ã
- è¨ç®éã¯ãå
¥åãµã¤ãºï¼ãªã©ï¼ãåãåã£ã¦ã¢ã«ã´ãªãºã ã®è¨ç®åæ°ï¼ãªã©ï¼ãè¿ãé¢æ°ã ã
- ãã®é¢æ°ã®ãªã¼ãã¼ã«ã¤ãã¦ã®è°è«ãããè¡ãããã
- ãªã¼ãã¼ãä¸ããæããã¨ã㯠\(O\)ãä¸ããæããã¨ã㯠\(\Omega\) ã使ãã
- ãªã¼ãã¼ãä¸ä¸ä¸¡æ¹ããæãããã¨ã㯠\(\Theta\) ã使ãã
- ããã \(O\) ã ã¨æã£ã¦ãã人ãããããããã
- é©åãªæèã§é©åãªè¨å·ã使ããã¨ãããã
- ã¾ããã
- æ¬é¡
- å°å ¥ã»åæ©
- å®ç¾©ï¼+ ãããã¡ï¼
- è¨å·ã®æ¿«ç¨ã«ã¤ãã¦
- ãããã誤解ã»èª¤ç¨
- è¨ç®éã®è¦ç©ããã«é¢ãã¦
- 競æããã°ã©ãã³ã°ã¨ãªã¼ãã¼ã®ç¸æ§ãªã©
- ãããããªè¨ç®é
- ãããããªè¨ç®ã¢ãã«
- ãããããªãã©ã¡ã¼ã¿
- ã½ã¼ãã®è¨ç®éã«é¢ãã¦
- ã³ã¡ã³ãè¿ä¿¡
- åèæç®
- ãã¨ãã
- ããã
ã¾ããã
ãããããç¹ã«ç«¶æããã°ã©ãã³ã°ã®æèã§ã¯ãè¨ç®éã¯æåã®æ®µéã§åºç¤çãªäºé ã¨ãã¦ãµãã£ã¨æãããããã¨ãå¤ãã§ãã ãã®ããããã¾ãå³å¯ã«æãããããã¨ã¯ãªãããã®å¾ãèªåã§è©³ããå¦ã¶äººã¯å¤ãã¯ãªãå°è±¡ã§ãã ãããã人ãããã«ä¸æ£ç¢ºãªå 容ã®è¨äºãæ¸ãã¦ãããã¨ãå¤ãã§ã*2ã
以ä¸ã§ã¯ãã¾ãè¨ç®éãèããåæ©ãè¿°ã¹ã¦ãããè¨ç®éã®å®ç¾©ã¨ããã«é¢ããè¨æ³ã®å°å ¥ããã¾ããããã«ãè¨ç®éã«é¢ãããããã誤解ã»èª¤ç¨ã«ã¤ãã¦ãè¿°ã¹ã¾ããä¸åå¼·ãªäººãããé¥ã誤ç¨ã¨ã¯å¥ã«ãå¦è¡çãªæèã§ãããè¡ãããæ¿«ç¨ããããããã«ã¤ãã¦ãè¿°ã¹ã¾ãã
å¤å°é·ãã®è¨äºã§ãããããç¨åº¦ã®æ ¹æ°ãæã£ã¦èªãã§ããã ãããã¨æãã¾ã*3ã
ãã¡ããããã¯è¨ç®éã«ã¤ãã¦å¦ã³ãã人ï¼ãããã¯å¦ã¶å¿ è¦ãåºã¦ãã人ï¼ã«å¯¾ããè¨äºã§ãã£ã¦ãèªã¿ãããªã人ã«åããè¨äºã§ã¯ãªãã§ã*4ã
æ¬é¡
注æï¼ä»¥ä¸ã§å®ç¾©ãããã¾ã§ã¯ããè¨ç®éãã¨ããæ¦å¿µãå¿ãã¦èªãã§ã¿ã¦ãã ãã*5ã
å°å ¥ã»åæ©
ãã¨ãã°ãé·ã \(n\) ã®æ´æ°ã®é å \(a = (a_1, a_2, \dots, a_n)\) ãã½ã¼ãããããªã£ãã¨ãã¾ãã 以ä¸ã® 3 ã¤ã®ã¢ã«ã´ãªãºã *6ãæãããã¦ã好ããªãã®ãå®è¡ããã°ãããã¨ã«ãªãã¾ããã
ã¢ã«ã´ãªãºã 1 ã¯æ¬¡ã®éãã§ãã
- for \(i\) in \(1, \dots, n-1\)
- // \( (a_i, \dots, a_n)\) ã®æå°å¤ãæ¢ãã\(a_i\) ã¨äº¤æããã
- \(i' \gets i\) // ä»ã¾ã§è¦ãæå°å¤ã®æ·»å
- for \(j\) in \(i+1, \dots, n\)
- if \(a_{i'} \gt a_j\)
- \(i' \gets j\)
- \(x \gets a_i\)
- \(a_i \gets a_{i'}\)
- \(a_{i'} \gets x\)
ã¢ã«ã´ãªãºã 2 ã¯æ¬¡ã®éãã§ãã
- for \(j\) in \(2, \dots, n\)
- \(x \gets a_j\)
- \(i \gets j-1\)
- // \( (a_1, \dots, a_{j-1})\) ãã½ã¼ãæ¸ã¿ãé©åãªä½ç½®ã«æ¿å ¥ããã
- while \(i\ge 1\) and \(a_i \ge x\)
- \(a_{i+1} \gets a_i\)
- \(i \gets i-1\)
- \(a_i \gets x\)
ã¢ã«ã´ãªãºã 3 ã¯æ¬¡ã®éãã§ãã
- é·ã \(n\) ã®æªåæåã®é å \( (b_1, \dots, b_n)\) ãç¨æãã
- for \(e\) in \(1, \dots, \infty\)
- \(l \gets 2^e\)
- // é·ã \(l/2\) ã®é£ãåãã½ã¼ãåããé·ã \(l\) ã®ã½ã¼ãåãä½ã
- for \(i\) in \(0, \dots, \lceil n/l\rceil-1\)
- \(i_l \gets i\cdot l\)
- \(i_r \gets \min\{n, i_l+l/2\}\)
- \(e_l \gets i_r\)
- \(e_r \gets \min\{n, i_l+l\}\)
- \(j \gets i_l\)
- while \(i_l \lt e_l\) and \(i_r \lt e_r\)
- if \(a_{i_l} \lt a_{i_r}\)
- \(b_j \gets a_{i_l}\)
- \(i_l \gets i_l+1\)
- else
- \(b_j \gets a_{i_r}\)
- \(i_r \gets i_r+1\)
- \(j \gets j+1\)
- for \(i'\) in \(i_l, \dots, e_l -1\)
- \(b_{j+i'-i_l} \gets a_{i'}\)
- for \(i'\) in \(i_r, \dots, e_r-1\)
- \(b_{j+i'-i_r} \gets a_{i'}\)
- for \(i'\) in \(i\cdot l, \dots, e_r-1\)
- \(a_{i'} \gets b_{i'}\)
- if \(l \ge n\)
- break
åã¢ã«ã´ãªãºã ã®ãæ°æã¡æ¦è¦ï¼ã¯ãªãã¯ãã¦å±éï¼
ãæ°æã¡æ¦è¦ã¨ã½ã¼ãããã¦ããã¤ã¡ã¼ã¸ã¿ãããªã®ãæ¸ãã¦ããã¾ãã[]
ã®é¨åãã½ã¼ãããã¦ããã®ãä¿è¨¼ããã¦ããé¨åã§ãã
Algorithm 1ï¼æå°å¤ãæ¢ãã¦ä¸çªå·¦ã«ç½®ããæ®ã£ããã®ããæå°å¤ãæ¢ãã¦ãã£ãã®æå°å¤ã®é£ã«ç½®ããã¨ããã®ãç¹°ãè¿ãã
input: 8 2 7 6 4 5 3 1 [1] 2 7 6 4 5 3 8 [1 2] 7 6 4 5 3 8 [1 2 3] 6 4 5 7 8 [1 2 3 4] 6 5 7 8 [1 2 3 4 5] 6 7 8 [1 2 3 4 5 6] 7 8 [1 2 3 4 5 6 7] 8 [1 2 3 4 5 6 7 8]
Algorithm 2ï¼å·¦ç«¯ããã½ã¼ãåãä½ã£ã¦ä¼¸ã°ãã¦ãããä»ã¾ã§ã§ãã¦ããã½ã¼ãåã®é©åãªä½ç½®ã«æ°ããªè¦ç´ ãæ¿å ¥ããã¨ããã®ãç¹°ãè¿ãã
input: 8 2 7 6 4 5 3 1 [8] 2 7 6 4 5 3 1 [2 8] 7 6 4 5 3 1 [2 7 8] 6 4 5 3 1 [2 6 7 8] 4 5 3 1 [2 4 6 7 8] 5 3 1 [2 4 5 6 7 8] 3 1 [2 3 4 5 6 7 8] 1 [1 2 3 4 5 6 7 8]
Algorithm 3ï¼é£ãåãè¦ç´ ã§ãã¢ãä½ããé·ã 2 ã®ã½ã¼ãåãã¡ãä½ããé£ãåãé·ã 2 ã®ã½ã¼ãåãé©åãªé ã§ãã£ã¤ãã¦ãé·ã 4 ã®ã½ã¼ãåãã¡ãä½ãããããç¹°ãè¿ãã¦ã½ã¼ãåã®é·ããåã ã«å¢ããã¦ããã
input: 8 2 7 6 4 5 3 1 [2 8] [6 7] [4 5] [1 3] [2 6 7 8] [1 3 4 5] [1 2 3 4 5 6 7 8]
ï¼ã¯ãªãã¯ãã¦å±éé¨åãããï¼
ãã¦ãå¤ãã®ç¶æ³ã§ããã°ãæãæ©ãçµäºããã¢ã«ã´ãªãºã ã使ãããã¯ãã§ãã å¥ã«æãé ãçµäºãããã®ãé¸ãã§ãããã§ããããªãã«ãããåã ã©ã®ããããããã®ãããããªããã¨ã«ã¯é¸ã¹ã¾ãã*7ã
å¥ã®ä¾ã¨ãã¦ãä¸è¨ã¢ã«ã´ãªãºã 1 ã使ã£ã¦è¦ããã§ãã人ã«å¯¾ãã¦ãã¢ã«ã´ãªãºã 3 ã®æ¹ãå¹çãããã§ãããã¨æãããã¨ãã«ããããããææ¨ããªããã¨ã«ã¯èª¬å¾ããã«ããããã§ãããã¡ãããæããããå´ããã®ææ¨ãç¥ã£ã¦ããã®ãæã¾ãã¾ãã
解æã®ä¾
ããã§ãåè¡ã«å¯¾ãã¦ããããå®è¡ããããã®æéãã¨ããã®å®è¡åæ°ãã®ç©ã調ã¹ããã®åè¨ããã®ã¢ã«ã´ãªãºã ã®æéè¨ç®éã¨å¼ã¶ãã¨ã«ãã¾ãã æéè¨ç®éãå°ããã»ã©æãæ©ãçµäºãããã¨ã«ãªãã¾ãã
åè¡ãå®è¡ããããã®æéã¯ãæ¼ç®ã®ç¨®é¡ãï¼ç¾å®ã«å³ãã¦è¨ãã°ï¼CPU ã®æ§è½ãªã©ã«ããã¾ãããããå®æ°ã ã¨ãã¦ã\(i\) è¡ç®ã®å®è¡ã«ã¯ \(c_i\) ããããã¨ãããã¨ã«ãã¾ã*8ã
ã¢ã«ã´ãªãºã èªä½ã¯ãã½ã³ã³ã¨ã¯ç¬ç«ãªæ¦å¿µï¼æãªã©ã§å®è¡ãããã¨ãã§ãã¾ããï¼ãªã®ã§ãã¢ã«ã´ãªãºã ã®è©ä¾¡ãããéã« CPU ãªã©ã«ä¾åãããããªè¡¨ç¾ã¯ããããªãã¨ããæ°æã¡ãããã¾ãã
ãªããå®æ°æéã§ã§ãããã¨ã«ããå½ä»¤ã»ããã¯ãã®æã ã®ç¶æ³ã«å¿ãã¦é©åã«è¨å®ãã¾ããããããã§ã¯é åã¸ã®ã¢ã¯ã»ã¹ãååæ¼ç®ã»å¤§å°æ¯è¼ãä»£å ¥ãªã©ãä»®å®ãã¦ãã¾ãã
ã¨ããã¨ãåã¢ã«ã´ãªãºã ã®æéè¨ç®éã¯æ¬¡ã®ããã«ãªãã¾ãã
ã¢ã«ã´ãªãºã 1 ã®è¡¨
ã³ã¼ã | ã³ã¹ã | åæ° |
---|---|---|
for \(i\) in \(1, \dots, n-1\) | \(c_1\) | \(n\) |
ã\(i' \gets i\) | \(c_2\) | \(n-1\) |
ãfor \(j\) in \(i+1, \dots, n\) | \(c_3\) | \(\sum_{i=1}^{n-1} (n-i+1)\) |
ããif \(a_{i'} \gt a_j\) | \(c_4\) | \(\sum_{i=1}^{n-1} (n-i)\) |
ããã\(i' \gets j\) | \(c_5\) | \(\sum_{i=1}^{n-1} t_i\) |
ã\(x \gets a_i\) | \(c_6\) | \(n-1\) |
ã\(a_i \gets a_{i'}\) | \(c_7\) | \(n-1\) |
ã\(a_{i'} \gets x\) | \(c_8\) | \(n-1\) |
ããã§ãfor \(i\) in \(1, \dots, n-1\) ã«ã¤ãã¦ã¯ãã«ã¼ãã®ä¸ã«å ¥ãããã® \(n-1\) åã¨ã«ã¼ããæããããã® \(1\) åãåãã㦠\(n\) åå®è¡ãããã¨ãã¾ãã ã¾ããif ã®å é¨ã¯å ¥åã«ãã£ã¦å®è¡åæ°ãå¤ããã®ã§ãå \(i\) ã«å¯¾ããå®è¡åæ°ã \(t_i\) (\(0\le t_i\le n-i\)) ã¨ãã¾ãã
\(\sum_{i=1}^{n-1} (n-i)\) ã®é¨åã¯ãå \(i\) ã«å¯¾ã㦠\(n-i\) åå®è¡ãããã®ã§ãããã®åè¨ã§ãã¨ãããã¨ã§ãã
ãã£ã¦ããã®æéè¨ç®é \(T_1(n)\) ã¯æ¬¡ã®ããã«ãªãã¾ã*9ã \[ \begin{aligned} T_1(n) &= c_1\cdot n + (c_2+c_6+c_7+c_8)\cdot(n-1) + c_3\cdot\sum_{i=1}^{n-1} (n-i+1) + c_4\cdot\sum_{i=1}^{n-1} (n-i) + c_5\cdot\sum_{i=1}^{n-1} t_i \\ &= (c_1+c_2+c_3+c_6+c_7+c_8)\cdot n - (c_2+c_3+c_6+c_7+c_8)\cdot 1 + (c_3+c_4)\cdot\sum_{i=1}^{n-1}(n-i) + c_5\cdot\sum_{i=1}^{n-1} t_i\\ &= (c_1+c_2+c_3+c_6+c_7+c_8)\cdot n - (c_2+c_3+c_6+c_7+c_8)\cdot 1 + (c_3+c_4)\cdot\frac{1}{2}\cdot n(n-1) + c_5\cdot\sum_{i=1}^{n-1} t_i\\ \end{aligned} \] ä¿æ°ãåé·ãªã®ã§ãé©å®ç½®ãç´ãã¦æ¬¡ã®ããã«ãªãã¾ãã \[ T_1(n) = a_2\cdot n^2 + a_1\cdot n + a_0\cdot 1 + a_t\cdot\sum_{i=1}^{n-1} t_i \]
ããã§ã\(\sum t_i\) ã®é¨åã«é¢ãã¦ãæãå°ãããªãã¨ã㯠\(0\)ãæã大ããã¦ã 2 次å¼ãªã®ã§ã次ã®ããã«ãªãã¾ãã \[ a_2\cdot n^2 + a_1\cdot n + a_0\cdot 1 \le T_1(n) \le b_2\cdot n^2 + b_1\cdot n + b_0\cdot 1 \]
ããã§ã\(n\) ãåå大ãããªã£ãã¨ãã\(a_1\cdot n+a_0\cdot 1\) 㯠\(a_2\cdot n^2\) ã¨æ¯ã¹ã¦ååã«å°ãããªãã¾ãã ãªã®ã§ãæãå½±é¿ã®å¤§ãã \(a_2\cdot n^2\) ã®é ã«ã®ã¿çç®ãããã§ã*10ã \(n\) ãåå大ãããªã£ãã¨ãã«æãå½±é¿ã®å¤§ããé ãæ¯é é ãç¡è¦ã§ããé ãéæ¯é é ã¨å¼ã¶ãã¨ã«ãã¾ã*11ã
\(T_1(n)\) ã®æ¯é é ã \(a\cdot n^2\) ã§ãããã¨ããå¾ã§ã¡ããã¨å®ç¾©ããè¨æ³ãç¨ã㦠\(T_1(n)\in\Theta(n^2)\) ã \(T_1(n)=\Theta(n^2)\) ã¨æ¸ãã¾ã*12ã
\(a_2\cdot n^2 + a_1\cdot n + a_0\cdot 1\in\Theta(n^2)\) 㨠\(b_2\cdot n^2 + b_1\cdot n + b_0\cdot 1\in\Theta(n^2)\) ãæãç«ã¡ã¾ããããªãã¡ãã¢ã«ã´ãªãºã 1 ã®æéè¨ç®é \(T_1(n)\) ã«å¯¾ãã¦ã\(T_1(n)\) ãæãå°ããã¨ãï¼æè¯æ (best case)ï¼\(T_1(n)\in\Theta(n^2)\)ãæã大ããã¨ãï¼ææªæ (worst case)ï¼\(T_1(n)\in\Theta(n^2)\) ãæãç«ã¡ã¾ãã
ã¢ã«ã´ãªãºã 2, 3 ã«ã¤ãã¦ã®è°è«ã¯ãé·ããªã£ãã®ã§æããããã§ããã¾ãã
ã¢ã«ã´ãªãºã 2, 3 ã«ã¤ãã¦ã®è°è«ï¼ã¯ãªãã¯ãã¦å±éï¼
ã¢ã«ã´ãªãºã 2 ã®è¡¨ã¯æ¬¡ã®éãã§ãã
ã³ã¼ã | ã³ã¹ã | åæ° |
---|---|---|
for \(j\) in \(2, \dots, n\) | \(c_1\) | \(n\) |
ã\(x \gets a_j\) | \(c_2\) | \(n-1\) |
ã\(i \gets j-1\) | \(c_3\) | \(n-1\) |
ãwhile \(i\ge 1\) and \(a_i \ge x\) | \(c_4\) | \(\sum_{j=2}^n (u_j+1)\) |
ãã\(a_{i+1} \gets a_i\) | \(c_5\) | \(\sum_{j=2}^n u_j\) |
ãã\(i \gets i-1\) | \(c_6\) | \(\sum_{j=2}^n u_j\) |
ã\(a_i \gets x\) | \(c_7\) | \(n-1\) |
\(u_j\) 㯠while æã®ä¸ã®å®è¡åæ°ã®ããã®å¤æ°ã§ã\(0\le u_j\le j-1\) ã§ãã
ã¢ã«ã´ãªãºã 1 åæ§ã«ãé©å®å¤æ°ãç½®ãç´ãã¦ãæéè¨ç®é \(T_2(n)\) ã¯æ¬¡ã®ããã«ãªãã¾ãã \[ T_2(n) = a_1'\cdot n + a_0'\cdot 1 + a_t'\cdot\sum_{j=2}^n u_j \]
ããã§ã\(\sum u_j\) ã®é¨åã«é¢ãã¦ãæè¯æ㯠\(u_j = 0\) ãªã®ã§ã\(T_2(n) = a_1'\cdot n+a_0'\cdot 1\) ã¨ãªãã\(T_2(n)\in\Theta(n)\) ã¨ãªãã¾ãã ææªæã¯ãå ¥åãéé ã«ä¸¦ãã§ããã¨ã \(u_j = j-1\) 㧠\(\sum_{j=2}^n (j-1)\) 㯠2 次å¼ã¨ãªãã®ã§ã\(T_2(n) = b_2'\cdot n^2+b_1'\cdot n+b_0'\cdot 1\) ã¨ãªãã¾ããããªãã¡ã\(T_2(n)\in\Theta(n^2)\) ã§ãã
èªè ã®èª²é¡ã«ãããã¨ããã§ãããã¢ã«ã´ãªãºã 3 ã«ã¤ãã¦ãè¡ãã¾ãã
ã³ã¼ã | ã³ã¹ã | åæ° |
---|---|---|
for \(e\) in \(1, \dots, \infty\) | \(c_1\) | \(v\) |
ã\(l \gets 2^e\) | \(c_2\) | \(v\) |
ãfor \(i\) in \(0, \dots, \lceil n/l\rceil-1\) | \(c_3\) | \(\sum_{e=1}^v (\lceil n/2^e\rceil+1)\) |
ããçç¥ | - | - |
ãã\(e_r \gets \min\{n, i_l+l\}\) | \(c_7\) | \(\sum_{e=1}^v \lceil n/2^e\rceil\) |
ããçç¥ | - | - |
ããfor \(i'\) in \(i\cdot l, \dots, e_r-1\) | \(c_{21}\) | \(\sum_{e=1}^v \sum_{i=0}^{\lceil n/l\rceil-1} (e_r-i\cdot l+1)\) |
ããã\(a_{i'} \gets b_{i'}\) | \(c_{22}\) | \(\sum_{e=1}^v \sum_{i=0}^{\lceil n/l\rceil-1} (e_r-i\cdot l)\) |
ãif \(l \ge n\) | \(c_{23}\) | \(v\) |
ããbreak | \(c_{24}\) | \(1\) |
\(\lceil x\rceil\) ã¯ã\(x\) 以ä¸ã®æå°ã®æ´æ°ã§ãã
è¥å¹²æªããã§ãããé·ã \(n\) ã®æªåæåã®é åãç¨æããæéã¯ç¡è¦ã§ãããã¨ã«ãã¾ãã ããã㯠\(c_0\cdot n\) æéã¨ãã¦ãããã§ããã©ããæ¯é çã«ãªããªãã®ã§ï¼ãã£ã¨å¤§ãããªãã¨ã¯èãã«ãããï¼ã
1 è¡ç®ã®å®è¡åæ° \(v\) ã¯ã23â24 è¡ç®ã®åå²ã¨ break ã«ãã£ã¦å®ã¾ãã¾ãã
\(\sum_{i=0}^{\lceil n/l\rceil-1}(e_r-i\cdot l) = n\) ã§ããã\(v = \lceil\log_2(n)\rceil\) ã§ãã ãã£ã¦ã\(a_{i'}\gets b_{i'}\) ã®å®è¡åæ°ã¯ \(n\lceil\log_2(n)\rceil\) ã¨ãªãã¾ãã ä»ã®è¡ã«ã¤ãã¦ãããã« \(n\) ã足ãããç¨åº¦ä»¥ä¸ãªã®ã§ãçµå±ãå ¨ä½ã®æéè¨ç®é \(T_3(n)\) ã¯æ¬¡ã®ããã«ãªãã¾ãã \[ a''\cdot n\lceil\log_2(n)\rceil \le T_3(n) \le a'''\cdot n\lceil\log_2(n)\rceil \]
\(a''\le a\le a'''\) ãªã \(a\) ãåå¨ã㦠\(a\cdot n\lceil\log_2(n)\rceil\) ã¨æ¸ããã¨è¨ã£ã¦ããããã§ã¯ãªããã¨ã«æ³¨æãã¦ãã ããã \(a\cdot \lceil\log_2(n)\rceil\cdot(n+b)\) ã®ãããªå½¢ã¯ããã§ã¯è¡¨ç¾ã§ãã¾ããã®ã§ã
ãã£ã¦ãæè¯æãææªæã \(T_3(n)\in\Theta(n\lceil\log_2(n)\rceil)\) ã¨ãªãã¾ãã \({}_2\) ã®é¨å㨠\(\lceil\rceil\) ã®é¨åãæ¶å»ãã¦ã\(T_3(n)\in\Theta(n\log(n))\) ã¨æ¸ããã¨ãæ®éã§ãã \(\lceil x\rceil\) 㯠\(x\) ã¨é«ã \(1\) ããå¤ãããããã®å·®ã¯æ¯é çã§ã¯ãªãããã§ããã \({}_2\) ã«ã¤ãã¦ã¯å¾è¿°ãã¾ãã
ï¼ã¯ãªãã¯ãã¦å±éé¨åãããï¼
çµå±ã3 ã¤ã®ã¢ã«ã´ãªãºã ã«å¯¾ãã¦ãæè¯æã¨ææªæã¯æ¬¡ã®ããã«ãªãã¨ãããã¾ããã
ã¢ã«ã´ãªãºã | æè¯æã®æéè¨ç®é | ææªæã®æéè¨ç®é |
---|---|---|
1 | \(\Theta(n^2)\) | \(\Theta(n^2)\) |
2 | \(\Theta(n)\) | \(\Theta(n^2)\) |
3 | \(\Theta(n\log(n))\) | \(\Theta(n\log(n))\) |
ã¢ã«ã´ãªãºã 1 ã®æéè¨ç®éã¯æè¯æã§ãã \(\Theta(n^2)\) ã§ããã¢ã«ã´ãªãºã 2 ã¯æè¯æ㯠\(\Theta(n)\) ã§ãã 3 ã¤ã®ãã¡ã§æãããããã§ãããææªæã¯ã¢ã«ã´ãªãºã 1 ã¨åãã \(\Theta(n^2)\) ã§ãã ææªæã®æéè¨ç®éãå°ãããããã®ã§ããã°ãã¢ã«ã´ãªãºã 3 ãé¸ã¶ã®ãããããã§ãã
注æï¼
- \(\Theta(\bullet)\) ã®è¨æ³ãç¨ããéã«ã\(n\) ãåå大ãããã°ãã¨æã£ãéãã\(n\) ãå°ããå ´åã®ãã¨ã¯ãã®è¡¨ããã¯èªã¿åããªãã
- ã¢ã«ã´ãªãºã 1, 2 ã®ææªæã¯ã©ã¡ãã \(\Theta(n^2)\) ã ããããã¯ããããã®æéè¨ç®éãå
¨ãåãã§ãããã¨ãã主張ã§ã¯ãªãã
- ãããäºã¤ã®ææªæã®æéè¨ç®éãããå³å¯ã«æ¯è¼ãããã®ã§ããã°ãæ¶å»ãã \(a_2\) ãªã©ã®å®æ°åãéæ¯é é ãèæ ®ããå¿ è¦ãããã
log ã«æ
£ãã¦ããªãåå¿è
åãï¼ã¯ãªãã¯ãã¦å±éï¼
\(\log_2(n)\) ã¯ã\(2\) ãä½ä¹ããã \(n\) ã«ãªããã¨ããå¤ã表ãã¾ãã ããªãã¡ã\(2\) ã \(\log_2(n)\) ä¹ããã \(n\) ã«ãªãã¨ãããã¨ã§ãã ãã¨ãã°ã\(2^6 = 64\) ãªã®ã§ã\(\log_2(64)=6\) ã§ããã¾ãã\(\log_2(3) = 1.5849625{\dots}\) ã \(\log_2(7) = 2.80735492{\dots}\) ãªã©ã2 ã®åªä¹ä»¥å¤ã«ãå®ç¾©ããã¦ãã¾ãã
端æ°ãé©å½ã«åãä¸ãããããã°ãã\(n\) ãä½å \(2\) ã§å²ãã° \(1\) 以ä¸ã«ãªããï¼ãã¨ããåæ°ã¨ãè¦ãªãã¾ãã
ä¸è¬ã«ã\(\log_a(n)\) 㯠\(a\) ãä½ä¹ããã \(n\) ã«ãªããã表ãã¾ãã \(a\) ã®é¨åãåºï¼ã¦ãï¼ã¨å¼ã³ãåºã®å¤æå ¬å¼ \(\log_a(n) = \log_a(b)\cdot \log_b(n)\) ã¨ããã®ãããã¾ãã é©å½ãªå®æ° \(\log_a(b)\) ãæãããã¨ã§ã\(\log_b(n)\) ã®é¨åã \(\log_a(n)\) ã«ã§ããã¨ãããã¨ã§ããã
ãã£ã¦ã\(\log_a(n)\) ã \(\log_b(n)\) ãå®æ°åã®éããããªãããã®æèã«ããã¦ã¯èå³ã®ãªãé¨åãªã®ã§ãçç¥ãã¦åã« \(\log(n)\) ã¨æ¸ããããã¨ãå¤ãã§ãã\(\log_e(n)\) ã \(\log_{10}(n)\) ã®åºãçç¥ããã®ã¨ã¯å¥ã®äºæ ã§ãã
ï¼ã¯ãªãã¯ãã¦å±éé¨åãããï¼
ãªããã¢ã«ã´ãªãºã 1, 2, 3 ã¯ãããããé¸æã½ã¼ããæ¿å ¥ã½ã¼ãããã¼ã¸ã½ã¼ãã¨å¼ã°ãã¦ãããã®ã§ãã
å®ç¾©ï¼+ ãããã¡ï¼
è¨ç®é
ä¸ã§è§¦ãã¦ãã¾ã£ãéããã¢ã«ã´ãªãºã ãè¡ã£ãå¦çã®ã³ã¹ãã®ç·åã æéè¨ç®é (time complexity) ã¨å¼ã³ã¾ãã æéè¨ç®éã¯å ¥åãµã¤ãº \(n\) ã®é¢æ°ã¨ãã¦è¨è¿°ããããã¨ãå¤ãã§ãããä¸ã§è¦ãéããå ¥åãµã¤ãº \(n\) ã®ã¿ã§ã¯æ±ºã¾ãããå ¥åèªä½ã«ãã£ã¦å¤ãããã¨ãå¤ãã§ãã æãæéè¨ç®éãå°ãããªããããªå ¥åã«ãããæéè¨ç®éã æè¯æéè¨ç®é (best-case time complexity)ãæã大ãããªãããã ææªæéè¨ç®é (worst-case time complexity) ã¨å¼ã³ã¾ãã å½ç¶ããã以å¤ã®å ¥åã«ãããæéè¨ç®éã¯ãæè¯æéè¨ç®é以ä¸ãææªæéè¨ç®é以ä¸ã¨ãªãã¾ãã
ï¼æ³¨ï¼ããã¢ã«ã´ãªãºã ã«ãããææªã®å ¥åã±ã¼ã¹ã¯ãå¥ã®ã¢ã«ã´ãªãºã ã«ãããææªã®å ¥åã±ã¼ã¹ã¨ä¸è´ããã¨ã¯éãã¾ããã
ã¢ã«ã´ãªãºã ä¸ã§ä½¿ç¨ããã¡ã¢ãªã®éãæã㦠空éè¨ç®é (space complexity) ã¨å¼ã°ãããã®ãããã¾ãã ã¢ã«ã´ãªãºã 1, 2 ã§ã¯ï¼å ¥åã®é åãé¤ãã¦ã¯ï¼ããã¤ãã®å¤æ°ãã使ã£ã¦ããªãã§ãããã¢ã«ã´ãªãºã 3 ã§ã¯é·ã \(n\) ã®é åãç¨ããã®ã§ããããæ¯ã¹ããã¨ãã£ãã¨ãã«ä½¿ããã®ã§ããã
åã« è¨ç®é (complexity) ã¨å¼ãã å ´åã¯æéè¨ç®éãæããã¨ãå¤ãã§ãããå®éã«ã¯ãæéã空é以å¤ã®è¨ç®ãªã½ã¼ã¹ãæãæèãããã¨æãã¾ããå¿ è¦ã¨ãªãåè·¯ã®åæ°ã¨ãï¼
ãè¨ç®éã¯ææªæã§ããã以ä¸ã§ããã¼ããè¨ç®éã¯æè¯æã«ã¯ãã以ä¸ã«ãªãã¾ããã¼ãã¨ããè©ä¾¡ã®ã»ããæµï¼ï¼ï¼ã®ã¢ã«ã´ãªãºã ã«å¯¾ãã¦ãããã¯ææªæã«ã¯ãã以ä¸ã«ãªã£ã¡ããã¾ããã¼ãã¨ããè©ä¾¡ããã¾ãããã®è©ä¾¡ã®ããã®æ¦å¨ãããããå°å ¥ãã¾ãã
ãªã¼ãã¼
ãã¦ã\(n\) ã®é¢æ°ã¨ãã¦è¨ç®éãè¨è¿°ãã¾ãããæä¸ã§ãããããã®ã½ã¼ãã®è¨ç®é㯠\(\frac{13}{2}n^2-\frac{5}{2}n+4\) ã§ãããããªã©ã¨è¨ããããªãç¶æ³ã¯ãã¾ããªããæ¯é é ã®ã¿ã«çç®ãã¦ãæ¯é é 㯠\(n^2\) ã®å®æ°åã§ããã¨ãã£ãæ¨ã®ä¸»å¼µã®ã¿ã§äºè¶³ãããã¨ãå¤ãã§ãã æ¯é é ã®å®æ°åãè½ã¨ã㦠\(5n^2\) ãã \(4n^2\) ã«æ¹åããããããæ¯é é ã®å¢å 度åããè½ã¨ã㦠\(5n^2\) ãã \(100n\) ã«æ¹åããæ¹ãããããã§ããåè ã®æ¹åã§ã¯ãæ¹åå¾ãã\(n\) ã 10 åã«ãªãã°è¨ç®éã¯ï¼éæ¯é é ãç¡è¦ãã¦ï¼100 åã«ãªããã¨ããç¶æ³ã«ã¯å¤ããããã¾ããããå¾è ã®æ¹åã§ã¯ã\(n\) ã 10 åã«ãªã£ã¦ãè¨ç®é㯠10 åã«ãããªããªããã¨ããç¶æ³ã«ãªã£ã¦ããããã§ãã
ããã§åºã¦ããå¢å 度åãã®ãã¨ãæãã¦åã« ãªã¼ãã¼ (order of growth) ã¨å¼ã³ã¾ããä¸è¨ã®è©±ãè¨ãæããã¨ããå®æ°åã®åæ¸ï¼ãããã¯éæ¯é é ã®æ¹åï¼ããããå¢å 度åãã®åæ¸*13ã®æ¹ããããããã¨ãããã¨ã§ããã
ãªã¼ãã¼ã®ã¡ããã¨ããï¼æ°å¦çãªï¼å®ç¾©ã¯ããããè¡ãã¾ãã
\(\Theta\) è¨æ³ (Theta)
ã¨ãã£ãããã§ããªã¼ãã¼ã«é¢ããè¨æ³ãå°å ¥ãã¾ãã \(f(n)\) ã®ãªã¼ãã¼ã \(g(n)\) ã§ããï¼ããã㯠\(f(n)\) ã \(g(n)\) ã®ãªã¼ãã¼ã§ããï¼ã¨ã¯ã次ã®æ¡ä»¶ãæãç«ã¤ãã¨ã¨ãã¾ãã
ããå®æ° \(n_0\) ãåºå®ãã¾ãã ããã«å¯¾ããããå®æ° \(c_L, c_U \gt 0\) ãåå¨ãã\(n_0\) 以ä¸ã®ãã¹ã¦ã® \(n\) ã«ã¤ãã¦æ¬¡ã®å¼ãæãç«ã¤ï¼ \[ 0\le c_L\cdot g(n)\le f(n)\le c_U\cdot g(n) \]
ç´æçã«è¨ãæããã°ãåå大ãã \(n\) ã«å¯¾ãã¦ã\(g(n)\) ã®å®æ°å㧠\(f(n)\) ããµã³ãã¤ããã§ããã¨ãããã¨ã§ãã ãµã³ãã¤ããã§ããã®ã§ããã°ãåããããªå¢å 度åãã§ããããã¨ãã£ããããªæ°æã¡ã§ãã
\(f(n)\) ã \(g(n)\) ã®ãªã¼ãã¼ã§ãããã¨ãã\(f(n) \in \Theta(g(n))\) ã¨æ¸ãã¾ãã\(\Theta\) 㯠\(\theta\) ã®å¤§æåã§ãã·ã¼ã¿ (theta) ã¨èªã¿ã¾ãã
\(\Theta(g(n))\) ã¨ããã®ã¯ããªã¼ãã¼ã \(g(n)\) ã§ãããããªé¢æ°ã®éåã§ãããã¨ãã°ã次ãæãç«ã¡ã¾ãã \[ \begin{aligned} n^2+3n &\in \Theta(n^2) \\ n^2 &\notin \Theta(n^3) \\ \Theta(2n) &= \Theta(n+1) = \Theta(n) \end{aligned} \]
証æï¼ã¯ãªãã¯ãã¦å±éï¼
- \(n^2+3n\in\Theta(n^2)\)
ãã \(n_0, c_L, c_U\) ãåºå®ãã\(n\ge n_0\) ã«å¯¾ã㦠\(0\le c_L\cdot n^2\le n^2+3n\le c_U\cdot n^2\) ãæãç«ã¤ãã¨ãè¨ãã°ããã \(n_0\) ãåå大ããåãã° \(n=0\) ã®ãã¨ã¯ç¡è¦ã§ããã®ã§åã \(n^2\) ã§å²ãã \[ 0\le c_L\le 1+\frac{3}{n}\le c_U \] ãå¾ãã\(n\ge 3\) ã®ã¨ã \(1\le 1+\frac{3}{n} \le 2\) ãæãç«ã¤ã®ã§ã\( (n_0, c_L, c_U) = (3, 1, 2)\) ã¨ããã°ããã\(\square\)
- \(n^2\notin\Theta(n^3)\)
ãããªã \(n_0, c_L, c_U\) ã«å¯¾ãã¦ãããã \(n\ge n_0\) ã«å¯¾ã㦠\(0\le c_L\cdot n^3\le n^2\le c_U\cdot n^3\) ãæãç«ããªããã¨ãè¨ãã°ããã åãã \(n^3\) ã§å²ãã \[ 0\le c_L\le \frac{1}{n}\le c_U \] ã¨ãªããä¸æ¹ã\(n\gt \frac{1}{c_L}\) ã«å¯¾ã㦠\(\frac{1}{n}\lt c_L\) ã¨ãªãããããã®å¼ãæãç«ãããªã \(n\ge n_0\) ãåå¨ããã\(\square\)
- \(\Theta(2n) = \Theta(n+1) = \Theta(n)\)
ãã¹ã¦ã® \(f(n)\in\Theta(2n)\) ã«å¯¾ã㦠\(f(n)\in\Theta(n)\) ã¨ãªããã¨ãªã©ã示ãã°ãããï¼\(n+1\) ãå«ããã¢ãªã©ã«å¯¾ãã¦ãåæ§ãçç¥ãï¼
ããã \(n_0, c_L, c_U\) ãåå¨ãããã¹ã¦ã® \(n\ge n_0\) ã«å¯¾ã㦠\(0\le c_L\cdot (2n)\le f(n)\le c_U\cdot (2n)\) ãæãç«ã¤ãã¨ããããã \(n_0', c_L', c_U'\) ãåå¨ãããã¹ã¦ã® \(n\ge n_0'\) ã«å¯¾ã㦠\(0\le c_L'\cdot n\le f(n)\le c_U'\cdot n\) ãæãç«ã¤ããã¨ã示ãã°ããã \( (n_0', c_L', c_U') = (n_0, 2c_L, 2c_U)\) ã¨ããã°ããããããã\(\square\)
ï¼ã¯ãªãã¯ãã¦å±éé¨åãããï¼
é·ã£ããããé¢æ°ã®æ¯é é ã®ã¿ã«æ³¨ç®ããããã« \(\Theta\) ã使ãã®ã§ããããã \(\Theta(n^2+3n+1)\) ãªã©ã¨ãã£ã表è¨ããããã¨ã¯æ» å¤ã«ããã¾ããããè¨æ³èªä½ã¯èª¤ãã§ã¯ãªãã§ãã
ä¸çå¼ã®é¨åã«ã¤ãã¦
\(\Theta\) ã®å®ç¾©ã®é¨å㧠\(f(n)\) ã«é¢ããä¸çå¼ãæ¸ãã¦ãã¾ãããããã \(n\) ã«å¯¾ã㦠\(f(n)\) ã®å¤ããã®ç¯å²ã§å¤åããï¼ãããããªå¤ãåãï¼ãã®ãããªæå³åãã§ã¯ãªããåã«ãé¢æ° \(f(n)\) ãé¢æ° \(g(n)\) ã®å®æ°åã§ã¯ããããã¨ãã£ãæ°æã¡ã§ãã
è¨ç®éã®æèã§è¨ãã°ãã\(c_L \cdot g(n)\) ãæè¯è¨ç®é㧠\(c_U \cdot g(n)\) ãææªè¨ç®éãã®ãããªæå³åãã§ã¯ãªãã®ã§é©å®æ³¨æãã¾ãããã ã\(f(n)\) ã¯ææªè¨ç®éã¨ãããã¨åºå®ããã¨ãã«ãããã \(g(n)\) ã®å®æ°åã§ã¯ãããã¨ãã£ããã¨ã§ããã
è¨ãåãã«ã¤ãã¦
ãªãã\(\Theta\) ã¯è¨ç®éã®ã¿ã«ã¤ãã¦ç¨ããè¨æ³ã§ã¯ãªããé¢æ°ä¸è¬ã«å¯¾ãã¦ç¨ãããã¨ãã§ããè¨æ³ã§ãã \(\Theta\) è¨æ³ã®ç¯ã§ã¯è¨ç®éã®è©±ã¯ç¹ã«åºã¦ããªãã£ãã§ããããã ãã¨ãã°ã以ä¸ã®ãããªè¡¨ç¾ãã§ãã¾ã*14ã
- \(1\) ä»¥ä¸ \(n\) 以ä¸ã®æ´æ°ã®ç·å㯠\(\Theta(n^2)\) ã§ãã
- è¦ç´ æ° \(n\) ã®éåã«å¯¾ãã¦ã空ã§ãªãé¨åéåã®åæ°ã¯ \(\Theta(2^n)\) åã§ãã
- \(\Theta(n)\) applications of the binary operation
- ãäºé æ¼ç® (binary operation) ã® \(\Theta(n)\) åã®é©ç¨ (application)ãã¨ãã£ãæå³
- 足ãç®ã¿ãããªã®ã \(\Theta(n)\) åè¡ãã¨ãããããªæè
ä¸çªä¸ã®ä¾ã¯ããããé¢æ° \(f(n)\in\Theta(n^2)\) ãåå¨ãã¦ã\(1\) ä»¥ä¸ \(n\) 以ä¸ã®æ´æ°ã®ç·åã \(f(n)\) ã«ãªããã¨ãã£ãæå³åãã§ã*15ãä¸çªä¸ã®ä¾ã®ããã«è±èªã§ã使ããã¾ãã\(\Theta(1)\) ãåæ°ã¨ã¯éããªãã®ã§ã\(\Theta(1)\) elements ã®ããã«è¤æ°å½¢ã«ãªã£ãããã¾ããã
åæ§ã«ããè¨ç®é㯠\(\Theta(n^2)\) ã§ããã¨ããã®ã¯ããã \(f(n)\in\Theta(n^2)\) ãåå¨ãã¦è¨ç®é㯠\(f(n)\) ã§ããã¨ãã£ã主張ã§ãããè¨ç®éã¨ããã®ã¯ \(\Theta(\bullet)\) ã®å½¢ã§æ¸ããªãããããªãã®ã§ããã« \(n^2\) ãå ¥ãããã®ãããªèªèã«ã¯ãªããªãã§ã»ããã§ãã
èªåã¯ãè¨ç®é㯠\(\Theta(n^2)\) ã§ãããã¨è¨ã£ãã¨ããã1+1 㯠2 ã§ãããã®ããã«å¤ã示ãã¦ããã®ã§ã¯ãªããã1+1 ã¯å¶æ°ã§ãããã®ããã«æ§è³ªã示ãã¦ããã¨ããèªèããã¦ããæ°ããã¾ãããªã¼ãã¼ã \(n^2\) ã§ããã¨ããã®ãããã®è¨ç®éã®æ§è³ªã«å¯¾å¿ããããã§ããã
ã¾ãããã¨ãã° C++ ã®é¢æ°ã®è¦æ ¼ã§ã¯ã次ã®ãããªè¡¨ç¾ãããåºã¦ãã¾ãï¼$\Theta$ ã§ã¯ãªã $O$ ãæ¥ã«åºã¦ãã¾ãããæå³ãéãã ãã§ææ³ã¨ãã¦ã¯ä¼¼ãæãã®ä½¿ãæ¹ãã§ãã¾ããï¼ã
Complexity: \(O(n\log(n))\) comparisons
æ¯è¼æ¼ç®ä»¥å¤ã«ã¤ãã¦ã¯ç¹ã«è¨åãã¦ãã¾ããããæ¯è¼ã®åæ°ãå ¨ä½ã®ããã«ããã¯ã«ãªãã®ã§ããã®åæ°ã \(O(n\log(n))\) åã ã¨è¨ã£ã¦ããã¨ããèæ¯ãããã¨æãã¾ã*16ã
æ¯è¼ã \(O(1)\) æéã§ã¯ãªã classã»struct ãå¼æ°ã¨ããå ´åã®ãã¨ãæ°ã«ã㦠\(O(n\log(n))\) time ã¨æ¸ãã¦ããªãã¨ããè¦æ¹ãããã¨æãã¾ãã
\(O\) è¨æ³ (big-O)
ä¸æ¹ã§ããææªæã§ãè¨ç®éã¯ããã¾ã§ããæªããªããªããã¨ãããã¨ã示ãããã¨ãã«ããããããµã³ãã¤ããããå¿ è¦ã¯ããã¾ããã ä¸ããã ãæã㦠\(0\le f(n)\le c_U\cdot g(n)\) ã¨ããè¨ã£ã¦ãã¾ãã°ååã§ã*17ãããã表ãè¨æ³ã \(O\) ã§ã*18ã
ã»ã¼ã³ããã§ããã以ä¸ãæãç«ã¤ã¨ãã\(f(n)\in O(g(n))\) ã¨æ¸ãã\(f(n)\) ã¯é«ã \(g(n)\) ã®ãªã¼ãã¼ã§ããã¨è¨ãã¾ã*19ã
ããå®æ° \(n_0\) ãåºå®ãã¾ãã ããã«å¯¾ããããå®æ° \(c_U \gt 0\) ãåå¨ãã\(n_0\) 以ä¸ã®ãã¹ã¦ã® \(n\) ã«ã¤ãã¦æ¬¡ã®å¼ãæãç«ã¤ï¼ \[ 0\le f(n)\le c_U\cdot g(n) \]
ãã¨ãã°ã次ã®å¼ãæãç«ã¡ã¾ãã \[ \begin{aligned} n^2+3n &\in O(n^2) \\ n^2 &\in O(n^3) \\ n^4 &\notin O(n^3) \\ O(2n) &= O(n+1) = O(n) \end{aligned} \]
\(n^2\in O(n^3)\) ã«éåæãè¦ãã人ã¯ãè½ã¡ã¤ãã¦ãã ããã
å å«é¢ä¿ãããã¤ãæ¸ãã¦ã¿ã¾ãã \[ O(1) \subset O(\log(n)) \subset O(\sqrt{n}) \subset O(n^{0.999}) \subset O(n/\log(n)) \subset O(n) \] \[ O(n) \subset O(n\log(n)) \subset O(n^2) \subset O(n^{1000}) \subset O(2^n) \subset O(3^n) \subset O(n!) \] \[ O(n!) \subset O( (n+1)!) \subset O(2^{2^n}) \subset \cdots \]
ãã¨ãã° \(O(n^2)\subset O(n^{1000})\) ã¯ããªã¼ãã¼ãé«ã \(n^{1000}\) ã®é¢æ°ãã¹ã¦ãããªãéåã¯ããªã¼ãã¼ãããã ã \(n^2\) ã®é¢æ°ããã¹ã¦å«ããããããããªã¼ãã¼ãé«ã \(n^2\) ã§ã¯ãªããé«ã \(n^{1000}\) ã§ããé¢æ°ã¯åå¨ãããã¨ãã£ãæå³ã§ã*20ã
ãªã¼ãã¼ãä¸ããæãã¦ããã ããªã®ã§ããã¨ãã°å®éã«ã¯ \(O(n\log(n))\) æéã®ã¢ã«ã´ãªãºã ãæã㦠\(O(n^2)\) æéã§ãã¨è¨ã£ã¦ãããããã§ãã ããããæªãè¦ç©ããå¿ è¦ã¯ãªãã®ã§ã¯ï¼ã¨ããæè¦ããããããªã®ã§ãä¾ãæãã¦ã¿ã¾ãã
- \(O(n^2)\) æéã§è¨ç®ã§ããé¢æ° \(f(n)\) ã¨ãè¨ç®éã®è§£æãæ¸ãã§ããªãé¢æ° \(g(n)\) ãããã
- \(f(n)\) 㨠\(g(n)\) ãä¸åº¦ãã¤å¼ã³åºãã
- å ¨ä½ã§ \(O(n^2)\) æéã«ãªã£ã¦ãããã¨ã示ãããã
- \(g(n)\) ã®è¨ç®éã¯å®éã«ã¯ \(\Theta(n\log(n))\) æéã ãããããããªãã¯ã¾ã ç¥ããªãã
ããããç¶æ³ã®ã¨ãã\(g(n)\) ã®è¨ç®éã \(\Theta(n^2)\) ããå°ãããã¨ã示ãã¦ãã\(f(n)\) å´ãæ¯é çã«ãªã£ã¦ãã¾ãã¾ãã \(g(n)\) ã®è¨ç®éã \(\Theta(n^2)\) ãã大ãããªã㨠\(g(n)\) å´ãæ¯é çã«ãªãå ¨ä½ã§ \(O(n^2)\) ã«ãªããªããªã£ã¦ãã¾ãã®ã§ãããã§ãªããã¨ãã示ãã°ããã§ãã ããã§ã\(g(n)\) ã \(O(n^2)\) æéã§è¨ç®ã§ããã®ã楽ã«ç¤ºããã®ãªããå³å¯ã«ã¯è§£æããã«ã\(g(n)\) 㯠\(O(n^2)\) æéã§è¨ç®ã§ãããã¨ãè¨ã£ã¦ãã¾ã£ã¦ããããã§ãã ãã®ç¶æ³ã§ã\(g(n)\) ã®è¨ç®é㯠\(\Theta(n^2)\) æéã§ããã¨ã¯è¨ããªãã®ã§ã\(O\) ã使ã£ã¦ãµããã¡ãªãããããããã§ããã
\(\Omega\) è¨æ³ (big-Omega)
éã«ãä¸ããæãããå±é¢ãããã¾ãã ãã¨ãã°ã\(O(n^3)\) æéã¨è¨ããã¦ããæ¢åã®ã¢ã«ã´ãªãºã ãæ¹åãã\(\Theta(n^2)\) æéã®ã¢ã«ã´ãªãºã ãæãã¤ããã¨ãã¾ãã ããããåè¿°ã®éã \(O\) ã¯ä¸ããæãã¦ããã ããªã®ã§ãå®éã«ã¯ï¼è¨ç®é解æãé£ããã¦ç¤ºãã¦ããªãã£ããã天æããè¦ãã°è§£æã§ãããªã©ã§ï¼\(\Theta(n)\) æéã®ã¢ã«ã´ãªãºã ã ã£ãã¨ãããã¨ã¯ãããã¾ã*21ã
ãã£ã¦ããæ¢åã®ã¢ã«ã´ãªãºã ã®ãªã¼ãã¼ã¯å°ãªãã¨ã \(n^3\) ãªã®ã§ãèªåã® \(O(n^2)\) ã®æ¹ãåªãã¦ãããã¨ãã£ãå½¢ã§ç¤ºãå¿ è¦ãããããã§ãããããããå±é¢ã§ä½¿ãè¨æ³ã \(\Omega\) ã§ãã
ãªãã¡ããã®è¨ç®é㯠\(O(n^3)\) ã§ãã¼ã¨è¨ã£ãã¨ããå®éã«ææªæ \(\Theta(n^3)\) ãããããªãã§ããã©ãè¨æ³ããã¯ãã®æ å ±ã¯å¾ãããªãããã§ãã*22ã
ä¾ï¼ã¯ãªãã¯ãã¦å±éï¼
\(1\) ãã \(n\) ã¾ã§ã®æ´æ°ã®ç´ æ°å¤å®ãããç¶æ³ãèãã¾ãã \(i\) ã®ç´ æ°å¤å®ã¯ \(\sqrt{i}\) ã¾ã§ã®æ´æ°ã§å²ããã調ã¹ãã°ã§ããã®ã§ã\(O(n\sqrt{n})\) æéã§ã§ãã¾ãã ããããç·å½¢ç¯©ã¨ããã¢ã«ã´ãªãºã ãç¨ããã° \(O(n)\) æéã§ã§ãã¾ãããã大æ¹åã§ããï¼ã¨è¨ã£ã人ãããã¨ãã¾ãã
ãããããããèãããã¾ãã 4 以ä¸ã®å¶æ°ã«ã¤ãã¦ã¯ 2 ã§å²ã£ãæç¹ã§ç´ æ°ã§ãªããã¨ããããã¾ãã9 以ä¸ã® 3 ã®åæ°ã«ã¤ãã¦ã 3 ã§å²ã£ãæç¹ã§ãããã¾ãããã®ããã«ãå°ããæ°ã§å²ã£ãæç¹ã§ç´ æ°ã§ãªãã¨ãããæ°ã¯ããããããããã§ãã ããèããã¨ãå®ã¯ãã£ã¨è¨ç®éã¯å°ãªãã®ã§ã¯ï¼ã¨ããç念ãåºã¦ãã¾ããã
å®éã«ã¯ã\(n\) 以ä¸ã®ç´ æ°ã®åæ°ã¯ç´ æ°å®çãã \(\Theta(n/\log(n))\) åã§ãããç´ æ° \(i\) ã«é¢ãã¦ã¯ \(\Theta(\sqrt{i})\) æéããããã¨ãªã©ããã\(\Omega(n\sqrt{n}/\log(n))\) æéã¯ãããã¨è¨ããã®ã§ãæ¹åããã¨è¨ãããã§ãã
ããããããã示ããã«æ¹åããã¨ä¸»å¼µããã®ã¯ãæç´ã®ã¢ã«ã´ãªãºã ã«å¯¾ãã¦ä¸èª å®ããã§ãã
è£è¶³ï¼Atkin ã®ç¯©ã¯ \(o(n)\) æéã ãï¼ã¨è¨ãããããªæ°ããã¾ãããããããç¨ã㦠\(n\) åã®å¤å®ãããã«ã¯ãã¯ã \(\Omega(n)\) æéããã£ã¦ãã¾ãããã§ãã
ï¼ã¯ãªãã¯ãã¦å±éé¨åãããï¼
ã¾ãã³ããã§ããã以ä¸ãæãç«ã¤ã¨ãã\(f(n)\in \Omega(g(n))\) ã¨æ¸ãã\(f(n)\) ã¯å°ãªãã¨ã \(g(n)\) ã®ãªã¼ãã¼ã§ããã¨è¨ãã¾ãã
ããå®æ° \(n_0\) ãåºå®ãã¾ãã ããã«å¯¾ããããå®æ° \(c_L\gt 0\) ãåå¨ãã\(n_0\) 以ä¸ã®ãã¹ã¦ã® \(n\) ã«ã¤ãã¦æ¬¡ã®å¼ãæãç«ã¤ï¼ \[ 0\le c_L\cdot g(n)\le f(n) \]
ãããããã®ã¢ã«ã´ãªãºã 㯠\(O(f(n))\) æéã§ããã¨è¨ã£ãã¨ãã常㫠\(O(f(n))\) æéã ã¨è¨ã£ã¦ããããã«èãããã®ã§ãworst case 㧠\(O(f(n))\) æéã¨è§£éããã¾ããã¾ããããããã®ã¢ã«ã´ãªãºã 㯠\(\Omega(f(n))\) æéã§ããã¨è¨ã£ãã¨ãã常㫠\(\Omega(f(n))\) æéã ã¨è¨ã£ã¦ããããã«èãããã®ã§ãbest case ã§ã \(\Omega(f(n))\) æéã ã¨è§£éãããæ°ããã¾ãï¼æèã«ãããããï¼ãä¼ããããã¨ãããã§ãªããªãããã¨ãã°ãworst case ã§ã¯ \(\Omega(f(n))\) æéã«ãªã£ã¦ãã¾ããã®ãããªè¨ãæ¹ãããã¨èª¤è§£ããªãã§ãããã
ã¾ãã\(f(n)\in O(g(n))\) ã㤠\(f(n)\in\Omega(g(n))\) ãè¨ããã°ã\(f(n)\in\Theta(g(n))\) ãè¨ãã¾ãã ç·´ç¿ã¨ãã¦ã証æãã¦ã¿ãã¨ããããã§ã*23ã
Note: \(\Omega\) ã«ã¤ãã¦ã¯ãè¨ç®æ©ç§å¦ä»¥å¤ã§ã¯ããã¨ç°ãªãå®ç¾©ãç¨ãããããããªã®ã§ã注æãå¿ è¦ããããã¾ãããè¨ç®æ©ç§å¦ã®æèã§ãäºç¨®ã¯ãã£ã¦ã\(\stackrel{\infty}{\Omega}\) ãªã©ãããããã§ãã
\(o\) è¨æ³ (little-o)
ããé¢æ°ã«å¯¾ãã¦ãããã®é¢æ°ã¯ããããããªã¼ãã¼ãçã«å°ãããã¼ãã¨ããã®ãè¨ããããã¨ã¯ããã¾ããã ã\(T(n)\) æéã®ã¢ã«ã´ãªãºã ãç¥ã£ã¦ããããããããããªã¼ãã¼ãæ¹åã§ããã®ãï¼ãã¨ãã£ãç¶æ³ãªã©ã§ãã
ãã®ããã®è¨æ³ã \(o\) ã§ã*24ãä¸è¨ã®ç¶æ³ã§ã¯ã\(o(T(n))\) æéã§è§£ããï¼ãã¨ãè¨ãã¾ãã ããã¯ã³ããã§ã¯ãªãã¦ã以ä¸ãæãç«ã¤ã¨ãã\(f(n) \in o(g(n))\) ã¨æ¸ãã¾ãã
ä»»æã® \(c_U\gt 0\) ã«å¯¾ãã¦ããã \(n_0\) ãåå¨ãã\(n_0\) 以ä¸ã®ãã¹ã¦ã® \(n\) ã«ã¤ãã¦æ¬¡ã®å¼ãæãç«ã¤ï¼ \[ 0\le f(n) \lt c_U\cdot g(n) \]
\(O\) ã§ã¯ãã \(c_U\) ã«å¯¾ãã¦æããã°ããã£ãã§ããã\(o\) ã§ã¯ä»»æã® \(c_U\) ã«å¯¾ãã¦æããå¿ è¦ãããã¾ã*25ã
ãã¨ãã°ã次ã®å¼ãæãç«ã¡ã¾ãã \[ \begin{aligned} n^2 &\in o(n^3) \\ n^2+3n &\notin o(n^2) \\ n^4 &\notin o(n^3) \\ o(2n) &= o(n+1) = o(n) \end{aligned} \]
証æï¼ã¯ãªãã¯ãã¦å±éï¼
- \(n^2 \in o(n^3)\)
ä»»æã® \(c_U\gt 0\) ã«å¯¾ãã¦ãåå \(n\) ã大ãããã° \(0\le n^2 \lt c_U\cdot n^3\) ã示ãå¿ è¦ãããã 以ååæ§ \(n=0\) ã¯é¤å¤ã§ããã®ã§ã\(0\le \frac{1}{n}\lt c_U\)ã
\(n\gt \frac{1}{c_U}\) ã¨ããã°æãç«ã¤ã®ã§ã\(n_0=\frac{1}{c_U}+1\) ã¨ãã¦ããã°ããã
ä¾ã§è£è¶³ï¼\(c_U = 0.00001\) ã®ã¨ãã\(n=1\) ã \(n=100000\) ã§ã¯ä¸å¼ã¯æãç«ããªããã\(n=100001\) ã¨ããã°æãç«ã¤ã®ã§ãããã \(n_0\) ã¨ããã°ããã
- \(n^2+3n \notin o(n^2)\)
ãã \(c_U\gt 0\) ã«å¯¾ãã¦ã¯ããã \(n\le n_0\) ã«å¯¾ã㦠\(0\le n^2+3n \lt c_U\cdot n^2\) ãæãç«ããªããã¨ã示ãå¿ è¦ãããã
ãã¨ãã°ã\(c_U=1\) ã«å¯¾ãã¦ã\(n\ge 0\) ã®ã¨ã常㫠\(n^2+3n\ge n^2\) ãªã®ã§ãä¸å¼ãæãç«ããããã㪠\(n_0\) ã¯åå¨ããªãã\(\square\)
ï¼ã¯ãªãã¯ãã¦å±éé¨åãããï¼
\(\omega\) è¨æ³ (little-omega)
éã«ããçã«å¤§ãããã¼ãã¨ããã®ã示ãããã¨ãã«ã¯ \(\omega\) ã使ãã¾ãã ããã®ã¢ã«ã´ãªãºã 㯠\(T(n)\) æéã ããæ¢åã®ã¯ \(\omega(T(n))\) æéãªã®ã§ãæ¹åãããã¼ãã¨ãã£ãå ·åã§ãã
以ä¸ãæãç«ã¤ã¨ãã\(f(n) \in \omega(g(n))\) ã¨æ¸ãã¾ãã
ä»»æã® \(c_L\gt 0\) ã«å¯¾ãã¦ããã \(n_0\) ãåå¨ãã\(n_0\) 以ä¸ã®ãã¹ã¦ã® \(n\) ã«ã¤ãã¦æ¬¡ã®å¼ãæãç«ã¤ï¼ \[ 0\le c_L\cdot g(n) \lt f(n) \]
ãã¨ãã°ã次ã®å¼ãæãç«ã¡ã¾ãã \[ \begin{aligned} n^2 &\notin \omega(n^3) \\ n^2+3n &\notin \omega(n^2) \\ n^4 &\in \omega(n^3) \\ \omega(2n) &= \omega(n+1) = \omega(n) \end{aligned} \]
è¨æ³ã¯ãã® 5 種é¡ã§ãã5 種é¡ã®çå·ã»ä¸çå· \(=, \le, \ge, \lt, \gt\) 㨠\(\Theta, O, \Omega, o, \omega\) ã対å¿ã¥ãã¦èªèããã¨ããããã§ãï¼ãªã¼ãã¼ãåãã¨ãããªã¼ãã¼ãçã«å°ããã¨ããã£ãå ·åã§ãï¼ã
è¨å·ã®æ¿«ç¨ã«ã¤ãã¦
æ¿«ç¨ ã«ã¤ãã¦è§¦ãã¾ãã
\(2n + o(n) = O(n)\) ã¨ãã£ããããªä½¿ãæ¹ã¯ããã¨é »ç¹ã«ãªããã¾ãã ããã¯ããä»»æã® \(f(n)\in o(n)\) ã«å¯¾ãã¦ããã \(g(n)\in O(n)\) ãåå¨ã㦠\(2n+f(n)=g(n)\) ãæãç«ã¤ãã¨ãã£ãããã«è§£éããã¾ãã
çåï¼ã¯ãªãã¯ãã¦å±éï¼
ä¸è¨ã§ \(2n\) ã®ãããªé ããªãã\(o(n) = O(n)\) ã¨æ¸ãã¦ãã¾ã£ãå ´åã®ãã¨ãèãã¾ãã ä¸è¨ã®è§£éã§ããã°ããã¯æãç«ã¡ã¾ãããéåã¨ãã¦ã¯æããã«çãããªãã®ã§æãç«ã¡ã¾ããã
ããããææ§æ§ã®ããå¼ã¯æ¸ããªãæ¹ãç¡é£ããã§ãã
ï¼ã¯ãªãã¯ãã¦å±éé¨åãããï¼
\(3n = \Theta(n)\) ã®ãããªä½¿ããæ¹ããã¾ããããããã\(3n\in\Theta(n)\) ã¨ãã£ãããã«è§£éããã¾ãã ä¸è¨ã®ä¾ã§ãã§ã«å·¦è¾ºãããªã¼ãã¼ã«é¢ããè¨æ³ãæ¶å»ãããå¾ã ã¨è¦ããã¨ãã§ãã¾ãã
æéè¨ç®éã®æèã§ã¯æ¯é é ã®å®æ°åãæ°ã«ããªããã¨ãå¤ãã®ã§ \(O(n)\) æéãªã©ã¨ãããã¨ãå¤ãã§ããã空éè¨ç®éã®æèã§ã¯ã使ç¨ãããããæ°ã詳細ã«è¦ç©ããå ´åãããã¾ãã ããããã¨ãããã¨ãã° \(n+cn\cdot\frac{\log(\log(n))}{\log(n)} = n+o(n)\) bits ã¨ãã\(4n\log(n)+o(n\log(n))\) bits ã¨ãè¨ã£ãããã¾ãã
ç°¡æ½ãã¼ã¿æ§é ããã£ã¦ããã®ã§ãã³ã¼ãã£ã³ã°ã¤ã³ã¿ãã¥ã¼ã§ç©ºéè¨ç®éãèãããã¨ã4nlogn+o(nlogn) bitsã§ãã¿ããã«çã㦠ã¯ï¼ï¼ ã£ã¦ãªã¢ã¯ã·ã§ã³ããããã¨ãããã¾ã
— ä½å»º (@smiken_61) 2019å¹´7æ3æ¥
空éã«å³ãããã¼ã¿æ§é ã®ç 究ããã¦ãã人以å¤ã«ã¯é¦´æã¿ããªãããããã¾ããã
ã¾ãããé·ã \(n\) ã®å ¥åãååãã¤ã«åãã¦åã åæ§ã®ã¢ã«ã´ãªãºã ã§è§£ãããã®çãã«åºã¥ã㦠\(O(n)\) æéããã¦å åé¡ã解ããã¨ãã£ãå½¢å¼ã®ã¢ã«ã´ãªãºã ã¯ããããã¾ãã ãã®è¨ç®éã \(T(n)\) ã¨ããã\(T(n) = 2\cdot T(n/2)+O(n)\) ã¨ãã£ãå½¢ã®æ¹ç¨å¼ãç«ã¦ãããã¾ãã\(T(1)=O(1)\) ã¨ãã¦ãããã解ã㨠\(T(n)=O(n\log(n))\) ã¨ãªãã¾ãã
ãããã誤解ã»èª¤ç¨
ãããã誤解ã誤ç¨ã«ã¤ãã¦ããæ°æã¡è¡¨æããã¾ãã
ããã¯æ£ãããªãã§ããã¼ã¨ããã®ãæ¸ãã¦ããã¾ãããããæ¬ç·¨ã¨ãã説ãããã¾ãã
å¼ç¨ã®å½¢å¼ã§æ¸ããã¦ããé¨åã誤解ã®é¨åã§ããã®å¾ããæ°æã¡è¡¨æã§ãã
ãã®è¨äºã§ã¯ããã¦è¨ç®éãç¥ã£ã人ã¯ããããã®èª¤ç¨ãè¦ããã¨ããªããããããªãã®ã§å°ãããããã¾ãããããããæã人ããããã ãªãããããã®æ°æã¡ã§ãã£ããçºãã¦ããããããããããã§ãã
\(O(f(n))\) ã¨ã¯ \(f(n)\) ã«æ¯ä¾ããè¨ç®éã¨ããæå³ã§ã
\(O\) ã¯è¨ç®éã¨ã¯éããªãã¨ããã®ãã¨ãããã念æ¼ããã¦ããã¾ãã
ä¸è¨ã®éãã\(O(f(n))\) 㯠\(f(n)\) ã®å®æ°åã§ä¸ããæããããã¨ããã ãã§ãæ¯ä¾ããã¨ã¯è¨ã£ã¦ãã¾ããã
\(\Theta(f(n))\) ã«é¢ãã¦ããå®æ°åã§ãµã³ãã¤ããã§ããã¨è¨ã£ã¦ããã ãã§ãæ¯ä¾ããã¨ã¯è¨ã£ã¦ãã¾ããã
ï¼éæ¯é
é
ãç¡è¦ããã°æ¯ä¾ããã¨è¨ã£ã¦ããæ°ãããã®ã§ãç®ããããç«ã¦ããããã§ãããæ£ç¢ºã§ã¯ãªãæ°ããã¾ãï¼
ããæããã«åã§ãã\(n\) ãå¶æ°ãªã \(2n\)ãå¥æ°ãªã \(3n\)ããªã©ã¨ãã¦å®ç¾©ãã \(f(n)\) ãåä¾ã¨ãã¦ããã¾ããç§ãæªãã£ãã§ãï¼ãºããï¼ã
ãã¦ãããçéã§ã¯ã\(O\) ã \(\Theta\) ã®æå³ã ã¨åéããã¦ãã人ãå¤ãå°è±¡ã§ãã ç¹ã«ç«¶æããã°ã©ãã³ã°ã®æèã§ãå¹çãååãããè¦ç©ããã«ã¯ãè¨ç®éãä¸ããæããã°ååãªãã¨ãå¤ãã\(O\) ã使ãã°ãããã¨ãå¤ãã§ãã ç¹ã«ã解æãèªæãªä¾ã«ããã¦ã¯ãåã«ããã㯠\(O(n^2)\) æéã§ããã¨è¨ã£ãã¨ããå®éã« \(\Theta(n^2)\) æéã§ãããï¼ä¸ãããã¡ããã¨æãããã¦ããï¼ãã¨ãå¤ãã§ãã ãªã®ã§ãããããä¾ãè¦ã¦ãµã³ãã¤ããç¨ã®è¨æ³ã ã¨åéãããã®ããªï¼ã¨æã£ãããã¾ãã
è¨ç®éã¨ã¯å®è¡æéãè¦ç©ãã大éæãªææ¨ã§ã
è¨ç®éã®è¡¨ãéã«å¤§éæãªææ¨ã§ãã \(O\) ã使ããã¨ãå¤ãã§ãããè¨ç®éèªä½ã大éæã§ããã¨ããã®ã¯èªå¼ããããããªæ°ããã¾ãã
ï¼\(O(f(n))\) ãæãã¦ï¼ãªã¼ãã¼ \(f(n)\) ã¨èªã
\(O\) 㨠\(\Theta\) ãæ··åãã人ã«å¤ããã¤ã§ãããé«ã \(f(n)\) ã®ãªã¼ãã¼ã§ã¯ãªããã¡ããã© \(f(n)\) ã®ãªã¼ãã¼ã¨è¨ã£ã¦ããããã«èããã¾ãã
åã«ããªã¼ \(f(n)\)ãã¨ããããã°ãªã¼ \(f(n)\)ãã¨èªãã®ãããã¯ãã§ããè±èªã§ã¯ \(f(n)\) 㯠f of n ã¨èªãã®ã§ãbig-O of f of n ã¨èªãã ãããããã§ãã
å°ãªãã¨ã \(O(n^2)\) ã®ãªã¼ãã¼ã
ãå°ãªãã¨ã 1000 å以ä¸ã ãã¨ãã主張ãä¸æçãªã®ã¨åãã§ãæªããã§ãã\(\Omega(n^2)\) ã ã¨è¨ãããã£ã¦ããå¯è½æ§ãé«ãã§ãã
\(O(n^2)\) ãªã®ã§å¹çããããªãããã® \(O(n\log(n))\) ã®æ¹ããã
ä¸ã§ \(\Omega\) ãå®ç¾©ããéã«ãè¿°ã¹ã¾ãããã誤ã£ã使ãæ¹ã§ããä¸è¨ã®ããã« \(\Theta\) ã®æå³ã§ä½¿ã£ã¦ãããã ãããªãã¨ããæ°ããã¾ãã...
ãã¨ãã°ãããã®åå㯠1000 å以ä¸ãªã®ã§å®ããªãããã® 400 å以ä¸ã®ååã®æ¹ãå®ããã¨è¨ãããã¨ãããå®éã«ã¯åè ã®åå㯠200 åã§ãå¾è ã®ååã 300 åã§ããç¶æ³ãããå¾ãã®ã§ã¯ï¼ãã¨ããæ°æã¡ã«ãªãã®ã¨ä¼¼ãéåæãããã¾ãã ãã¡ãããåè ã®æ¹ãé«ãç¶æ³ãå½ç¶ããå¾ã¾ããã大å°æ¯è¼ãã¡ããã¨ã§ããæ å ±ãæã£ã¦ããªã主張ã ã¨ãããã¨ã§ããã
çè«çã«ã¯ \(\Theta(n\log(n))\) ã®ã¢ã«ã´ãªãºã ã®æ¹ãå¹çãããããå®æ¸¬ã§ã¯ \(\Theta(n^2)\) ã®æ¹ãéããã¨ããã
å¥ã«ãçè«çã« \(\Theta(n\log(n))\) ã®æ¹ã \(\Theta(n^2)\) ããå¹çããããã¨ã¯ä¸»å¼µãã¦ãã¾ããã \(\Theta\) ãªã©ã®è¨æ³ã¯ \(n\) ãåå大ããã¨ãã®è©±ã§ã\(n\) ãå°ããã¨ãã«é¢ãã¦ã¯ç¹ã«ä¸»å¼µãã¦ãã¾ãã*26ã
å®éãå°ãã \(n\) ã«å¯¾ãã¦éæ¯é é ãå®æ°åã®å½±é¿ãç¡è¦ã§ããªããã¨èªä½ã¯ããããã¾ãã ã©ã®ç¨åº¦å¤§ãããã°ãããã¯ç¶æ³ã«ããã¾ãã
C++ ã®ããããæ¨æºã©ã¤ãã©ãªã®å®è£ ã§ã¯ããµã¤ãºãå°ããã¨ãã«ã¯æ¿å ¥ã½ã¼ãï¼å°å ¥ã«ããã½ã¼ãã¢ã«ã´ãªãºã 2ï¼ãå®è¡ããããã¦ããããã§ãã
è¨ç®é㯠\(O(\bullet)\) ã®å½¢ã§è¡¨ãã»\(O(\bullet)\) ã¯è¨ç®éã®ããã®è¨æ³ã§ãã
ä¸è¨ã§è§¦ããéãã§ããã©ã¡ãããããããããã§ã¯ãªãã§ãã
\(\Theta(1)\) ã¨ããã®ã¯å°ããå®æ°ã§ããï¼ã¨ããèªèï¼
ãã¨ãã° \(2+\sin(n)=\Theta(1)\) ãªã®ã§ \(\Theta(1)\) ãå®æ°ã¨ããã®èªä½æªããæ°ããã¾ãããèªå¼ã®ãªãæèã§ã¯ãã使ãããããã¾ãã
ãã¦ãå®æ°ã¨ããã®ã¯å°ããæ°ã¨ããæå³ã§ã¯ãªãã§ãã ãã¨ãã°ãããã®ã¢ã«ã´ãªãºã ã§ã¯ã\(\lfloor\log_2(n)\rfloor+\Theta(1)\) åã®é¢æ°å¼ã³åºããè¡ãã¾ããã¨ããä»æ§ã®ã¢ã«ã´ãªãºã ãå®éã«ã¯ \(\log_2(n)+100000000\) åã®é¢æ°å¼ã³åºããããã¨ãã¦ãã表è¨ã¨ãã¦ã¯æ£ããã§ãã ä»æ§ã«å¯¾ãã¦æå¥ãè¨ãã®ã¯å¦¥å½ã ã¨æãã¾ãã...*27
ãã¨ãã°ããäºåæ¢ç´¢ã§ \(\log_2(n)\) åæ¯è¼ããããã¨å¿µã®ããå¢çä»è¿ã 1 ã¤èª¿ã¹ã¦ããããäºåæ¢ç´¢ã§ \(\log_2(n)\) åæ¯è¼ããããã¨å¿µã®ããå¢çä»è¿ã 100000000 ã¤èª¿ã¹ã¦ããããªã©ã¯ãã©ã¡ãã \(\log_2(n)+O(1)\) ã¨è¨ãã¦ãã¾ãããã§ãã
å®éãC++ ã® std::lower_bound
ãªã©ã®é¢æ°ã¯ãé«ã
\(\log_2(n)+O(1)\) åã®æ¯è¼ãè¡ãã¨è¦å®ããã¦ããããããããªå¦çç³»ï¼ï¼ã§ã¯ããã¡ããã¡ããªãã¨ã«ãªãããã ãªãã¨ããæ°ããã¾ãã
\(\Theta(1)\) æéã¨ããã®ã¯ä¸çºã§çããåºãã¢ã«ã´ãªãºã ã§ãã
ãã¨ãã°ã4 層ã®ãã¼ã¿æ§é ãç¨æãã¦ãããããããç¨ãã¦ï¼åã ãå®æ°æéã®è¨ç®ããã¦ï¼çããè¿ãã®ã¯ \(\Theta(1)\) æéã¢ã«ã´ãªãºã ã§ãããä¸çºã§çããåºãã¦ããã¨ã¯è¨ããªãããã§ãã
\(O(1)\) ã¨ã¯å®æ°ã®ãã¨ã§ãã
ä¸è¨ã® \(\Theta(1)\) ã§ã触ãã¾ããããããã¨ã¯å¥ã«ã\(O(1/n^2)\) ã®ãããªéåãããã¾ãã å®éãã\(O(n)\) æéãããå¦çãçºçããåæ°ã®æå¾ å¤ã \(O(1/n^2)\) åã§ãããã¨ãã£ã解æãããç¶æ³ãããã¾ã*28ã
ãã¡ãããè¨ç®éãå®æ°ã§ãããããªã¨ãã«ã\(O(1)\) æéã§ããã¨ä¸»å¼µããã®ã¯ï¼ä¸ããæãã¦ããã ããªã®ã§ï¼åé¡ããã¾ããããããæ··åãã¦ãã¾ãã¨å°ãããããã¾ããã
ãã®ã½ã¼ã¹ã³ã¼ãã®è¨ç®é㯠\(\Theta(n)\) æéã§ããå¹çãæªã
ãã¨ãã°ã\(1\) ä»¥ä¸ \(n\) 以ä¸ã®æ´æ°ã®ç·åãæ±ããé¢æ°ãæ¸ããã¨ãã¾ãã
int sum(int n) { int res = 0; for (int i = 1; i <= n; ++i) res += i; return res; }
ããã¯ãClang ã§ã³ã³ãã¤ã«ããã¨ã«ã¼ãã®ãªãå½¢ã«æé©åããã¾ãã
ã¢ã»ã³ããªã®è§£èª¬ã¯ããã§ã¯å²æãã¾ããã\(1+2+\dots+n = \frac{n(n+1)}{2}\) ãç¨ããæé©åããã¦ããã¨ãããã¨ã§ããã
ãªã®ã§ãããå³å¯ã«ã¯ï¼ç¹ã«å¹çãæªãã¨ä¸»å¼µããéã«ã¯ï¼ãã½ã¼ã¹ã³ã¼ãã§è°è«ããã®ãæ£ãããªãã±ã¼ã¹ãããã¾ãã
å¤ãã®å ´åã¯ã³ã³ãã¤ã©ã¡ããããªã¼ãã¼ãæ¹åãã¦ããããã¨ã¯ãªãæ°ãããã®ã§åé¡ãªãããã§ããããã³ã³ãã¤ã©ã¡ããã¯ãªã¼ãã¼ã®æé©åãããªããã¨ããæãè¾¼ã¿ã¯ãããªããããããªããã¨ãããã¨ã§ã*29ã
ãã³ã³ãã¤ã©ã¡ããã¯ãªã¼ãã¼ãæªåãããªããã¨ããæãè¾¼ã¿ããã¦ããã¨ããææãããã¾ããæªåãããªãã§ã»ããã§ãã
\(\log(n)\) ã¯å®æ°ã»\(\alpha(n)\) ã¯ç¾å®çãªç¯å²ã§ 5 以ä¸ç¨åº¦ãªã®ã§å®æ°
\(\alpha(n)\) 㯠inverse Ackermann function ã¨å¼ã°ãããå¢å ãé常ã«é ãé¢æ°ã§ãã åã°ã®ã£ã°ã§è¨ããã¦ããã¨ãæãã¾ã*30ããå®æ°ã§ã¯ããªããããã
ã¨ã¯ããã\(\log(n)\) ã \(\alpha(n)\) 㯠\(n\) ã¨æ¯ã¹ã¦é常ã«é ãå¢å ããã®ã¯äºå®ã§ãã ãã®ããã®è¨æ³ã¨ãã¦ãsoft-O è¨æ³ã¨ããã®ãããã¾ãã\(f(n)=\widetilde{O}(g(n))\) ã¯ããã \(k\) ã«å¯¾ã㦠\(f(n)=O(g(n)\log(g(n))^k)\) ã¨ãªããã¨ãæå³ãã¾ãã
ç¹ã«ã\(O(n\log(n)\log(\log(n)))\) ã®ãã㪠log ãç ©éã«ãªããã®ãããã®ç ©éãªé¨åã®è©³ç´°ãªè§£æãé£ããã¨ãï¼ãããã¯æ¬è³ªçã«è°è«ãããã®ã¯ããã§ã¯ãªãã¨ãï¼ãªã©ã«ã\(\widetilde{O}(n)\) ã®ããã«æ¸ãããããã¾ãã
åæ§ã«ã\(2^n\) ã«å¯¾ã㦠\(n\) ã®å¤é å¼ã¯ååå°ããã®ã§ãããããé ãé½ã«èæ ®ããããªãã¨ããããã¾ãã ããããã¨ãã«ã\(n\) ã®å¤é å¼ã \(\poly(n)\) ã¨æ¸ããã¨ãããã¾ãã\(n^{O(1)}\) ã®æå³ã ã¨æãã¾ã*31ã \(O(2^n\cdot \poly(n))\) ã®ããã«ä½¿ãã¾ãã
ã¾ãã\(\log(n)\) ã®å¤é å¼ã \(\polylog(n)\) ã¨æ¸ããã¨ãããã¾ãã\(n\cdot\log(n)\log(\log(n))\in O(n\cdot\polylog(n))=\widetilde{O}(n)\) ã®ãããªæãã§ãã
ã¨ããã§ã\(\alpha\) ã¯ç«¶ããçéã§ã¯ãã¾ãç¥ããã¦ããªãæããã£ãã®ã§ã調ã¹ã¦è¨äºãæ¸ãã¾ããã
\(64\) åé«éåããã®ã§ \(O(n/64)\)ã\(10^{10}\) åã®æ¼ç®ãããã®ã§ \(O(10^{10})\) ãªã©
å®æ°åã¯ç¡è¦ããã®ã§ããããã \(O(n)\), \(O(1)\) ã¨åãæå³ã§ãã è¨æ³ã¨ãã¦ã¯ã10000 åå¹çãæªããã¦ã \(O(n/64)\) ã¨æ¸ãããã1 åããæ¼ç®ããªãã¦ã \(O(10^{10})\) ã¨æ¸ãã¾ããæ¸ãæå³ã¯ãªãã§ããã©...
ããã¯ããã¨ãã¦ã表ç¾ããããã¨ã«å¯¾ãã¦è¨æ³ãä¸é©åããã§ãã \(64\) åé«éåããå ´åã¯ããå ã®è¨ç®éã \(T(n)\) ã¨ã㦠\(\frac{1}{64}T(n)\) ã«ãªã£ããã¨ãè¨ãã®ãã¢ãªãªæ°ããã¾ãï¼*32
ã¾ããå®éã« \(10^{10}\) åã®æ¼ç®åæ°ã§ãããã¨ãç¥ã£ã¦ããã®ãªãã\(O\) ã使ãå¿ è¦ã¯ãªãã§ããã ãã®èª¤ç¨ãåºã¦ããæèã¯ããå ¥åã¨ãã¦ãããã \(n\) ã®æ大å¤ã¯ \(10^5\) ã§ã\(O(n^2)\) ã®è¨ç®éãªã®ã§ã\(n=10^5\) ãä»£å ¥ã㦠\(O(10^{10})\)ãã®ãããªã³ã£ããç¨æ³*33ãªã®ã§ãããä¸åå¼·ãªäººã«ã¨ã£ã¦ã¯èªç¶ã«è¦ãããããã§ãï¼
ãªããå½ç¶ã\(n\) ãã©ãã ãå¢ãã¦ãå®æ°åã®è¨ç®ã§æ¸ãã®ã§ããã°ã\(O(10^{10})\) ã¨æ¸ãã¦ãæ£ããã§ãã\(O(1)\) ã¨æ¸ãã°ããã§ããã©ãèªç±ãªã®ã§ã
ã¨ããã§ãbool ã®é ååã ã«å¯¾ãã¦è«çæ¼ç®ãè¡ã代ããã« 64 bit æ´æ°ãç¨ã㦠64 åã® bit ããããæ¼ç®ã§ä¸¦åã«å¦çããã®ã 64 åï¼å®æ°åï¼é«éåã¨è¨ãã®ã¯å¦¥å½ãï¼ã¨ããçåããããããã«é¢ãã è¨äº ãæ¸ãã¾ãããå®ã¯ \(\log(n)\) åã®é«éåã¨ããè¦æ¹ãã§ãããã§ãã
ãã®ã¢ã«ã´ãªãºã ããã®ã¢ã«ã´ãªãºã ã \(\Theta(n)\) æéãªã®ã§è¨ç®éã¯åãã»ã«ã¼ãåæ°ãååã«ãããã©è¨ç®éã¯åãããªã©
å®éã®è¨ç®éã¨ããã®ãªã¼ãã¼ã¯å¥ç©ã§ããããªã¼ãã¼ã¯åãããããã®è¨ãæ¹ã妥å½ã§ããããã
ãªã¼ãã¼ã§è¨ãã°ãã1 åãã¼ã¸ã½ã¼ããè¡ããããã·ã£ããã«ãã¦ãããã¼ã¸ã½ã¼ããè¡ãã®ã 500 ä¸åç¹°ãè¿ãããåãè¦ãç®ã«ãªã£ã¦ãã¾ãã¾ããããããã®è¨ç®éï¼ãªã¼ãã¼ã®ãã¨ã§ã¯ãªãã³ã¹ãã®ç·åï¼ã¯ç°ãªãã¾ããã
ããã \(\varepsilon\gt 0\) ãåå¨ã㦠\(O(n^{2-\varepsilon})\) æéã§è§£ãããã ã\(o(n^2)\) æéã§è§£ãããã¨è¨ãæãã¦ãã¾ã
ãããã¯åå¤ã§ã¯ãªãã§ãã
ãã¨ãã°ã\(\Theta(n^2/\log(n))\) æéã§è§£ããã¨ãã¾ããããã¯ã\(o(n^2)\) æéã§è§£ãããã¨ã«ã¯ãªãã¾ããã\(n^2/\log(n)\in O(n^{2-\varepsilon})\) ãªã \(\varepsilon\gt 0\) ã¯åå¨ãã¾ããã ãã¨ãã° \(n^{1.99999999}\) ããã \(n^2/\log(n)\) ã®æ¹ãå¢å 度åãã大ããã§ãã
\(\varepsilon\) ãã©ãã ãå°ãããã¦ãã\(n\) ãåå大ãããªã㨠\(n^2/\log(n)\) ã®æ¹ã大ãããªã£ã¦ãã¾ãããã\(n^{2-\varepsilon}\) æéã§è§£ãããã¨ã«ã¯ãªããªãããã§ããã
\(2^{\Theta(n)} = \Theta(2^n)\)
ãã¨ãã°ã\(2n\in\Theta(n)\) ã左辺ã«ä»£å ¥ããã¨ã\(4^n\) ã«ãªãã¾ããããã㯠\(\Theta(2^n)\) ã§ã¯ããã¾ããã
åæ§ã« \(2^{o(n)}\neq o(2^n)\) ãæãç«ã¡ã¾ãããããã®è¨¼æã¨ãªããããªé¢æ°ãæãã¦ã¿ã¦ãã ããã \(2n\notin o(n)\) ãªã®ã§ãä¸è¨ã®ä¾ããã®ã¾ã¾ä½¿ããã¨ã¯ã§ããªããã¨ã«æ³¨æãã¦ãã ããã
å®å ã¯ãã¡ãã§ã*34ï¼
o(2^n) 㨠2^{o(n)} ã£ã¦å¥ï¼
— ãã³ã¡ããððð¦ (@rsk0315_h4x) 2021å¹´4æ24æ¥
\(\phi(n)\) 㯠\(O(\sqrt{n})\)
ããã¯èª¤ç¨ã¨ããããã¯çç¥ããã¦ããã ã*35ã¨ããæ°ããã¾ãããä¸å¿è§¦ãã¦ããã¾ãã
Euler ã® totient é¢æ°ã¨å¼ã°ããé¢æ° \(\phi(n)\) ãããã¾ãã ããã¯ç«¶æããã°ã©ãã³ã°ãªã©ã§ã¯æ¯è¼çé »ç¹ã«åºã¦ããã®ã§ããããã¨ãã°å \(n\) ã«å¯¾ã㦠\(\phi(2^{n}) = 2^{n-1}\) ã¨ãªãã®ã§ã\(\phi(n)\in O(\sqrt{n})\) ã¨ã¯ãªãã¾ããã
ããã¯ããã¨ãã¦ãç´ æ´ãªæ¹æ³ã§ãã®å¤ãæ±ããã¢ã«ã´ãªãºã 㯠\(O(\sqrt{n})\) æéãããã®ã§ããããæãã¦ãã®ãããªè¨ãæ¹ãï¼ç¹ã«å£èªçãªæèã§ã¯ï¼ãªãããã¨ããããããã§ãã*36ã
ä»»æã® \(f(n)\) 㨠\(g(n)\) ã«å¯¾ãã¦ã\(f(n)\in(O(g(n))\cup\Omega(g(n)))\) ã¨ãªã
ããã¯ãããããã¨æå¤ãããªã®ã§ãããæãç«ã¡ã¾ããã
\(g(n)\) ãã¾ã¨ããªé¢æ°ã¨ã¯éããªãããã§ããã
\(f(n) = n\), \(g(n) = n^{1+\sin(n)}\) ãªã©ã®ä¾ã§ã¯ã\(g(n)\) ã \(n^0\) ãããå°ãããªã£ãã \(n^2\) ããã大ãããªã£ãããã¦ãã¾ãã¾ãã åå大ãã \(n\) ã«å¯¾ãã¦ã \(g(n)\) ã®å®æ°åã常㫠\(f(n)\) 以ä¸ã«ãªã£ããããããã¯å¸¸ã« \(f(n)\) 以ä¸ã«ãªã£ããã¯ãã¦ãããªãããã§ãã
ãã£ã¨ãµãããä¾ã¨ãã¦ã\(f(n)\) ã¯ã\(n\) ãå¥æ°ãªã \(\log(n)\)ãå¶æ°ãªã \(2^{n}\)ãã\(g(n)\) ã¯ã\(n\) ãå¥æ°ãªã \(2^{n}\)ãå¶æ°ãªã \(\log(n)\)ããªã©ãæãããã¾ãã
\(n+1\in O(n)\) ã \(2^{n+1}\in O(2^n)\) ãªã®ã§å½ç¶ \( (n+1)!\in O(n!)\)
\( (n+1)! \ge n\cdot n!\) ãªã®ã§ã\( (n+1)!\) 㯠\(n!\) ãã \(n\) ã²ã¨ã¤ã¶ãéãå¢å ãã¦ãã¾ãã¾ããããã£ã¦æãç«ã¡ã¾ããã
ç´æçãªã¤ã¡ã¼ã¸ã¨ãã¦ã¯ãã\(n\) ã \(2^n\) ã®ãããªçºæ£ãé ãé¢æ°ã§ã¯ \(1\) å¢ããã¨ããã§å·®ã¯ç¡è¦ã§ãããã\(n^n\) ã®ããã«éãé¢æ°ã§ã¯ããã§ã¯ãªããã¨ãã£ãæãã ã¨æãã¾ãã
åæ§ã«ããã¨ãã° \(O(n^{\log_3(4)})\ne O(n^{\log_2(3)})\) ã§ãã
\(\Theta(\log(n^2))\) ãã \(\Theta(\log(n))\) ã«ãªã¼ãã¼ãæ¹åãã
\(\log(n^2) = 2\log(n)\) ã§ãããå®æ°åã®éããããªãã®ã§ããªã¼ãã¼ã¯æ¹åããã¦ãã¾ããã
ã\(10^5\) ã®ãªã¼ãã¼ããªã©ã®è¨ãåã
ãã ããã 6 æ¡ç¨åº¦ã®å¤§ãããã®ãããªæèã§ä½¿ãããã¡ãªããªã¼ãã¼ãã¨ã¯æå³åããç°ãªãã¾ãã big-O ã¯ããããæèã®ããã®è¨å·ã§ã¯ãªãã®ã§ã\(O(10^5)\) ãªã©ã¨æ¸ãã®ã¯ãã³ã»ã³ã¹ã§ãã
ãã ããã \(10^5\)ãã®ãããªæèã§ã¯ã\(\sim 10^5\)ãã®ãããªæ¸ãæ¹ã®æ¹ã妥å½ã«è¦ããæ°ããã¾ãã
è¨ç®éã®è¦ç©ããã«é¢ãã¦
é·ãã£ãã§ãããå®ç¨çã«è¨ç®éãè¦ç©ããããã®æ¹æ³ã®è©±ã§ã*37ã ä¸è¨ã®é¨åã¯ããããã誤解ããã¦ã»ãããªããããã«æ¸ããé¨åã§ãã
ä¸çªæåã®ã½ã¼ãã®è¨ç®éè¦ç©ããã§ãã£ãããã«ãå®æ°åã«é¢ããæ å ±ãæ¨ã¦ã¦ãã¾ããã¨ãå¤ãã§ãï¼æ£ç¢ºã«è¦ç©ããã®ãå°é£ãªä¸ãããã¾ã§è¦ç©ããã¡ãªããããªããã¨ãå¤ãã®ã§ï¼ã
ãã®åæã§ã¯ãåè¡ã«ãã¡ãã¡ã³ã¹ã \(c_i\) ãªã©ãå²ãå½ã¦ããããã
- for \(i\) in \(1, \dots, n\)
- \(\Theta(1)\) æéã®å¦ç
ã®ããã«ç°¡ç¥åãã¦ãã\(\Theta(1)\) æéã®å¦çã \(n\) åããã®ã§ \(\Theta(n)\) æéãã¨è¨ã£ã¦ãã¾ã£ã¦ããã§ãããã
ãã㧠\(\Theta(1)\) æéã®å¦çã¨è¨ã£ã¦ããã®ã¯ãååæ¼ç®ããããæ¼ç®ãé åã¸ã®ã¢ã¯ã»ã¹ã\(\Theta(1)\) æéã§çµãããã¨ãããã£ã¦ããé¢æ°ã®å¼ã³åºããªã©ã§ãã é¢æ°å¼ã³åºãã§ãã£ã¦ããé·ã \(n\) ã®é åãã³ãã¼ãã¦å¼æ°ã¨ãã¦æ¸¡ãã¨ããé¢æ°ã®å é¨ã§ \(\omega(1)\) æéã®å¦çããã¦ããã¨ãããããã£ããã®ã¯ã ãã§ã*38ã
ã¾ãã2 éã«ã¼ãã§ãã£ã¦ãã
- for \(i\) in \(1, \dots, n\)
- for \(j\) in \(1, \dots, n\)
- \(\Theta(1)\) æéã®å¦ç
- for \(j\) in \(1, \dots, n\)
ã¨ãã§ããã°ã
- for \(i\) in \(1, \dots, n\)
- \(\Theta(n)\) æéã®å¦ç
ã¨ãªããã\(\Theta(n)\) æéã®å¦çã \(n\) åããã®ã§ \(\Theta(n^2)\) æéãã¨è¨ãã¾ãã å å´ã®ã«ã¼ãã®ç¯å²ã \(1, \dots, i\) ã ã¨ãã¦ããåæ§ã« \(\Theta(n^2)\) æéã«ãªãã¾ãã\(\sum_{i=1}^n i = \Theta(n^2)\) ã ããã§ããã
ã«ã¼ããä½éã«ãªã£ã¦ããåã«ã¼ãã®ç¯å²ã \(n\) ã ã£ããå¤å´ã®ã«ã¼ãã§åºå®ããå¤ï¼ä¸ã®ä¾ã§è¨ãã¨ããã® \(i\)ï¼ã§ããã°ã\(\Theta(n^{\bullet})\) ã®å½¢ã«ãªãã¾ãã ãªããã«ã¼ãã®éä¸ã§ break ã continue ããã£ããããå ´åã¯ãæ¡ä»¶ã«å¿ãã¦åå¥ã«è§£æããå¿ è¦ãããã¾ãã
2 éã«ã¼ãã§ããã«ã¼ããã¨ã®ç¯å²ãç¹æ®ãªå½¢ã®å ´åã¯ã\(o(n^2)\) ã«ãªãã±ã¼ã¹ãããã¾ãã
- for \(i\) in \(1, \dots, n\)
- for \(j\) in \(1, \dots, \lfloor n/i\rfloor\)
- \(O(1)\) æéã®å¦ç
- for \(j\) in \(1, \dots, \lfloor n/i\rfloor\)
ããã¯ã\(O(\sum_{i=1}^n \frac{n}{i})\) æéã¨ãªãã¾ããã \[ \begin{aligned} \sum_{i=1}^n \frac{n}{i} &= n\cdot\sum_{i=1}^n \frac{1}{i} \\ &= n\cdot \left(\frac{1}{1} + \frac{1}{2} + \dots + \frac{1}{n}\right) \\ &\le n\cdot (1+\log_e(n)) \\ \end{aligned} \] ã¨ãªããã¨ãç¥ããã¦ããã\(O(n\log(n))\) æéã¨ãªãã¾ã*39ã çéã§ã¯ãã調åç´æ°*40㧠log ã«ãªããã¤ããªã©ã®æ称ï¼ï¼ã§è¦ªãã¾ãã¦ãã¾ãã
ä»ã«ããèªæã§ãªãè¨ç®é解æã¯å¤ã ããã¾ãããããã§ã¯å²æãã¾ã*41ã
ã¾ããããã§ã¯è©³ç´°ãçãã¾ãããvectorã»setã»listã»heap ãªã©ã«ä»£è¡¨ããããã¼ã¿æ§é ã®åæä½ã¯ \(O(1)\) æéã§è¡ããªããã®ãããã¾ãããããããã®ããã¨ãã° 1 åããã \(\Theta(\log(n))\) æéããããã®ã¯ã\(n\) åå®è¡ãã㨠\(\Theta(n\log(n))\) æéã«ãªãã¾ããã ãããã¯ãstring å士ã®å¤ãçãããã©ããã®æ¯è¼ãææªæ㯠\(O(1)\) æéã§ã¯ã§ãã¾ãããã
éè«
åãã¼ã¿æ§é ãã©ãã ãã®æéãè¦ãããã«ã¤ãã¦ããããããã¨è¨ç®éã®è¡¨ã丸æè¨ãããã¨ãã¦ãã人ãããã®ããããã¾ããããããããã©ãããæ§é ã§ãã¼ã¿ã管çãã¦ããã®ããç¥ãã¨ãèªç¶ã«è¦ããããæ°ããã¾ãã
è¨èªã«ãã£ã¦ã¯ list.contains(x)
ã¨ã set.find { isEven(x) }
ã¨ãè¨è¿°ã§ããããã¦ãã¾ãã¾ãããå
é¨ã§ã«ã¼ããã¦ããã®ã§ææªæ \(\Omega(n)\) æéã«ãªã£ã¦ãã¾ãã¾ãããé«éã«ããããæ±ãããç¶æ³ã§åå¿è
ããã£ãããããã®ã¡ã½ããã使ã£ã¦ãã¾ãã®ãé²ããããlist.iKnowThatAListTakesLinearTimeToTestWhetherItContainsTheValueButIReallyWantToDoIt(x)
ãªã©ã®ååã«ããã¹ãã¨ããæè¦ï¼ï¼ãããã¾ã*42ã
å ãã¿ï¼
æµ®åå°æ°ç¹æ°ã®æ¯è¼ã¯çµã¿è¾¼ã¿ã®æ¼ç®åãããªã㦠I_do_understand_the_behavior_of_floating_point_numbers_and_really_want_to_compare_them ã¿ãããªååã®é¢æ°ã使ãããããã«ããã¹ãã ã£ã
— mod_poppo (@mod_poppo) 2017å¹´10æ29æ¥
ããã¾ã§ããªãã¦ããã³ã¹ãã大ããã¡ã½ãã㯠list.contains$(x)
ã¿ããã« $
ãã¤ããããã¨ãã®è¨è¨ã¯ååã¢ãªãªãããªæ°ããã¾ããä½ããã£ã¦ã³ã¹ãã大ããã¨ãããã§è°è«ãããããã§ãããã³ã¹ããããã«å¤§ããã¨ã㯠$$
ã®ããã«å¢ãã¦ããã¨ããé¢ç½ããã§ããã
ãã¨ãã°ã
res = 0; for$$ i in 0..n { for$$ j in 0..n { if set.contains$(i + j) { res += 1; } } }
ã®ããã«æ¸ããfor$$
ãµãã¤ã¨ set.contains$
ã²ã¨ã¤ãåããã¦ã³ã¹ãã¯åè¨ $
5 ã¤åã¨ããï¼ãã¹ãããã¦ããé¨åã«ã¤ã㦠$
ãåè¨ããï¼ï¼åãã¹ãã«å¯¾ãã¦ãããè¡ãããã®æ大å¤ãããã°ã©ã å
¨ä½ã®ã³ã¹ãã¨è¦ç©ããï¼ãã®ãããªãã¶ã¤ã³ã®è¨èªããã£ã¦ãé¢ç½ãããªæ°ããã¾ãã
ãã¡ãã break
ã¨ããããã¨ãã¾ããããªããªãã®ã§ãä¸è¨ã®æ¡ã¯ã¾ã ã¾ã ã§ããã
競æããã°ã©ãã³ã°ã¨ãªã¼ãã¼ã®ç¸æ§ãªã©
ç¹ã«ç«¶æããã°ã©ãã³ã°ã®æèã«ããã¦ã¯ã\(n\) ã®ä¸éã¨å®è¡å¶éæéãä¸ãããããã®æéã«éã«åãã³ã¼ããæ¸ãã®ã主ã§ãã ãã® \(n\) ã®ä¸éå¤ãï¼æ³¨ç®ãã¦ããã¢ã«ã´ãªãºã ã®è¨ç®éã®è¦ç©ããã«é¢ãã¦ï¼åå大ããã¨ã¯è¨ããªãã¨ãã¯ãå¿ ããããªã¼ãã¼ã«ããè°è«ãå½¹ã«ç«ããªããã¨ããããããã§ãã
ã¾ããé¤ç®ãªã©ã³ã¹ãã®éãå¦çãããããè¡ãå ´åã¨ãå ç®ãªã©ã®è»½ãå¦çãåãåæ°è¡ãå ´åãæ¯ã¹ãã¨ãå®è¡æéã«ãããªãã«å¤§ããå·®ãåºããã¨ãäºæ³ããã¾ãã ããããå ´åã«ãå®æ°åãç¡è¦ãã¦è°è«ããã¨ï¼ç«¶æããã°ã©ãã³ã°ã¯ãªã¼ãã¼ã§ã¯ãªãå®è¡æéå¶éã«å¯¾ãã¦ãã¾ãããå¿ è¦ãããã®ã§ï¼å¤±æãããã¨ãããã¾ãã ãé¤ç®ã \(2n+o(n)\) åããã以å¤ã®å¦çã \(O(n)\) åãã¨ãè¦ç©ããã¨ããã®ããªã*43ã
ãªãã«ããã競æããã°ã©ãã³ã°ã¨ï¼ä¸è¨ã®æå³ã§ï¼ç¸æ§ã®ããè¨ç®éè¦ç©ããã®è¨æ³ã¯ããããããªãã®ãç¾ç¶ã§ãã å¤ãã®å ´åã¯ãªã¼ãã¼ã§äºè¶³ããã®ã§åé¡ãªãã§ãããå®æ°åãéæ¯é é ãéããã®ãè¦ç©ããããã¨ãã¯ã¡ãã£ã¨å°ãã¾ãã
ãã°ãããè¨æ³ãææ¡ããã°ããã¶ã人æ°è ã«ãªããã¨æãã¾ãã
å®éã®è¨ç®æéã®è¦ç©ãã
ãªã¼ãã¼ã ãè¦ç©ãã£ã¦ãå··ã§è¨ããã¦ããããã«ã\(n\) ã \(10^5\) ãããã«ãªããããªã \(\Theta(n^2)\) æéã®ã¢ã«ã´ãªãºã 㯠2 ç§ã«ã¯åã¾ããªãã ãããªã*44ãã¨ããã\(n\) ã \(10^5\) ããããªãã\(O(n\log(n)^2)\) ã¯ã¾ã 2 ç§ã«åã¾ãã ãããªããã¨ãã£ãæ¨æ¸¬ããããã¨ã¯å°ãªãã¯ãªãã§ãã
å®éãï¼\(O\) ãªã©ã®è¨æ³ãä¿è¨¼ãã¦ãããäºå®ã§ã¯ãªãã«ããï¼\(O(f(n))\) ã® \(f(n)\) ã«å ¥åã® \(n\) ã®æ大å¤ãä»£å ¥ãã¦è¨ç®ããå¤ã \(10^8\) ç¨åº¦ä»¥ä¸ãªã 2 ç§ã«éã«åãã ãããªãã¨ãã£ãè¦ç©ããããã¦å¤§ããå¤ããªããã¨ã¯å¤ãã§ãã ãã\(O\) èªä½ãããããç¨éã®ããã®è¨æ³ã§ã¯ãªããã¨ã¯çæãã¦ãããæ¹ãããããã§ãã
å®æ°åããã¡ããã¡ã大ããã¢ã«ã´ãªãºã ã¨ããã®ã¯åå¨ãã¦ããã¨ãã° \(2^{65536}\) ã¨ããããããã大ãããããªã¢ã«ã´ãªãºã ããããããã§ã*45ãæ®æ®µã¯ããããã¢ã«ã´ãªãºã ãä½ããªãã¯ããªã®ã§ã\(O\) ã®å®æ°åããã¾ã大ãããªãã ããã¨ããæé»ã®äºè§£ã¿ãããªã®ããã£ã¦ãã¾ã£ã¦ããããã§ããã
ãããããªè¨ç®é
ä¸è¨ã§ã¯ãæè¯è¨ç®éã¨ææªè¨ç®éã«ã¤ãã¦ç´¹ä»ãã¾ããããä»ã«ã å¹³åè¨ç®é (average complexity)ãåå´è¨ç®éã»ãªããè¨ç®é (amortized complexity)ãæå¾ è¨ç®é (expected complexity) ã¨å¼ã°ããæ¦å¿µãããã¾ãã
æ¦è¦ã ã説æããã¨ã次ã®éãã§ãã
- å¹³åè¨ç®é
- ãµã¤ãº \(n\) ã®å ¥åãã¹ã¦ã«å¯¾ãã¦ã®è¨ç®éãèãããã®å¹³åã表ãã
- ææªè¨ç®é㯠\(\Theta(n^2)\) ã ããå¤ãã®ã±ã¼ã¹ã§ã¯ \(O(n\log(n))\) ã§ãããå¹³åãã㨠\(O(n\log(n))\) ã¨ããä¾ãããã
- 代表çãªä¾ã¨ãã¦ï¼ç´ æ´ãªå®è£ ã®ï¼ã¯ã¤ãã¯ã½ã¼ããããã
- åå´è¨ç®é
- ãã¼ã¿æ§é ã¸ã®æä½ã¯ãæ¯ååããªã¼ãã¼ã®æéããããã¨ã¯éããªãã
- åæä½ã«ãããè¨ç®éãå¹³åãããã®ã
- å¤ãã®ç¶æ³ã§ã¯æéããã¾ãããããªããããã¾ã«æéããããã¨ãã£ããã¨ã¯ããããã
- é åãå確ä¿ããã¨ããã
- ææªã±ã¼ã¹ã¯ \(\Theta(n)\) ãããããå¹³åããã¨æä½ä¸ã¤ããã \(O(1)\) ã¨ãããã¨ã¯ããããã
- æå¾
è¨ç®é
- ä¹±æ°ãç¨ããã¢ã«ã´ãªãºã ãããããã
- ä¹±æ°ã®å¤ã«ãã£ã¦è¨ç®éãå¤ãããã¨ã¯ãããããããã®æå¾ å¤ï¼ç¢ºçã®éã¿ã¤ãå¹³åï¼ã
ãããã¯åã å¹³åã«é¢ãããã®ã§ãããä½ã«é¢ããå¹³åãªã®ããç°ãªãã®ã§ãè¨èã使ãéã«ã¯æ³¨æãå¿ è¦ã§ãã ãã¨ãã°ãåå´è¨ç®éã®ã¤ããã§å¹³åè¨ç®éã¨è¨ã£ã¦ãã¾ãã¨é½é½¬ãçãã¾ãã
å¹³åè¨ç®éã¯ãå ¥åã«ãã£ã¦ã¯é ããªãã±ã¼ã¹ããããããã¨ããè¦ç©ãããªã®ã«å¯¾ããåå´è¨ç®éã¯ãããã話ã§ã¯ãªãã®ã§ã競ããã«ããã¦ã¯å¾è ã®è¦ç©ããã®æ¹ãããããã§ãã ãã¡ããææªè¨ç®éãè¦ç©ãããã°ããã«è¶ãããã¨ã¯ãªãã§ãããåå´è¨ç®éã§è¦ç©ãã£ã¦ãæãããã¨ã¯ãªãããã§ãã
ãã¡ãã®è¨äºã詳ããã§ãã noshi91.hatenablog.com
ãããããªè¨ç®ã¢ãã«
ä¸è¨ã§ã¯ãååæ¼ç®ã \(\Theta(1)\) æéã§ã§ããã¨ããä»®å®ããã¦ãã¾ããããããã¯ã¡ãã£ã¨æªããã§ãã ãã¨ãã°ãã¡ã¢ãªã«ã®ãªã®ãªä¹ããããªæ´æ°ï¼16 GB = 237 bitsã\(2^{2^{37}} = 2^{137438953472}\)ã41373247568 æ¡ï¼ã«é¢ããååæ¼ç®ããã¯ã¼ãã²ã¨ã¤ã¶ãï¼64 bitsã\(2^{64}\)ã20 æ¡ï¼ã¨åãæéã§ã§ããã¨ããä»®å®ã¯å¼·ããã»éç¾å®çã§ãã
ããã§ãã¯ã¼ãã®å¤§ãããè¨ç®éã«èæ ®ããã¢ãã«ã¨ãã¦ãword RAM ã¨ããã¢ãã«ãããã¾ãï¼ä¸è¨ã®ã¯ã¼ããµã¤ãºãèæ ®ããªããã®ã¯ RAM ã¨å¼ã°ããã¢ãã«ã«è¿ããã§ãï¼ã ä»ã«ããå®è¡ã§ããæ¼ç®ã®ç¨®é¡ãç°ãªãã¡ã¸ã£ã¼ãªã¢ãã«ãããã¤ãããã¾ãã é¤ç®ã¯éãã®ã§å®æ°æéã®å½ä»¤ã»ããã«ã¯å«ããªãã¨ããç«å ´ããããããã§ãï¼*46
word RAM ã«ã¤ãã¦ã¯ãã®è¨äºã§è§¦ãã¾ããã
åè·¯ï¼AND ã OR ãªã©ã®ã²ã¼ããããï¼ã¨ããç¨ããã¢ãã«ãããã¾ãããã¥ã¼ãªã³ã°æ©æ¢°ã¨ããããã¾ããã 競æããã°ã©ãã³ã°ã®æèã§ããããåºã¦ãããã¨ã¯ãã¾ããªãããã§ããããããããã¨æãã人ã«ã¯ãããããããªåéã ã¨æãã¾ãã
ãã£ããã以ä¸ã®ãããªã¤ã¡ã¼ã¸ã®æ©æ¢°ã§ãï¼èªåã¨å¼ãã§ããã®ãæ©æ¢°ã¡ããã§ãï¼ã
- ç¡éã«é·ãç¶ããã¹ç®ããããåãã¹ç®ã«ã¯è¨å·ãæ¸ããã¦ããã
- ãããå ¥åã¨è¦ãªãã
- æåãèªåã¯ãéå§ãã¹ãã«ããããåæç¶æ ãã¨ããç¶æ ã§ããã¨ããã
- ä»ãããã¹ã«æ¸ãããè¨å·ã¨èªåã®ç¶æ
ã«å¿ãã¦ä»¥ä¸ã®å¦çãè¡ãã
- èªåã®ç¶æ ãæ´æ°ããï¼åãç¶æ ã®ã¾ã¾ã§ãããï¼ã
- ä»ãããã¹ã®è¨å·ãæ´æ°ããï¼åãè¨å·ãæ¸ãã¦ãããï¼ã
- å·¦å³ã©ã¡ããé£ã«ç§»åãããããã®å ´ã«çã¾ãã
- ãçµäºç¶æ
ãã«ãªã£ããçµäº
- ãã®ã¨ããã¹ç®ãã¡ã«æ¸ãããè¨å·åãåºåã¨è¦ãªãã
ããã ãã®æä½ã§ãæã ãæ®æ®µä½¿ã£ã¦ãããããªããã°ã©ãã³ã°è¨èªã¨åçã®åé¡ã解ãããããã§ãã
ãããããªãã©ã¡ã¼ã¿
ä¸è¨ã§ã¯å ¥åãµã¤ãº \(n\) ã«é¢ãã¦ã®é¢æ°ã§è¨è¿°ãã¾ããããåºåã«é¢ããå¤ããå ¥åã«å«ã¾ããå¤ã®æ大å¤ããã®ä»ä½ããã®å¤ï¼å ¥åã®æ´æ°ãæã¤ç´ å æ°ã®ç¨®é¡æ°ãå ¥åã®ã°ã©ããæã¤ãã©ã¡ã¼ã¿ï¼ãªã©ãè¦ç©ããã®ããã«ä½¿ããã®ã§ããã°ããããã使ã£ã¦ãã¾ã£ã¦ããã§ãã
ç¹ã«ãåºåã«é¢ããå¤ï¼ãæ¡ä»¶ãæºãããã®ããã¹ã¦åºåãã¦ããã¨ãã£ãåé¡ã«ãããåºåã®åæ°ãªã©ï¼ã§è¨ç®éãæããããã¢ã«ã´ãªãºã ã¯ãoutput-sensitive algorithm ã¨å¼ã°ããããã¦ãã¾ãã
ã½ã¼ãã®è¨ç®éã«é¢ãã¦
çºå±çãªå 容ã§ããé©å®èª¿ã¹ãªããèªããªããç¥èãã¤ãã¦ããèªã¿ç´ããªããå¼·ã人ã«èããªããèªããªããã¦ãããããããããããã§ãã
æåã«ç´¹ä»ããã½ã¼ãã¢ã«ã´ãªãºã 1 ã§ã¯åºéã®æå°å¤ãé ã«æ±ãã¦ããã¾ããã è³¢æãªèªè ã¯ãæ°ã¥ãã®éããããã¯ã»ã°æ¨ã§é«éåã§ãã¾ãããã£ã¦ã\(O(n\log(n))\) æéã«ã§ãã¾ãã
ã¨ããã§ã大å°æ¯è¼ã«åºã¥ãã½ã¼ãã¢ã«ã´ãªãºã 㯠worst \(\Omega(n\log(n))\) æéããããã¨ãç¥ããã¦ãã¾ãã é·ã \(n\) ã®é å㯠\(n!\) éãããã¾ãããä¸åã®å¤§å°æ¯è¼ã§ã¯ååç¨åº¦ã«çµãè¾¼ããã¨ããã§ããªãã®ã§ã\(\log_2(n!)\) åç¨åº¦ã®æ¯è¼ãå¿ è¦ã«ãªãã¾ãã\(\log_2(n!)\in\Theta(n\log(n))\) ã§ããã®ã§ããããªãã¾ã*47ã
\(\log_2(n!)\in\Theta(n\log(n))\) ã«é¢ãã¦ï¼ã¯ãªãã¯ãã¦å±éï¼
Stirling ã®è¿ä¼¼å ¬å¼ã使ãã°æ¸ãã¨è¨ã人ãããããStirling ã®è¿ä¼¼å ¬å¼ã使ããªãã¦ãæ¸ãã¨è¨ã人ããã¾ãã 大éå ·ã使ã£ã¦ããçµããããæ¹ã楽ãã大éå ·ã使ããã«æ¸ã¾ããæ¹ã楽ãã¿ãããªè©±ã§ããã
åè ã§ããã°å ¬å¼ã調ã¹ãã°ããçµããã®ã§ãããã§ã¯å¾è ãç´¹ä»ãã¦ã¿ã¾ãã ãªããã½ã¼ãã®è©±ã«é¢ãã¦è¨ãããã«ã¯ \(\log_2(n!)\in\Omega(n\log(n))\) ããè¨ãã°ããã§ããã\(O(n\log(n))\) ã®æ¹ãã¤ãã§ã«ç¤ºãã¾ãã
ã¾ããèªæã« \(n! \le n^n\) ãªã®ã§ã両辺㫠log ãé©ç¨ã㦠\(\log(n!)\le n\log(n)\) ã§ãããã£ã¦ \(\log(n!)\in O(n\log(n))\) ã§ãã
ãããããããã®ã§ããããã¨ãã°ã\(6!\ge 6\cdot 5\cdot 4\ge 3\cdot 3\cdot 3 = (6/2)^{6/2}\) ã \(7!\ge 7\cdot 6\cdot 5\cdot 4\ge 3\cdot 3\cdot 3 = \floor{7/2}^{\floor{7/2}}\) ã®ãããªãã®ãèãã¦ãã ããã \[ \begin{aligned} n! &\ge \frac{n!}{\lfloor n/2\rfloor !} \\ &\ge \lfloor n/2\rfloor^{\lfloor n/2\rfloor} \end{aligned} \] ãã£ã¦ãlog ãé©ç¨ã㦠\(\log(n!)\ge \lfloor n/2\rfloor\log(\lfloor n/2\rfloor)\) ãå¾ã¾ãã ããªãã¡ \(\log(n!)\in\Omega(n\log(n))\) ã§ãã
以ä¸ããã\(\log(n!)\in\Theta(n\log(n))\) ã§ãã
ï¼ã¯ãªãã¯ãã¦å±éé¨åãããï¼
ã¨ããããã§ãåºéæå°å¤ãæ±ããæä½ã¨ã»å¤ã®æ´æ°ãããæä½ãä¸¡æ¹ \(o(\log(n))\) æéã§ã§ãã¦ãã¾ããã¼ã¿æ§é ãæãã¤ãã¦ãã¾ã£ãå ´åãèªåã®åéããçãã®ãè³¢æã§ã*48ã
ããããï¼
å ¨ãåãæµãã§ç¬ã£ã https://t.co/IZp28nWI5A pic.twitter.com/vZYf8SlKJF
— monkukui (@monkukui) 2021å¹´10æ13æ¥
ãã®ããã«ã解ããªãåé¡ãç¥ã£ã¦ããã¨ããèªåãæãã¤ãã¦ãã¾ã£ããã¼ã¿æ§é ã使ãã¨ã解ããªããã¨ã証æããã¦ããåé¡ã解ãã¦ãã¾ãããã£ã¦èªåãåéãããã¦ããã¯ãã ãã¨æ°ã¥ããã¨ãã§ããå ´åãããã¾ã*49ã
ãããã¯ãããã®äºæ³ãæ£ããã¨ããã¨ããã®åé¡ã¯ \(O(n^{2-\varepsilon})\) ã§ã¯è§£ããªããã¨ãç¥ããã¦ãããã¨ãã£ãå½¢å¼ã®äºå®ãããã¾ãã ããããåé¡ã \(O(n^{2-\varepsilon})\) ã§è§£ãã°ããã®äºæ³ãæ£ãããªãã¨è¨¼æãããã¨ã«ãªãããã§ããã
ã³ã¡ã³ãè¿ä¿¡
ã¤ãã¸ããªã¼ã³ã¡ã³ãã«å¯¾ãã¦è¿ä¿¡ãã¦ããã¾ãã
ã\(O\) ããµã³ãã¤ããã®è¨æ³ã§ã¯ãªããã¨ã¯ããã£ãããï¼ãªãããã®çç±ã§ï¼ãµã³ãã¤ããã®è¨æ³ã¨ã㦠\(O\) ã使ãç¶ãããï¼ãã¨ããã³ã¡ã³ããå¤ãã£ãã§ãã æ£ç´ããããã誤ç¨ãç¶ãããæ°æã¡ã¯ãããã¾ããããå®æ´¾ãéããããªã®ã§ç¹ã«è«ãæ°ã¯ãã¾ããã
ããã¯ããã¨ãã¦ãO ãå ¥åããæéã¨æ¯ã¹ã¦ Ω ã Î ãå ¥åããã®ãé¢åãããã¨ããåé¡ã¯ããã¾ãã mac ã§ããã°ãoption + z*50 㧠Ω ãå ¥åã§ãã¾ã*51ããããã§ãããé¢åã§ãã æ£ããã«åºå·ããªã人ã«ã¨ã£ã¦ã¯ç¡è¦ã§ããªãã³ã¹ãã ã¨è¨ããã¦ãç´å¾ãã¦ãã¾ãã¾ãã
ã¨ããããã¯ã¡ããã¨ç解ãã¦ãæ£ããæ¸ãã¨è¨ããããæ¸ãããããã«ãªã£ã¦ããããããããã§ãã
ï¼è¿½è¨ï¼æ¡ã®å®ããããã®äººã®ç®ã«è§¦ãã¦ãã¾ããå¤ãã®ã³ã¡ã³ããããã ãã¦ãã¾ãã¾ããã温ããã³ã¡ã³ãããå¿ãªãã³ã¡ã³ãã¾ã§ããããããã¾ããããããã¨ããããã¾ãã
ããããªã®ç¥ãå¿ è¦ããªããã¨ããæ¨ã®ã³ã¡ã³ãã¨ããããªã®å¦é¨ã§ CS ãã£ã¦ãã°ã¿ããªç¥ã£ã¦ã*52ãã¨ããæ¨ã®ã³ã¡ã³ãã両æ¹ã¤ãã¦ããã人ã«ã¯äººã®å¸¸èã ãªãã¨æãã¾ããã
åèæç®
- Introduction to Algorithms
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2009). Introduction to algorithms. MIT press.
- æ¥æ¬èªç ãããã¾ãã
- ç¹ã«ãè¨ç®éã®è©±ã«ã¤ãã¦ã¯è©¦ãèªã¿ãã§ãã¾ãããããã
ã¡ããã¨ããè¨äºã¯ä»ã«ãããã¾ãã torus711.hatenablog.com
ãã¨ãã
å½åã®äºå®ããæ¸ããããã¨ãããããå¢ãã¦ãã¾ã£ã¦ãï¼æ°å¼é¨åã® TeX ãªã©ãå«ããã¨ï¼44k æåãè¶ ãã¦ãã¾ãã¾ããã èªãã§ããæ¹ãç²ãã¦ããã§ãããããæ¸ãã¦ãã人ãç²ãã¦ãã¾ãã ããã¾ã§èªãã§ãããã¦ãããããããã§ãããããã¨ããããã¾ãã
ï¼ããã¯è¨åãããè¿·ã£ãã®ã§ããï¼å³ããã Twitter ã§è¨ç®éã®è©±ãæµè¡ã£ã¦ãã¾ã£ã¦ãã¾ãã ãã§ã«ããã誰ãè¨ç®éã®è©±ãã¦ãªããã®ç¶æ ã«ãªã£ã¦ããããã§ããã話é¡ã«ä¹ããã¨ãã¦æ¸ããè¨äºã§ãªããã¨ã¯ä¸»å¼µãããã§ã*53ã
ãã¼æ¸ããããã¨æ¸ãçµããæ°ãããã37k æåãããã§ããåã£ã¦ãã
— ãã³ã¡ããððð¦ (@rsk0315_h4x) 2021å¹´10æ12æ¥
ãã®æç¹ãã 7k æåãããå¢ãã¦ãã¾ãã...
ããã
ãããã§ãã
ãç²ããã¾ã§ãã«ãã
ã
ã
ã
*1:ãéã«ä½è£ããã人ã¯è³¼å ¥ãã¦ãããã§ãã
*2:å®éããè¨ç®éãã§ã°ã°ã£ã 1 ãã¼ã¸ã®è¨äºã¯æªããè¨è¿°ãå¤ããæ³£ãåºãã¦ãã¾ãã¾ããã
*3:çãæãã¦ããã¨ãã£ãè¦æã«å¿ããæ°ã¯ããã¾ãããæ 度ãæ¹ãã¦ãããã¨ããããã§ããâ ã¨æåæ¸ãã¦ããã®ã§ããããã¡ãã«ãåé¡ããããããã®é·ãã«ãªã£ã¦ããæ°ããã¾ãã
*4:ãªããããããªã®ç¥ãå¿ è¦ãªããã¿ãããªã³ã¡ã³ããããã¤ãè¦ãããã®ã§è£è¶³ãã¡ããã¾ãã
*5:å®ç¾©ãããåã«ãã®è¨äºãéãã¦ãã¾ã£ãå ´åã¯é©å®æãåºããªããå¿ããã¾ã¾éãããªããã¦ãã ããã
*6:ã¢ã«ã´ãªãºã ã¨ã¯ãå ¥åã»åºåãæ確ã§ãå½ä»¤ã«ææ§æ§ããªããåæ¢ãããã¨ãä¿è¨¼ããã¦ããæéã®å½ä»¤åã§ãããã®ä¾ã§ã¯é å \(a\) ãå ¥åããããã½ã¼ããããã®ãåºåã§ãã
*7:ãä¸çªæåã«æ¸ãã¦ãã£ããã¤ãã¨ãã®åºæºã§ããã°ãããããªãã¦ãé¸ã¹ã¾ãã...
*8:å®éã«ã¯ããã£ãã·ã¥ãå¹ããããã¦å®æ°ã§ã¯ãªããã¨ãããã§ããããããã¾ã§ã®èæ ®ãå¿ è¦ãªããããã«å¿ããã¢ãã«ãèããæ¹ãããããã§ãã
*9:åå¿è çã«ã¯ \(\sum\) ã®è¨ç®ããã¼ã¤ããªãããããã¾ããããåãªãç·åãªã®ã§ãæ¯ããã«é©å®èª¿ã¹ãããã¦ãããã¨å©ããã¾ãã
*10:ããæ¹ã楽ã ãªãã¨ããã ãã§ãå¿ ãããããªãã¦ã¯ãããªãã¨ããæå³ã§ã¯ãªãã§ãã
*11:åè ã主è¦é ãå¾è ãä½æ¬¡ã®é ã¨å¼ãã ãããã¾ãã
*12:ãã㧠\(T_1(n)=O(n^2)\) ã§ã¯ï¼ã¨æã£ã人ã¯ãæ£ããç¥ã£ã¦ããªãå¯è½æ§ãé«ãã§ãã
*13:å ã®é¢æ°ã®å¢å 度åãã¯ãæ¯é é ã®å¢å 度åãã¨ä¸è´ãã¾ããã¨ããããå ã®é¢æ°ã®å¢å 度åãã®åå ã¨ãªã£ã¦ããé ãæ¯é é ã¨è¨ãæ¹ãæ£ãããã§ãããªã®ã§ããæ¯é é ã®å¢å 度åãã®æ¹åãã§ã¯ãªãåã«ããªã¼ãã¼ã®æ¹åãã¨è¨ã£ã¦ããã§ãã
*14:ä¿æ°ãéæ¯é é ããã¡ãã¡æ±ããã®ãé¢åãªå ´åã»ããããéè¦ã§ãªãå ´åã«ä½¿ã£ãããã¾ãã
*15:ãå¶æ°åãã¨ãã3 æ¡ä¸åãã¨ãããããå¶æ° \(n\) ããã£ã¦ \(n\) åã¨ãããã 3 æ¡ã®æ° \(n\) ããã£ã¦ \(n\) ä¸åã¨ãã£ãæå³åãã§ãããã
*16:ãµãããå¦çç³»ã«ããã¦ã¯ãæ¯è¼ä»¥å¤ã«ç¡é§ãªãã¨ããã¦ãã¡ããã¡ããªè¨ç®éã«ãããã¨ã許ãããï¼
*17:\(x\) ã«å¯¾ãã¦ãã \(y\) ã使ã£ã¦ \(x\le y\) ã \(x\lt y\) ã示ããã¨ãã\(x\) ãï¼\(y\) ã§ï¼ä¸ããæããã¨è¨ãã¾ããéåããªãä¸ããæããã¨è¨ãã¾ãã
*18:æç®ã«ãã£ã¦ã¯ \(\mathrm{O}\) ã¨æ¸ããã \(\mathcal{O}\) ã¨æ¸ãããããã¾ããæå³ã¯åãã§ãã
*19:ãé«ã ãããã¯ããã以ä¸ãã¨ä¼¼ããããªæå³ã§ããat most ããã
*20:\(S=T\) ãå«æããã¨ãã« \(S\subset T\) ã¨æ¸ããããã§ãªãã¨ãã« \(S\subsetneq T\) ãªã©ã使ãå®æ´¾ãããã¾ããããã§ã¯ãåè ã« \(S\subseteq T\)ãå¾è ã« \(S\subset T\) ã使ãå®æ´¾ãæ¡ç¨ãã¦ããã¤ããã§ãã
*21:ããããã¨ããããããããç¶æ³ã§ãªãã§ããã¼ã¨ããã®ã \(O\) ã§ã¯ç¤ºããªãã¨ãããã¨ã§ãã
*22:ã\(x+\frac{1}{x}\) ã®æå°å¤ãæ±ãã¦ããã¨ããåé¡ã§ãç¸å ç¸ä¹å¹³åã®å ¬å¼ãã \(x+\frac{1}{x}\ge 2\sqrt{x\cdot\frac{1}{x}}=2\)ãã¨ããä¸çå¼ã ã示ãã¦æºè¶³ãã¦ãã¾ããã¤ã«ä¼¼ã¦ãã¾ããå®éã«æå°å¤ \(2\) ãã¨ã \(x\) ã®åå¨ã¯ãã®ä¸çå¼ããã¯è¨ããªãã§ããã
*23:\(O\) ã示ãã¨ãã«ä½¿ã£ã \(n_0\) 㨠\(\Omega\) ã示ãããã«ä½¿ã£ã \(n_0\) ã®æ大å¤ãã\(\Theta\) ã示ãããã® \(n_0\) ã¨ãã¦ä½¿ãã°ããããã§ãã
*24:\(O\) ã®ã¨ãåæ§ã\(\mathrm{o}\) ã¨æ¸ãæç®ãããã¾ãã
*25:åå¿è åãï¼ãä»»æã® \(c_U\)ãã¯ããªãã®ä»»æã§é¸ãã \(c_U\) ã§ã¯ãªããããªãã®æµã®ä»»æã§é¸ãã \(c_U\) ã¨ãã£ããããªæå³åãã§ããæµãã©ã \(c_U\) ãé¸ãã§ãããªã㯠\(n_0\) ãè¦ã¤ããå¿ è¦ãããã¾ãã
*26:çè«ãæªãã®ã§ã¯ãªããçè«ãé©ç¨ãããç¶æ³ãééã£ã¦ããã¨ãããããããã¤ãªæ°ããã¾ãã
*27:å°ããæåã§æ¸ãã¦ãããããããªå¥ç´æ¸ã£ã½ããæãã¾ãã
*28:https://web.stanford.edu/class/archive/cs/cs166/cs166.1206/lectures/12/Slides12.pdf p.242
*29:ã³ã³ãã¤ã©ããªã¼ãã¼ãè½ã¨ãã¦ãããã¨ãç¥ããã«æã§ä¿®æ£ãããé度ãæ¹åããªãã£ããã¨ããç¶æ³ãèãããããã§ãã
*30:ã\(\log(n)\) ã¯å®æ°ã ã \(\log(n)^2\) ã¯å®æ°ã§ã¯ãªããã2 ã®å®æ°ä¹ã¯å®æ°ãªã®ã§ã\(n=2^{\log(n)}\) ãå½ç¶å®æ°ããã»ã°æ¨ã® log ã¯å®æ°ã ã set ã® log ã¯å®æ°ã§ã¯ãªããetc.
*31:ãããã \(\poly(n)\) ã®é¨åãå®éã«ã¯å®æ°ã«ãªããã¨ãããã¯ãã§ã\(0\notin\Theta(n)\) ãªã®ã§ã\(n^{\Theta(1)}\) ã§ã¯ãªãã¨æãã¾ãããæèã«ããããã§ãã
*32:æ£ç¢ºã« \(1/64\) ã®è¨ç®éã«ãªã£ãã¨ãã主張ã«è¦ããã®ã§ãããã¯ããã§æªãããè¦ãã¾ããã
*33:ä¸åº¦å®æ°åãç¡è¦ãã¦ããå¤ã«é¢ããè°è«ãããã®ããã³ã»ã³ã¹ã£ã½ãã
*34:å½æãå¥ã§ãããã¨ã¯æã£ã¦ãããã®ã®ãä¾ãããã¨åºã¦ããããã®ãããªãã¤ã¼ããããéã³ã¨ãªã£ã¦ãã¾ãã¾ããã
*35:ããªãæã§ããã¨ããææãããã¾ããã
*36:ç§ã¨ãã¦ã¯ãã¾ã好ããªä½¿ãæ¹ã§ã¯ãªãã§ãããå人ã®ææ³ãªã®ã§ã
*37:å··ã«ããè¨äºã® \(O\) ã \(\Theta\) ã«æ¸ãæãã¦ä¼¼ããããªè©±ããã¦ããã ãã¨ããè¦æ¹ãããããã§ãã
*38:å¼æ°ã®æºåã¨ãé¢æ°å é¨ã®å¦çã¨ãã¯æãã«ãã¦ãé¢æ°ãå¼ã¶ãã®èªä½ã¯ \(O(1)\) ã¨ããç«å ´ãããããå ¨é¨ãå«ãã¦é¢æ°ãå¼ãã§ããã¨ããç«å ´ãããããã§ããããã§ã®èª¬æã¯å¾è ã§ããã
*39:\(\Omega\) ãå°å ¥ãããããã®ä¾ã§è¨ãã¨ããã®ã\(O(n^2)\) æéã§ãããã¨ã¯ããã£ã¦ãããã天æè¦ç¹ã§ã¯ \(O(n\log(n) )\) æéã«ãªã£ã¦ãããã¨ããæãã§ããã
*40:\(\frac{1}{1}+\frac{1}{2}+\cdots+\frac{1}{n}+\cdots\) ã®å½¢ã®åã調åç´æ°ã¨å¼ã³ã¾ãã
*41:èå³ããã人åããã¼ã¯ã¼ãï¼åå²çµ±æ²»æ³ã®è¨ç®éï¼ãã¹ã¿ã¼å®çï¼ãé¨åéååæã \(O(3^n)\) ã«ãªããã¤ãäºä¹ã®æ¨ DP ãªã©ãåå¿è ã«ããããã«ã¯é«åº¦ããã
*42:linear time ã¯ãå ¥åãµã¤ãº \(n\) ã«å¯¾ã㦠\Theta(n)\) æéã¨ãªããã¨ãæ¥æ¬èªã§ã¯ç·å½¢æéã¨è¨ãã¾ãã
*43:ããã 㨠\(o(n)\) ã®å®æ°åãå°ãããã¨ã¯ä¿è¨¼ãã¦ããªãã§ããããã¼ããå°ãã¾ããã
*44:å£èªã§ã¯ã10 ã® 5 㧠\(n^2\) ã¯ã¤ããã ãããããã®è¨ãæ¹ãããããªãã®ã§ããããã©ã¼ãã«ãªè¨ãæ¹ã§ã¯ãªãã§ããã
*45:ããããã®ã galactic algorithm ã¨å¼ã¶ãããã§ãã
*46:Newton æ³ã§å®æ°åã®ä¹ç®ã§ã§ãããã§ãã...ï¼ ãããã¯ä¹ç®ã許ããªãã®ããã å®æ°æéã®ä¹ç®ã \(\log(w)\) åãããããå¿
è¦ãããã®ã§ãå®æ°æéã«ã¯ãªãã¾ããã§ããã\(M(n)\) æéã®ä¹ç®ã使ã£ã¦ \(O(M(n) )\) æéã§é¤ç®ãã§ããã®ã¨æ··åãã¦ãã¾ããããã¿ã¾ããã
*47:ãããµãããè°è«ãèãé åã«å¯¾å¿ããäºåæ¨ã®é«ãã®ä¸éãªã©ãèããã¨ããã¨æãã¾ãã
*48:ãæ°¸ä¹ æ©é¢å®æããã³ã´ï½ï½ï½ï½ãã£ã¦ãã¤ã§ãããéããªãã£ããå¥ã«ããã§ãã
*49:ããã§ã¯ãããæé以ä¸ã§ã¯è§£ããªããã¨ããä¾ã示ãã¾ãããããããæéãããã¦ã解ããªããã¨ãç¥ããã¦ããåé¡ã¨ããã®ãããã¾ãã
*50:å ¨ç¶é¢ä¿ãªããã§ããã©ãâ¥Z ã£ã¦æ´æ¿¯è¡¨ç¤ºã«ãããããªè¦ãç®ãã¦ã¾ãããï¼ ããã§ããªããã
*51:æ¥æ¬èªå ¥åæã¯ Ï ã«ãªãã£ã½ãã
*52:ããã ã¨æã£ã¦ãããã§ããã©ãããã§ããªããããã§ããã©ããã¦ï¼