proof-of-correctness-pogo.tex 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. \documentclass[a4paper]{scrartcl}
  2. \usepackage{amssymb, amsmath} % needed for math
  3. \usepackage[utf8]{inputenc} % this is needed for umlauts
  4. \usepackage[ngerman]{babel} % this is needed for umlauts
  5. \usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
  6. \usepackage[margin=2.5cm]{geometry} %layout
  7. \usepackage{hyperref} % links im text
  8. \usepackage{parskip} % no indentation on new paragraphs
  9. \usepackage{color}
  10. \usepackage{framed}
  11. \usepackage{enumerate} % for advanced numbering of lists
  12. \usepackage{algorithm,algpseudocode}
  13. \usepackage{braket} % needed for \Set
  14. \clubpenalty = 10000 % Schusterjungen verhindern
  15. \widowpenalty = 10000 % Hurenkinder verhindern
  16. \hypersetup{
  17. pdfauthor = {Martin Thoma},
  18. pdfkeywords = {Google Code Jam, Round 1C 2013, Pogo},
  19. pdftitle = {Proof of correctness for an algorithm for pogo}
  20. }
  21. % From http://www.matthewflickinger.com/blog/archives/2005/02/20/latex_mod_spacing.asp
  22. % Thanks!
  23. \makeatletter
  24. \def\imod#1{\allowbreak\mkern10mu({\operator@font mod}\,\,#1)}
  25. \makeatother
  26. \renewcommand{\algorithmicrequire}{\textbf{Input: }}
  27. \renewcommand{\algorithmicensure}{\textbf{Output: }}
  28. \newenvironment{myindentpar}[1]%
  29. {\begin{list}{}%
  30. {\setlength{\leftmargin}{#1}}%
  31. \item[]%
  32. }
  33. {\end{list}}
  34. \begin{document}
  35. \section{The Problem}
  36. You're on a two-dimensional grid $\mathbb{Z} \times \mathbb{Z}$ and
  37. have to find a way to get to one coordinate $(x,y) \in \mathbb{Z} \times \mathbb{Z}$. You start at
  38. $(0, 0)$.
  39. In your
  40. $i$-th step you move either $\underbrace{(+i,0)}_{=: E}$,
  41. $\underbrace{(-i,0)}_{=: W}$, $\underbrace{(0,+i)}_{=: N}$ or
  42. $\underbrace{(0,-i)}_{=: S}$.
  43. \section{The algorithm}
  44. \begin{algorithm}
  45. \begin{algorithmic}
  46. \Function{calculateSteps}{$x \in \mathbb{Z}$, $y \in \mathbb{Z}$}
  47. \State $s \gets 0$
  48. \State $dist \gets |x| + |y|$
  49. \\
  50. \While{$\overbrace{\frac{s^2 + s}{2} < dist}^\text{condition 1}$ or $\overbrace{\frac{s^2 + s}{2} \not\equiv dist \imod{2}}^\text{condition 2}$}
  51. \State $s \gets s + 1$
  52. \EndWhile
  53. \\
  54. \State \Return $s$
  55. \EndFunction
  56. \end{algorithmic}
  57. \caption{Algorithm to calculate the minimum amount of steps}
  58. \label{alg:calculateSteps}
  59. \end{algorithm}
  60. \clearpage
  61. \begin{algorithm}[ht!]
  62. \begin{algorithmic}[ht!]
  63. \Function{solvePogo}{$x \in \mathbb{Z}$, $y \in \mathbb{Z}$}
  64. \State $s_{\min} \gets$ \Call{calculateSteps}{$x, y$}
  65. \\
  66. \State $solution \gets \varepsilon$
  67. \For{$i$ in $s_{\min}, \dots, 1$}
  68. \If{$|x| > |y|$}
  69. \If{$x > 0$}
  70. \State $solution \gets solution + E$
  71. \State $x \gets x - i$
  72. \Else
  73. \State $solution \gets solution + W$
  74. \State $x \gets x + i$
  75. \EndIf
  76. \Else
  77. \If{$y > 0$}
  78. \State $solution \gets solution + N$
  79. \State $y \gets y - i$
  80. \Else
  81. \State $solution \gets solution + S$
  82. \State $y \gets y + i$
  83. \EndIf
  84. \EndIf
  85. \EndFor
  86. \\
  87. \State \Return \Call{reverse}{$solution$}
  88. \EndFunction
  89. \end{algorithmic}
  90. \caption{Algorithm to solve the pogo problem}
  91. \label{alg:solvePogo}
  92. \end{algorithm}
  93. \section{Correctness}
  94. \subsection{calculateSteps}
  95. Let $x,y \in \mathbb{Z}$ and $s := \Call{calculateSteps}{x, y}$.
  96. Let $s_{\min}$ be the minimum amount of necessary steps to get from $(0,0)$
  97. to $(x,y)$ when you move $i$ units in your $i$'th step.
  98. \textbf{Theorem: } $s = s_{\min}$
  99. It's enough to proof $s \geq s_{\min}$ and $s \leq s_{\min}$.
  100. \begin{myindentpar}{1cm}
  101. \textbf{Theorem: } $s \leq s_{\min}$ (we don't make too many steps)
  102. \textbf{Proof: }
  103. \begin{myindentpar}{1cm}
  104. We have to get from $(0,0)$ to $(x, y)$. As we may only move in
  105. taxicab geometry we have to use the taxicab distance measure $d_1$:
  106. \[d_1 \left (p, q \right ) := \sum_{i=1}^2 |p_i -q_i|\]
  107. So in our scenario:
  108. \[d_1 \left ((0,0), (x,y) \right ) = |x| + |y|\]
  109. This means we have to move at least $|x| + |y|$ units to get
  110. from $(0,0)$ to $(x, y)$. As we move $i$ units in the $i$'th step,
  111. we have to solve the following equations for $s_{\min1}$:
  112. \begin{align}
  113. \sum_{i=1}^{s_{\min1}} i &\geq |x| + |y| &&\text{ and } &|x| + |y| &> \sum_{i=1}^{s_{\min1} - 1} i\\
  114. \frac{s_{\min1}^2 + s_{\min1}}{2} &\geq |x| + |y| && & &> \sum_{i=1}^{s_{\min1} - 1} i &
  115. \end{align}
  116. This is what algorithm \ref{alg:calculateSteps} check with \texttt{condition 1}.
  117. As the algorithm increases $s$ only by one in each loop, it makes
  118. sure that $\sum_{i=1}^{s_{\min1} - 1} i$ is bigger than $|x| + |y|$.
  119. You can undo moves by going back. But this will always make an even
  120. number undone. When you go $(+i, 0)$ and later $(-j, 0)$ it is the
  121. same as if you've been going $(i-j, 0)$. So $2\cdot i$ steps got undone.
  122. But $2\cdot i$ is an even number. You will never be able to undo
  123. an odd number of moved units. This means, the parity of the minimum
  124. number of units you would have to move if you would move one unit per
  125. step has to be the same as the parity of the moves you actually do.
  126. This is exactly what \texttt{condition 2} makes sure.
  127. So we need at least $s$ steps $\Rightarrow s \leq s_{\min} \square$
  128. \end{myindentpar}
  129. \textbf{Theorem: } $s \geq s_{\min}$ (we make enough steps)
  130. \textbf{Proof: }
  131. \begin{myindentpar}{1cm}
  132. We chose $s$ in a way that \texttt{condition 1} is true.
  133. As we have to go $i \in 1,\dots,s$, we can get every possible sum $\Sigma \in \Set{-\frac{s^2+s}{2}, \dots +\frac{s^2+s}{2}}$
  134. with a subset of $\Set{1, \dots, s}$\footnote{This can easily be proved by induction over $\Sigma$.}.
  135. This means we can make a partition $(A, \underbrace{\Set{1, \dots, s} \setminus A}_{=: B})$
  136. such that $|\sum_{i \in A} i| = |x|$ and $|\sum_{i \in B} i|-2\cdot j = |y|$.
  137. This means, we can reach $(x,y)$ from $(0,0)$.
  138. \end{myindentpar}
  139. \end{myindentpar}
  140. \subsection{solvePogo}
  141. \textbf{Theorem: } \Call{solvePogo}{$x,y$} returns a valid, minimal sequence of steps to get from $(0, 0)$ to $(x,y)$
  142. \textbf{Proof: }
  143. \begin{myindentpar}{1cm}
  144. As $s_{\min}$ is the minimum amount of steps you need to get from
  145. $(0,0)$ to $(x,y)$, \Call{solvePogo}{$x,y$} will return a minimal
  146. sequence of steps to get from $(0, 0)$ to $(x,y)$ (see proof above).
  147. We only have to prove that the sequence of steps that \Call{solvePogo}{$x,y$}
  148. is valid, i.e. that you will get from $(0,0)$ to $(x,y)$ with the given
  149. sequence.
  150. TODO.
  151. \end{myindentpar}
  152. \end{document}