talk.tex 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. % Copyright (C) 2017 Koz Ross <koz.ross@retro-freedom.nz>
  2. % Copyright (C) 2008 Karta24
  3. % This work is licensed under the Creative Commons Attribution-ShareAlike 4.0
  4. % International License. To view a copy of this license, visit
  5. % http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to Creative
  6. % Commons, PO Box 1866, Mountain View, CA 94042, USA.
  7. % Karta's attribution applies to the image '2-generals-dead-messenger.svg', per
  8. % the requirements of the CC-BY-SA license.
  9. \documentclass[style=fyma]{powerdot}
  10. \usepackage{enumitem}
  11. \usepackage{graphicx}
  12. \usepackage{amsmath}
  13. \usepackage{amsfonts}
  14. \usepackage{amsthm}
  15. \usepackage[font={itshape},begintext={``},endtext={''}]{quoting}
  16. \usepackage{algpseudocode}
  17. \usepackage[normalem]{ulem}
  18. \usepackage{xcolor}
  19. \usepackage{booktabs}
  20. \newcommand\redsout{\bgroup\markoverwith{\textcolor{red}{\rule[0.5ex]{2pt}{0.4pt}}}\ULon}
  21. \newtheorem*{theorem}{Theorem}
  22. \newtheorem*{observation}{Observation}
  23. \pdsetup{theslide=,
  24. palette=gray}
  25. \begin{document}
  26. \begin{slide}[toc=]{}
  27. \begin{center}
  28. {\LARGE \textbf{Introduction to asymptotic analysis} \par}
  29. {\Large Or: How computer science is like cooking \par}
  30. \vspace{1cm}
  31. \includegraphics[scale=0.75]{logo.eps}
  32. \vspace{1cm}
  33. \linebreak
  34. Koz Ross
  35. \medskip
  36. March 30, 2017
  37. \end{center}
  38. \end{slide}
  39. \begin{slide}[toc=]{Overview}
  40. \tableofcontents[content=sections]
  41. \end{slide}
  42. \section{What are algorithms, and why do we care?}
  43. \begin{slide}{Definition of an algorithm}
  44. \begin{quoting}
  45. A procedure, described in a finite number of instructions, for transforming
  46. inputs into an output to solve a specific problem. The instructions must be
  47. unambiguous, guaranteed to terminate, and solve the problem correctly in all
  48. cases.
  49. \end{quoting}\pause{}
  50. \medskip
  51. Alternatively, you can think of an algorithm as a {\em recipe\/}:\pause{}
  52. \begin{itemize}
  53. \item The ingredients are the {\em inputs\/}\pause{}
  54. \item The recipe directions are the {\em instructions\/}\pause{}
  55. \item The (hopefully!) resulting food is the {\em output\/}
  56. \end{itemize}
  57. \end{slide}
  58. \begin{slide}{Why we care}
  59. \begin{itemize}
  60. \item Algorithms are {\em very\/} fundamental to computer science (which is
  61. why it's all about cooking!)\pause{}
  62. \item They define what we can do {\em at all}, and what we can do {\em
  63. efficiently\/}\pause{}
  64. \item Improved algorithms lead to improved everything-else\pause{}
  65. \item `Ready-baked' efficient solutions to many problems (theoretical {\em
  66. and\/} practical)\pause{}
  67. \item If you don't know this stuff, everything you do on a computer will run
  68. like hot garbage, and you won't know {\em why}, or what to do about
  69. it\pause{}
  70. \end{itemize}
  71. \medskip
  72. Thus, we really need to be able to {\em compare\/} algorithms to each other,
  73. and also {\em understand\/} why they run the way they do, if we have any hope
  74. of making them do our bidding.
  75. \end{slide}
  76. \begin{slide}{What do we mean by `efficient'?}
  77. \vspace{1cm}
  78. {\em Efficiency\/} is ultimately doing more with less effort. We can measure
  79. the `effort' required to run an algorithm in two ways:\pause{}
  80. \begin{itemize}
  81. \item How much {\em time\/} we need to solve a problem instance; and\pause{}
  82. \item How much {\em space\/} (in terms of memory) we would have to use as
  83. part of this process.\pause{}
  84. \end{itemize}
  85. \medskip
  86. Determining these two factors is an important part of {\em algorithm
  87. analysis}.\pause{} Additionally, any method we use must explain {\em why\/} we
  88. get the performance it claims.
  89. \end{slide}
  90. \section{Building up}
  91. \begin{slide}{Analysis by implementation}
  92. \begin{quoting}
  93. To analyze the efficiency of an algorithm, implement it, run it on some inputs,
  94. and measure the time required and the memory used.
  95. \end{quoting}\pause{}
  96. \medskip
  97. Not bad {\em per se}, but has a few notable downsides:\pause{}
  98. \begin{itemize}
  99. \item Very labour-intensive (programming is hard!)\pause{}
  100. \item Too many external factors (programmer skill, language, OS, hardware,
  101. workload$\ldots$)\pause{}
  102. \item The test data problem (what do we test against?)\pause{}
  103. \end{itemize}
  104. \medskip
  105. While this method is useful at the {\em end\/} of the process, it's not a very
  106. good analytical tool in general.\pause{} To do better than this, we need a
  107. different view of the problem, as well as a different method.
  108. \end{slide}
  109. \begin{slide}{A model}
  110. In sciences (of all sorts), a {\em model\/} is a way of looking at the world
  111. which:\pause{}
  112. \begin{itemize}
  113. \item {\em Minimizes\/} the impact of factors we {\em don't\/} care about\pause{}
  114. \item Stays {\em true to the reality\/} of factors we {\em do\/} care
  115. about\pause{}
  116. \item {\em Explains\/} why phenomena occur like they do.\pause{}
  117. \end{itemize}
  118. \medskip
  119. Examples of models include:\pause{}
  120. \begin{itemize}
  121. \item The Standard Model\pause{}
  122. \item The orbital model\pause{}
  123. \item Statistical models\pause{}
  124. \end{itemize}
  125. Models allow us to {\em analyze\/} and {\em understand\/} their subject matter
  126. without having to `manually verify' everything.
  127. \end{slide}
  128. \begin{slide}[toc=]{A model {\em of a computer\/}}
  129. Because algorithms ultimately run on computers, our model must be of a
  130. computer.\pause{}
  131. \medskip
  132. There are {\em many\/} choices of model, depending on what kind of computer we
  133. want to study, as well as what aspects of it interest us.\pause{}
  134. \medskip
  135. The most traditional (and foundational) model for algorithm analysis is the
  136. {\em random access model\/} (RAM).\pause{} This closely represents a computer at the
  137. time when algorithm analysis first became a topic in its own right (around
  138. 1950).
  139. \end{slide}
  140. \begin{slide}[toc=]{The random access model}
  141. In RAM, we have access to:\pause{}
  142. \begin{itemize}
  143. \item A single, sequential {\em processing unit\/} (PU)\pause{}
  144. \item {\em Addressable memory}, divided into words of $w$ bits\pause{}
  145. \end{itemize}
  146. A {\em primitive instruction\/} is any of the following:\pause{}
  147. \begin{itemize}
  148. \item Arithmetic ($+, -, \cdot, \div$) on $w$-bit integers\pause{}
  149. \item Comparison ($<, >, \leq, \geq, =, \neq$) of two $w$-bit
  150. integers or flags\pause{}
  151. \item Allocation of any number of consecutively-addressed words\pause{}
  152. \item Given an address, a one-word read or write there\pause{}
  153. \item Conditional or unconditional branch\pause{}
  154. \item A procedure call\pause{}
  155. \item A return of a value\pause{}
  156. \end{itemize}
  157. Any primitive instruction requires one {\em time unit\/} to execute.
  158. \end{slide}
  159. \begin{slide}{Time and space complexity}
  160. Let $A$ be an algorithm, and $n$ be the size of the largest input to
  161. $A$.\pause{}
  162. \medskip
  163. We define the {\em time complexity of $A$ for inputs of size
  164. $n$\/} (written $T_{A}(n)$) as the number of primitive instructions that the PU
  165. must execute to produce correct output for $A$ from any inputs of largest size
  166. $n$.\pause{}
  167. \medskip
  168. We define the {\em space complexity of $A$ for inputs of size $n$\/} (written
  169. $S_{A}(n)$) as the number of words of memory that we must allocate to produce
  170. correct output for $A$ from any inputs of largest size $n$.\pause{}
  171. \medskip
  172. If there are multiple possible values for $T_{A}(n), S_{A}(n)$ for a
  173. particular $n$, we take the {\em highest\/} possible value.\pause{} Thus, our
  174. analysis is {\em worst-case}.\pause{}
  175. \end{slide}
  176. \begin{slide}{An example algorithm}
  177. Problem: {\em Given a non-empty array $\mathsf{arr}$ of one-word integers,
  178. find the largest integer in $\mathsf{arr}$}.\pause{}
  179. \begin{algorithmic}
  180. \Function{$\mathsf{Max}$}{$\mathsf{arr}$}
  181. \State{}$\mathsf{x} \gets \mathsf{arr}[1]$
  182. \For{$\mathsf{i} \in 2, 3, \ldots$
  183. \Call{$\mathsf{,length}$}{$\mathsf{arr}$}}
  184. \If{$\mathsf{x} < \mathsf{arr}[\mathsf{i}]$}
  185. \State{}$\mathsf{x} \gets \mathsf{arr}[\mathsf{i}]$
  186. \EndIf{}
  187. \EndFor{}
  188. \State{}\Return{}$\mathsf{x}$
  189. \EndFunction{}
  190. \end{algorithmic}\pause{}
  191. \medskip
  192. $T_{\mathsf{Max}}(n) = $ \pause{} $4 + 6(n - 1) + 1 = 6n - 1$\pause{}
  193. \medskip
  194. $S_{\mathsf{Max}}(n) = $ \pause{} $2$
  195. \end{slide}
  196. \section{Going asymptotic}
  197. \begin{slide}{Asymptotic analysis}
  198. Ultimately, we don't actually care about what $T(n), S(n)$ {\em are}.\pause{}
  199. What really matters is {\em how they behave as their input sizes
  200. grow}.\pause{} Our current method focuses too much on the former, and not
  201. enough on the latter.
  202. \medskip
  203. Thus, we make the following assumptions:\pause{}
  204. \begin{itemize}
  205. \item Input sizes can grow arbitrarily large\pause{}
  206. \item Constant additions or factors are irrelevant\pause{}
  207. \item Out of multiple $n$-terms, only the {\em largest\/} matters\pause{}
  208. \item We will talk about rate of growth, {\em not\/} value\pause{}
  209. \end{itemize}
  210. \medskip
  211. We call this kind of analysis {\em asymptotic}.\pause{} This is because the
  212. input can grow as big as we like, and we're only interested in how the
  213. running time (or space) changes with the growth of the input.
  214. \end{slide}
  215. \begin{slide}{Reviewing the example}
  216. \begin{algorithmic}
  217. \Function{$\mathsf{Max}$}{$\mathsf{arr}$}
  218. \State{}$\mathsf{x} \gets \mathsf{arr}[1]$
  219. \For{$\mathsf{i} \in 2, 3, \ldots$
  220. \Call{$\mathsf{,length}$}{$\mathsf{arr}$}}
  221. \If{$\mathsf{x} < \mathsf{arr}[\mathsf{i}]$}
  222. \State{}$\mathsf{x} \gets \mathsf{arr}[\mathsf{i}]$
  223. \EndIf{}
  224. \EndFor{}
  225. \State{}\Return{}$\mathsf{x}$
  226. \EndFunction{}
  227. \end{algorithmic}
  228. \medskip
  229. $T_{\mathsf{Max}}(n) = 6n - 1$
  230. \medskip
  231. $S_{\mathsf{Max}}(n) = 2$
  232. \end{slide}
  233. \begin{slide}[toc=]{Reviewing the example}
  234. \begin{algorithmic}
  235. \Function{$\mathsf{Max}$}{$\mathsf{arr}$}
  236. \State{}$\mathsf{x} \gets \mathsf{arr}[1]$
  237. \For{$\mathsf{i} \in 2, 3, \ldots$
  238. \Call{$\mathsf{,length}$}{$\mathsf{arr}$}}
  239. \If{$\mathsf{x} < \mathsf{arr}[\mathsf{i}]$}
  240. \State{}$\mathsf{x} \gets \mathsf{arr}[\mathsf{i}]$
  241. \EndIf{}
  242. \EndFor{}
  243. \State{}\Return{}$\mathsf{x}$
  244. \EndFunction{}
  245. \end{algorithmic}
  246. \medskip
  247. $T_{\mathsf{Max}}(n) =$ \redsout{$6$}$n$ \redsout{$- 1$}
  248. \medskip
  249. $S_{\mathsf{Max}}(n) = 2$
  250. \end{slide}
  251. \begin{slide}[toc=]{Reviewing the example}
  252. \begin{algorithmic}
  253. \Function{$\mathsf{Max}$}{$\mathsf{arr}$}
  254. \State{}$\mathsf{x} \gets \mathsf{arr}[1]$
  255. \For{$\mathsf{i} \in 2, 3, \ldots$
  256. \Call{$\mathsf{,length}$}{$\mathsf{arr}$}}
  257. \If{$\mathsf{x} < \mathsf{arr}[\mathsf{i}]$}
  258. \State{}$\mathsf{x} \gets \mathsf{arr}[\mathsf{i}]$
  259. \EndIf{}
  260. \EndFor{}
  261. \State{}\Return{}$\mathsf{x}$
  262. \EndFunction{}
  263. \end{algorithmic}
  264. \medskip
  265. $T_{\mathsf{Max}}(n) =$ \redsout{$6$}$n$ \redsout{$- 1$}
  266. \medskip
  267. $S_{\mathsf{Max}}(n) =$ \redsout{$2$}
  268. \end{slide}
  269. \begin{slide}[toc=]{Reviewing the example}
  270. \begin{algorithmic}
  271. \Function{$\mathsf{Max}$}{$\mathsf{arr}$}
  272. \State{}$\mathsf{x} \gets \mathsf{arr}[1]$
  273. \For{$\mathsf{i} \in 2, 3, \ldots$
  274. \Call{$\mathsf{,length}$}{$\mathsf{arr}$}}
  275. \If{$\mathsf{x} < \mathsf{arr}[\mathsf{i}]$}
  276. \State{}$\mathsf{x} \gets \mathsf{arr}[\mathsf{i}]$
  277. \EndIf{}
  278. \EndFor{}
  279. \State{}\Return{}$\mathsf{x}$
  280. \EndFunction{}
  281. \end{algorithmic}
  282. \medskip
  283. $T_{\mathsf{Max}}(n) =$ \redsout{$6$}$n$ \redsout{$- 1$} \hspace{1cm} \textcolor{red}{WRONG!}
  284. \medskip
  285. $S_{\mathsf{Max}}(n) =$ \redsout{$2$} \hspace{1.8cm} \textcolor{red}{WRONG!}
  286. \end{slide}
  287. \begin{slide}[toc=]{Reviewing the example}
  288. \begin{algorithmic}
  289. \Function{$\mathsf{Max}$}{$\mathsf{arr}$}
  290. \State{}$\mathsf{x} \gets \mathsf{arr}[1]$
  291. \For{$\mathsf{i} \in 2, 3, \ldots$
  292. \Call{$\mathsf{,length}$}{$\mathsf{arr}$}}
  293. \If{$\mathsf{x} < \mathsf{arr}[\mathsf{i}]$}
  294. \State{}$\mathsf{x} \gets \mathsf{arr}[\mathsf{i}]$
  295. \EndIf{}
  296. \EndFor{}
  297. \State{}\Return{}$\mathsf{x}$
  298. \EndFunction{}
  299. \end{algorithmic}
  300. \medskip
  301. $T_{\mathsf{Max}}(n) \text{ \textcolor{red}{is} } \textcolor{red}{O(n)}$
  302. \medskip
  303. $S_{\mathsf{Max}}(n) \text{ \textcolor{red}{is} }
  304. \textcolor{red}{O(1)}$\pause{}
  305. \medskip
  306. We also say ``the time complexity of $\mathsf{Max}$ is $O(n)$'', or even
  307. ``$\mathsf{Max}$ is $O(n)$''.\pause{} The second one is a bit unclear, so I'd
  308. avoid it.
  309. \end{slide}
  310. \begin{slide}{Benefits of asymptotic complexity}
  311. \vspace{1cm}
  312. \begin{itemize}
  313. \item {\em Describes\/} performance on the basis of how our time and space
  314. requirements will grow relative the size of the problem, not pegged to a
  315. particular machine (or even model!)\pause{}
  316. \item {\em Explains\/} performance by showing us what causes the growth to
  317. be as bad (or good) as it is in the algorithm itself\pause{}
  318. \item Eliminates `noise' constants, focusing our attention where it really
  319. counts\pause{}
  320. \item Separates algorithms into well-defined groupings\pause{}
  321. \item Can be determined (and verified) very quickly\pause{}
  322. \end{itemize}
  323. \medskip
  324. It's definitely not perfect though --- but that's material for another talk.
  325. \end{slide}
  326. \section{Questions}
  327. \end{document}