Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 5.35 KB | None | 0 0
  1. \documentclass{article}
  2.  
  3. % \usepackage{minted} % Good for syntax highlighting but complex to install
  4. % \usepackage{verbose} % Allows you to write code directly but no syntax highlighting
  5. % Algorithm packages for ...
  6. \usepackage{amsmath}
  7. \usepackage{algorithm}  % the algorithm environment (similar to a figure environment)
  8. \usepackage{algorithmicx} % for algorithmic environment with commands useful in pseudo-code
  9. \usepackage[noend]{algpseudocode} % Additional commands such as \Function{}{}
  10. % Additional packages
  11. \usepackage{fullpage,latexsym,amsmath,amsfonts}
  12. \usepackage{enumerate} % List package
  13. % \input{macros.tex}
  14. \newcounter{problemnumber}
  15. \newenvironment{problem}{
  16.     {\vskip 0.1in \noindent
  17.         \bf Problem~\addtocounter{problemnumber}{1}\arabic{problemnumber}:}
  18.    }{}
  19. \newenvironment{solution}{
  20.     {\vskip 0.1in \noindent
  21.         \bf Solution~\arabic{problemnumber}
  22.    }
  23. }
  24. {\ \newline\smallskip\lineacross\smallskip}
  25. \newcommand{\lineacross}{\noindent\mbox{}\hrulefill\mbox{}}
  26.  
  27. \title{CS 141 Assignment 1}
  28. \author{Daniel Stinson-Diess 861266420} % Replace with your name and SID
  29.  
  30.  
  31. \begin{document}
  32.  
  33. \maketitle
  34.  
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36. \begin{problem}
  37. The Fibonacci numbers are the numbers in the following integer
  38. sequence.
  39. \smallskip
  40.  
  41. \begin{center}
  42.    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,...
  43. \end{center}
  44.  
  45. \noindent In  mathematical  terms,  the  sequence  F  n  of  Fibonacci  numbers  is  defined  by  the  recurrence relation:
  46. \smallskip
  47.  
  48. \begin{center}
  49.    $F_n = F_(n-1) + F_(n-2)$, where $F_0 = 0$, and $F_1 = 1$.
  50. \end{center}
  51.  
  52. \noindent Discuss the correctness of the following algorithms that calculate Fibonacci numbers:
  53.  
  54. \end{problem}
  55. \begin{solution}
  56.  
  57. \begin{enumerate}
  58.    \item The first algorithm is incorrect because does not have any base cases in the recursive function so this will endlessly subtract the values on \emph{n} going into the negative integers and under flowing back to the vary large integers until a stack overflow is reached causing the program to cease execution.
  59.    
  60.    \item The second algorithm correctly generates the Fibonacci numbers. This can be shown using a loop invariant which is:
  61.    
  62.    \item The third algorithm is incorrect because it generates the wrong values for the Fibonacci sequence. The error in the algorithm is that within the for loop, the order in which variable \emph{b}, and \emph{a} are assigned should be reversed, and that correction would correctly return the Fibonacci number for \emph{n}
  63. \end{enumerate}
  64.  
  65. \end{solution}
  66. \newpage
  67. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  68.  
  69. \begin{problem}
  70. (20 points) Is the following statement true or false?
  71.  
  72. $$ 3^n = \Theta(2^n)$$
  73. \noindent Justify your answer using the basic definition of the $\Theta$-notation.
  74. \end{problem}
  75. \begin{solution}
  76. \vskip 0.1in
  77. \noindent $\Theta$ is a tight asymptotic bound on the function, implying both a $O(2^n)$ and $\Omega(2^n)$. With an upper-bound of $2^n$ on the function $3^n$ this is clearly false because for no choice for the constant \emph{c} will the statement: $3^n \leq c*f(n)$ for sufficiently large values of \emph{n}.
  78. \end{solution}
  79. \newpage
  80. \begin{problem}
  81. (20 points) Give a tight bound (using the big-theta notation) on the time complexity of following method as a function of n.  For simplicity, you can assume n to be a power of two.
  82. \begin{algorithm}[h]
  83.    \caption{WeirdLoop}
  84.    \begin{algorithmic}[1]
  85.        \Function{WeirdLoop}{$n: \mbox{\bf integer}$}
  86.            \State $i \leftarrow n$
  87.            \While{$i \geq 1$}
  88.                \For{$j \leftarrow 1 \text{\textbf{ to }} i$}
  89.                    \State $k \leftarrow 1$
  90.                    \While{$k \leq n$}
  91.                        \State $k \leftarrow 2k$
  92.                    \EndWhile
  93.                    \State $ i \leftarrow i/2$
  94.                \EndFor
  95.            \EndWhile
  96.        \EndFunction
  97.    \end{algorithmic}
  98. \end{algorithm}
  99. \end{problem}
  100. \begin{solution}
  101. $$T(n) = \Theta(n*log^2(n))$$
  102.    
  103. \begin{itemize}
  104.    \item The outer while loop will run a total of log(n) times
  105.    \item The for loop will run for a worst case of n times
  106.    \item The inner while loop will also run for a worst case of log(n) times
  107. \end{itemize}
  108. \end{solution}
  109. \newpage
  110. \begin{problem}
  111. (20 points) Order the following list of functions by the big-Oh notation, i.e.,rank them by order of growth. Group together (for example, by underlining) those functions
  112. that are big-Theta of one another. Logarithms are base two unless indicated otherwise.
  113. \end{problem}
  114. \begin{solution}
  115. \vskip 0.1in
  116. \end{solution}
  117. \newpage
  118. \begin{problem}
  119. (20 points) Given \emph{n} non-negative integers $a_1$, $a_2$, ... $a_n$, where each represents a point at coordinate $(i, a_i)$. \emph{n} vertical lines are drawn such that the two endpoints of line \emph{i} is at $(i, a_i)$ and $(i,0)$.  Find two lines, which together with x-axis forms a container, such that the container contains the most water.
  120.  
  121. \begin{enumerate}
  122.    \item  Solve  the  problem  with  brute-force  approach.   What  is  the  time  complexity  of  this approach?
  123.    \item Is there any better solution? If so, describe your solution with time complexity analysis.
  124. \end{enumerate}
  125.  
  126. \noindent \emph{Note:} You may not slant the container and \emph{n} is at least 2.
  127. \end{problem}
  128.  
  129. \begin{solution}
  130. \vskip 0.1in
  131.  
  132. \end{solution}
  133.  
  134. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement