Solving a complex recurrence relation - recurrence

How to solve the below recurrence relation?
T(n) = 2T(root(n)) + logn/loglogn if n > 4
T(n) = 1 if n <= 4
Preferably by master theorem otherwise by any method.
I know Master Theorem fails,But is there any extension for these type of problems?
Can you guide me any stuff for solving complex relation like above?

I think this should work :
if n = 2^m and T(2^m) = s(m) then
logn = m , loglogn = logm ;
s(m) = 2*s(m/2) + m/logm ;
now solving the above equation is our problem
now you can not use the master theorem for solving this , so you have to use other methods like expanding this equation by writing s(m/2) and s(m/4) and then you might solve this problem , and after doing that you change your parameters to n again .

According to me
if n = 2^m and T(2^m) = s(m) then
logn = m , loglogn = logm ;
s(m) = 2*s(m/2) + m/logm ;

Related

Minimizing memory usage in Julia function

This function is a workhorse which I want to optimize. Any idea on how its memory usage can be limited would be great.
function F(len, rNo, n, ratio = 0.5)
s = zeros(len); m = copy(s); d = copy(s);
s[rNo]=1
rNo ≤ len-1 && (m[rNo + 1] = s[rNo+1] = -n[rNo])
rNo > 1 && (m[rNo - 1] = s[rowNo-1] = n[rowNo-1])
r=1
while true
for i ∈ 2:len-1
d[i] = (n[i]*m[i+1] - n[i-1]*m[i-1])/(r+1)
end
d[1] = n[1]*m[2]/(r+1);
d[len] = -n[len-1]*m[len-1]/(r+1);
for i ∈ 1:len
s[i]+=d[i]
end
sum(abs.(d))/sum(abs.(m)) < ratio && break #converged
m = copy(d); r+=1
end
return reshape(s, 1, :)
end
It calculates rows of a special matrix exponential which I stack later.
Although the full method is quite faster than built in exp thanks to the special properties, it takes up far more memory as measured by #time.
Since I am a noob in memory management and also in Julia, I am sure it can be optimized quite a bit..
Am I doing something obviously wrong?
I think most of your allocations come from sum(abs.(d))/sum(abs.(m)) < ratio && break #converged. If you replace it with sum(abs, d)/sum(abs,m) < ratio && break #converged those allocations should go away. (it also will be a speed boost).
Your other allocations can be removed by replacing m = copy(d) with m .= d which does an element-wise copy.
There are also a couple of style things where I think you could make this a nicer function to read and use. My changes would be as follows
function F(rNo, v, ratio = 0.5)
len = length(v)
s = zeros(len+1); m = copy(s); d = copy(s);
s[rNo]=1
rNo ≤ len && (m[rNo + 1] = s[rNo+1] = -v[rNo])
rNo > 1 && (m[rNo - 1] = s[rowNo-1] = v[rowNo-1])
r=1
while true
for i ∈ 2:len
d[i] = (v[i]*m[i+1] - v[i-1]*m[i-1]) / (r+1)
end
d[1] = v[1]*m[2]/(r+1);
d[end] = -v[end]*m[end]/(r+1);
s .+= d
sum(abs, d)/sum(abs, m) < ratio && break #converged
m .= d; r+=1
end
return reshape(s, 1, :)
end
The most notable change is removing len from the arguments. Including an array length argument is common in C (and probably others) where finding the length of an array is hard, but in Julia length is cheap (O(1)), and adding extra arguments is just more clutter and confusion for the people using it. I also made use of the fact that julia is able to turn s[end] into s[length(x)] to make this a little cleaner. Also, in general when using Julia you should look for ways to use dotted operations rather than writing for loops. The for loops will be fast, but why take 3 lines to do what you could in 1 shorter line? (I also renamed n to v since to me n is a number and v is a vector, but that is pure preference).
I hope this helps.

Solve Recurrence for T(n) = 7T(n/7) + n

I'm trying to solve the recurrence for T(n) = 7T(n/7) + n.
I know using Master Theorem it's O(nlog7n), but I want to solve it by substitution.
At level i, I get: 7^i T(n/7^i) + (n+7n+7^2n+ .... + 7^i n)
By setting i = log7n, the above becomes: 7^(log7n)*T(1) + (n + 7n + 7^2n ..... + 7^(log7n) n
Since 7^log7n = n, the above finally becomes n+ (n+7n+(7^2)n+ ....n*n)
This solves to O(n^2) to me not O(nlog7n), any idea what's wrong?
T(n)=7T(n/7)+n=7[7T(n/72)+n/7]+n=72T(n/72)+2n=...=7kT(n/7k)+kn
n/7k=c ⇒ k=O(logn)
⇒T(n)=O(nlogn)

Evaluation under the derivative

I give
depends(u,r)$
er : diff(u,r)$
er, u = r^2;
I am getting:
d 2
(%o47) -- (r )
dr
However, I would like to obtain
2 r
How can force MAXIMA to evaluate that derivative?
I solved it! Use the keyword nouns:
depends(u,r)$
er : diff(u,r)$
er, nouns, u = r^2;

Write Recurrence for Given Function

I am trying to write the recurrence relation for the running time of the following function:
function G(n):
if n>0 then:
x=0
for i = 1 to n:
x = x + 1
G(n-1)
end if
What I came up with was:
T(n) = 1 if n <= 0
T(n) = T(n-1) + 1 if n>0
However I was told that this was incorrect and I don't know why or what the correct solution would be. Any help is greatly appreciated!
T(n) = 1 if n <= 0
T(n) = T(n-1) + O(n) if n>0
Instead of O(1), it should be O(n), because you are looping from 1 to n
If you solve the recurrence, the overall complexity will be O(n2)

Is an admisible heuristic always monotone (consistent)?

For the A* search algorithm, provided an heuristic h, supose h is admisible.
That is:
h(n) ≤ h*(n) for every node n, where h* is the real cost from n to goal.
Does this ensure the heuristic is monotone?
That is:
f(n) ≤ g(n') + h(n') for every sucesor n' of n, where f(n)= h(n) + g(n) and g(n) is the accumulated cost.
No.
Assume you have three successor states s1, s2, s3 and a goal state g so that s1 -> s2 -> s3 -> g.
s1 is the starting node.
Consider also the following values for h(s) and h*(s) (i.e. true cost):
h(s1) = 3 , h*(s1) = 6
h(s2) = 4 , h*(s2) = 5
h(s3) = 3 , h*(s3) = 3
h(g) = 0 , h*(g) = 0
Following the only path to the goal we can have that:
g(s1) = 0, g(s2) = 1, g(s3) = 3, g(g) = 6, coinciding with the true cost above.
Although the heuristic function is admissible (h(s) <= h*(s)), f(n) will not be monotonic. For instance f(s1) = h(s1) + g(s1) = 3 while f(s2) = h(s2) + g(s2) = 5 with f(s1) < f(s2). Same holds between f(s2) and f(s3).
Of course this means you have a quite uninformative heuristic.

Resources