{"id":476,"date":"2013-05-13T21:18:32","date_gmt":"2013-05-14T01:18:32","guid":{"rendered":"http:\/\/learninglover.com\/blog\/?p=476"},"modified":"2013-05-13T21:18:32","modified_gmt":"2013-05-14T01:18:32","slug":"permutation-problems","status":"publish","type":"post","link":"https:\/\/learninglover.com\/blog\/index.php\/2013\/05\/13\/permutation-problems\/","title":{"rendered":"Permutation Problems"},"content":{"rendered":"<p>I love to play with puzzles. When I was in grade school I would spend hours at a time figuring out ways to solve from things like Tetris, Mindsweeper, Solitare, and Freecell. Later I was introduced to puzzles involving numbers like <a href=\"http:\/\/www.learninglover.com\/examples.php?id=10\" title=\"Sudoku\">Sudoku<\/a> and <a href=\"http:\/\/www.learninglover.com\/examples.php?id=35\" title=\"Nonograms\">Nonograms<\/a>. <\/p>\n<p>These puzzles are often interesting in part because there is generally a very large way that things can be arranged, but only a few of these arrangements are correct. Generally a person solving a puzzle will figure out certain things that must be true or cannot be true, which helps in solving the puzzle and reducing the number of possible cases. Initially, though, we are often left with a situation where we have a new puzzle and our only method is to keep trying every possible solution until we start to notice a pattern (or reach a solution). <\/p>\n<p>For example, consider a Sudoku puzzle. We are given a partially filled in grid and our job is to fill in the remaining cells with the rules that every row, column and marked subgrid must have the numbers 1 &#8211; 9 exactly once. One initial attempt at solving such a puzzle could be to attempt to permute the string 1, 2, 3, 4, 5, 6, 7, 8, 9 until we find a solution that fits the first row, then do the same with the second row, and so on and so forth. <\/p>\n<p>One immediate question is how many ways are there to permute the numbers 1, &#8230;, 9? We can answer this by realizing that each permutation is a new string. So for each string that we construct, we have 9 choices for the first element in the string. Then once that element has been chosen, we are not allowed for that element to appear anywhere else. So there are only 8 possible choices for what can go in the second string. Continuing this process, we see that the number of possible permutations we can construct from the string 1, &#8230;, 9 is <\/p>\n<p><u> 9 <\/u> * <u> 8 <\/u>  * <u> 7 <\/u> * <u> 6 <\/u> * <u> 5 <\/u> * <u> 4 <\/u> * <u> 3 <\/u> * <u> 2 <\/u> * <u> 1 <\/u> = 362880<\/p>\n<p>This is a large number of possible strings to generate just to get one row of a Sudoku, so hopefully you&#8217;ll be able to notice the pattern before going through this whole set (because once you&#8217;ve generated the first row you still have to do the other 8 rows). <\/p>\n<p>Nonetheless because there is often great value that can be gained by knowing how to permute through all possible solutions, I have written three functions that help with this process: Next_Permutation, Previous_Permutation, and Random_Permutation. <\/p>\n<p>Before I give these algorithms, I want to highlight two notations on ordering string. A string (a<sub>1<\/sub>, a<sub>2<\/sub>, &#8230;, a<sub>n<\/sub>) is said to be in lexicographical order (or alphabetical order) if for each i [in] 1, &#8230;, n-1, a<sub>i<\/sub> [<=] a<sub>i+1<\/sub>. Likewise, a string is said to be in reverse lexicographical order if for each i [in] 1, &#8230;, n-1, a<sub>i<\/sub> [>=] a<sub>i+1<\/sub>.<\/p>\n<p><b>Next Permutation<\/b><\/p>\n<p>If the given string is not in reverse lexicographic order, then there exists two elements j<sub>1<\/sub> and j<sub>2<\/sub> such that j<sub>1<\/sub> [<] j<sub>2<\/sub> and a<sub>j<sub>1<\/sub><\/sub> [<] a<sub>j<sub>2<\/sub><\/sub>.<br \/>\n1. The Next_Permutation algorithm first searches for the largest element <i>j<sub>1<\/sub><\/i> such that a<sub>j<sub>1<\/sub><\/sub> [<] a<sub>j<sub>1<\/sub> + 1<\/sub>. Since we said the string is not in reverse lexicographic order, this j<sub>1<\/sub> must exist.<br \/>\n2. Once this j<sub>1<\/sub> is found, we search for the smallest element a<sub>j<sub>2<\/sub><\/sub> such that a<sub>j<sub>2<\/sub><\/sub> [>] a<sub>j<sub>1<\/sub><\/sub>. Again, since we know that this is true for j<sub>1<\/sub> + 1, we know that such a j<sub>2<\/sub> must exist.<br \/>\n3. We swap the elements a<sub>j<sub>1<\/sub><\/sub> and a<sub>j<\/sub>2<\/sub><\/sub>.<br \/>\n4. The elements after j<sub>1<\/sub> + 1 are then placed in lexicographic order (i.e. in order such that a<sub>i<\/sub> [>=] a<sub>i+1<\/sub><\/p>\n<p>If the given string is in reverse lexicographic order, then we simply reverse the string. <\/p>\n<p><b>Previous Permutation<\/b><\/p>\n<p>If the given string is not in lexicographic order, then there exists two elements j<sub>1<\/sub> and j<sub>2<\/sub> such that j<sub>1<\/sub> [<] j<sub>2<\/sub> and a<sub>j<sub>1<\/sub><\/sub> [>] a<sub>j<sub>2<\/sub><\/sub>.<br \/>\n1. The Previous_Permutation algorithm first searches for the largest element <i>j<sub>1<\/sub><\/i> such that a<sub>j<sub>1<\/sub><\/sub> [>] a<sub>j<sub>1<\/sub> + 1<\/sub>. Since we said the string is not in reverse lexicographic order, this j<sub>1<\/sub> must exist.<br \/>\n2. Once this j<sub>1<\/sub> is found, we search for the smallest element a<sub>j<sub>2<\/sub><\/sub> such that a<sub>j<sub>1<\/sub><\/sub> [>] a<sub>j<sub>2<\/sub><\/sub>. Again, since we know that this is true for j<sub>1<\/sub> + 1, we know that such a j<sub>2<\/sub> must exist.<br \/>\n3. We swap the elements a<sub>j<sub>1<\/sub><\/sub> and a<sub>j<\/sub>2<\/sub><\/sub>.<br \/>\n4. The elements after j<sub>1<\/sub> + 1 are then placed in reverse lexicographic order (i.e. in order such that a<sub>i<\/sub> [<=] a<sub>i+1<\/sub><\/p>\n<p>If the given string is in lexicographic order, then we simply reverse the string. <\/p>\n<p><b>Random Permutation<\/b><\/p>\n<p>This generates a random string permutation. <\/p>\n<p><a href=\"http:\/\/www.learninglover.com\/examples.php?id=40\" title=\"Permutation Script\">This script can be seen here, set to work on permutations of colors. <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I love to play with puzzles. When I was in grade school I would spend hours at a time figuring out ways to solve from things like Tetris, Mindsweeper, Solitare, and Freecell. Later I was introduced to puzzles involving numbers like Sudoku and Nonograms. These puzzles are often interesting in part because there is generally [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-476","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=476"}],"version-history":[{"count":0,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/476\/revisions"}],"wp:attachment":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}