341-a3-q5.tex 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. \documentclass[a4paper]{article}
  2. %% Language and font encodings
  3. \usepackage[english]{babel}
  4. \usepackage[utf8x]{inputenc}
  5. \usepackage[T1]{fontenc}
  6. %% Sets page size and margins
  7. \usepackage[a4paper,top=2cm,bottom=2cm,left=2cm,right=2cm,marginparwidth=1.75cm]{geometry}
  8. %% Useful packages
  9. \usepackage{amsmath}
  10. \usepackage{graphicx}
  11. \usepackage[colorinlistoftodos]{todonotes}
  12. \usepackage[colorlinks=true, allcolors=blue]{hyperref}
  13. \title{CS 341 A3 Q4}
  14. \author{Tareef Dedhar - 20621325}
  15. \begin{document}
  16. \maketitle
  17. \section{Shipping Paper}
  18. \subsection{Description/Correctness}
  19. Sort the list of factories by the absolute value of vi - wi, or the marginal savings you would get by choosing the optimal warehouse for each factory. Then, greedily choose as much of the optimal warehouse's paper as possible for the first factory, and if you hit capacity for that warehouse fill the rest with the other warehouse. Repeat this until you've iterated through the entire list.
  20. \subsection{Pseudocode}
  21. \begin{verbatim}
  22. int diff[n];
  23. max_heap sorted[n][2]; // stores an index and the marginal difference in cost, sorted by the latter
  24. int used_V[n]; // represents amount used from warehouse V for each factory Fi
  25. for (int i = 0; i < n; ++i)
  26. {
  27. int diff = |vi - wi|;
  28. sorted.insert(i, diff);
  29. }
  30. tuple factory = sorted.pop()
  31. while (factory)
  32. {
  33. if (vi > wi)
  34. {
  35. int temp = rV - di;
  36. if (temp >= 0)
  37. {
  38. rV -= di;
  39. used_V[factory.index] = di;
  40. }
  41. else
  42. {
  43. rW -= di - rV;
  44. used_V[factory.index] = rV;
  45. rV = 0;
  46. }
  47. }
  48. else wi > vi
  49. {
  50. int temp = rW - di;
  51. if (temp >= 0)
  52. {
  53. rW -= di;
  54. used_V[factory.index] = 0;
  55. }
  56. else
  57. {
  58. rV -= di - rW;
  59. used_V[factory.index] = di - rW;
  60. rW = 0;
  61. }
  62. }
  63. }
  64. return used_V;
  65. \end{verbatim}
  66. \subsection{Time Complexity}
  67. First, we initalize 2 empty arrays and 1 empty max heap. This should take constant time.
  68. Next, n times, we perform constant time subtraction and a log(n) time heap insert. This is a total of $\Theta$(n logn) time.
  69. Finally, we loop through the max heap of length n, each time performing a limited amount of integer comparisons and array/tuple acceses/writes, so constant time operations. This means we are again $\Theta$(n logn), as each iteration is a log(n) heap pop, and constant time operations to populate the used\textunderscore{}V output array.
  70. \subsection{Correctness}
  71. Assume for the sake of contradiction that for some greedy solution A, there exists a better optimal solution B. This means that there must be at least 1 factory in solution B where it uses more paper from the cheaper fatory. This means that for this factory, the greedy solution uses more of the more expensive factory than the optimal solution. However, by definition, the greedy solution could only have done so if there was another factory for which the difference in price per good was higher or equal to the cost of this factory using more of the worse choice. If the difference in the second factory was higher, than the greedy would be better than the optimal, which is impossible. So, this means they have to be equal, meaning that there cannot be a better optimal solution, as they must be equal.
  72. \end{document}