{"id":94177,"date":"2022-05-03T18:35:09","date_gmt":"2022-05-03T18:35:09","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=94177"},"modified":"2022-05-03T18:35:09","modified_gmt":"2022-05-03T18:35:09","slug":"combinations-permutations-and-derangements","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/theory-and-design\/combinations-permutations-and-derangements\/","title":{"rendered":"Combinations, permutations, and derangements"},"content":{"rendered":"<p>Before I get to the database stuff, here are a few mathematical preliminaries.<\/p>\n<h2>Factorials<\/h2>\n<p>The factorial function is usually written as <code>(n)!<\/code>, and it is defined as the product of the first <code>(n)<\/code> natural numbers. Thus,<code> 5!<\/code> evaluates to <code>(5 \u00b7 4 \u00b7 3 \u00b7 2 \u00b7 1) = 120<\/code>. As usual, zero becomes a special case, <code>0! = 1<\/code> which can be proven with a slightly different derivation of a factorial. Instead of defining it as a product, define it recursively, like this: <code>n! = CASE WHEN n = 0 THEN 1 ELSE n \u00b7 (n-1)! END.<\/code><\/p>\n<p>Showing the process one step at a time, the recursion unrolls like this:<\/p>\n<p>5! = 5 \u00b7 4!<br \/>\n4! = 4 \u00b7 3!<br \/>\n3! = 3 \u00b7 2!<br \/>\n2! = 2 \u00b7 1!<br \/>\n1! = 1 \u00b7 0!<\/p>\n<p>Look at the last step of the recursion. Now divide both sides by one to get <code>(1! \/ 1) = 0!<\/code> or <code>1 = 0!<\/code> Notice that everything done so far is procedural, not set-oriented. RDBMS folks prefer to get away from procedural code. For the rest of this article, you can think of <code>n!<\/code> as the number of ways to arrange <code>(n)<\/code> elements of a set into a sequence. Clearly, if you have one element, then you have only one arrangement. But likewise, if you have zero elements, you are also done. Just as you have only one empty set, so you also have only one empty sequence.<\/p>\n<p>Yes, it is possible to define factorials for negative numbers, imaginary numbers, the gamma function for real numbers, and other things. Unless you\u2019re a math major, you will have absolutely no use for any of these fancy tricks. Since SQL is a database language and not a computational one, you might want to use table lookup and not this recursive definition. You can find a <a href=\"https:\/\/www.mymathtables.com\/numbers\/100-factorial-tables-chart.html\">table of the numbers<\/a> one to hundred to populate the table. The factorial function gets big fast!<\/p>\n<p>0! = 1<br \/>\n1! = 1<br \/>\n2! = 2<br \/>\n3! = 6<br \/>\n4! = 24<br \/>\n5! = 120<br \/>\n6! = 720<br \/>\n7! = 5,040<br \/>\n8! = 40,320<br \/>\n9! = 362,880<br \/>\n10! =3,628,800<br \/>\n11! = 39,916,800<br \/>\n12! = 479,001,600<\/p>\n<h2>Combinations<\/h2>\n<p>Combinations assume there is a set of <code>(n)<\/code> distinct elements, and you want to get the number of subsets you can pull from this set. There is no concern with exactly what elements go in a particular subset, just the count. The order is not important.<\/p>\n<p>One common notation for this, though not the only notation, is <code>nCr<\/code> read as <em>Set of (n) things, Choose (r) of them.<\/em> Notice that <code>(0 \u2264 r \u2264 n)<\/code>, and that you can choose an empty set. Given a set, the number of subsets in it is <em>2n<\/em> ; For example, <em>{a, b, c}<\/em> has subsets <em>{}<\/em>,<em> {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}<\/em> for total count of 8.<\/p>\n<p>I\u2019ve never understood the term &#8216;combination locks&#8217; when the order of the numbers that you put into it is very important. Perhaps traditional mechanical security features were not designed by mathematicians.<\/p>\n<h2>Permutations<\/h2>\n<p>Permutations also assume there is a set of <code>(n)<\/code> distinct elements, and the goal is to get the number of sequences from this set in any order. However, in this case, the order does matter; <em>(a, b, c)<\/em> is not the same permutation as <em>(c, b, a)<\/em>.<\/p>\n<h2>Derangements<\/h2>\n<p>To be honest, I just think that the term &#8216;derangement&#8217; is really cool. Given two sequences of the same length and with the same data elements, one is a derangement of the other when the data elements do not appear in the same sequence position in the two sequences. For example, <em>(a, b, c)<\/em> is not a derangement of <em>(c, b, a)<\/em> because the data element <em>&#8216;b&#8217;<\/em> is in the second position in both sequences. Instead, there are two derangements, <em>(c, a, b)<\/em> and <em>(b, c, a)<\/em>.<\/p>\n<p>A derangement can also be called a permutation with no fixed points. The two notations for derangement of <code>(n)<\/code> elements are either <code>!(n)<\/code> or <code>D(n)<\/code>. I find the first one with the leading exclamation point to be a bad choice. It looks too much like a factorial.<\/p>\n<p>The number of derangements of a set with <code>(n)<\/code> distinct elements is given by the recursive formula : <code>D(n) = (n-1)[D(n-1) + D(n-2)]<\/code>. If you know <code>D(1) = 0<\/code> and <code>D(2) = 1<\/code>, you can generate subsequent values for <code>D(n)<\/code>. Or you would prefer : <code>D(n) = n D(n-1) + (-1)<sup>n<\/sup><\/code>?<\/p>\n<p>Obviously, the derangements will be fewer in number than the original permutation.<\/p>\n<p>Derangements of <code>n<\/code> Elements<\/p>\n<p>D(n)<br \/>\nD(0) = 1<br \/>\nD(1) = 0<br \/>\nD(2) = 1<br \/>\nD(3) = 2<br \/>\nD(4) = 9<br \/>\nD(5) = 44<br \/>\nD(6) = 265<br \/>\nD(7) = 1,854<br \/>\nD(8) = 14,833<br \/>\nD(9) = 133,496<br \/>\nD(10) = 1,334,961<br \/>\nD(11) = 14,684,570<br \/>\nD(12) = 76,214,841<\/p>\n<p>Derangements provide a way to make sure that everyone gets a fair shake when making job assignments. Nobody is stuck in the same job from one assignment cycle to the next. The idea is that eventually, all your personnel would be assigned to every job, but you don\u2019t necessarily know in which assignment cycle any employee will get a particular job.<\/p>\n<p>My favorite use for derangement is when an office does a \u201cSecret Santa\u201d Christmas gift program. If you have never had one of these, the idea is that everyone brings a wrapped gift to work. The gift-giver then picks the name of another employee randomly, and the second employee becomes the recipient. The first rule is that you don\u2019t give a gift to yourself. A second rule is that you\u2019re not supposed to be able to figure out who is the gift giver.<\/p>\n<p>Gift givers are the set, and the recipients are supposed to be an unpredictable derangement. You try doing this by drawing one name at a time, and very quickly a wind up with a situation where only a few possible derangements are left. So much for maintaining secrecy. To get an idea how this works, look at the prior example, with only three data elements in the original permutation. You really need to pick your derangement all at once.<\/p>\n<p>A cute trick for doing this in the real world is to put everybody\u2019s name on a card twice, once on the top and once on the bottom. Shuffle the card deck. Cut the cards in half, leaving the top and bottom halves paired. Take the top half-card from the top halves deck and put it on the bottom of the same top halves deck. Now draw the pairs of cards from the decks, assembling the tops and bottoms to make a new card. The result will be a new card deck with an unpredictable derangement.<\/p>\n<h2>Permutations, combinations, and derangements in databases<\/h2>\n<p>While all these are interesting, a database person is probably going to be more interested in<em> actually generating<\/em> the permutations, combinations, and derangements. The problem is that SQL is not really built for this kind of computation. The sets are in databases are in the form of tables, which all have a fixed number of columns. Databases don\u2019t have arrays, link lists, or other data structures, so it\u2019s faked with tables.<\/p>\n<p>If all the columns are the same data element, then this is an example of de-normalizing a table with a repeated group. Here\u2019s a simple example for (n = 3)<\/p>\n<pre class=\"lang:tsql decode:true \">CREATE TABLE #Permutations\r\n(c1 INTEGER NOT NULL,\r\n c2 INTEGER NOT NULL,\r\n c3 INTEGER NOT NULL,\r\nPRIMARY KEY (c1, c2, c3),\r\nCONSTRAINT Unique_columns\r\nCHECK (\r\n\t\tc1 NOT IN (c2, c3)\r\n\t\tAND c2 NOT IN (c1, c3)\r\n\t\tAND c3 NOT IN (c1, c2)\r\n\t\t\r\n\t));<\/pre>\n<p>Remember that all tables must have a key. In these cases, the rows that represent a particular set all have to be unique, and so do all of the columns in that row. This means each row is a key.<\/p>\n<p>The <code>CHECK()<\/code> constraint ensures that all columns are unique with a row. A lot of the ranges of the dimensions, constraints about membership, etc., in other data structures must be explicitly defined in SQL check constraints. This is one of the many reasons that I tell newbies that 80 to 90% of the work in SQL is done in the DDL, not the DML. Think about trying to write constraints into procedural code for 500 application programs; instead of putting it in one <code>CHECK()<\/code> constraint, you must duplicate the same code and magically hope that you get it right in every procedural chunk in your system. And when the rules change, you must go back through those 500 pieces of procedural code and update them. Lots of luck with that.<\/p>\n<p>Modeling combinations is easy. Since order doesn\u2019t matter, any simple list of the values will do. That\u2019s a perfect description of a table with one column.<\/p>\n<p>There are several algorithms for generating permutations. Two of the best known are the Heap algorithm (1963) and the Fike algorithm. Both are recursive and are based on the facts that<\/p>\n<p>1) There are n! permutations of the set {1, 2, 3, &#8230;,}. This let you know how many times you have to cycle through a loop or how deep your recursion has to go.<\/p>\n<p>2). The next permutation can be generated from the current permutation without fear of duplication.<\/p>\n<p>Robert Sedgwick (you might know from his textbooks that are widely used) wrote a <a href=\"https:\/\/www.princeton.edu\/~rblee\/ELE572Papers\/p137-sedgewick.pdf\">paper<\/a> on the various methods for generating permutations. He classified them as<\/p>\n<p><strong>1 METHODS BASED ON EXCHANGES<\/strong><br \/>\nRecursive methods<br \/>\nAdjacent exchanges<br \/>\nFactorial counting<br \/>\n&#8220;Loopless&#8221; algorithms<\/p>\n<p><strong>2 OTHER TYPES OF ALGORITHMS<br \/>\n<\/strong>Nested cycling<br \/>\nLexicographic algorithms<br \/>\nRandom permutation<\/p>\n<h2>References:<\/h2>\n<p>https:\/\/en.wikipedia.org\/wiki\/Heap%27s_algorithm<\/p>\n<p>https:\/\/academic.oup.com\/comjnl\/article\/19\/2\/156\/408726. The Computer Journal, vol 19 Issue 2. pages 156-159<\/p>\n<p>https:\/\/homepage.math.uiowa.edu\/~goodman\/22m150.dir\/2007\/Permutation%20Generation%20Methods.pdf<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Joe Celko explains how several mathematical concepts, combinations, permutations, and derangements, relate to databases.&hellip;<\/p>\n","protected":false},"author":96214,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[53,143539],"tags":[5134],"coauthors":[6781],"class_list":["post-94177","post","type-post","status-publish","format-standard","hentry","category-featured","category-theory-and-design","tag-sql-prompt"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/94177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/96214"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=94177"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/94177\/revisions"}],"predecessor-version":[{"id":94184,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/94177\/revisions\/94184"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=94177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=94177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=94177"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=94177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}