talk.tex 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. % Copyright (C) 2017 Koz Ross <koz.ross@retro-freedom.nz>
  2. % This work is licensed under the Creative Commons Attribution-ShareAlike 4.0
  3. % International License. To view a copy of this license, visit
  4. % http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to Creative
  5. % Commons, PO Box 1866, Mountain View, CA 94042, USA.
  6. \documentclass{beamer}
  7. \usepackage[utf8]{inputenc}
  8. \usecolortheme{seahorse}
  9. \usefonttheme{professionalfonts}
  10. \usepackage{fontspec}
  11. \setmainfont{DejaVu Sans}
  12. \setbeamertemplate{navigation symbols}{}
  13. \usepackage{caption}
  14. \usepackage[font=itshape, begintext=``, endtext='']{quoting}
  15. \usepackage{fancyvrb}
  16. \usepackage{color}
  17. \usepackage[noend]{algpseudocode}
  18. \usepackage{amsmath}
  19. \title{Sorting}
  20. \subtitle{Or: some of what Koz spent about two years of his life on}
  21. \titlegraphic{\includegraphics[scale=0.8]{img/logo.pdf}}
  22. \author{Koz Ross}
  23. \date{5th October, 2017}
  24. \begin{document}
  25. \begin{frame}[c]
  26. \titlepage{}
  27. \end{frame}
  28. \begin{frame}[c]
  29. \frametitle{Outline}
  30. \tableofcontents
  31. \end{frame}
  32. \section{Introduction}
  33. \begin{frame}[fragile, c]
  34. \frametitle{What is sorting and why we care}
  35. {\em Sorting\/} is putting things in some kind of order.\pause{} This is very
  36. useful, for a range of reasons:\pause{}
  37. \begin{itemize}
  38. \item Some tasks are impossible without it (e.g.\ finding the
  39. median)\pause{}
  40. \item Some tasks are much easier on sorted data (e.g.\ searching for a
  41. specific item)\pause{}
  42. \item Generalizes many other tasks (e.g.\ top-$k$ queries)\pause{}
  43. \item Sorting is used {\em everywhere\/}\pause{}
  44. \end{itemize}
  45. \bigskip
  46. As a result, sorting is one of {\em the\/} oldest computer science problems we
  47. have, and has been studied {\em to death}.
  48. \end{frame}
  49. \begin{frame}[fragile,c]
  50. \frametitle{A little tour}
  51. \pause{}
  52. \begin{itemize}
  53. \item A precise definition of sorting\pause{}
  54. \item Some known sorting algorithms\pause{}
  55. \item Inherent limit on the performance of {\em any\/} algorithm\pause{}
  56. \item Limitations of this result\pause{}
  57. \end{itemize}
  58. \bigskip
  59. Without further ado, let's commence!
  60. \end{frame}
  61. \section{Defining sorting precisely}
  62. \begin{frame}[fragile,c]
  63. \frametitle{Some basics}
  64. Let $\mathbb{N} = \{0, 1, 2, \ldots\}$ be the set of {\em natural
  65. numbers}.\pause{} A {\em $n$-tuple\/} $t = (a_0, a_1, \ldots, a_{n-1})$ is a
  66. collection of elements with fixed positions. We use $t[i]$ to denote $a_i$ for
  67. $i \in 0,1,\ldots,n - 1$.\pause{}
  68. \begin{definition}
  69. Let $A,B$ be sets. The {\em Cartesian product\/} of $A$ and $B$ is the set
  70. \[
  71. A \times B = \{(x, y) \mid x \in A, y \in B\}
  72. \]
  73. \end{definition}\pause{}
  74. \begin{example}
  75. Let $A = \{1, 2, 3\}, B = \{a, b\}$. $A \times B$ is
  76. \[
  77. \{(1,a), (1,b), (2,a), (2,b), (3,a), (3,b)\}
  78. \]
  79. \end{example}\pause{}
  80. We denote the special case of $A \times A$ as $A^2$.
  81. \end{frame}
  82. \begin{frame}[fragile,c]
  83. \frametitle{Relations}
  84. \begin{definition}
  85. Let $A,B$ be sets. A {\em (binary) relation between $A$ and $B$\/} is $R \subseteq A
  86. \times B$. For $x \in A, y \in B$, we write $xRy$ to mean $(x,y) \in R$.
  87. \end{definition}\pause{}
  88. \bigskip
  89. We can think of a relation as explicitly spelling out what things in $A$ and
  90. $B$ are related.
  91. \end{frame}
  92. \begin{frame}[fragile,c]
  93. \frametitle{Ordering}
  94. \begin{definition}
  95. Let $S$ be a set. A {\em (non-strict) ordering on $S$\/} is a binary relation
  96. $\leqq \subseteq S^2$, such that $\leqq$ has the following
  97. properties:\pause{}
  98. \begin{description}[Antisymmetry:]
  99. \item[Antisymmetry:] For any $a,b \in S$, if $a \leqq b$ and $b \leqq a$,
  100. then $a = b$;\pause{}
  101. \item[Transitivity:] For any $a,b,c \in S$, if $a \leqq b$ and $b \leqq
  102. c$, then $a \leqq c$; and\pause{}
  103. \item[Totality:] For any $a,b \in S$, $a \leqq b$ or $b \leqq a$.\pause{}
  104. \end{description}
  105. \end{definition}
  106. \bigskip
  107. We can read $a \leqq b$ as `$a$ does not come after $b$ according to the
  108. ordering $\leqq$'.\pause{} We can have multiple orderings on various sets.
  109. \end{frame}
  110. \begin{frame}[fragile,c]
  111. \frametitle{Ordering examples}
  112. Consider $\mathbb{N}$. We can see that $\leq$ is an ordering on
  113. $\mathbb{N}$:\pause{}
  114. \begin{description}[Antisymmetry:]
  115. \item[Antisymmetry:] If we have two natural numbers $x,y$ such that $x \leq
  116. y$ and $y \leq x$, they must be equal;\pause{}
  117. \item[Transitivity:] We can `chain together' $\leq$ in exactly this
  118. way;\pause{}
  119. \item[Totality:] Any two natural numbers can be compared with
  120. $\leq$.\pause{}
  121. \end{description}
  122. We can also do something similar with $\geq$. If we look at other sets, we get
  123. many other orderings (e.g.\ lex and reverse lex for strings).
  124. \end{frame}
  125. \begin{frame}[fragile,c]
  126. \frametitle{Defining sorting}
  127. \pause{}
  128. \begin{definition}
  129. Let $S$ be a set, $t$ be an $n$-tuple of elements of $S$, and $\leqq$ be an
  130. ordering on $S$. Given $t,\leqq$ as input, the {\em sorting problem\/} requires
  131. us to return an $n$-tuple $t^{\prime}$ such that:\pause{}
  132. \begin{itemize}
  133. \item Every element of $t$ is an element of $t^{\prime}$; and\pause{}
  134. \item For any $i, j \in 0, 1, \ldots, n-1$, if $i \leq j$ then
  135. $t^{\prime}[i] \leqq t^{\prime}[j]$.\pause{}
  136. \end{itemize}
  137. \end{definition}
  138. \bigskip
  139. Put simply, the sorting problem requires us to put $t$ `in order' according to
  140. $\leqq$.
  141. \end{frame}
  142. \section{Some sorting algorithms}
  143. \begin{frame}[fragile,c]
  144. \frametitle{Shifting to algorithms}
  145. Since we're dealing with algorithms, we have to be precise with our
  146. assumptions. We also want to be as general as possible.\pause{}
  147. \begin{definition}
  148. A {\em comparison sort\/} is an algorithm for the sorting problem that does
  149. not make any additional assumptions about the input tuple, aside from what
  150. is required by the sorting problem.
  151. \end{definition}\pause{}
  152. \bigskip
  153. We can view this in several ways:\pause{}
  154. \begin{itemize}
  155. \item Most general kind of sort (assumes only what it must)\pause{}
  156. \item Most `difficult' sort (no additional `hacks' we can lean on based on
  157. the data)\pause{}
  158. \item Closest to a `pure' view of how hard the sorting problem is (no `extra
  159. baggage' to confuse analysis)
  160. \end{itemize}
  161. \end{frame}
  162. \begin{frame}[fragile,c]
  163. \frametitle{Our approach to analysis}
  164. We'll take the `default' for algorithm analysis:\pause{}
  165. \bigskip
  166. \begin{description}[Asymptotic:]
  167. \item[RAM model:]
  168. \begin{itemize}
  169. \item Serial processor (can only do one thing at a time)\pause{}
  170. \item Non-hierarchical, addressable memory\pause{}
  171. \item Primitive operations take constant time\pause{}
  172. \end{itemize}
  173. \item[Asymptotic:]
  174. \begin{itemize}
  175. \item Based on the size of the input\pause{}
  176. \item Care about the {\em growth rate\/} of the time required\pause{}
  177. \end{itemize}
  178. \item[Worst-case:]
  179. \begin{itemize}
  180. \item If we'd have different results for same-size inputs, take the
  181. worst one\pause{}
  182. \item All input tuple elements are unique\pause{}
  183. \item Tuple elements are `random' (i.e.\ no sorted sub-sequences)
  184. \end{itemize}
  185. \end{description}
  186. \end{frame}
  187. \begin{frame}[fragile,c]
  188. \frametitle{Algorithms we know}
  189. \pause{}
  190. \begin{description}[`Fast' ($O(n \log(n))$):]
  191. \item[`Slow' ($O(n^2)$):] Bubble sort, insertion sort, selection sort,
  192. etc.\pause{}
  193. \item[`Fast' ($O(n \log(n))$):] Mergesort, quicksort, introsort, timesort,
  194. etc.\pause{}
  195. \end{description}
  196. \bigskip
  197. Can we do better than this? Is there {\em any\/} algorithm that can beat the
  198. `fast' ones?\pause{} \alert{No.}
  199. \end{frame}
  200. \section{Limits on performance}
  201. \begin{frame}[fragile, c]
  202. \begin{centering}
  203. \includegraphics[scale=0.5]{img/bigger-preliminaries.pdf}
  204. \end{centering}
  205. \end{frame}
  206. \begin{frame}[fragile,c]
  207. \frametitle{Factorials and permutations}
  208. \begin{definition}
  209. Let $n \in \mathbb{N}$. The {\em factorial of $n$\/} is
  210. \[
  211. n! = \begin{cases}
  212. 1 & \text{if } n = 0\text{; and}\\
  213. n \cdot n - 1 & \text{otherwise}\\
  214. \end{cases}
  215. \]
  216. Alternatively,
  217. \[
  218. n! = \prod_{i=1}^{n} i = 1 \times 2 \times \cdots \times n
  219. \]
  220. \end{definition}\pause{}
  221. \begin{definition}
  222. Let $S$ be a set of $n$ elements. A {\em permutation of $S$\/} is an $n$-tuple
  223. of unique elements of $S$.
  224. \end{definition}\pause{}
  225. \begin{example}
  226. Two possible permutations of $S = \{1, 2, 3\}$ are $(1,3,2), (2, 3, 1)$.
  227. \end{example}
  228. \end{frame}
  229. \begin{frame}[fragile,c]
  230. \frametitle{Relating factorials and permutations}
  231. \begin{lemma}
  232. Let $S$ be a set of $n$ elements. There are $n!$ possible permutations of
  233. $S$.
  234. \end{lemma}\pause{}
  235. \begin{proof}
  236. We use induction on $n$.\pause{} When $n = 0$, we
  237. observe that the only permutation is $()$. As $0! =
  238. 1$, the lemma holds for $n = 0$.\pause{}
  239. \medskip
  240. When $n > 0$, suppose that the lemma holds to some $k \geq 0$. Thus, there
  241. are $k!$ permutations of any $k$-element set $S$.\pause{} Without loss of
  242. generality, we observe that any
  243. $k+1$-element set $S^{\prime}$ is equal to some $k$-element set $S$ with an
  244. additional element $u \not\in S$.\pause{} Thus, to convert a permutation of
  245. $S$ into a permutation $p$ of $S^{\prime}$, we need to `insert' $u$ into
  246. $p$.\pause{} As
  247. there are $k+1$ possible positions to insert into for each permutation of
  248. $S$, the number of
  249. possible permutations of $S^{\prime}$ is thus $k!(k+1) = (k+1)!$. Thus, the
  250. lemma holds for all $n$.
  251. \end{proof}
  252. \end{frame}
  253. \begin{frame}[fragile,c]
  254. \frametitle{Why this matters}
  255. \pause{}
  256. Given our assumptions (worst-case), the sorting problem basically involves
  257. finding a specific permutation (the one where everything is in the right
  258. place).\pause{} Thus, the amount of work {\em any\/} comparison sort will have
  259. to do will be based on $n!$ somehow.\pause{}
  260. \bigskip
  261. The factorial function is a gigantic pain to analyze. Let's make our lives
  262. simpler:\pause{}
  263. \begin{definition}[Stirling approximation]
  264. $\log_2(n!)$ is $O(n\log(n))$ and $n\log(n)$ is $O(\log_2(n!))$.
  265. \end{definition}\pause{}
  266. \bigskip
  267. Essentially, the Stirling approximation tells us that $\log_2(n!)$ and $n
  268. \log(n)$ grow at the same rate asymptotically.\pause{} We say $\log_2(n!)$ is
  269. $\Theta(n\log(n))$ in such a case.
  270. \end{frame}
  271. \begin{frame}[fragile,c]
  272. \frametitle{The proof, at last}
  273. \begin{theorem}
  274. Any comparison sort must perform at least $\Theta(n\log(n))$ comparisons
  275. between elements of the input.
  276. \end{theorem}\pause{}
  277. \begin{proof}
  278. In order to do its work, a comparison sort must find one specific
  279. permutation out of $n!$ possibilities.\pause{} Each comparison we perform
  280. can eliminate at most half of the possible permutations at that
  281. step.\pause{} Thus, any comparison sort must perform at least $\log_2(n!)$
  282. comparisons to ensure correctness.\pause{} By the Stirling approximation,
  283. this is $\Theta(n\log(n))$.
  284. \end{proof}\pause{}
  285. \begin{corollary}
  286. Under our assumptions, no comparison sort with a time complexity better than
  287. $\Theta(n\log(n))$ (and thus, $O(n\log(n))$) can exist.
  288. \end{corollary}
  289. \end{frame}
  290. \begin{frame}[fragile,c]
  291. \frametitle{Is there nothing we can do?}
  292. \pause{}
  293. This is definitely a strong result that transcends any particular algorithm.
  294. However:\pause{}
  295. \begin{itemize}
  296. \item There are {\em practical\/} improvements we can make:\pause{}
  297. \begin{itemize}
  298. \item Not all data will be this bad!\pause{}
  299. \item Not all $O(n\log(n))$ algorithms are born equal (consider timsort
  300. versus mergesort)\pause{}
  301. \end{itemize}
  302. \item We usually know more about our data (numbers, limited number of unique
  303. items, strings, etc)\pause{}
  304. \item RAM is not the most accurate model of modern computers:\pause{}
  305. \begin{itemize}
  306. \item Modern machines are parallel --- lots of different optimality
  307. points there!\pause{}
  308. \item Modern memory is {\em very\/} hierarchical --- also lots of
  309. optimality points to consider\pause{}
  310. \end{itemize}
  311. \item Data is not static or centralized anymore:\pause{}
  312. \begin{itemize}
  313. \item Interest in partial or online sorts\pause{}
  314. \item Fully-dynamic sorts (data can change in arbitrary ways)\pause{}
  315. \item Distributed computing
  316. \end{itemize}
  317. \end{itemize}
  318. \end{frame}
  319. \begin{frame}[fragile,c]
  320. \frametitle{The most important corollary}
  321. \begin{corollary}
  322. Know your data, your tools and your problem, and you can work (or discover)
  323. performance wonders.
  324. \end{corollary}\pause{}
  325. Still work to be done in this area --- for many years to come!
  326. \end{frame}
  327. \section{Questions}
  328. \begin{frame}[fragile, c]
  329. \frametitle{Questions?}
  330. \begin{center}
  331. \includegraphics[scale=0.27]{img/questions.pdf}
  332. \end{center}
  333. \end{frame}
  334. \end{document}