diff options
author | Yigit Sever | 2020-11-06 03:02:23 +0300 |
---|---|---|
committer | Yigit Sever | 2020-11-06 03:02:23 +0300 |
commit | ca2af8e5bf4f6523dde2f56a17426a01e40f7eba (patch) | |
tree | 1a13d303cfdfa95a10777ae8e3a1dc4bb8191b2b /main.tex | |
parent | 053eba43648e9f74dae2c81a51bf7eea92800e31 (diff) | |
download | hw1-ca2af8e5bf4f6523dde2f56a17426a01e40f7eba.tar.gz hw1-ca2af8e5bf4f6523dde2f56a17426a01e40f7eba.tar.bz2 hw1-ca2af8e5bf4f6523dde2f56a17426a01e40f7eba.zip |
Typeset the answer of 3rd question
Diffstat (limited to 'main.tex')
-rw-r--r-- | main.tex | 65 |
1 files changed, 56 insertions, 9 deletions
@@ -212,12 +212,57 @@ Matching z with c (a) | |||
212 | \section{Asymptotics}% | 212 | \section{Asymptotics}% |
213 | \label{sec:asymptotics} | 213 | \label{sec:asymptotics} |
214 | 214 | ||
215 | % asymptotics TODO {{{1 % | 215 | % asymptotics ✅ {{{1 % |
216 | 216 | ||
217 | \begin{question} | 217 | \begin{question} |
218 | What is the running time of this algorithm as a function of n? Specify a function f such that the running time of the algorithm is $\Theta(f(n))$. | 218 | What is the running time of this algorithm as a function of $n$? Specify a function $f$ such that the running time of the algorithm is $\Theta(f(n))$. |
219 | \end{question} | 219 | \end{question} |
220 | 220 | ||
221 | {\centering | ||
222 | \begin{minipage}{.7\linewidth} | ||
223 | \begin{algorithm}[H] | ||
224 | \For(\tcc*[f]{outer loop}){$i = 2$; $i < n$; $i += 1$ }{ | ||
225 | \For(\tcc*[f]{inner loop}){$j=1$; $j < n$; $j * = i$}{ | ||
226 | Some $\Theta(1)$ operation\; | ||
227 | } | ||
228 | } | ||
229 | \caption{Equivalent algorithm to one given in Question 3, edited for brevity}% | ||
230 | \label{alg:question_3} | ||
231 | \end{algorithm} | ||
232 | \end{minipage} | ||
233 | \par | ||
234 | } | ||
235 | |||
236 | The outer loop runs in linear time $\mathcal{O}(n)$ whereas the inner loop requires some deconstruction; | ||
237 | |||
238 | Take the first iteration of the inner loop, $i = 2$ and $j$ is initialized at $1$. | ||
239 | |||
240 | \begin{align*}% | ||
241 | \label{eq:3_iterations} | ||
242 | 1^{\text{st}} ~ \text{iteration} &\rightarrow j = j * i \implies j = 2 \\ | ||
243 | 2^{\text{nd}} ~ \text{iteration} &\rightarrow j = 4 \\ | ||
244 | 3^{\text{rd}} ~ \text{iteration} &\rightarrow j = 8 \\ | ||
245 | \dots \\ | ||
246 | m^{\text{th}} ~ \text{iteration} &\rightarrow j = i^{m} < n \\ | ||
247 | \end{align*} | ||
248 | |||
249 | $m$ is the last iteration of the inner loop because it hit the stopping condition $i^{m} < n$. | ||
250 | Using the Equations above we can find the running time for the inner loop to hit stopping condition; | ||
251 | |||
252 | \begin{gather*} | ||
253 | i^{m} < n \\ | ||
254 | m < \log_{i} n | ||
255 | \end{gather*} | ||
256 | |||
257 | In other words, the inner loop has a running time in the order of $\mathcal{O}(\log{n})$. By the product property of $\mathcal{O}$-notation we have the running time of $\mathcal{O}(n\log_{a}{n}), a > 1$ for the entire algorithm. The base $a$ is useful to answer the rest of the question; | ||
258 | |||
259 | We can specify a function $f$ such as $f = n \log_{b}(n)$ where $b > 1$. From the course slides; | ||
260 | |||
261 | \begin{equation*} | ||
262 | \lim_{n \to \infty}\frac{n\log_{a}{n}}{n \log_{b}{n}} = c | ||
263 | \end{equation*} | ||
264 | |||
265 | And when the limit of two functions $f, g$ converge to some constant $c$, then $f(n) = \Theta(g(n))$. | ||
221 | 266 | ||
222 | % 1}}} % | 267 | % 1}}} % |
223 | 268 | ||
@@ -280,11 +325,13 @@ By the definition of Big $\mathcal{O}$ notation, we have; | |||
280 | 325 | ||
281 | For $c > 1$ and $n_{0} > 1$ we have; | 326 | For $c > 1$ and $n_{0} > 1$ we have; |
282 | 327 | ||
283 | \begin{eqnarray*}% | 328 | % TODO: this makes zero sense? <06-11-20, yigit> % |
284 | f(n) \le f(n)^{2} \quad \forall n \ge n_{0} \\ | 329 | |
285 | g(n) = f(n)^{2} \\ | 330 | \begin{align*}% |
286 | f(n) \le c\cdot g(n) \quad \forall n \ge n_{0} | 331 | f(n) &\le f(n)^{2} \quad \forall n \ge n_{0} \\ |
287 | \end{eqnarray*} | 332 | \text{take} ~ g(n) &= f(n)^{2} \\ |
333 | f(n) &\le c\cdot g(n) \quad \forall n \ge n_{0} | ||
334 | \end{align*} | ||
288 | 335 | ||
289 | \begin{info}[] | 336 | \begin{info}[] |
290 | $f(n) + o(f(n)) = \Theta(f(n))$ | 337 | $f(n) + o(f(n)) = \Theta(f(n))$ |
@@ -337,11 +384,11 @@ Otherwise, indicate that H or L do not exist. | |||
337 | \end{info} | 384 | \end{info} |
338 | 385 | ||
339 | \begin{info}[] | 386 | \begin{info}[] |
340 | $f(n) = \sum^{\lceil{log n}\rceil}_{k=0} \frac{n}{2^{k}}$ | 387 | $f(n) = \sum^{\lceil{\log{n}}\rceil}_{k=0} \frac{n}{2^{k}}$ |
341 | \end{info} | 388 | \end{info} |
342 | 389 | ||
343 | \begin{info}[] | 390 | \begin{info}[] |
344 | $f(n) = n(log n)^{2}$ | 391 | $f(n) = n(\log n)^{2}$ |
345 | \end{info} | 392 | \end{info} |
346 | 393 | ||
347 | 394 | ||