递归函数以获取两个或多个的所有唯一组合

时间:2019-02-28 01:48:42

标签: javascript recursion combinations

我需要编写一个函数,该函数从元素数组中获取两个或多个元素的所有唯一组合。我已经为此工作了几天。最初,我编写了单独的函数以获取一些不同大小的组合,这有助于我了解它们的相似之处,希望与目前相比,我可以更接近可行的解决方案。这就是我到目前为止所拥有的。...

function getAll (elements, comboSize, startingIndex) {
  let finalStartingIndex = /*startingIndex + */elements.length - comboSize;
  let finalIndex = finalStartingIndex + 1;
  let tmpStartingIndex = startingIndex;
  let qstrings = [];

  if (finalIndex >= elements.length) {
    finalIndex = finalStartingIndex;
  }

  if (finalStartingIndex < finalIndex) {
    while (tmpStartingIndex <= finalStartingIndex) {
      let nextIndex = tmpStartingIndex + 1;

      while (nextIndex <= finalIndex) {
        let tmpComboSize = comboSize - 1;
        let tmpQstring = '';
        let newQstring = '';

        if (tmpComboSize > 1) {
          tmpQstring = getAll(elements, tmpComboSize, nextIndex);
          console.log('tmpQstring :: ', tmpQstring);
        }

        if (tmpQstring != '') {
          newQstring = elements[tmpStartingIndex] + ', ' + tmpQstring[nextIndex];
          console.log('tmpQstring[nextIndex] :: ', tmpQstring[nextIndex]);
          console.log('newQstring :: ', newQstring);
        }
        let qstring = elements[tmpStartingIndex] + ', ' + elements[nextIndex];
        qstrings.push(qstring);
        nextIndex++;
      }

      /*nextIndex = tmpStartingIndex;

      let tmpComboSize = comboSize - 1;

      if (tmpComboSize > 1) {
        let tmpQstring = getAll(elements, tmpComboSize, nextIndex);

        let stringVal = elements[tmpStartingIndex] + ', ' + tmpQstring[nextIndex];
        qstrings.push(stringVal);
      }*/
      tmpStartingIndex++;
    }
  } else {
    qstrings[finalStartingIndex] = elements[startingIndex] + ', ' + elements[finalStartingIndex];
    console.log(qstrings);
  }

  return qstrings;
}

function getAllTwo (elements, comboSize, startingIndex) {
  let finalStartingIndex = startingIndex + elements.length - comboSize;
  let finalIndex = finalStartingIndex + 1;
  let tmpStartingIndex = startingIndex;
  let qstrings = [];

  while (tmpStartingIndex <= finalStartingIndex) {
    let finalNextIndex = tmpStartingIndex + 1;

    while (finalNextIndex <= finalIndex) {
      let qstring = elements[tmpStartingIndex] + ', ' + elements[finalNextIndex];
      qstrings.push(qstring);
      finalNextIndex++;
    }

    tmpStartingIndex++;
  }

  return qstrings;
}

function getAllThree (elements, comboSize, startingIndex) {
  let finalStartingIndex = startingIndex + elements.length - comboSize;
  let finalFirstNextIndex = finalStartingIndex + 1;
  let finalIndex = finalFirstNextIndex + 1;
  let tmpStartingIndex = startingIndex;
  let qstrings = [];

  while (tmpStartingIndex <= finalStartingIndex) {
    let firstNextIndex = tmpStartingIndex + 1;

    while (firstNextIndex <= finalFirstNextIndex) {
      let finalNextIndex = firstNextIndex + 1;

      while (finalNextIndex <= finalIndex) {
        let qstring = elements[tmpStartingIndex] + ', ' + elements[firstNextIndex] + ', ' + elements[finalNextIndex];
        qstrings.push(qstring);
        finalNextIndex++;
      }

      firstNextIndex++;
    }

    tmpStartingIndex++;
  }

  return qstrings;
}

function getAllFour (elements, comboSize, startingIndex) {
  let finalStartingIndex = startingIndex + elements.length - comboSize;
  let finalFirstNextIndex = finalStartingIndex + 1;
  let finalSecondNextIndex = finalFirstNextIndex + 1;
  let finalIndex = finalSecondNextIndex + 1;
  let tmpStartingIndex = startingIndex;
  let qstrings = [];

  while (tmpStartingIndex <= finalStartingIndex) {
    let firstNextIndex = tmpStartingIndex + 1;

    while (firstNextIndex <= finalFirstNextIndex) {
      let secondNextIndex = firstNextIndex + 1;

      while (secondNextIndex <= finalSecondNextIndex) {
        let finalNextIndex = secondNextIndex + 1;

        while (finalNextIndex <= finalIndex) {
          let qstring = elements[tmpStartingIndex] + ', ' + elements[firstNextIndex] + ', ' + elements[secondNextIndex] + ', ' + elements[finalNextIndex];
          qstrings.push(qstring);
          finalNextIndex++;
        }

        secondNextIndex++;
      }

      firstNextIndex++;
    }

    tmpStartingIndex++;
  }

  return qstrings;
}

function getAllFive (elements, comboSize, startingIndex) {
  let finalStartingIndex = startingIndex + elements.length - comboSize;
  let firstFinalIndex = finalStartingIndex + 1;
  let secondFinalIndex = firstFinalIndex + 1;
  let thirdFinalIndex = secondFinalIndex + 1;
  let finalIndex = thirdFinalIndex + 1;
  let tmpStartingIndex = startingIndex;
  let qstrings = [];

  while (tmpStartingIndex <= finalStartingIndex) {
    let firstIndex = tmpStartingIndex + 1;

    while (firstIndex <= firstFinalIndex) {
      let secondIndex = firstIndex + 1;

      while (secondIndex <= secondFinalIndex) {
        let thirdIndex = secondIndex + 1;

        while (thirdIndex <= thirdFinalIndex) {
          let finalNextIndex = thirdIndex + 1;

          while (finalNextIndex <= finalIndex) {
            let qstring = elements[tmpStartingIndex] + ', ' + elements[firstIndex] + ', ' + elements[secondIndex] + ', ' + elements[thirdIndex] + ', ' + elements[finalNextIndex];
            qstrings.push(qstring);
            console.log('qstrings being built: ', qstrings);
            finalNextIndex++;
          }

          thirdIndex++;
        }

        secondIndex++;
      }

      firstIndex++;
    }

    tmpStartingIndex++;
  }

  return qstrings;
}

let finalStrings = [];
let elements = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

for (let comboSize = 2; comboSize <= elements.length; comboSize++) {
  let finalString = [];
  let tstFinalString = [];

  switch (comboSize) {
    case 2:
      tstFinalString = getAll(elements, comboSize, 0);
      //console.log('tstFinalString, comboSize 2 :: ', tstFinalString);
      //finalString = getAllTwo(elements, comboSize, 0);
      break;

    case 3:
      tstFinalString = getAll(elements, comboSize, 0);
      console.log('tstFinalString, comboSize 3 :: ', tstFinalString);
      //finalString = getAllThree(elements, comboSize, 0);
      break;

    /*case 4:
      finalString = getAllFour(elements, comboSize, 0);
      break;

    case 5:
      finalString = getAllFive(elements, comboSize, 0);
      console.log(finalString);
      break;*/

    default:
      break;
  }

  finalStrings.push(finalString);
}

第一个功能是我尝试适当的递归。当前,它可以获取所有两个元素组合,但是不能超出此范围。我觉得我缺少一些简单的东西,但是我看不到它。我写了其他函数来帮助我思考逻辑并确保我得到了期望的数据。这些功能确实有效,但是显然它不可扩展。您可以提供任何帮助以指出我所缺少的内容,将不胜感激。

哦,如果有关系的话,目前是用Java语言编写的。

2 个答案:

答案 0 :(得分:2)

我认为这将是k大小的组合的规范方法。

// return n choose k combinations
function choose(arr, k, prefix=[], i=0){
  // if the remainder of the array will complete the
  // combination length exactly, combine it with
  // the current prefix and add to results
  if (prefix.length + arr.length - i == k){
    return [prefix.concat(arr.slice(i))];

  // if the prefix is long enough, add it to the results
  } else if (prefix.length == k){
    return [prefix];

  // otherwise, push combinations with and without
  // the current element
  } else {
    return choose(arr, k, prefix.concat(arr[i]), i + 1)
      .concat(choose(arr, k, prefix, i + 1));
  }
}

let arr = ["A","B","C","D","E"];
console.log('Input: ' + JSON.stringify(arr) + '\n');
let cs = choose(arr, 3);
console.log('\nOutput:');
for (let c of cs)
  console.log(JSON.stringify(c));

要获得全部,我们可以做类似的事情:

function powerset(arr, prefix=[], i=0){
  if (i == arr.length)
    return [prefix];

  return powerset(arr, prefix.concat(arr[i]), i + 1)
    .concat(powerset(arr, prefix, i + 1));
}

let arr = ['a', 'b', 'c', 'd', 'e'];
let ps = powerset(arr);
for (let s of ps)
  console.log(JSON.stringify(s));

答案 1 :(得分:0)

这是使用生成器的另一种方法。这种方法的优点是组合是延迟生成的。根据您的计算,有时您可能能够在生成所有组合之前达到所需的结果。由于许多组合问题都涉及组合的巨大集合,因此我们可以避免跳过不必要的组合而节省大量资源。此外,该概念可以扩展为适用于无限流,在这种情况下,可能会产生无限数量的组合。

const append = (xs, x) =>
  xs .concat ([ x ])

const ncomb = function* (n, xs = [])
{ const gen = function* (n, acc)
  { if (n === 0)
      yield acc
    else
      for (const x of xs)
        yield* gen (n - 1, append (acc, x))
  }
  yield* gen (n, [])
}

const data =
  [ 1, 2, 3 ]

const print = (...xs) =>
  console .log (...xs .map (x => JSON .stringify (x)))

print
  ( Array .from (ncomb (0, data))
    // [[]]
    
  , Array .from (ncomb (1, data))
    // [[1],[2],[3]]
    
  , Array .from (ncomb (2, data))
    // [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]]
    
  , Array .from (ncomb (3, data))
    // [[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2],[1,2,3],[1,3,1],[1,3,2],[1,3,3],[2,1,1],[2,1,2],[2,1,3],[2,2,1],[2,2,2],[2,2,3],[2,3,1],[2,3,2],[2,3,3],[3,1,1],[3,1,2],[3,1,3],[3,2,1],[3,2,2],[3,2,3],[3,3,1],[3,3,2],[3,3,3]]
  )

上面的组合会增加最右边的元素,但是可以更改此顺序。如果将append操作更改为prepend,则会生成组合,其中最左边的元素将递增–

const prepend = (xs, x) =>
  [ x ] .concat (xs)

print
  ( Array .from (ncomb (3, data))
    // [[1,1,1],[2,1,1],[3,1,1],[1,2,1],[2,2,1],[3,2,1],[1,3,1],[2,3,1],[3,3,1],[1,1,2],[2,1,2],[3,1,2],[1,2,2],[2,2,2],[3,2,2],[1,3,2],[2,3,2],[3,3,2],[1,1,3],[2,1,3],[3,1,3],[1,2,3],[2,2,3],[3,2,3],[1,3,3],[2,3,3],[3,3,3]]
  )

为演示发电机的短路行为,请考虑一个函数solve,该函数可以接收任意数量的数字并返回第一个Pythagorean triple。找到第一个直角三角形后,将不再生成其他组合-

const isRightTriangle = (a, b, c) =>
  // pythagorean triple
  (a ** 2) + (b ** 2) === (c ** 2)

const solve = (...numbers) =>
{ for (const [ a, b, c ] of ncomb (3, numbers))
    if (isRightTriangle (a, b, c))
      return `solution: ${a}, ${b}, ${c}`
  return `no solution`
}

console .log (solve (1, 2, 3, 4, 5, 6, 7, 9, 10))
// solution: 3, 4, 5

console .log (solve (8, 11, 6, 9, 5, 10, 12, 7))
// solution: 8, 6, 10

console .log (solve (19, 6, 8, 7, 21, 4, 3, 15))
// no solution

展开以下代码段,以在您自己的浏览器中验证结果-

const append = (xs, x) =>
  xs .concat ([ x ])

const ncomb = function* (n, xs = [])
{ const gen = function* (n, acc)
  { if (n === 0)
      yield acc
    else
      for (const x of xs)
        yield* gen (n - 1, append (acc, x))
  }
  yield* gen (n, [])
}

const isTriangle = (a, b, c) =>
  // pythagorean theorem
  (a ** 2) + (b ** 2) === (c ** 2)

const solve = (...numbers) =>
{ for (const [ a, b, c ] of ncomb (3, numbers))
    if (isTriangle (a, b, c))
      return `solution: ${a}, ${b}, ${c}`
  return `no solution`
}

console .log (solve (1, 2, 3, 4, 5, 6, 7, 9, 10))
// solution: 3, 4, 5

console .log (solve (8, 11, 6, 9, 5, 10, 12, 7))
// solution: 8, 6, 10

console .log (solve (19, 6, 8, 7, 21, 4, 3, 15))
// no solution