算法来计算排列 [英] Algorithm to calculate permutations

查看:138
本文介绍了算法来计算排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道堆的算法来​​计算给定顺序排列,但是,如果我想计算的K元素的排列为子集给定序列N +

我在想这个时候的解决方案是一种回溯,但它需要生成的子元素每次删除一个递归调用置换功能的新序列。这听起来很昂贵,我想知道是否有更好的解决办法

解决方案
  1. 使用的算法从一组N的生成,规格K的组合 (随便选从SO问题:<一href="http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n">Algorithm返回从n个 k个元素)的所有组合。
  2. 使用的结果,适用堆的算法创建此K-元素的所有排列子集(或另一<一href="http://stackoverflow.com/questions/2710713/algorithm-to-generate-all-possible-permutations-of-a-list">Algorithm生成一个列表所有可能的排列)。
  3. 生成的大小K和重复(步骤1和2),直到K规格的所有子集已列举的下一个子集。

I'm aware of Heap's algorithm to calculate permutations of a given sequence, but what if I wanted to calculate the permutations of a k-elements subset for a given sequence N?

The solution I'm thinking of this time is a backtracking one, but it would need to generate a new sequence of sub-elements each time deleting one and recursively calling the permutation function. This sounds expensive and I would like to know if there's a better solution

解决方案

  1. Use an algorithm to generate combinations of size K from the set of N. (Pick any from the SO question: Algorithm to return all combinations of k elements from n).
  2. Using the result, apply Heap's Algorithm to create all permutations of this k-element subset (or another Algorithm to generate all possible permutations of a list).
  3. Generate the next subset of size K and repeat (steps 1 and 2) until all subsets of size K have been enumerated.

这篇关于算法来计算排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆