title: How to improve efficiency of algorithm which generates next lexicographic permutation?
body: <p>It must be noted here that I performed the mathematics by hand on paper to derive the foregoing proofs. I am not sure if the proofs would have become apparent by solely using the medium of the modern computer.</p>
  
  <p>The definition of "efficiency" as used below meaning completing a discrete portion of the algorithm, or the entire algorithm in the least amount of time. Both as to mathematically and, programmatically, or computationally.</p>
  
  <p>While further examining procedures for generating the next lexicographic permutation from the original set or current permutation, following re-reading the Answer by @chqrlie at <a href="https://stackoverflow.com/questions/34013675/permutations-without-recursive-function-call">Permutations without recursive function call</a>, I began the inquiry by writing the indexes on paper in an attempt to observe any mathematical relationships between the indexes that could be utilized to perform the specific task.</p>
  
  <p>I discovered several interesting truths, the proofs thereof demonstrated below.</p>
  
  <p>When we write, for example, the values</p>
  
  <pre><code>a,b,c
  </code></pre>
  
  <p>or </p>
  
  <pre><code>abc
  </code></pre>
  
  <p>or, instead write the indexes representing the values</p>
  
  <pre><code>0,1,2
  </code></pre>
  
  <p>or</p>
  
  <pre><code>012
  </code></pre>
  
  <p>Since we know, given a set </p>
  
  <pre><code>abc
  </code></pre>
  
  <p>that we can generate the next lexicographic permutation by swapping the last two values or indexes of the set</p>
  
  <pre><code>acb
  </code></pre>
  
  <p>or</p>
  
  <pre><code>021
  </code></pre>
  
  <p>we can ignore the values, which could be any type of data, and concentrate on using the indexes for our examinations, as discrete numbers are more suited for correlating possible relationships than a possibly infinite amount of diversified values.</p>
  
  <p>Therefore, with the second lexicographic index of the original set</p>
  
  <pre><code>abc
  </code></pre>
  
  <p>we can denote the values of the indexes as numbers, and observe</p>
  
  <pre><code>0,2,1
  </code></pre>
  
  <p>or</p>
  
  <pre><code>021
  </code></pre>
  
  <p>The first indexes being</p>
  
  <pre><code>012
  </code></pre>
  
  <p>the second being</p>
  
  <pre><code>021
  </code></pre>
  
  <p>Since we know the <code>.length</code> of the original set is <code>3</code>, if we remember the <code>.length</code> of the original set of values, we can remove the starting</p>
  
  <pre><code>0
  </code></pre>
  
  <p>where to reduce the indexes to the number</p>
  
  <pre><code>12
  </code></pre>
  
  <p>and</p>
  
  <pre><code>21
  </code></pre>
  
  <p>respectively. Where the <code>0</code> can be referenced as the index from original set to get the value at index <code>0</code> when the resulting set of the next operation is less than the original set.</p>
  
  <p>When we attempt to graph potential relationships between <code>12</code> and <code>21</code>, we find that</p>
  
  <pre><code>21 - 12 = 9
  </code></pre>
  
  <p>When we continue, we find that the next lexicographic indexes are</p>
  
  <pre><code>102
  </code></pre>
  
  <p>subtracting the previous indexes</p>
  
  <pre><code>102 - 21 = 81
  </code></pre>
  
  <p>where <code>102</code> is the next lexicographic permutation, represented as the values</p>
  
  <pre><code>bac
  </code></pre>
  
  <p>which provides us with the common relationship between the indexes being the number nine, represented numerically as </p>
  
  <pre><code>9
  </code></pre>
  
  <p>This relationship is evident and reproducible for an infinite set of input values represented as numbers. We can graphs of the relationships, which, depending on the perspective of the observer, can be viewed as two slopes, with an inverted apex offset when beginning the graph from the first value of the set of resulting permutations</p>
  
  <pre><code>// graph 1
  0,9,81
  
  // graph 2
  abc 012 0
  acb 021 1 9
  bac 102 2 81
  bca 120 3 18
  cab 201 4 81
  cba 210 5 9
  
   /\/\
  /    \
  </code></pre>
  
  <p>We can observe here that the number on the graph at the inclination slope is identical to the number at the correpsonding declination slope, following the inversion of the number of the divided total number of possible permutations divided by half, where for example, for a total of six permutation, we subtract one, leaving remainder of five, remembering that we still need the odd set of indexes, we use the number at the inverted apex as a pivot, leaving four indexes, which we denote as inclination and declination slopes, respectively.</p>
  
  <p>We observer here that the numbers on the graph of the declination slope are identical to the correponding adjacent angle of the inclination slope at the same y coordinate.</p>
  
  <p>Thus,below I demonstrate the proof that an infinite set of permutations given an infinite input set can be calculated, generated, filtered, derived by utilizing addition or multiplication of the number nine</p>
  
  <pre><code>9
  </code></pre>
  
  <p>by matching numbers which include only the number of the index of an input number, without duplicate in the set.</p>
  
  <p>Further, I demonstrate the proof that need only the indexes as numbers on inclination slope, or the total amount of possible permutations divided by two plus one, are needed to derive the total number of permutations of a given input.</p>
  
  <p>As stressed at the preface of this post, this calculations could perhaps not have been possible without many hours of doing the math by hand on paper. The media screen simple does not provide the same medium as composing the characters one by one on paper; with the ability to view the paper from various physical dimensions.</p>
  
  <p>The expression of the algorithm using a coding language is another task unto itself.</p>
  
  <p>The following is a progression of the discoveries, proofs and expressions thereof implemented using the "JavaScript" programming language. The first version has a <code>RegExp</code> that is not accurate as to the expected result, as pointed out by <a href="https://stackoverflow.com/questions/34487374/most-efficient-method-to-check-for-range-of-numbers-within-number-without-duplic#comment56716683_34487374">@Tushar</a>, with corrected <code>RegExp</code>, though incorrect <code>RegExp</code> returns same result.</p>
  
  <p>Given input as array</p>
  
  <pre><code>var arr = ["a", "b", "c", "d"];
  </code></pre>
  
  <p></p>
  
  <pre><code>// version 1
  
  function getNextLexicographicPermutation(arr) {
    var len = arr.length;
    var idx = arr.map(function(_, index) {
      return index
    });
    var re = new RegExp("[" + len + "-9]|(.)(?!=\\1)");
    var n = Math.pow(10, len - 1);
    var p = 9;
    var last = Number(idx.slice().reverse().join(""));
    var res = [idx];
    var curr = Number(res[res.length - 1].join(""));
    while (curr &lt; last) {
      for (var prev = curr, next, k; prev &lt;= last; prev += p) {
        if ((re.test(prev))) {
          k = String(prev);
          if (k.length &gt;= len - 1) { //  &amp;&amp; Math.max.apply(Math, j) &lt; len
            next = [];
            for (var i = 0; i &lt; k.length; i++) {
              if (next.indexOf(Number(k[i])) == -1 
                &amp;&amp; idx.indexOf(Number(k[i])) !== -1) {
                  next.push(Number(k[i]))
              }
            }
            if (prev &lt; n &amp;&amp; next.length === len - 1 
               || next.length === len &amp;&amp; prev &gt; curr) {
                 res[res.length] = next.length &lt; len 
                                   ? [0].concat.apply([0], next) 
                                   : next;
            }
          }
        }
        curr = prev;
      }
    };
    return res.map(function(value) {
      return value.map(function(index) {
        return arr[index]
      })
    })
  }
  
  getNextLexicographicPermutation(arr);
  </code></pre>
  
  <p>The graph for numerical difference between the numbers as indexes for the array <code>arr</code> will be</p>
  
  <pre><code>// graph 3
  // reflecting input `abcd`
  [9,81,18,81,9,702,9,171,27,72,18,693,18,72,27,171,9,702,9,81,18,81,9]
  </code></pre>
  
  <p></p>
  
  <pre><code>// version 2.0 without using `RegExp`
  
  function getNextLexicographicPermutation(arr) {
    var len = arr.length;
    var idx = arr.map(function(_, index) {
      return index
    });
    var n = Math.pow(10, len - 1);
    var p = 9;
    var last = Number(idx.slice().reverse().join(""));
    var res = [];
    var curr = Number(idx.join(""));
    var prev, k, next;
  
    while (curr &lt;= last) {
      prev = curr;            
      k = String(prev).split("").map(Number);
      if (k.every(function(v) {
          return idx.indexOf(v) !== -1
        }) &amp;&amp; k.length &gt;= len - 1) {
        next = [];
        for (var i = 0; i &lt; k.length; i++) {
          if (next.indexOf(Number(k[i])) == -1 
            &amp;&amp; idx.indexOf(Number(k[i])) !== -1) {
              next.push(Number(k[i]))
          }
        }
        if (prev &lt; n &amp;&amp; next.length === len - 1 
            || prev &gt; n &amp;&amp; next.length === len)) {
          res[res.length] = next.length &lt; len 
                            ? [0].concat.apply([0], next) 
                            : next;
        }
      }
      prev += p;
      curr = prev;
    };
    return res.map(function(item) {
      return item.map(function(v) {
        return arr[v]
      })
    })
  }
  getNextLexicographicPermutation(arr)
  </code></pre>
  
  <p>The efficiency of the second version was greatly improved over the first version by substituting bitmask for <code>RegExp</code> by Answer of @lleaff at <a href="https://stackoverflow.com/questions/34487374/most-efficient-method-to-check-for-range-of-numbers-within-number-without-duplic">Most efficient method to check for range of numbers within number without duplicates</a>. </p>
  
  <p>The relevant profiles generated by <code>DevTools</code> between the <code>RegExp</code> version and bitmask version should be reprodicible at chromium browser, however due to neglecting commenting the exact tests performed, am not able to precisely reproduce the numbers and times posted without devoting more time to verifying the numbers posted at previous Question. Cannot remember precisely, though browser tab may have crashed when <code>.length</code> of input set was ten. What was important was that the bitmask test version was more efficient than <code>RegExp</code> test version.</p>
  
  <pre><code>// version 2.1, substituting bitmask for `RegExp`
  
  function getNextLexicographicPermutation(arr) {
    function checkDigits(min, max, n) {
      var digits = 0;
      while (n) {
        d = (n % 10);
        n = n / 10 &gt;&gt; 0;
        if (d &lt; min || d &gt; max || (digits &amp; (1 &lt;&lt; d)))
          return false;
        else
          digits |= 1 &lt;&lt; d;
      }
      return true;
    }
    var len = arr.length,
      idx = arr.map(function(_, index) {
        return index
      }),
      p = 9,
      min = 0,
      max = len - 1,
      last = Number(idx.slice().reverse().join("")),
      res = [],
      curr = Number(idx.join("")),
      next;
  
    while (curr &lt; last) {
      next = (curr += p);
      if (checkDigits(min, max, next)) res[res.length] = next;
      curr = next;
    };
  
    return res.map(function(item) {
      var item = String(item).split("").map(Number);
      item = item.length &lt; arr.length ? [0].concat(item) : item;
      return item.map(function(index) {
        return arr[index]
      }).filter(Boolean)
    })
  }    
  getNextLexicographicPermutation(arr);
  </code></pre>
  
  <p>The notes and process took the better part of a year, over a year ago, to show and prove. Have mainly simply thought how to get indexes for either side of slope simultaneously, using only inclination slope indexes, rather than coding the algorithm therefore.</p>
  
  <p>The bulk of the math was in trying to derive a further correlation between the adjacent multiples of the number nine, for the ability to calaculate the exact next multiple of the number nine</p>
  
  <pre><code>9 
  </code></pre>
  
  <p>for incrementing a number by nine then filtering duplicate values from the resulting number. I have not yet been able to decipher the inter-relationships between the adjacent multiples of nine on the inclination slope to the degree that multiplication or division could be substituted for addition and exclusion.</p>
  
  <p>Decided to finally create proof of concept for the proposition of generating an infinite number of permutations from an infinite input set, using only the inclination slope, or only the indexes as numbers, of the first half of possible permutations plus one.</p>
  
  <pre><code>// version 3, generate second half of permutations using indexes of first 
  // half of permutations
  
  function getNextLexicographicPermutation(arr) {
  
      for (var l = 1, i = k = arr.length; l &lt; i ; k *= l++);
  
      function checkDigits(min, max, n) {
          var digits = 0;
          while (n) {
              d = (n % 10);
              n = n / 10 &gt;&gt; 0;
              if (d &lt; min || d &gt; max || (digits &amp; (1 &lt;&lt; d)))
                  return false;
              else
                  digits |= 1 &lt;&lt; d;
          }
          return true;
      }
  
      var len = arr.length
      , idx = arr.map(function(_, index) {
          return index
      })
      , p = 9
      , min = 0
      , max = len - 1
      , last = Number(idx.slice().reverse().join(""))
      , curr = Number(idx.join(""))
      , res = []
      , diff = []
      , result = []
      , next;
  
      while (res.length &lt; (k / 2) + 1) {
          next = (curr += p);
          if (checkDigits(min, max, next)) res[res.length] = next;      
          curr = next;
      };
  
      for (var i = 0; i &lt; res.length; i++) {
        var item = res[i];
        item = String(item).split("").map(Number);
        item = (item.length &lt; arr.length ? [0].concat(item) : item)
               .map(function(index) {
                  return arr[index]
               }).filter(Boolean);
        result.push(item)
      }
  
      res.reduce(function(a, b) {
        diff.push(b - a);
        return b
      });
  
      for (var i = 0, curr = res[res.length - 1], n = diff.length - 2
          ; result.length &lt; k;  i++, n--) {
            curr = curr + diff[n];
            result.push(
              String(curr).split("")
              .map(function(index) {
                  return arr[index]
              })
            );
      }
      return result;
  }
  getNextLexicographicPermutation(arr);
  </code></pre>
  
  <p>Another eventual step in development of the algorithm will be given an arbitrary <code>.length</code> input, to be able to calculate the indexes, and thus the values of the <em>nth</em> permutation of the set mathematically; by using only a single formula of multiplication, division, algebra, trigonotetry or calculus.</p>
  
  <p>Please include reproducible benchmarks within Answer. The reason being is that cannot remember exactly how I derive the numbers for <code>Profiles</code> at <code>DevTools</code>, though if remember correctly used <code>console.time()</code>, <code>console.timeEnd()</code> and <code>console.profile()</code> at beginning and end of respective portions where used. Backup your experiments; you never know if or when a hard drive or OS will crash. You can generally retrieve the data from the disk, though at cost of time and effort to do so. Save your tests in same fashion you save versions of algorithms as well, for ability to reproduce yourself and for others to reproduce. The full gamut of the original tests are lost.</p>
  
  <p>The surviving remnants of the test for how many permutations could be derived before browser tab crashed are only comments retrieved from a different OS</p>
  
  <pre><code>// 362880 876543210 876543219 
  var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
  </code></pre>
  
  <p>where if recollect accurately, when <code>"j"</code> was added to the array, the active tab at chromium crashed. The first number is the total amount of permutations for the set "a" through "i"; the next two number are probably, though not certain, the results of two tests for one of the versions before version 3, which composed today.</p>
  
  <p>That is another reason for now posting the above disclosure here at stackoverflow.com, to preserve the principals of the algorithm and work on the code done so far, less some catastrophe destroy all of the original notes and work; for example, being awake for several days in a row trying to interpret patterns and relationships between numbers; neglecting to document all of the specific test patterns tried attempting to port the algorithm to code at comments within code; or as @JaromandaX described the circumstance <a href="https://stackoverflow.com/questions/43245657/how-do-i-append-a-file-to-formdata#comment73562215_43245657">"PEBCAK"</a>.</p>
  
  <p>Fresh eyes can probably see the algorithm from a different perspective as to efficiency.</p>
  
  <p>We can reproduce a graph of the results from some of the preserved code versions above, for example using <code>console.time()</code>, <code>console.timeEnd()</code>, <code>performance.now()</code> or other appropriate tests involving time to complete the function, which can be reproduced.</p>
  
  <pre><code>// console.time("version N");
  // getNextLexicographicPermutation(arr);
  // console.timeEnd("version N");
  
  var tests = [];
  
  for (var i = 0; i &lt; 10000; i++) {
    var from = window.performance.now();
    getNextLexicographicPermutation(arr);
    tests.push(window.performance.now() - from);
  }
  
  for (var i = 0, k = 0; i &lt; tests.length; i++) {
    k += tests[i];
  }
  
  var avg = k/tests.length;
  
  // version 1 `avg`: 0.2989265000001993
  // version 2.0 `avg`: 0.8271295000007376
  // version 2.1 `avg`: 0.17173000000003666
  // version 3 `avg`: 0.12989749999987543
  </code></pre>
  
  <p>As a footnote, I will mention that I used the principals of the algorithm to derive the expected results at <a href="https://stackoverflow.com/questions/8374079/fastest-way-to-generate-all-binary-strings-of-size-n-into-a-boolean-array/42591823#42591823">Fastest way to generate all binary strings of size n into a boolean array?</a>, where again, the number nine appeared as a key number within the inclination slope, and the matching angle of declination is also observable. Though did not perform futher tests and automation of the specific form of input and result described at that Question. The Answer was a proof of concept as to the viability of the approach of ignoring the values and using only a wave-like incrementing pattern applied to a single number initial number, zero, to derive infinite permutations of a set.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li><p>How can the algorithm be improved as to efficiency; both
  computationally and mathematically?</p></li>
  <li><p>Can we create the indexes for both inclination and declination slope at the same time; rather than determining the declination slope after the inclination slope has been calculated?</p>
  
  <ol start="3">
  <li><p>Are there a mathematical relationships or patterns between the indexes as numbers, that is, for example, the set of numbers at <em>graph 1</em>, <em>graph 2</em> or <em>graph 3</em>, derived from the input of any four values, for example</p>
  
  <p>abcd</p></li>
  </ol>
  
  <p>or</p>
  
  <pre><code>["a", "b", "c", "d"]
  </code></pre>
  
  <p>that the author has not yet to recognize; which could be used to
  further reduce the number of computations currently implemented to
  derive results?</p></li>
  </ol>