Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. %\def\includemarkingscheme{YES} % uncomment to produce marking scheme
  2.  
  3.  
  4. \documentclass[12pt]{article}
  5. \usepackage[utf8]{inputenc}
  6. \usepackage[T1]{fontenc}
  7. \usepackage{lmodern}
  8.  
  9. \usepackage{algpseudocode,enumitem,calc,multicol}
  10. \setlist[enumerate]{%
  11. font=\bfseries,
  12. % listparindent=\parindent,
  13. }
  14.  
  15. \SetEnumitemKey{margin}{%
  16. leftmargin={\widthof{#1}+\labelsep},
  17. labelwidth={\widthof{#1}},
  18. }
  19. \usepackage[hyphens]{url}
  20. \usepackage{fullpage,amssymb,epsfig,color,xspace,tikz,amsmath}
  21. \usetikzlibrary{positioning,calc}
  22.  
  23. \usepackage{tikz}
  24.  
  25. \usepackage{listings}
  26. \lstset{%
  27. language=C++,
  28. keepspaces=true,
  29. basicstyle=\small\ttfamily,
  30. commentstyle=\footnotesize\itshape{},
  31. identifierstyle=\slshape{},
  32. keywordstyle=\bfseries,
  33. numbers=left,
  34. numberstyle=\tiny{},
  35. numberblanklines=false,
  36. inputencoding={utf8},
  37. columns=fullflexible,
  38. basewidth=.5em,
  39. fontadjust=true,
  40. tabsize=3,
  41. emptylines=*1,
  42. breaklines,
  43. breakindent=30pt,
  44. prebreak=\smash{\raisebox{-.5ex}{\hbox{\tiny$\hookleftarrow$}}},
  45. escapeinside={//*}{\^^M} % Allow to set labels and the like in comments starting with //*
  46. }
  47.  
  48.  
  49. \usepackage[
  50. pdftitle={},%
  51. pdfsubject={},%
  52. pdfauthor={},
  53. hidelinks,
  54. ]{hyperref}
  55. %\RequirePackage{pstricks,pst-node,pst-tree} % draw trees, requires using xetex
  56. \newlength{\nodeLength}
  57. \newcommand{\Node}{A}
  58. \newcommand{\setnode}[1]{
  59. \settowidth{\nodeLength}{#1}
  60. \renewcommand{\Node}[1]{
  61. \Tcircle[name=#1]{\makebox[\nodeLength]{##1}}
  62. }
  63. }
  64. \setnode{99}
  65. \newcommand{\quesbox}[2]{\begin{center} \framebox[.5\textwidth]{%
  66. \raisebox{-5mm}[0mm][#1]{\begin{minipage}[t]{.45\textwidth}%
  67. {\normalsize\sf #2}{\phantom{ans}}\end{minipage}}} \end{center}}
  68. \newcommand{\ceil}[1]{\left\lceil#1\right\rceil}
  69. \newcommand{\floor}[1]{\left\lfloor#1\right\rfloor}
  70. \let\oldsection\section
  71. \renewcommand\section[2][]{\oldsection[\thesection{}]{[#2]}}
  72. \definecolor{typo}{rgb}{0.75,0,0}
  73. \definecolor{care}{rgb}{0,0,0}
  74.  
  75. \setlength\parindent{0pt}
  76. \setlength\parskip{\medskipamount}
  77.  
  78. \usepackage{todonotes}
  79.  
  80. % Nicer tables; defines \[top,mid,bottom]rule
  81. \usepackage{booktabs}
  82. \usepackage{multirow} % tabular cells extending over several rows
  83. \usepackage{multicol} % tabular cells extending over several columns
  84.  
  85. \usepackage{pbox} % \pbox is a \parbox with automatic width
  86. \usepackage{relsize} % \smaller / \larger
  87. \usepackage{needspace} % \needspace{10\baselineskip} inserts pagebreak unless room for 10 more lines
  88.  
  89. % Add a build timestamp
  90. \RequirePackage[yyyymmdd]{datetime}\renewcommand{\dateseparator}{-}
  91. \newcommand\buildTimestamp{\texttt{\today~\currenttime}}
  92.  
  93. % include a file into the pdf
  94. \usepackage{attachfile2}
  95. \newcommand\attachsource{%
  96. \textattachfile[]{\jobname.tex}{%
  97. \begin{tikzpicture}
  98. \node[drop shadow, rounded corners, thick, draw=black,fill=black!10,text=black]
  99. {\sffamily \jobname.tex};
  100. \end{tikzpicture}%
  101. }%
  102. }
  103. \usetikzlibrary{shadows}
  104.  
  105.  
  106. \ifdefined\includemarkingscheme
  107. \input{\jobname-marking-scheme}
  108. \fi%
  109.  
  110. \newcommand\marking[1]{% %1:problemnumber
  111. \ifdefined\includemarkingscheme
  112. \markingscheme{#1}
  113. \fi%
  114. }
  115.  
  116. \setcounter{section}{3}
  117. \usepackage{graphicx}
  118. \graphicspath{ {images/} }
  119.  
  120. \begin{document}
  121.  
  122. \maketitle
  123.  
  124. \textbf{Problem 1:}
  125.  
  126.  
  127. \textbf{Description:}
  128.  
  129. We'll break the array up into two pieces of equal size, find the maximum subrange minus the smallest value sum in each part, and then take the maximum. But we also need to consider when the subrange sum straddles the middle. \newline
  130.  
  131. There are 2 cases: \newline
  132.  
  133. 1) We add the maximum suffix mins the smallest value of the first half to the maximum prefix of the second half. \newline
  134.  
  135. 2) We add the maximum suffix of the first half to the maximum prefix minus the smallest value of the second half. \newline
  136.  
  137. \textbf{Justification of Correctness:}
  138.  
  139. The algorithm is basically identical to the original subrange sum algorithm with the exception that the way the subrange sum which passes through the middle is calculated.
  140.  
  141. We can prove that our method is correct because we can either exclude the min element from the first half of the array or the second. If we exclude it from the first half it makes sense to find largest subrange minus one that passes through the middle in the first half (maxleftminusone variable in psuedocode) and then we would have to combine it with the largest prefix from the second half. The same also works when you remove the min element from the second half.
  142.  
  143. \begin{lstlisting}
  144. maxsubrangesumminusone(x,lower,upper);
  145.  
  146. if lower > upper then
  147. return(0);
  148.  
  149. if lower = upper then
  150. return(max(0,x[lower]));
  151.  
  152. m := floor( (lower+upper)/2 );
  153.  
  154. suml := 0;
  155. maxleft := 0;
  156. minleftvalue := infinity;
  157. maxleftminusone := 0;
  158. for i := m downto lower do
  159. suml := suml + x[i];
  160. maxleft := max(maxleft,suml);
  161. minleftvalue := min(minleftvalue, x[i])
  162. maxleftminusone := max(maxleftminusone, suml - minleftvalue);
  163.  
  164. sumr := 0;
  165. maxright := 0;
  166. minrightvalue := infinity;
  167. maxrightminusone := 0;
  168. for i := m + 1 to upper do
  169. sumr := sumr + x[i];
  170. maxright := max(maxright,sumr);
  171. minrightvalue := min(minrightvalue, x[i])
  172. maxrightminusone := max(maxrightminusone, sumr - minrightvalue);
  173.  
  174. maxa := maxsubrangesumminusone(x, lower, m);
  175. maxb := maxsubrangesumminusone(x, m+1, upper);
  176.  
  177. return(max(maxa, maxb, maxleft+maxrightminusone, maxleftminusone+maxright));
  178. \end{lstlisting}
  179.  
  180. \textbf{Runtime and justification:}
  181.  
  182. This algorithm has the same exact runtime as the original subrange sum algorithm. It does not do any additional loops anywhere.
  183.  
  184. Letting T(n) denote the time needed for n items, this algorithm's running time satisfies the recurrence T(n) = 2 T(n/2) + O(n). This is the same recurrence as for mergesort, so T(n) = O(n log n). Much better!
  185.  
  186.  
  187. \pagebreak
  188.  
  189. \textbf{Problem 2:}
  190.  
  191. \textbf{Description:}
  192.  
  193. This problem is very similar to the LCS problem. The key observation that it suffices to look at the last character of each string, still holds for the weighted LCS problem just like it held for the original LCS problem. So, instead of adding one when adding a new character to the LCS we add the weight of the new character. \newline
  194.  
  195. \textbf{Justification:}
  196.  
  197. Let X = x[1..m] and Y = y[1..n] and W be the weight a dict that maps each letter to their weights \newline
  198.  
  199. If x[m] = y[n] = a, then any maximum weight substring Z = z[1..k] of X and Y will end in z[k] = a, \textbf{given that W(a) is a positive number}. Furthermore, in this case z[1..k-1] is a maximum weight substring of x[1..m-1] and y[1..n-1]. \newline
  200.  
  201. Otherwise, x[m] ≠ y[n]. So either z[k] ≠ x[m] or z[k] ≠ y[n]. There are two cases: \newline
  202.  
  203. 1) If z[k] ≠ x[m], then z[1..k] is a maximum weight substring of x[1..m-1] and y[1..n]. For if there were a common substring with larger weight of x[1..m-1] and y[1..n], it would also be the max weight substring of x[1..m] and y[1..n]. \newline
  204.  
  205. 2) If z[k] ≠ y[n], then z[1..k] is a max weight substring of x[1..m] and y[1..n-1] - by the same reasoning as the case just before. \newline
  206.  
  207. \begin{lstlisting}
  208. LCS-LENGTH-WEIGHT(x[1..m], y[1..n])
  209.  
  210. c := array(0..m,0..n) of integer;
  211.  
  212. for i := 0 to m do
  213. c[i,0] := 0;
  214.  
  215. for j := 1 to n do
  216. c[0,j] := 0;
  217.  
  218. for i := 1 to m do
  219. for j := 1 to n do
  220. if x[i] == y[j] then
  221. c[i,j] := max(weight[x[i]] + c[i-1,j-1], c[i-1,j-1])
  222. else
  223. c[i,j] := max(c[i-1,j], c[i,j-1]);
  224. \end{lstlisting}
  225.  
  226. \textbf{Runtime and justification:}
  227.  
  228. This clearly costs O(mn) because it simply has 2 nested for loops which perform a constant time operation each time.
  229.  
  230. \pagebreak
  231.  
  232. \textbf{Problem 3:}
  233.  
  234. \textbf{Description:}
  235. I was not sure how to use DP to implement the maze search algorithm so I just implemented it as a simple DFS. It traverses the maze recursively and does not revisit a node more than once given if it is on the same cur\_letter.
  236.  
  237. \textbf{Justification:}
  238. It visits every single possible path that is at least the length of the word letters away. Therefore, if there is a path it will always find the path no matter what.
  239.  
  240.  
  241. \begin{lstlisting}
  242. def find_word(word, cur_letter, i, j, path):
  243. if cur_letter == len(word):
  244. return path
  245.  
  246. if word[cur_letter] == M[i][j]:
  247. res = find_word(word, cur_letter + 1, i + 1, j, path.add(i, j))
  248.  
  249. if(res != False):
  250. return res
  251.  
  252. res = find_word(word, cur_letter + 1, i, j - 1, path.add(i, j))
  253.  
  254. if(res != False):
  255. return res
  256.  
  257. return False
  258. else:
  259. return False
  260. \end{lstlisting}
  261.  
  262.  
  263. \textbf{Runtime:}
  264.  
  265. A DFS takes O(V + E) time to run in the worst case if it visits every node and scans every single edge a single time, where V is the number of vertices and E is the number of edges.
  266.  
  267. The number of vertices is $n^2$ and the because every single vertex has only 2 edges which are right and bottom the total number of edges is $2n^2$ this means that the total running time of this algorithm should be $O(n^2)$ which is the same time that the DP algorithm would have taken.
  268.  
  269. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement