我找到了标题中写的简单算法代码-将其添加到了这篇文章(source)中。
我发现很难计算该算法的空间复杂度和运行时间。以为它可能是O((nCr)*r)
,但我认为我们在这里只传递了nCr
个项目(并且可能在打印循环中对每个项目执行r
个迭代)。
能否在这里解释如何计算空间复杂度和运行时间?
#include <stdio.h>
void combinationUtil(int arr[], int n, int r,
int index, int data[], int i);
// The main function that prints all combinations of
// size r in arr[] of size n. This function mainly
// uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination
// one by one
int data[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}
/* arr[] ---> Input Array
n ---> Size of input array
r ---> Size of a combination to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store current combination
i ---> index of current element in arr[] */
void combinationUtil(int arr[], int n, int r, int index,
int data[], int i)
{
// Current cobination is ready, print it
if (index == r) {
for (int j = 0; j < r; j++)
printf("%d ", data[j]);
printf("\n");
return;
}
// When no more elements are there to put in data[]
if (i >= n)
return;
// current is included, put next at next location
data[index] = arr[i];
combinationUtil(arr, n, r, index + 1, data, i + 1);
// current is excluded, replace it with next
// (Note that i+1 is passed, but index is not
// changed)
combinationUtil(arr, n, r, index, data, i + 1);
}