Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Can someone please explain how the time complexity of this algorithm is O(n) using recurrence relation.
def exercise2(N):
count = 0
i = N
while ( i > 0 ):
for j in range(0,i):
count = count + 1
i = i//2
For positive 𝑛 the number of executions of count = count + 1 is representative of the complexity, as the other lines of code don't execute significantly more times.
The recurrence relation can use the first iteration of the outer loop: that represents 𝑛 iterations of the inner loop. The loop continues for 𝑛/2, and that can be represented with a recursive case:
𝑇𝑛 = 𝑛 + 𝑇𝑛/2
The base case is when 𝑛 is zero, in which case there is no iteration of the loop, or we could also set it when 𝑛 is one, in which case there is just one iteration of the loop:
𝑇1 = 1
So this relation expands as follows:
𝑇𝑛 = 𝑛 + 𝑛/2 + 𝑛/4 + ... + 1 = ∑𝑖=0..𝑛 (𝑛 / 2𝑖)
This is a geomatric series that -- when we perform integer divisions and 𝑛 is a power of 2 -- is equal to 2𝑛−1 and otherwise is not greater than 2𝑛−1.
So we can say the algorithm has a complexity of O(2𝑛) = O(𝑛)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Suppose I want to find partition number of n, aka p(n). Here Euler's Pentagonal number theorem, a dynamic programming based solution is present, which has time and complexities O(n^2), O(n^2\log(n)) respectively.
Is there any improvement over this algorithm to reduce complexity or is there any proof showing that this algorithm is the best possible for this problem/ reducing complexity bellow this is NP-hard. Also what about the space-time trade off. Can we reduce time/space complexity by increasing space/time complexity respectively (keeping in mind that each complexity should not be more that O(n^3).
The following recurrence can be directly translated to code:
where
,
import numpy as np
def num_partitions(n):
# recursive function with an auxiliary cache to avoid recomputing
# the same value more than once
def get(n, k, aux):
# terminate the recursion
if n < k:
return 0
if k == 1 or k == n:
return 1
# check if the value is already in the cache - if not, compute
# it recursively
if aux[n][k] == -1:
aux[n][k] = get(n-k, k, aux) + get(n-1, k-1, aux)
return aux[n][k]
return np.sum([get(n, k, np.ones((n+1,n+1)) * -1) for k in range(1, n+1)], dtype=np.int)
import sys
print(num_partitions(int(sys.argv[1])))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Given an array of integers, how can I efficiently calculate the number of pairs of integers with a difference of at least k? My goal is to do this in better than O(n^2) time.
If you need to list them, you might need to list every pair which would be O(n^2). If you just need to count them, you can get O(n log n) by sorting the list, then for each start_index, find the smallest end_index for which the difference in values is at least k. There will be n - end_index of these.
Each successive step is to increment start_index by 1, then increment end_index until the difference in values is at least k, again add n - end_index, and repeat.
Here's O(n) code (in Ruby) for a sorted array.
def count_pairs (arr, k)
count=0
end_index = 0
0.upto(arr.length - 1) do |start_index|
while arr[end_index] - arr[start_index] < k do
end_index += 1
return count if end_index >= arr.length
end
count += arr.length - end_index
end
return count
end
If the list is already sorted, this is O(n).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Simple linear search to find max min algo
maxmin(a,n,max,min){
max=min=a[1];
for i=2 to n do{
if a[i]>max then max:=a[i];
else if a[i]<min then min:=a[i];
}
}
1.Average case complexity of the above algo given that the first if conditions fails for n/2 elments
2.Average case complexity of the above algo if the first ccondition fails 1/2 times plz xplain
The average case complexity for both cases is O(n). if k is the number of times the first if fails, then the number of comparisons is 2*n - 2 - k.
maxmin(a,n,max,min){
max=min=a[1];
for i=2 to n do{ // goes through the loop n-1 times
if a[i]>max then max:=a[i]; // out of n-1 times succeeds k times and fails n-1-k times
else if a[i]<min then min:=a[i]; // runs this n-1-k times
}
}
n-1 + n-1-k -> 2*n - 2 - k
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
T(1) = 1
T(n) = T(n^1/3) + 1
How can I solve it? By "solve" I mean to found it's "complexity" (I don't really know how to say it in English), such as O(nlogn) ecc.
I couldn't guess the substitution method; i go nowhere with the iteration method, and I can't apply Master Theorem.
I'm arrived here, but I'm not sure:
T(n) = T(n^(1/3^k))) +k
Can you give me and advice please?
I'll try to formulate some possible solutions. You can pick one depending on further constraints.
The recursion will run until n becomes 1. This is:
1 = n^(1/3^k)
or more generally
b = n^(1/3^k)
where k is the recursion depth. Solving this for k yields:
ln(b) = 1/3^k * ln(n)
ln(ln(b) / ln(n)) = k * ln(1/3)
-ln(ln(b) / ln(n)) / ln(3) = k
If we set b to 1, then the equation becomes unsolvable, because ln(0) is not specified. This would be equivalent to an endless recursion.
However, we can say that in the last recursion n should be "roughly 1". So we actually have a b != 1. Then k is:
k = -ln(ln(b) / ln(n)) / ln(3)
= -ln(c1 / ln(n)) / c2
= -(ln(c1) - lnln(n)) / c2
= (-c3 + lnln(n)) / c2
This should be O(log log n).
If you want to truncate n to its integer part, the calculation becomes pretty messy, because you have special cases after each step. However, we could approximate the result by specifying b = 1.999999. This would yield the same complexity as above.
If this is a recursive function, so what i did understand is that
T(n)= T(integerpart(cubicsquare(n)) +1 ;
in this case :
S=0;
if (n>=1){
S++;
N= n;
while (N>1){
N=integerpart(N^1/3);
S++;
}
}
T(n)= S ;
that's mean that T(n) is a simple function : with integer bound, and the width of the keme interval is 2^(3^k) - 2^(3^(k-1))
you can see, first interval is if n in ]1,8[ T(n)=2; then if n in [8,252[ ,T(n)=3...
so, as we can say then that t(2^(3^k)) = k+1 ;
then t(n) ~O(ln(ln(n))/ln(3)) (consider suite 2^3^k)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Kind of a math question, but very programming related. Doing some Big-O problems and I have an algorithm where a for loop will run n times, where k = input size, n = max power of 4 where (k)/(4^n) >= 1. How can I represent max power of 4 where (k)/(4^n) >= 1 in one mathematic statement?
floor ( (log k)/(log 4) ).
Or something along those lines.
Mathematic statement: [log_4(k)]
Code: floor( log(k) / log(4) )
log base 4 of k? Can take the floor if you only care about integer n.
Taking (k)/(4^n) >= 1, multiply both sides by 4^n to get k >= 4^n, and then take the log base 4 (log_4) of both sides to get log_4 k >= n, or n <= log_4 k. (Equivalently, take log of both sides and get log k >= log(4^n), then note log(4^n) = n log(4), and divide to get (log k)/(log 4) >= n). Choose the largest integer n satisfying this inequality, which is floor(log_4 k).