n选m排列问题的递归算法

n选m排列是一个经典算法题,如果m = n则称为全排列。

n选m排列问题的递归算法逻辑为:

1,将m个数的排列分为两部分:一部分为包含某个数k(1 <= k <= n)的m排列,一部分为不包含k的m排列;

2,对于包含k的排列,又可以分为两部分:一部分为包含某个数s(k < s <= n)的m-1排列,一部分为不包含s的m-1排列;

3,对于2步所得m-1排列,将k插入到每一个排列中的m个位置可以得到m个新的m排列;

4,同步骤2,对于不包含k的排列可以进行类似的操作,最终将得到所有的n个元素的m排列。

递归算法的伪代码:

Begin:
input m, n;
if m is bigger than n, then set m = n;
if m is smaller than or equal to 0, then return;
if n is smaller than or equal to 0, then return;

PartA: get m permutation contains 1;
PartB: get m permutation not contains 1;

collect two permutations;

End;

PartA:
input k, m, n;
if m is equal to 1, then return 1 permutation only k;

PartA: get m-1 permutation contains k+1;

if m is smaller than (n - k + 1), then:
PartB: get m-1 permutation not contains k+1;

collect two permutation sets in one set;

foreach m-1 permutation, insert k to m different positions in this permutation, then get m permutation sets;

 End;

PartB:
input k, m, n
if m is equal to 1, then return (n-k+1) ones 1 permutation, each 1 permutation only contains k ~ n one element;

PartA: get m permutation contains k;
if m is smaller than (n - k + 1), then:
PartB: get m permutation not contains k;

collect two m permutation;

End;

C++代码实现:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


void FullPremutationPartA(int firstNo, int numPremu, int numCount, vector<vector<int>> &vec);

void FullPremutationPartB(int secNo, int numPremu, int numCount, vector<vector<int>> &vec);

void FullPremutation(int numPremu, int numCount, vector<vector<int>> &vec);

int main()
{
	int numPremu,
		numCount;
	vector<vector<int>> vec;

	cout << "Input number of Premutation: ";
	cin >> numPremu;
	cout << "Input number of count: ";
	cin >> numCount;


	FullPremutation(numPremu, numCount, vec);

	int vecSize = vec.size();
	//for (int vecIndex = 0; vecIndex < vecSize; vecIndex++)
	//{
	//	int termSize = vec[vecIndex].size();
	//	for (int tIndex = 0; tIndex < termSize; tIndex++)
	//	{
	//		cout << vec[vecIndex][tIndex] << "\t";
	//	}

	//	cout << endl;
	//}
	cout << "count = " << vecSize << endl;

	return 0;
}




void FullPremutation(int numPremu, int numCount, vector<vector<int>> &vec)
{
	if ((numPremu <= 0) || (numCount <= 0))
		return;

	if (1 == numPremu)
	{
		for (int premuIndex = 1; premuIndex <= numCount; premuIndex++)
		{
			vector<int> premuvec;
			premuvec.push_back(premuIndex);
			vec.push_back(premuvec);
		}

		return;
	}

	if (1 == numCount)
	{
		vector<int> premuvec;
		premuvec.push_back(numCount);
		vec.push_back(premuvec);
		return;
	}

	if (numPremu > numCount)
	{
		FullPremutation(numCount, numCount, vec);
		return;
	}

	vector<vector<int>> PartAVector;
	FullPremutationPartA(1, numPremu, numCount, PartAVector);
	vec.insert(vec.end(), PartAVector.begin(), PartAVector.end());

	if (numPremu < numCount)
	{
		vector<vector<int>> PartBVector;
		FullPremutationPartB(2, numPremu, numCount, PartBVector);
		vec.insert(vec.end(), PartBVector.begin(), PartBVector.end());
	}

	return;
}


/****************************************************************
 * Function that process Premutation from firstNo to numCount
 * with count number of numPremu containing firstNo.
*****************************************************************/
void FullPremutationPartA(int firstNo, int numPremu, int numCount, vector<vector<int>> &vec)
{
	if (firstNo > numCount)
		return;


	if ((1 == numPremu) || (firstNo == numCount))
	{
		vector<int> premuVec;
		premuVec.push_back(firstNo);
		vec.push_back(premuVec);
		return;
	}


	vector<vector<int>> PartAVector;
	FullPremutationPartA(firstNo + 1, numPremu - 1, numCount, PartAVector);

	vector<vector<int>> nunFullVector;
	nunFullVector.insert(nunFullVector.end(), PartAVector.begin(), PartAVector.end());
	
	if (numPremu < (numCount - firstNo + 1))
	{
		vector<vector<int>> PartBVector;
		FullPremutationPartB(firstNo + 2, numPremu - 1, numCount, PartBVector);
	
		nunFullVector.insert(nunFullVector.end(), PartBVector.begin(), PartBVector.end());
	}
		
	
	int fullSize = nunFullVector.size();
	int termSize = nunFullVector[0].size();
	vec.resize(fullSize * (termSize + 1));
	int vecIndex = 0;
	
	for (int preIndex = 0; preIndex < fullSize; preIndex++)
	{
		vector<int> tmpVector;
		tmpVector.resize(termSize + 1);
		copy(nunFullVector[preIndex].begin(), nunFullVector[preIndex].end(), tmpVector.begin());
		tmpVector[termSize] = firstNo;
		vec[vecIndex].assign(tmpVector.begin(), tmpVector.end());
		vecIndex++;
		
		for (int termIndex = termSize; termIndex > 0 ; termIndex--)
		{
			tmpVector[termIndex] = tmpVector[termIndex - 1];
			tmpVector[termIndex - 1] = firstNo;

			vec[vecIndex].assign(tmpVector.begin(), tmpVector.end());
			vecIndex++;
		}
	}

	return;
}

/****************************************************************
 * Function that process Premutation from SecNo to numCount
 * with count number of numPremu
*****************************************************************/
void FullPremutationPartB(int secNo, int numPremu, int numCount, vector<vector<int>> &vec)
{
	if (secNo > numCount)
		return;

	if (1 == numPremu)
	{
		for (int premuIndex = secNo; premuIndex <= numCount; premuIndex++)
		{
			vector<int> premuVec;
			premuVec.push_back(premuIndex);
			vec.push_back(premuVec);			
		}

		return;
	}

	if (secNo == numCount)
	{
		vector<int> premuVec;
		premuVec.push_back(secNo);
		vec.push_back(premuVec);
		return;
	}	

	
	vector<vector<int>> PartAVector;
	FullPremutationPartA(secNo, numPremu, numCount, PartAVector);
	vec.insert(vec.end(), PartAVector.begin(), PartAVector.end());

	if (numPremu < (numCount - secNo + 1))
	{
		vector<vector<int>> PartBVector;
		FullPremutationPartB(secNo + 1, numPremu, numCount, PartBVector);
		vec.insert(vec.end(), PartBVector.begin(), PartBVector.end());
	}

	return;
}

我的实现对于n较小的时候,效率问题体现不出来,当n比较大的时候,效率特别慢,有更好的方法的朋友可以提点一些。

在实现的过程中,发现设计很重要,刚开始没有进行详细的设计,仅仅根据一点思路去实现,走了很多弯路,也花了很多的时间。所以算法设计一定要设计清晰,在实现中才能有的放矢的完善细节。

    原文作者:递归算法
    原文地址: https://blog.csdn.net/aitazhixin/article/details/61414329
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞