complexity-theory

How is O(N) algorithm also an O(N^2) algorithm?

…衆ロ難τιáo~ 提交于 2020-12-08 06:11:08
问题 I was reading about Big-O Notation So, any algorithm that is O(N) is also an O(N^2). It seems confusing to me, I know that Big-O gives upper bound only. But how can an O(N) algorithm also be an O(N^2) algorithm. Is there any examples where it is the case? I can't think of any. Can anyone explain it to me? 回答1: "Upper bound" means the algorithm takes no longer than (i.e. <= ) that long (as the input size tends to infinity, with relevant constant factors considered). It does not mean it will

Which C++ random number engines have a O(1) discard function?

放肆的年华 提交于 2020-12-02 06:12:52
问题 Since C++11 there are a number of std random number engines. One of the member functions they implement is void discard(int long long z) which skips over z randomly generated numbers. The complexity of this function is given as O(z) on www.cplusplus.com (http://www.cplusplus.com/reference/random/mersenne_twister_engine/discard/) However, on www.cppreference.com (http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine/discard) there is a note to say that For some engines, "fast

Efficient tensor contraction with Python

坚强是说给别人听的谎言 提交于 2020-07-22 06:08:41
问题 I have a piece of code with a bottleneck calculation involving tensor contractions. Lets say I want to calculate a tensor A_{i,j,k,l}( X ) whose non-zero entries for a single x\in X are N ~ 10^5, and X represents a grid with M total points, with M~1000 approximately. For a single element of the tensor A, the rhs of the equation looks something like: A_{ijkl}(M) = Sum_{m,n,p,q} S_{i,j, m,n }(M) B_{m,n,p,q}(M) T_{ p,q, k,l }(M) In addition, the middle tensor B_{m,n,p,q}(M) is obtained by

Are all NP problems also NP-complete?

别等时光非礼了梦想. 提交于 2020-07-04 09:51:47
问题 The definition of NP-complete is A problem is NP-complete if it belongs to class NP all the other problems in NP polynomially transform to it So, if all other problems in NP transform to an NP-complete problem, then does that not also mean that all NP problems are also NP-complete? What is the point of classifying the two if they are the same? In other words, if we have an NP problem then through (2) this problem can transform into an NP-complete problem. Therefore, the NP problem is now NP

Calculating the complexity of an algorithm with 3 loops

佐手、 提交于 2020-06-27 11:36:53
问题 I tried to solve the following exercise : What is the order of growth of the worst case running time of the following code fragment as a function of N? int sum = 0; for (int i = 1; i <= N; i++) for (int j = 1; j <= i*i; j++) for (int k = 1; k <= j*j; k++) sum++; and I found that the complexity is O(n 4 ), however the correct answer is : The answer is : N 7 For a given value of i, the body of the innermost loop is executed 1 2 + 2 2 + 3 2 + ... + (i 2 ) 2 ~ 1/3 i 6 times. Summing up over all

What is constant factors and low-order term in algorithms?

谁都会走 提交于 2020-05-23 02:53:49
问题 In the following video, there is an explanation of asymptotic analysis: https://class.coursera.org/algo-004/lecture/169 But I can't understand what is "low-order term" and "constant factor" itself? (it is at the 4th minute of the video). The merge sort is 6n*log2(n)+6n . Why 6n is the low-order term and 6 is constant factor in here? Do these terms have concrete definition? 回答1: Lower-order term: "Order" here refers to the order of magnitude. The concept is easy to understand and explain when

What is the complexity of bisect algorithm?

余生长醉 提交于 2020-05-12 20:31:46
问题 I wrote code to understand which of them is faster when it comes to search an element in a list. It turns out to be bisect. What I do not understand is what is complexity of bisect algorithm and does it use Van Emde Boas tree? #python inbuilt list search using 'in' took 0.0702499200317 secs def mul3(): a = [1, 2, 4, 5, 6, 7, 8, 10, 12, 42, 55, 65, 69, 95, 96, 101, 156, 199] for x in a: if x in a: print x, "True" else: print x, "False" #using bisect took 0.0649611193601 def mul4(): a = [1, 2,

Solving the recurrence T(n) = T(n / 2) + O(1) using the Master Theorem? [closed]

大兔子大兔子 提交于 2020-04-11 18:44:25
问题 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 6 years ago . I'm trying to solve a recurrence relation to find out the complexity of an algorithm using the Master Theorem and its recurrences concepts, how can I prove that: T(n) = T(n/2)+O(1) is T(n) = O(log(n)) ? Any explanation would be apprecciated!! 回答1: Your recurrence is T(n) = T(n / 2) + O(1) Since the Master

Solving the recurrence T(n) = T(n / 2) + O(1) using the Master Theorem? [closed]

╄→гoц情女王★ 提交于 2020-04-11 18:43:10
问题 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 6 years ago . I'm trying to solve a recurrence relation to find out the complexity of an algorithm using the Master Theorem and its recurrences concepts, how can I prove that: T(n) = T(n/2)+O(1) is T(n) = O(log(n)) ? Any explanation would be apprecciated!! 回答1: Your recurrence is T(n) = T(n / 2) + O(1) Since the Master