q4.tex 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. \documentclass[11pt]{article}
  2. \usepackage{amsthm,amsmath,amsfonts,amssymb}
  3. \usepackage[ruled,noline,noend]{algorithm2e}
  4. \usepackage[margin=1in]{geometry}
  5. \usepackage{enumerate}
  6. %%% Update the following commands with your name, ID, and acknowledgments.
  7. \newcommand{\YourName}{Tareef Dedhar}
  8. \newcommand{\YourStudentID}{20621325}
  9. \newcommand{\YourAck}{I have completed this question without outside sources.}
  10. \newenvironment{solution}{\paragraph{Solution.}}{}
  11. \title{
  12. \vspace{-0.8in} \normalsize
  13. \begin{flushright} \YourName \\ \YourStudentID \end{flushright}
  14. \rule{\textwidth}{.5pt}\\[0.4cm] \huge Assignment 1: Question 4 \\
  15. \rule{\textwidth}{2pt}
  16. \vspace{-1in}
  17. }
  18. \date{}
  19. \begin{document}
  20. \maketitle
  21. \paragraph{Acknowledgments.} \YourAck
  22. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  23. \section*{Common sum [10 marks]}
  24. In the \textsc{Common\,Sum} problem, we are given two arrays $A$ and $B$ of length $n$ containing
  25. non-negative (not necessarily distinct) integers, and we must determine whether there are indices
  26. $i_1, i_2, j_1, j_2 \in \{1,2,\ldots,n\}$ for which
  27. \[
  28. A[i_1] + A[i_2] = B[j_1] + B[j_2].
  29. \]
  30. Design an algorithm that solves the \textsc{Common\,Sum} problem and has time complexity $O(n^2 \log n)$ in the setting where operations on individual integers take constant time.
  31. \bigskip
  32. Your solution must include a description of the algorithm in words, the pseudocode for the algorithm,
  33. a proof of its correctness, and an analysis of its time complexity in big-$\Theta$ notation.
  34. \begin{solution}
  35. \begin{section}{Description}
  36. First, we take our two arrays of length n (a and b), and new empty array of length ((n)(n+1)/2) named c. We will fill c with all the possible sums in b (which number ((n)(n+1)/2). Then, we will sort c using an efficient sort such a mergesort. Then, for each sum in a, we will check if that sum is located in c, using a binary search. If we get a match, return true, otherwise return false.
  37. \end{section}
  38. \begin{section}{Pseudocode}
  39. \begin{verbatim}
  40. int[n] a,b;
  41. int[n(n+1)/2)] c;
  42. for (i from 0 to n)
  43. for (j from i to n)
  44. c.insert([b[i]+b[j])
  45. sort(c)
  46. for (i from 0 to n)
  47. for (j from i to n)
  48. if (binary_search((a[i]+a[j]), c)
  49. return true
  50. return false
  51. \end{verbatim}
  52. \end{section}
  53. \begin{section}{Proof}
  54. First, we will show the number of possible sums in an array numbers (n)(n+1)/2. Intuitively, if you want all the sums possible using b[0], you will add b[0] with b[i], where i = 0 to n. However, if you do this with every index, you will get repeats. For example, if we look at b[1], we don't need to add b[1] with b[0] since we already summed these in the previous step. The same is true for b[2] with b[0] and b[1], so on and so forth. We can express this as a sum $\sum_{i=0}^{n} \sum_{j=i}^{n} b[i]+b[j]$, since each time we start finding sums using b[i], we already added b[i] with b[0] through [i]. So, we can also justify how the array c is populated using this logic.
  55. We sort c to allow for binary search to function, as this allows us to be more efficient.
  56. As for the last loop, we use the same looping logic as above to generate all the sums in array a as we did with array b. However, instead of generating an array containing all these sums, for each of them, we will search our existing array of sums in b for the value of a[i] + a[j]. If it exists, we have a match, and there is a common sum. Otherwise, we return false if all possibilities have been exhausted.
  57. \end{section}
  58. \begin{section}{Time Complexity}
  59. This is $\Theta$(${n^2}\log{n}$), because:
  60. \begin{enumerate}
  61. \begin{item}
  62. Looping from i=0 to n is obviously done n times, and this is executing another loop that is up to n recurrences, where each internal operation is a constant time insertion. This is executing something of constant time up to n times, n times over. So our first step is $\Theta$(${n^2}$).
  63. \end{item}
  64. \begin{item}
  65. Sorting something of length ((n)(n+1)/2), which is of the order ${n^2}$, using an efficient sort such as mergesort is known to be of time complexity $\Theta$(${n^2}\log{n^2}$), which reduces to $\Theta$(${n^2}\log{n}$).
  66. \end{item}
  67. \begin{item}
  68. This is the same loop as in step 1, except for the innermost step; instead of a constant time insertion, we are performing a binary search on an array of length ${n^2}$. So, each internal step is $\Theta$($\log{n^2}$), which reduces to $\Theta$($\log{n}$). So, our total loop time efficiency is $\Theta$(${n^2}\log{n}$).
  69. \end{item}
  70. \begin{item}
  71. Since our longest step is $\Theta$(${n^2}\log{n}$), and the three steps are independent of each other, we can conclude our total efficiency is $\Theta$(${n^2}\log{n}$).
  72. \end{item}
  73. \end{enumerate}
  74. \end{section}
  75. \end{solution}
  76. \end{document}