Advertisement
UsSe3wa

Untitled

Jan 21st, 2025
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 4.99 KB | None | 0 0
  1. \documentclass[dvipsnames]{article}
  2. \usepackage[english]{babel}
  3. \usepackage{multirow}
  4. \usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
  5. \usepackage{amsmath}
  6. \usepackage{graphicx}
  7. \usepackage{minted}
  8. \usepackage{amsthm}
  9. \usepackage{mathtools}
  10. \usepackage[colorlinks=true, allcolors=blue]{hyperref}
  11. \usepackage{fancyhdr}
  12.  
  13. \begin{document}
  14. \section*{Week 1. Problem set (solutions by Matvey Panchenko)}
  15.  
  16. \thispagestyle{fancy}
  17. \lhead{Matvey Panchenko (\texttt{m.panchenko@innopolis.university}), group B24-DSAI-03}
  18.  
  19. \begin{enumerate}
  20.  \item
  21.    Compute asymptotic worst case time complexity of the following algorithm (see pseudocode conventions in \cite[\S 2.1]{Cormen2022}).
  22.    You \textbf{must} use $\Theta$-notation.
  23.    For justification, provide execution cost and frequency count for each line in the body of the \texttt{secret} procedure.
  24.    Optionally, you may provide the details for the computation of the running time $T(n)$ for worst case scenario.
  25.    Proof for the asymptotic bound is not required for this exercise.
  26.  
  27.    \begin{minipage}{0.55\textwidth}
  28.      \begin{minted}[linenos,escapeinside=||]{sql}
  29. /* A is a 0-indexed array,
  30.  * n is the number of items in A */
  31. |\color{blue}secret|(A, n):
  32.  |\color{red}for| i = 1 to n
  33.    s := 1
  34.    j := i - 1
  35.    |\color{red}while| j < n |\color{teal}and| A[j] |$\geq$| A[i-1]
  36.      j := j + s
  37.      s := s + 2
  38.    |\color{teal}exchange| A[i-1] |\color{teal}with| A[min(j, n - 1)]
  39.      \end{minted}
  40.    \end{minipage}%
  41.     \begin{minipage}{0.4\textwidth}
  42.      \small
  43.      \begin{tabular}{ c | c | c }
  44.      Line & Execution Cost & Frequency Count \\
  45.      \hline
  46.      1 & --- & --- \\  
  47.      2 & --- & --- \\
  48.      3 & --- & --- \\
  49.      4 &     c_4 &  n+1 \\
  50.      5 &     c_5 &  n \\
  51.      6 &     c_6 &  n \\
  52.      7 &     c_7 &  $\sum_{i=1}^n t_i - 1$ \\
  53.      8 &     c_8 &  $\sum_{i=1}^n t_i$ \\
  54.      9 &     c_9 &  $\sum_{i=1}^n t_i$ \\
  55.      10 &    c_{10} &  n \\
  56.      \end{tabular}
  57.    \end{minipage}
  58.  
  59.    \emph{(optional part) From this table, we conclude}
  60.    \[
  61.    T(n) = c_4(n+1) + c_5n + c_6n + c_7\frac{n(n+1)}{2} + c_8\frac{n(n+1)}{2} - c_8 + c_9\frac{n(n+1)}{2} - c_9 + c_{10}n = \Theta(n^2).
  62.    \]
  63.    qed.
  64. \color{black}
  65.  \item Indicate, for each pair of expressions (A, B) in the table below whether $A = O(B)$, $A = o(B)$, $A = \Omega(B)$, $A = \omega(B)$, or $A = \Theta(B)$. Write your answer in the form of the table with \emph{YES} or \emph{NO} written in each cell:
  66.  
  67.    \begin{center}
  68.    \bgroup
  69.    \def\arraystretch{1.5}
  70.    \begin{tabular}{ |c|c||c|c|c|c|c| }
  71.     \hline
  72.     $A$ & $B$ & $A = O(B)$ & $A = o(B)$ & $A = \Omega(B)$ & $A = \omega(B)$ & $A = \Theta(B)$ \\
  73.     \hline
  74.     \hline
  75.     $1 + \sin(n)$ & $\left(1 + \frac{1}{n}\right)^{\frac{1}{n}}$ & YES & NO & NO & NO & NO \\
  76.     \hline
  77.     $\sqrt[5]{n}$ & $\log_3^2 n$ & NO & NO & YES & YES & NO \\
  78.     \hline
  79.     $n^2$ & $\log_2^n n$ & YES & YES & NO & NO & NO \\
  80.     \hline
  81.     $(1 + \frac{1}{10^{100}})^n$ & $n^{(10^{100})}$ & YES & YES & NO & NO & NO \\
  82.     \hline
  83.     $\log_2 \left( (n + 1)! \right)$ & $n \log_2 n$ & YES & NO & YES & NO & YES \\
  84.     \hline
  85.    \end{tabular}
  86.    \egroup
  87.    \end{center}
  88.  
  89.  \clearpage
  90.  \item Let $f$ and $g$ be functions from positive integers to positive reals.
  91.    Assume $f(n) > n$ for $n > 2025$.
  92.    Using the formal definition of asymptotic notation~\cite[\S 3.2]{Cormen2022}, prove \emph{formally} that
  93.    \[
  94.      \min(f(n) - \sqrt{n}, g(n) + n) = O(f(n) + g(n)).
  95.    \]
  96.  
  97.    \begin{proof}
  98.    By definition of big-$O$ notation, we need to show that there exist constants $c > 0$ and $n_0 > 0$ such that:
  99.    \[
  100.      \min(f(n) - \sqrt{n}, g(n) + n) \leq c(f(n) + g(n)),
  101.    \]
  102.    for all $n \geq n_0$.
  103.  
  104.    Let $n_0 = 2069$ and $c = 1$. Then for $n \geq n_0$, we have $f(n) > n$.
  105.  
  106.    By assumption:
  107.    \[
  108.    \min(f(n) - \sqrt{n}, g(n) + n) \leq c(f(n) + g(n)).
  109.    \]
  110.  
  111.    Consider the following cases:
  112.    
  113.    \textbf{Case 1:} $\min = f(n) - \sqrt{n}$. \\
  114.    In this case:
  115.    \[
  116.    f(n) - \sqrt{n} \leq f(n), \text{ since } \sqrt{n} \geq 0.
  117.    \]
  118.    Moreover:
  119.    \[
  120.    f(n) \leq f(n) + g(n).
  121.    \]
  122.  
  123.    \textbf{Case 2:} $\min = g(n) + n$. \\
  124.    In this case:
  125.    \[
  126.    g(n) + n \leq g(n) + f(n), \text{ by the condition that } f(n) > n.
  127.    \]
  128.  
  129.    Thus, in both cases:
  130.    \[
  131.    \min(f(n) - \sqrt{n}, g(n) + n) \leq c(f(n) + g(n)).
  132.    \]
  133.  
  134.    Therefore, we have shown that:
  135.    \[
  136.    \min(f(n) - \sqrt{n}, g(n) + n) = O(f(n) + g(n)).
  137.    \]
  138.    qed.
  139.    \end{proof}
  140. \end{enumerate}
  141.  
  142. \begin{thebibliography}{TheLongestRefName}
  143.  
  144. \bibitem[CLRS]{Cormen2022}
  145. Cormen, T.H., Leiserson, C.E., Rivest, R.L. and Stein, C., 2022. \emph{Introduction to algorithms, Fourth Edition}. MIT press.
  146.  
  147. %\bibitem[Goldrich]{Goldrich2014}
  148. %M. T. Goodrich, R. Tamassia, and M. H. Goldwasser. \emph{Data Structures and Algorithms in Java.} WILEY 2014.
  149. \end{thebibliography}
  150.  
  151. \end{document}
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement