Advertisement
splash365

Time Complexity Analysis (Examples)

Sep 1st, 2020 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. O(1):
  2. int main()
  3. {
  4.     c = a+b; o(1);
  5.     d = b+c; O(1);
  6. }
  7.  
  8. ********************************************************************
  9.  
  10. O(cN + c) = O(N)
  11.  
  12. int main()
  13. {
  14.     for(int i = 0; i<15; i++)
  15.     {
  16.         ///O(1) expressions
  17.     }
  18.  
  19. }
  20.  
  21. ********************************************************************
  22.  
  23. O(N):
  24. for(int i = 0; i<N; i++)
  25. {
  26.     o(N);
  27. }
  28.  
  29.  
  30. ********************************************************************
  31.  
  32. O(N+M):
  33. for(int i = 0; i<N; i++)
  34. {
  35.     ///o(1) expressions
  36. }
  37. for(int i = 0; i<M; i++)
  38. {
  39.     ///o(1) expressions
  40. }
  41.  
  42. if(N==M) O(N+N) = O(2N) = O(N)
  43. if(M is constant) O(N+M) = O(N)
  44.  
  45.  
  46. ********************************************************************
  47.  
  48. O(N*M):
  49. for(int i = 0; i<N; i++)
  50. {
  51.     for(int j = 0; j<M; j++)
  52.     {
  53.         ///o(1) expressions
  54.     }
  55.  
  56.     O(1)
  57.     O(1)
  58.     .
  59.     .
  60.     .
  61.     O(1)
  62. }
  63.  
  64. Explanation:
  65.  
  66. i   j
  67. 0   M
  68. 1   M
  69. 2   M
  70. 3   M
  71. ....
  72. N   M
  73.  
  74. O(NM)
  75.  
  76. if(N==M) O(N^2)
  77.  
  78. ********************************************************************
  79.  
  80. O(N):
  81.  
  82. for(int i = 1; i<=n; i += 2)
  83. {
  84.     //O(1)
  85. }
  86.  
  87. O(1)
  88. O(1)
  89. .
  90. .
  91. .
  92. .
  93.  
  94.  
  95. i
  96. 1
  97. 3
  98. 5
  99. .
  100. .
  101. .
  102. K
  103.  
  104.  
  105. k = N
  106. O(N)
  107.  
  108. ********************************************************************
  109.  
  110. O(logN):
  111.  
  112. c = constant
  113. for(int i = 1; i<= N ; i = i*c)
  114. {
  115.     //O(1)
  116. }
  117.  
  118. i
  119. 1, C, C^2, C^3, .... , C^K
  120.  
  121. C^K = N
  122. K = logN -> log er base C
  123. O(logN) -> where base is constant C
  124.  
  125. ********************************************************************
  126.  
  127. O(logN):
  128.  
  129. for(int i = N; i> 0; i = i/2)
  130.  
  131. i
  132. N/2
  133. N/4
  134. N/8
  135. .
  136. .
  137. .
  138. N/2^k
  139.  
  140. N/2^k = 1
  141. 2^k = N
  142. k = logN
  143.  
  144. ********************************************************************
  145.  
  146. O(sqrt(N)):
  147.  
  148. for(i = 1; i*i <= n; i++)
  149. {
  150.     //O(1)
  151. }
  152.  
  153. i
  154. 1
  155. 4
  156. 9
  157. 16
  158. .
  159. .
  160. k^2
  161.  
  162. K^2 = N
  163.  
  164. k = sqrt(N)
  165.  
  166. O(sqrt(N))
  167.  
  168. ********************************************************************
  169.  
  170. O(sqrt(N)):
  171.  
  172. c = 0;
  173. for(i = 1; c<=n; i++)
  174. {
  175.     c = c+i;
  176. }
  177.  
  178.  
  179. c=0, i = 1
  180. while(c<=n)
  181. {
  182.     c = c+i;
  183.     i = i+1;
  184. }
  185.  
  186.  
  187. Explanation:
  188.  
  189. i   c
  190. 1   1
  191. 2   1+2
  192. 3   1+2+3
  193. 4   1+2+3+4
  194. 5   1+2+3+4+5
  195. .
  196. .
  197. .
  198. k   1+2+3+4+5+....+k
  199.  
  200. 1+2+3+4+5+....+k = N
  201. K(K+1)/2 = N
  202. K^2 + K = 2N
  203. K^2 = 2N - k
  204. k = sqrt(N)
  205.  
  206. O(sqrt(N))
  207.  
  208. ********************************************************************
  209.  
  210. O(log(logN)):
  211. for(int i = 2; i<=n; i = i*i)
  212. {
  213.     //some O(1) expression
  214. }
  215.  
  216. i
  217. 2
  218. 2^2
  219. 2^(2^2)
  220. 2^(2^3)
  221. 2^(2^4)
  222. .
  223. .
  224. .
  225. 2^(2^k)
  226.  
  227. 2^(2^k) = N
  228. 2^K = logN
  229. K= log(logN)
  230.  
  231.  
  232. ********************************************************************
  233.  
  234. O(logN + log(log(N))):
  235.  
  236. c = 0
  237. for(int i = 1; i<=n; i = i*2)
  238. {
  239.     c = c+1;
  240. }
  241. for(int j = 1; j<=c; j = j*2)
  242. {
  243.     ///O(1) expression
  244. }
  245.  
  246.  
  247. complexity of 1st loop: c = O(logN)
  248. complexity of 2nd loop: k = O(logC) = O(log(log(N)))
  249.  
  250.  
  251. total = O(logN + log(log(N)))
  252.  
  253.  
  254. ********************************************************************
  255.  
  256. int a,b; /// a,b > 0
  257. while(a!=b)
  258. {
  259.     if(a>b) a = a-b;
  260.     else b = b-a;
  261. }
  262.  
  263. in worst-case O(max(a,b))
  264.  
  265. ********************************************************************
  266.  
  267.  
  268. Computational Complexity:
  269.  
  270. int t;
  271. while(t--)
  272. {
  273.     for(int i = 0; i<n; i++)
  274.     {
  275.         ///
  276.     }
  277.     for(int i = 0; i<m; i++)
  278.     {
  279.         ///
  280.     }
  281.     for(int i = 0; i<1000; i++)
  282.     {
  283.  
  284.     }
  285. }
  286.  
  287. t = 50
  288.  
  289. 1<= m <= 10^5
  290. 1<= n <= 10^8
  291.  
  292. time : 1 sec
  293.  
  294. T(N) =  50*(10^8 + 10^5 + 1000)
  295.  
  296.  
  297.  
  298. /// Google Doc Link: https://docs.google.com/document/d/1juxI-0k_3rUWUXbpzVqapi0cm-41fNh71JapyLMPcUM/edit?usp=sharing
  299.  
  300.  
  301.  
  302.  
  303.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement