finding time complexity of backtracking solution for generate all subset problem - recursion

Given the problem of distinct integers, generate all subsets.
https://www.interviewbit.com/problems/subset/
I have found two solutions.
first solution::
void helper_subsets(vector<vector<int>> &res , vector<int> &A ,
vector<int> &subset ,int current)
{
if(current == A.size())
res.push_back(subset) ;
else
{
helper_subsets(res,A,subset,current+1) ;
subset.push_back(A[current]) ;
helper_subsets(res,A,subset,current+1) ;
subset.pop_back() ;
}
}
vector<vector<int> >subsets(vector<int> &A) {
vector<vector<int>> res ;
sort(A.begin(),A.end()) ;
vector<int> subset ;
helper_subsets(res , A , subset , 0 ) ;
sort(res.begin(),res.end()) ;
return res ;
}
Second solution ::
void helper_subsets(vector<vector<int>> &res , vector<int> &A ,
vector<int> &subset ,int current)
{
res.push_back(subset) ;
for(int i = current ; i < A.size() ; i++)
{
subset.push_back(A[i]) ;
helper_subsets(res,A,subset,i+1) ;
subset.pop_back() ;
}
}
vector<vector<int> > subsets(vector<int> &A) {
vector<vector<int>> res ;
sort(A.begin(),A.end()) ;
vector<int> subset ;
helper_subsets(res , A , subset , 0 ) ;
sort(res.begin(),res.end()) ;
return res ;
}
The problem is that I am able to calculate the time complexity of the first solution mathematically as well using recursion tree.
t(n) = 2t(n-1) + c (i.e 2 recursive calls with size n-1 and some constant time for each n)
t(n) = O(2^n) by solving the above recurrence relation.
But with the second solution, I am not able to define recurrence relation to finally use back substitution to get the time complexity and could not get it by recurrence tree method.Please help me find time complexity of second solution.

The analogous recurrence relation for problem 2 is:
n - 1
T(n) = Σ T(n - i) + c
i = 1
– which follows from the for-loop from current to A.size(). To solve this, expand the first term:
T(n) = T(n - 1) + T(n - 2) + T(n - 3) + ... + T(1) + c
--------
|
= | T(n - 2) + T(n - 3) + ... + T(1) + c +
---> T(n - 2) + T(n - 3) + ... + T(1) + c
= 2 * [T(n - 2) + T(n - 3) + ... + T(1) + c]
= 2 * T(n - 1)
i.e., a very similar recurrence relation differing only by a constant. It still evaluates to O(2^n), taking the base case to be T(1) = O(1).

Related

How to do it recursively if function depends on only one parameter

I need to do it with recursion, but the problem is that function depends on only ONE parameter and inside function it depends on two ( k and n ), also how to find minimum value if it returns only one value?
The function is :
I've already tried to make random k, but I don't think that is really good idea.
F1(int n) {
Random random = new Random();
int k = random.Next(1,10);
if (1 <= k && k <= n){
return Math.Min(F1(k - 1) + F1(n - k) + n);
} else {
return 0;
}
}
You need to make a loop traversing all k values in range 1..n. Something like this:
F1(int n) {
if (n == 0)
return ???? what is starting value?
minn = F1(0) + F1(n - 1) + n
for (int k = 2; k <= n; k++)
minn = Math.Min(minn, F1(k - 1) + F1(n - k) + n);
return minn;
}

Determining recurrence equation of a recursive method

Given the following recursive method:
/∗∗ Returns the product of integer elements in given array. ∗/
public static int product(int[] arr, int low, int high) {
if (high < low)
return 1;
if (low == high)
return arr[low];
return arr[low] * product(arr, low+1, high-1) * arr[high];
}
I determined the recurrence equation: T(n) = T(n - 2) + c
and the corresponding base case equation: T(0) = 1
To work out the closed form solution I do the following:
T(n) = T(n - 2) + c
T(n - 2) = T(n - 4) + c
//Substitute T(n - 2)
T(n) = T(n - 4) + 2.c
T(n) = T(n - 2k) + k.c
//Select k = 1/2.n and substitute
T(n) = T(0) + (1/2n).c
T(n) = 1 + (1/2n).c
1) Is my recurrence equation and base case correct and why?
2) Is my closed form solution correct and why?

sequence of numbers using recursion

I want to compute sequence of numbers like this:
n*(n-1)+n*(n-1)*(n-2)+n*(n-1)*(n-2)*(n-3)+n*(n-1)*(n-2)*(n-3)*(n-4)+...+n(n-1)...(n-n)
For example n=5 and sum equals 320.
I have a function, which compute one element:
int fac(int n, int s)
{
if (n > s)
return n*fac(n - 1, s);
return 1;
}
Recomputing the factorial for each summand is quite wasteful. Instead, I'd suggest to use memoization. If you reorder
n*(n-1) + n*(n-1)*(n-2) + n*(n-1)*(n-2)*(n-3) + n*(n-1)*(n-2)*(n-3)*...*1
you get
n*(n-1)*(n-2)*(n-3)*...*1 + n*(n-1)*(n-2)*(n-3) + n*(n-1)*(n-2) + n*(n-1)
Notice how you start with the product of 1..n, then you add the product of 1..n divided by 1, then you add the product divided by 1*2 etc.
I think a much more efficient definition of your function is (in Python):
def f(n):
p = product(range(1, n+1))
sum_ = p
for i in range(1, n-1):
p /= i
sum_ += p
return sum_
A recursive version of this definition is:
def f(n):
def go(sum_, i):
if i >= n-1:
return sum_
return sum_ + go(sum_ / i, i+1)
return go(product(range(1, n+1)), 1)
Last but not least, you can also define the function without any explicit recursion by using reduce to generate the list of summands (this is a more 'functional' -- as in functional programming -- style):
def f(n):
summands, _ = reduce(lambda (lst, p), i: (lst + [p], p / i),
range(1, n),
([], product(range(1, n+1))))
return sum(summands)
This style is very concise in functional programming languages such as Haskell; Haskell has a function call scanl which simplifies generating the summands so that the definition is just:
f n = sum $ scanl (/) (product [1..n]) [1..(n-2)]
Something like this?
function fac(int n, int s)
{
if (n >= s)
return n * fac(n - 1, s);
return 1;
}
int sum = 0;
int s = 4;
n = 5;
while(s > 0)
{
sum += fac(n, s);
s--;
}
print sum; //320
Loop-free version:
int fac(int n, int s)
{
if (n >= s)
return n * fac(n - 1, s);
return 1;
}
int compute(int n, int s, int sum = 0)
{
if(s > 0)
return compute(n, s - 1, sum + fac(n, s));
return sum;
}
print compute(5, 4); //320
Ok ther is not mutch to write. I would suggest 2 methodes if you want to solve this recursiv. (Becaus of the recrusiv faculty the complexity is a mess and runtime will increase drasticaly with big numbers!)
int func(int n){
return func(n, 2);
}
int func(int n, int i){
if (i < n){
return n*(fac(n-1,n-i)+func(n, i + 1));
}else return 0;
}
int fac(int i,int a){
if(i>a){
return i*fac(i-1, a);
}else return 1;
}

Quadratic functions in c

I have almost everything working except for solving for X in line 25 i keep getting an error saying " term does not evaluate to a function taking 1787 arguments" i had it giving me a 1 or a 0 but as i kept messing with it i lost where i was at and saved over the copy. still new to posting sorry if its hard to read
#include <stdio.h>
#include <math.h>
void quadratic_function()
{
int a,b,c; // variables
long int result; // my X in the quadractic function
long int y,x; // the result
long int quadratic;
printf("enter values for a,b,c\n");
scanf("%i\n %i\n %i", &a,&b,&c);
printf("A=%i B=%i C=%i\n", a,b,c); //Displays Variables
y= pow(b, 2);
result= (y)*-4*(a)*(c); // b^2-4ac
printf("\n%li\n",result);
if (result<0)
printf("Imaginary Number"); // if negative
else (result>0);
x=(-b/2*(a)) +- (sqrt(pow(b, 2)) (-4*(a)*(c))) / (2*(a));
//solving for x
printf("\n %li\n",x);
a = a*x;
b = b*x;
quadratic=pow(a, 2)*(b)*(c); // if positive
//printf("Quadratic equation equal to %li",quadratic); // result
}
int main()
{
quadratic_function();
return 0;
}
The first thing I noticed is that you were trying to do the + and - portions of the quadratic equation at the same time. The equation
x = (-b +- sqrt(b^2 - 4ac)) / 2a
means the same as
x = (-b + sqrt(b^2 - 4ac)) / 2a AND x = (-b - sqrt(b^2 - 4ac)) / 2a
In other words, the equation has two answers if b^2 - 4ac is greater than 0, one answer if it is 0, and no answer if it is negative.
Another thing, the line else (result>0); doesn't really do anything. The rest of the code after that will execute even if you get b^2 - 4ac < 0
Finally, I wasn't entirely sure about your groupings or C++'s precedence with the negative sign, so I changed your parentheses around a bit.
y = pow(b, 2);
result = (y) - (4*a*c); // b^2-4ac
printf("\n%li\n", result);
if (result < 0) {
printf("Imaginary Number"); // if negative
} else if (result == 0) {
x = (-b) / (2 * a); // sqrt(0) = 0, so don't bother calculating it
a = a*x;
b = b*x;
quadratic=pow(a, 2)*(b)*(c);
printf("Quadratic equation equal to %li",quadratic); // result
} else if (result > 0) {
// solve for (-b + sqrt(b^2 - 4ac)) / 2a
x = ((-b) + sqrt(pow(b, 2) - (4 * a * c))) / (2 * a);
printf("\n %li\n",x);
a = a*x;
b = b*x;
quadratic=pow(a, 2)*(b)*(c);
printf("Quadratic equation equal to %li",quadratic); // result
// do it again for (-b - sqrt(b^2 - 4ac)) / 2a
x = ((-b) - sqrt(pow(b, 2) - (4 * a * c))) / (2 * a);
printf("\n %li\n",x);
a = a*x;
b = b*x;
quadratic=pow(a, 2)*(b)*(c);
printf("Quadratic equation equal to %li",quadratic);
}

Optimization of Fibonacci sequence generating algorithm

As we all know, the simplest algorithm to generate Fibonacci sequence is as follows:
if(n<=0) return 0;
else if(n==1) return 1;
f(n) = f(n-1) + f(n-2);
But this algorithm has some repetitive calculation. For example, if you calculate f(5), it will calculate f(4) and f(3). When you calculate f(4), it will again calculate both f(3) and f(2). Could someone give me a more time-efficient recursive algorithm?
I have read about some of the methods for calculating Fibonacci with efficient time complexity following are some of them -
Method 1 - Dynamic Programming
Now here the substructure is commonly known hence I'll straightly Jump to the solution -
static int fib(int n)
{
int f[] = new int[n+2]; // 1 extra to handle case, n = 0
int i;
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
A space-optimized version of above can be done as follows -
static int fib(int n)
{
int a = 0, b = 1, c;
if (n == 0)
return a;
for (int i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}
Method 2- ( Using power of the matrix {{1,1},{1,0}} )
This an O(n) which relies on the fact that if we n times multiply the matrix M = {{1,1},{1,0}} to itself (in other words calculate power(M, n )), then we get the (n+1)th Fibonacci number as the element at row and column (0, 0) in the resultant matrix. This solution would have O(n) time.
The matrix representation gives the following closed expression for the Fibonacci numbers:
fibonaccimatrix
static int fib(int n)
{
int F[][] = new int[][]{{1,1},{1,0}};
if (n == 0)
return 0;
power(F, n-1);
return F[0][0];
}
/*multiplies 2 matrices F and M of size 2*2, and
puts the multiplication result back to F[][] */
static void multiply(int F[][], int M[][])
{
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
int y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
int z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
/*function that calculates F[][] raise to the power n and puts the
result in F[][]*/
static void power(int F[][], int n)
{
int i;
int M[][] = new int[][]{{1,1},{1,0}};
// n - 1 times multiply the matrix to {{1,0},{0,1}}
for (i = 2; i <= n; i++)
multiply(F, M);
}
This can be optimized to work in O(Logn) time complexity. We can do recursive multiplication to get power(M, n) in the previous method.
static int fib(int n)
{
int F[][] = new int[][]{{1,1},{1,0}};
if (n == 0)
return 0;
power(F, n-1);
return F[0][0];
}
static void multiply(int F[][], int M[][])
{
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
int y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
int z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
static void power(int F[][], int n)
{
if( n == 0 || n == 1)
return;
int M[][] = new int[][]{{1,1},{1,0}};
power(F, n/2);
multiply(F, F);
if (n%2 != 0)
multiply(F, M);
}
Method 3 (O(log n) Time)
Below is one more interesting recurrence formula that can be used to find nth Fibonacci Number in O(log n) time.
If n is even then k = n/2:
F(n) = [2*F(k-1) + F(k)]*F(k)
If n is odd then k = (n + 1)/2
F(n) = F(k)*F(k) + F(k-1)*F(k-1)
How does this formula work?
The formula can be derived from the above matrix equation.
fibonaccimatrix
Taking determinant on both sides, we get
(-1)n = Fn+1Fn-1 – Fn2
Moreover, since AnAm = An+m for any square matrix A, the following identities can be derived (they are obtained from two different coefficients of the matrix product)
FmFn + Fm-1Fn-1 = Fm+n-1
By putting n = n+1,
FmFn+1 + Fm-1Fn = Fm+n
Putting m = n
F2n-1 = Fn2 + Fn-12
F2n = (Fn-1 + Fn+1)Fn = (2Fn-1 + Fn)Fn (Source: Wiki)
To get the formula to be proved, we simply need to do the following
If n is even, we can put k = n/2
If n is odd, we can put k = (n+1)/2
public static int fib(int n)
{
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
// If fib(n) is already computed
if (f[n] != 0)
return f[n];
int k = (n & 1) == 1? (n + 1) / 2
: n / 2;
// Applyting above formula [See value
// n&1 is 1 if n is odd, else 0.
f[n] = (n & 1) == 1? (fib(k) * fib(k) +
fib(k - 1) * fib(k - 1))
: (2 * fib(k - 1) + fib(k))
* fib(k);
return f[n];
}
Method 4 - Using a formula
In this method, we directly implement the formula for the nth term in the Fibonacci series. Time O(1) Space O(1)
Fn = {[(√5 + 1)/2] ^ n} / √5
static int fib(int n) {
double phi = (1 + Math.sqrt(5)) / 2;
return (int) Math.round(Math.pow(phi, n)
/ Math.sqrt(5));
}
Reference: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html
Look here for implementation in Erlang which uses formula
. It shows nice linear resulting behavior because in O(M(n) log n) part M(n) is exponential for big numbers. It calculates fib of one million in 2s where result has 208988 digits. The trick is that you can compute exponentiation in O(log n) multiplications using (tail) recursive formula (tail means with O(1) space when used proper compiler or rewrite to cycle):
% compute X^N
power(X, N) when is_integer(N), N >= 0 ->
power(N, X, 1).
power(0, _, Acc) ->
Acc;
power(N, X, Acc) ->
if N rem 2 =:= 1 ->
power(N - 1, X, Acc * X);
true ->
power(N div 2, X * X, Acc)
end.
where X and Acc you substitute with matrices. X will be initiated with and Acc with identity I equals to .
One simple way is to calculate it iteratively instead of recursively. This will calculate F(n) in linear time.
def fib(n):
a,b = 0,1
for i in range(n):
a,b = a+b,a
return a
Hint: One way you achieve faster results is by using Binet's formula:
Here is a way of doing it in Python:
from decimal import *
def fib(n):
return int((Decimal(1.6180339)**Decimal(n)-Decimal(-0.6180339)**Decimal(n))/Decimal(2.236067977))
you can save your results and use them :
public static long[] fibs;
public long fib(int n) {
fibs = new long[n];
return internalFib(n);
}
public long internalFib(int n) {
if (n<=2) return 1;
fibs[n-1] = fibs[n-1]==0 ? internalFib(n-1) : fibs[n-1];
fibs[n-2] = fibs[n-2]==0 ? internalFib(n-2) : fibs[n-2];
return fibs[n-1]+fibs[n-2];
}
F(n) = (φ^n)/√5 and round to nearest integer, where φ is the golden ratio....
φ^n can be calculated in O(lg(n)) time hence F(n) can be calculated in O(lg(n)) time.
// D Programming Language
void vFibonacci ( const ulong X, const ulong Y, const int Limit ) {
// Equivalent : if ( Limit != 10 ). Former ( Limit ^ 0xA ) is More Efficient However.
if ( Limit ^ 0xA ) {
write ( Y, " " ) ;
vFibonacci ( Y, Y + X, Limit + 1 ) ;
} ;
} ;
// Call As
// By Default the Limit is 10 Numbers
vFibonacci ( 0, 1, 0 ) ;
EDIT: I actually think Hynek Vychodil's answer is superior to mine, but I'm leaving this here just in case someone is looking for an alternate method.
I think the other methods are all valid, but not optimal. Using Binet's formula should give you the right answer in principle, but rounding to the closest integer will give some problems for large values of n. The other solutions will unnecessarily recalculate the values upto n every time you call the function, and so the function is not optimized for repeated calling.
In my opinion the best thing to do is to define a global array and then to add new values to the array IF needed. In Python:
import numpy
fibo=numpy.array([1,1])
last_index=fibo.size
def fib(n):
global fibo,last_index
if (n>0):
if(n>last_index):
for i in range(last_index+1,n+1):
fibo=numpy.concatenate((fibo,numpy.array([fibo[i-2]+fibo[i-3]])))
last_index=fibo.size
return fibo[n-1]
else:
print "fib called for index less than 1"
quit()
Naturally, if you need to call fib for n>80 (approximately) then you will need to implement arbitrary precision integers, which is easy to do in python.
This will execute faster, O(n)
def fibo(n):
a, b = 0, 1
for i in range(n):
if i == 0:
print(i)
elif i == 1:
print(i)
else:
temp = a
a = b
b += temp
print(b)
n = int(input())
fibo(n)

Resources