gurobi - Error code = 10004 Unable to retrieve attribute 'X' - c++

I am getting an error in my c++/gurobi file:
Error code = 10004 Unable to retrieve attribute 'X'
I read that this might have something to do with labels? But I don't see how there is a problem.
It works for some input files, but not for others. So I have created a toy file, t5.txt in attachment. This file does not work, but removing the last column and setting 8 to 7 fixes it. I am puzzled...
Below is the output of model.write. Everything seems to make sense, any Ideas what I am doing wrong?
Whenever I do a model.write(test.sol), the program stops, so there seems to be something wrong with the solution>
Attachments:
main.cpp -> https://dl.dropboxusercontent.com/u/13564139/main.cpp
input.txt -> https://dl.dropboxusercontent.com/u/13564139/t5.txt
Maximize
15 student_has_projects4.1
Subject To
R0: student_has_projects0.0 + student_has_projects1.0
+ student_has_projects2.0 + student_has_projects3.0
+ student_has_projects4.0 + student_has_projects5.0
+ student_has_projects6.0 + student_has_projects7.0 <= 4
R1: student_has_projects1.0 + student_has_projects2.0 >= 1
R2: student_has_projects2.0 + 2 student_has_projects5.0 <= 2
R3: student_has_projects2.0 + 2 student_has_projects5.0 >= 1
R4: student_has_projects0.0 + student_has_projects3.0
+ student_has_projects4.0 + student_has_projects6.0
+ student_has_projects7.0 >= 1
R5: student_has_projects2.0 + student_has_projects5.0 <= 1
R6: student_has_projects0.1 + student_has_projects1.1
+ student_has_projects2.1 + student_has_projects3.1
+ student_has_projects4.1 + student_has_projects5.1
+ student_has_projects6.1 + student_has_projects7.1 <= 4
R7: student_has_projects1.1 + student_has_projects2.1 >= 1
R8: student_has_projects2.1 + 2 student_has_projects5.1 <= 2
R9: student_has_projects2.1 + 2 student_has_projects5.1 >= 1
R10: student_has_projects0.1 + student_has_projects3.1
+ student_has_projects4.1 + student_has_projects6.1
+ student_has_projects7.1 >= 1
R11: student_has_projects2.1 + student_has_projects5.1 <= 1
R12: student_has_projects0.2 + student_has_projects1.2
+ student_has_projects2.2 + student_has_projects3.2
+ student_has_projects4.2 + student_has_projects5.2
+ student_has_projects6.2 + student_has_projects7.2 <= 4
R13: student_has_projects1.2 + student_has_projects2.2 >= 1
R14: student_has_projects2.2 + 2 student_has_projects5.2 <= 2
R15: student_has_projects2.2 + 2 student_has_projects5.2 >= 1
R16: student_has_projects0.2 + student_has_projects3.2
+ student_has_projects4.2 + student_has_projects6.2
+ student_has_projects7.2 >= 1
R17: student_has_projects2.2 + student_has_projects5.2 <= 1
R18: student_has_projects0.0 + student_has_projects0.1
+ student_has_projects0.2 = 1
R19: student_has_projects1.0 + student_has_projects1.1
+ student_has_projects1.2 = 1
R20: student_has_projects2.0 + student_has_projects2.1
+ student_has_projects2.2 = 1
R21: student_has_projects3.0 + student_has_projects3.1
+ student_has_projects3.2 = 1
R22: student_has_projects4.0 + student_has_projects4.1
+ student_has_projects4.2 = 1
R23: student_has_projects5.0 + student_has_projects5.1
+ student_has_projects5.2 = 1
R24: student_has_projects6.0 + student_has_projects6.1
+ student_has_projects6.2 = 1
R25: student_has_projects7.0 + student_has_projects7.1
+ student_has_projects7.2 = 1
Bounds
Binaries
student_has_projects0.0 student_has_projects0.1 student_has_projects0.2
student_has_projects1.0 student_has_projects1.1 student_has_projects1.2
student_has_projects2.0 student_has_projects2.1 student_has_projects2.2
student_has_projects3.0 student_has_projects3.1 student_has_projects3.2
student_has_projects4.0 student_has_projects4.1 student_has_projects4.2
student_has_projects5.0 student_has_projects5.1 student_has_projects5.2
student_has_projects6.0 student_has_projects6.1 student_has_projects6.2
student_has_projects7.0 student_has_projects7.1 student_has_projects7.2
End

The issue is that your lp instance is infeasible so the call to .optimize() results in an infeasible status. From your code
model.write("test2.lp");
model.optimize();
model.write("forum2.sol");
if(model.get(GRB_IntAttr_Status) != GRB_OPTIMAL){
cout << "niet optimaal " << endl;
}
You are writing a .sol file before you check for success. Gurobi gets the 'X' attributes from the variables when it writes a .sol file. If the optimization fails, the 'X' attributes aren't available and an exception is thrown. You should make sure that gurobi has a solution before you write a .sol file, or get many attributes, including 'X', 'Pi' and 'ObjVal'. The OPTIMAL status codes assures you that that there is an available solution, but codes like SUBOPTIMAL also indicate that there is a solution available and others like TIME_LIMIT, NODE_LIMIT mean there might be a solution available. You can get the attribute SolCount to get a definitive indication that there is a solution available.
Your problem instance is infeasible because constraints (R1, R7, R13 imply you need at least 3 projects for students 1 and 2, but constraints (R19, R20) imply they can have exactly 1 project each. You can see this by using the IIS solver. In interactive gurobi you can get get an Irreducible Inconsistent Subsystem
m = read("test2.lp")
m.optimize()
m.computeIIS()
m.write("test2.ilp")

Related

Step by step recursion

int f(int x)
{
if (x < 1)
return 0;
else
return f(x - 3) + x;
}
Can some one explain how it calculates the final result for f(10)=22?
step by step please
Working through this step by step is not difficult.
f(10)
f(10 - 3) + 10
(f(7 - 3) + 7) + 10
((f(4 - 3) + 4) + 7) + 10
(((f(1 - 3) + 1) + 4) + 7) + 10
(((0 + 1) + 4) + 7) + 10
((1 + 4) + 7) + 10
(5 + 7) + 10
12 + 10
22
f(10) = f(7) + 10 = f(4) + 10 + 7 = f(1) + 10 + 7 + 4 = f(-2) + 10 + 7 + 4 + 1 = 0 + 10 + 7 + 4 + 1

Calculating time complexity of a recursive function having a loop inside it

I was working on a simple problem and I came up with a recursive function in C++, below is my function.
void test(int arr[],int n,int x = 0){
cout<<arr[x];
for(int i = x+1;i < n;i++){
test(arr, n, i);
}
}
I wonder what will be the time complexity of the above function if anyone can calculate the time complexity for the above method it will be a great help in improving my function.
You can write its recurrent relation likes the following:
T(n) = T(n-1) + T(n-2) + ... + T(1) + 1
Indeed T'(x) is T(n - x) and T(1) = 1 (The last one in the realtion is is for cout). We can see:
T(2) = T(1) + 1 = 2
T(3) = T(2) + T(1) + 1 = 2 + 1 + 1 = 4
T(4) = 4 + 2 + 1 + 1 = 2^2 + 2^1 + 2^0 + 1 = 8
T(5) = 8 + 4 + 2 + 1 + 1 = 2^3 + 2^2 + 2^1 + 2^0 + 1 = 16
.
.
.
T(n) = 2^{n-2} + 2^{n-1} + ... + 2^0 + 1 = 2^{n-1}
Hence, T(n) = \Theta(2^n).

Why does a basic optimal variable have positive reduced cost?

Why does a a basic optimal variable have reduced cost positive in a minimization problem?
The lp model is as follows:
\ENCODING=ISO-8859-1
\Problem name: IloCplex
Minimize
obj: 325255008 y(1) + 207016000 y(2) + 511.284124007454 x(1,2,1,2,1,1,1,1,1,1)
+ 511.284124007454 x(1,2,1,2,1,2,1,1,2,2)
+ 511.284124007454 x(1,2,1,2,2,2,2,2,2,2)
+ 511.284124007454 x(1,2,2,3,1,1,2,2,2,2)
+ 511.284124007454 x(1,2,2,3,1,2,2,2,3,3)
+ 511.284124007454 x(1,2,2,3,2,2,3,3,3,3)
+ 511.284124007454 x(1,2,3,1,1,1,3,3,3,3)
+ 511.284124007454 x(1,2,3,1,1,2,3,3,1,1)
+ 511.284124007454 x(1,2,3,1,2,2,1,1,1,1)
+ 511.284124007454 x(2,1,1,2,1,1,2,2,2,2)
+ 511.284124007454 x(2,1,1,2,2,1,1,1,2,2)
+ 511.284124007454 x(2,1,1,2,2,2,1,1,1,1)
+ 511.284124007454 x(2,1,2,3,1,1,3,3,3,3)
+ 511.284124007454 x(2,1,2,3,2,1,2,2,3,3)
+ 511.284124007454 x(2,1,2,3,2,2,2,2,2,2)
+ 511.284124007454 x(2,1,3,1,1,1,1,1,1,1)
+ 511.284124007454 x(2,1,3,1,2,1,3,3,1,1)
+ 511.284124007454 x(2,1,3,1,2,2,3,3,3,3) + 4201096 lambda(1)
+ 8400658 lambda(2) + 21000220 lambda(3)
Subject To
r_beta_1_2: x(1,2,1,2,1,1,1,1,1,1) + x(1,2,1,2,1,2,1,1,2,2)
+ x(1,2,1,2,2,2,2,2,2,2) + x(1,2,2,3,1,1,2,2,2,2)
+ x(1,2,2,3,1,2,2,2,3,3) + x(1,2,2,3,2,2,3,3,3,3)
+ x(1,2,3,1,1,1,3,3,3,3) + x(1,2,3,1,1,2,3,3,1,1)
+ x(1,2,3,1,2,2,1,1,1,1) = 1
r_beta_2_1: x(2,1,1,2,1,1,2,2,2,2) + x(2,1,1,2,2,1,1,1,2,2)
+ x(2,1,1,2,2,2,1,1,1,1) + x(2,1,2,3,1,1,3,3,3,3)
+ x(2,1,2,3,2,1,2,2,3,3) + x(2,1,2,3,2,2,2,2,2,2)
+ x(2,1,3,1,1,1,1,1,1,1) + x(2,1,3,1,2,1,3,3,1,1)
+ x(2,1,3,1,2,2,3,3,3,3) = 1
r_delta_1_2_1: - y(1) + x(1,2,1,2,1,1,1,1,1,1)
+ x(1,2,1,2,1,2,1,1,2,2) + x(1,2,2,3,1,1,2,2,2,2)
+ x(1,2,2,3,1,2,2,2,3,3) + x(1,2,3,1,1,1,3,3,3,3)
+ x(1,2,3,1,1,2,3,3,1,1) <= 0
r_delta_1_2_2: - y(2) + x(1,2,1,2,1,2,1,1,2,2)
+ x(1,2,1,2,2,2,2,2,2,2) + x(1,2,2,3,1,2,2,2,3,3)
+ x(1,2,2,3,2,2,3,3,3,3) + x(1,2,3,1,1,2,3,3,1,1)
+ x(1,2,3,1,2,2,1,1,1,1) <= 0
r_delta_2_1_1: - y(1) + x(2,1,1,2,1,1,2,2,2,2)
+ x(2,1,1,2,2,1,1,1,2,2) + x(2,1,2,3,1,1,3,3,3,3)
+ x(2,1,2,3,2,1,2,2,3,3) + x(2,1,3,1,1,1,1,1,1,1)
+ x(2,1,3,1,2,1,3,3,1,1) <= 0
r_delta_2_1_2: - y(2) + x(2,1,1,2,2,1,1,1,2,2)
+ x(2,1,1,2,2,2,1,1,1,1) + x(2,1,2,3,2,1,2,2,3,3)
+ x(2,1,2,3,2,2,2,2,2,2) + x(2,1,3,1,2,1,3,3,1,1)
+ x(2,1,3,1,2,2,3,3,3,3) <= 0
r_piI_1_2: y(1) + x(1,2,1,2,2,2,2,2,2,2) + x(1,2,2,3,2,2,3,3,3,3)
+ x(1,2,3,1,2,2,1,1,1,1) <= 1
r_piI_2_1: y(2) + x(2,1,1,2,1,1,2,2,2,2) + x(2,1,2,3,1,1,3,3,3,3)
+ x(2,1,3,1,1,1,1,1,1,1) <= 1
r_piJ_1_2: y(2) + x(1,2,1,2,1,1,1,1,1,1) + x(1,2,2,3,1,1,2,2,2,2)
+ x(1,2,3,1,1,1,3,3,3,3) <= 1
r_piJ_2_1: y(1) + x(2,1,1,2,2,2,1,1,1,1) + x(2,1,2,3,2,2,2,2,2,2)
+ x(2,1,3,1,2,2,3,3,3,3) <= 1
r_gamma_1_2_1_2: 17.7232875823975 x(1,2,1,2,1,1,1,1,1,1)
+ 17.7232875823975 x(1,2,1,2,1,2,1,1,2,2)
+ 17.7232875823975 x(1,2,1,2,2,2,2,2,2,2)
- 200 lambda(1) - 300 lambda(2) - 500 lambda(3) <= 0
r_gamma_1_2_2_3: 17.7232875823975 x(1,2,2,3,1,1,2,2,2,2)
+ 17.7232875823975 x(1,2,2,3,1,2,2,2,3,3)
+ 17.7232875823975 x(1,2,2,3,2,2,3,3,3,3) <= 0
r_gamma_1_2_3_1: 17.7232875823975 x(1,2,3,1,1,1,3,3,3,3)
+ 17.7232875823975 x(1,2,3,1,1,2,3,3,1,1)
+ 17.7232875823975 x(1,2,3,1,2,2,1,1,1,1) <= 0
r_gamma_2_1_1_2: 17.7232875823975 x(2,1,1,2,1,1,2,2,2,2)
+ 17.7232875823975 x(2,1,1,2,2,1,1,1,2,2)
+ 17.7232875823975 x(2,1,1,2,2,2,1,1,1,1) <= 0
r_gamma_2_1_2_3: 17.7232875823975 x(2,1,2,3,1,1,3,3,3,3)
+ 17.7232875823975 x(2,1,2,3,2,1,2,2,3,3)
+ 17.7232875823975 x(2,1,2,3,2,2,2,2,2,2)
- 200 lambda(1) - 300 lambda(2) - 500 lambda(3) <= 0
r_gamma_2_1_3_1: 17.7232875823975 x(2,1,3,1,1,1,1,1,1,1)
+ 17.7232875823975 x(2,1,3,1,2,1,3,3,1,1)
+ 17.7232875823975 x(2,1,3,1,2,2,3,3,3,3) <= 0
c17: lambda(1) - Rgc17 = 0
c18: lambda(2) - Rgc18 = 0
c19: lambda(3) - Rgc19 = 0
r_casamento_y_lambda_1: y(1) - lambda(1) - lambda(2) - lambda(3) <= 0
r_casamento_y_lambda_2: y(2) - lambda(1) - lambda(2) - lambda(3) <= 0
Bounds
0 <= y(1) <= 1
0 <= y(2) <= 1
0 <= x(1,2,1,2,1,1,1,1,1,1) <= 1
0 <= x(1,2,1,2,1,2,1,1,2,2) <= 1
0 <= x(1,2,1,2,2,2,2,2,2,2) <= 1
0 <= x(1,2,2,3,1,1,2,2,2,2) <= 1
0 <= x(1,2,2,3,1,2,2,2,3,3) <= 1
0 <= x(1,2,2,3,2,2,3,3,3,3) <= 1
0 <= x(1,2,3,1,1,1,3,3,3,3) <= 1
0 <= x(1,2,3,1,1,2,3,3,1,1) <= 1
0 <= x(1,2,3,1,2,2,1,1,1,1) <= 1
0 <= x(2,1,1,2,1,1,2,2,2,2) <= 1
0 <= x(2,1,1,2,2,1,1,1,2,2) <= 1
0 <= x(2,1,1,2,2,2,1,1,1,1) <= 1
0 <= x(2,1,2,3,1,1,3,3,3,3) <= 1
0 <= x(2,1,2,3,2,1,2,2,3,3) <= 1
0 <= x(2,1,2,3,2,2,2,2,2,2) <= 1
0 <= x(2,1,3,1,1,1,1,1,1,1) <= 1
0 <= x(2,1,3,1,2,1,3,3,1,1) <= 1
0 <= x(2,1,3,1,2,2,3,3,3,3) <= 1
0 <= lambda(1) <= 1
0 <= lambda(2) <= 1
0 <= lambda(3) <= 1
0 <= Rgc17 <= 3
0 <= Rgc18 <= 3
0 <= Rgc19 <= 3
End
I'm worried with lambdas vairables.
When I solve this model directly by the terminal using cplex, I can get this information :
Display which part of the solution: reduced
Display reduced costs for which variable(s): lambda(1)
Variable Name Reduced Cost
lambda(1) -4199562.000000
CPLEX> display solution reduced
Display reduced costs for which variable(s): lambda(2)
The reduced cost 'lambda(2)' is 0.
CPLEX> display solution reduced
Display reduced costs for which variable(s): lambda(3)
Variable Name Reduced Cost
lambda(3) 12599562.000000
CPLEX> display solution variables
Display values of which variable(s): lambda(1)
Variable Name Solution Value
lambda(1) 1.000000
CPLEX> display solution variables
Display values of which variable(s): lambda(2)
The variable 'lambda(2)' is 0.
CPLEX> display solution variables
Display values of which variable(s): lambda(3)
The variable 'lambda(3)' is 0.
Is that ok?
A variable can be non-basic if it is at one of its bounds (i.e. lower-bound or upper-bound). (Detail: free variable are special: they can be nonbasic between bounds -- sometimes called superbasic). So all variables λ are potentially non-basic when we look at the values. The reduced cost indicate that λ1 and λ3 must be non-basic and λ2 can be basic or non-basic (if the solution is degenerate). Use display solution basis to find all basic variables.
The sign of the reduced cost depends if the variable is nonbasic upper or nonbasic lower. It basically indicates how the objective can change if the corresponding bound changes. A positive rc for λ3 looks fine to me. (It is non-basic and not basic as you seem to think).

How to increase enough stack size in Pari/Gp for command to work

I am working with GP and minimum polynomials as follows running on ASUS x75:
(19:25) gp > elt=Mod(a*x^3+b*x^2+c*x+d,('x^5-1)/('x-1))
%122 = Mod(a*x^3 + b*x^2 + c*x + d, x^4 + x^3 + x^2 + x + 1)
(19:25) gp > (poly=minpoly(elt,x='x))
%123 = x^4 + (a + (b + (c - 4*d)))*x^3 + (a^2 + (-3*b + (2*c - 3*d))*a + (b^2 + (2*c - 3*d)*b + (c^2 - 3*d*c + 6*d^2)))*x^2 + (a^3 + (-2*b + (3*c - 2*d))*a^2 + (-2*b^2 + (c + 6*d)*b + (-2*c^2 - 4*d*c + 3*d^2))*a + (b^3 + (-2*c - 2*d)*b^2 + (3*c^2 - 4*d*c + 3*d^2)*b + (c^3 - 2*d*c^2 + 3*d^2*c - 4*d^3)))*x + (a^4 + (-b + (-c - d))*a^3 + (b^2 + (2*c + 2*d)*b + (c^2 - 3*d*c + d^2))*a^2 + (-b^3 + (-3*c + 2*d)*b^2 + (2*c^2 - d*c - 3*d^2)*b + (-c^3 + 2*d*c^2 + 2*d^2*c - d^3))*a + (b^4 + (-c - d)*b^3 + (c^2 + 2*d*c + d^2)*b^2 + (-c^3 - 3*d*c^2 + 2*d^2*c - d^3)*b + (c^4 - d*c^3 + d^2*c^2 - d^3*c + d^4)))
The first command came out successfully, while the second one below did finish successfully and gave an allocatemem() error. How is it possible to get the second command to work, without overheating the computer or program exhaustion? And the WHOLE output to command below this is needed. Thanks for the help.
(19:23) gp > elt=Mod(a*x^5+b*x^4+c*x^3+d*x^2+e*x+f,('x^7-1)/('x-1))
%120 = Mod(a*x^5 + b*x^4 + c*x^3 + d*x^2 + e*x + f, x^6 + x^5 + x^4 + x^3 + x^2 + x + 1)
(19:23) gp > (poly=minpoly(elt,x='x))
*** at top-level: poly=minpoly(elt,x='x)
*** ^-----------------
*** minpoly: the PARI stack overflows !
current stack size: 9000000 (8.583 Mbytes)
[hint] you can increase GP stack with allocatemem()
You can increase the PARI/GP's heap up to any limit you want at run-time following the example below (demonstrates how to set heap size to 120000000 bytes):
default(parisize, 120000000)
default(parisize, 10000000000) is more than 8 GB and in my case was
enough to make advanced calculations with matrices.

Complexity of an algorithm with two recursive calls

I have a strange algorithm than is being called recursively 2 times. It's
int alg(int n)
loop body = Θ(3n+1)
alg(n-1);
alg(n-2)
Somehow i need to find this algorithm's complexity. I've tried to find it with using characteristic polynomial of the above equation but the result system is too hard to solve so i was wondering if there was any other straight way..
Complexity: alg(n) = Θ(φ^n) where φ = Golden ratio = (1 + sqrt(5)) / 2
I can't formally prove it at first, but with a night's work, I find my missing part - The substitution method with subtracting a lower-order term. Sorry for my bad expression of provement (∵ poor English).
Let loop body = Θ(3n+1) ≦ tn
Assume (guess) that cφ^n ≦ alg(n) ≦ dφ^n - 2tn for an n (n ≧ 4)
Consider alg(n+1):
Θ(n) + alg(n) + alg(n-1) ≦ alg(n+1) ≦ Θ(n) + alg(n) + alg(n-1)
c * φ^n + c * φ^(n-1) ≦ alg(n+1) ≦ tn + dφ^n - 2tn + dφ^(n-1) - 2t(n-1)
c * φ^(n+1) ≦ alg(n+1) ≦ tn + d * φ^(n+1) - 4tn + 2
c * φ^(n+1) ≦ alg(n+1) ≦ d * φ^(n+1) - 3tn + 2
c * φ^(n+1) ≦ alg(n+1) ≦ d * φ^(n+1) - 2t(n+1) (∵ n ≧ 4)
So it is correct for n + 1. By mathematical induction, we can know that it's correct for all n.
So cφ^n ≦ alg(n) ≦ dφ^n - 2tn and then alg(n) = Θ(φ^n).
johnchen902 is correct:
alg(n)=Θ(φ^n) where φ = Golden ratio = (1 + sqrt(5)) / 2
but his argument is a bit too hand-waving, so let's make it strict. His original argument was incomplete, therefore I added mine, but now he has completed the argument.
loop body = Θ(3n+1)
Let us denote the cost of the loop body for the argument n with g(n). Then g(n) ∈ Θ(n) since Θ(n) = Θ(3n+1).
Further, let T(n) be the total cost of alg(n) for n >= 0. Then, for n >= 2 we have the recurrence
T(n) = T(n-1) + T(n-2) + g(n)
For n >= 3, we can insert the recurrence applied to T(n-1) into that,
T(n) = 2*T(n-2) + T(n-3) + g(n) + g(n-1)
and for n > 3, we can continue, applying the recurrence to T(n-2). For sufficiently large n, we therefore have
T(n) = 3*T(n-3) + 2*T(n-4) + g(n) + g(n-1) + 2*g(n-2)
= 5*T(n-4) + 3*T(n-5) + g(n) + g(n-1) + 2*g(n-2) + 3*g(n-3)
...
k-1
= F(k)*T(n+1-k) + F(k-1)*T(n-k) + ∑ F(j)*g(n+1-j)
j=1
n-1
= F(n)*T(1) + F(n-1)*T(0) + ∑ F(j)*g(n+1-j)
j=1
with the Fibonacci numbers F(n) [F(0) = 0, F(1) = F(2) = 1].
T(0) and T(1) are some constants, so the first part is obviously Θ(F(n)). It remains to investigate the sum.
Since g(n) ∈ Θ(n), we only need to investigate
n-1
A(n) = ∑ F(j)*(n+1-j)
j=1
Now,
n-1
A(n+1) - A(n) = ∑ F(j) + (((n+1)+1) - ((n+1)-1))*F((n+1)-1)
j=1
n-1
= ∑ F(j) + 2*F(n)
j=1
= F(n+1) - 1 + 2*F(n)
= F(n+2) + F(n) - 1
Summing that, starting with A(2) = 2 = F(5) + F(3) - 5, we obtain
A(n) = F(n+3) + F(n+1) - (n+3)
and therefore, with
c*n <= g(n) <= d*n
the estimate
F(n)*T(1) + F(n-1)*T(0) + c*A(n) <= T(n) <= F(n)*T(1) + F(n-1)*T(0) + d*A(n)
for n >= 2. Since F(n+1) <= A(n) < F(n+4), all terms depending on n in the left and right parts of the inequality are Θ(φ^n), q.e.d.
Assumptions:
1: n >= 0
2: Θ(3n+1) = 3n + 1
Complexity:
O(2 ^ n * (3n - 2));
Reasoning:
int alg(int n)
loop body = Θ(3n+1)// for every n you have O(3n+1)
alg(n-1);
alg(n-2)
Assuming the alg does not execute for n < 1, you have the following repetitions:
Step n:
3 * n + 1
alg(n - 1) => 3 * (n - 1) + 1
alg(n - 2) => 3 * (n - 2) + 1
Now you basically have a division. You have to imagine a number tree with N as parent and n-1 and n-2 as children.
n
n-1 n-2
n - 2 n - 3 n - 3 n - 4
n - 3 n - 4 n - 4 n - 5 n - 4 n - 5 n - 5 n - 6
n-4 n-5 | n-5 n-6 |n-5 n-6 |n-6 n-7 n-5 n-6 n-6 n-7 n-6 n-6| n-6 n-8
It's obvious that there is a repetition pattern here. For every pair (n - k, n - k - 1) in A = {k, with k from 0 to n) except the first two and the last two, (n - 1, n - 2) and (n-2, n-3) there is a 3k + 1 * (2 ^ (k - 1)) complexity.
I am looking at the number of repetitions of the pair (n - k, n - k - 1). So now for each k from 0 to n I have:
(3k + 1) * (2 ^ (k - 1)) iterations.
If you sum this up from 1 to n you should get the desired result. I will expand the expression:
(3k + 1) * (2 ^ (k - 1)) = 3k * 2 ^ (k - 1) + 2 ^ (k - 1)
Update
1 + 2 + 2^2 + 2^3 + ... + 2^n = 2 ^ (n + 1) - 1
In your case, this winds up being:
2^n - 1
Based on the summation formula and k = 0, n . Now the first one:
3k * 2 ^ (k - 1)
This is equal to 3 sum from k = 0, n of k * 2 ^ (k - 1).
That sum can be determined by switching to polinomial functions, integrating, contracting using the 1 + a ^ 2 + a ^ 3 + ... + a ^ n formula, and then differentiated again to obtain the result, which is (n - 1) * 2 ^ n + 1.
So you have:
2 ^ n - 1 + 3 * (n - 1) * 2 ^ n + 1
Which contracted is:
2 ^ n * (3n - 2);
The body of the function takes Θ(n) time.
The function is called twice recursively.
For the given function the complexity is,
T(n) = T(n-1) + T(n-2) + cn ----- 1
T(n-1) = T(n-2) + T(n-3) + c(n-1) ----- 2
1-2 -> T(n) = 2T(n-1) - T(n-3) + c ----- 3
3 --> T(n-1) = 2T(n-2) + T(n-4) + c ----- 4
3-4 -> T(n) = 3T(n-1) - 2T(n-2) - T(n-3) - T(n-4) ----- 5
Let g(n) = 3g(n-1)
There for, we can approximate T(n) = O(g(n))
g(n) is Θ(3n)
There for T(n) = O(3n)