complexity-theory

Big-O for various Fibonacci Implementations

安稳与你 提交于 2021-02-15 10:26:52
问题 I just tried implementing code (in Java) for various means by which the nth term of the Fibonacci sequence can be computed and I'm hoping to verify what I've learnt. The iterative implementation is as follows: public int iterativeFibonacci(int n) { if ( n == 1 ) return 0; else if ( n == 2 ) return 1; int i = 0, j = 1, sum = 0; for ( ; (n-2) != 0; --n ) { sum = i + j; i = j; j = sum; } return sum; } The recursive implementation is as follows :- public int recursiveFibonacci(int n) { if ( n ==

Big-O for various Fibonacci Implementations

泪湿孤枕 提交于 2021-02-15 10:23:15
问题 I just tried implementing code (in Java) for various means by which the nth term of the Fibonacci sequence can be computed and I'm hoping to verify what I've learnt. The iterative implementation is as follows: public int iterativeFibonacci(int n) { if ( n == 1 ) return 0; else if ( n == 2 ) return 1; int i = 0, j = 1, sum = 0; for ( ; (n-2) != 0; --n ) { sum = i + j; i = j; j = sum; } return sum; } The recursive implementation is as follows :- public int recursiveFibonacci(int n) { if ( n ==

Big-O for various Fibonacci Implementations

三世轮回 提交于 2021-02-15 10:20:54
问题 I just tried implementing code (in Java) for various means by which the nth term of the Fibonacci sequence can be computed and I'm hoping to verify what I've learnt. The iterative implementation is as follows: public int iterativeFibonacci(int n) { if ( n == 1 ) return 0; else if ( n == 2 ) return 1; int i = 0, j = 1, sum = 0; for ( ; (n-2) != 0; --n ) { sum = i + j; i = j; j = sum; } return sum; } The recursive implementation is as follows :- public int recursiveFibonacci(int n) { if ( n ==

Is the complexity of unordered_set::find predictable?

爱⌒轻易说出口 提交于 2021-02-08 13:54:58
问题 While looking for a container suitable for an application I'm building, I ran across documentation for unordered_set . Given that my application typically requires only insert and find functions, this class seems rather attractive. I'm slightly put off, however, by the fact that find is O(1) amortized, but O(n) worst case - I would be using the function frequently, and it could make or break my application. What causes the spike in complexity? Is the likelihood of running into an O(n) search

What is the time complexity of constructing a binary search tree?

安稳与你 提交于 2021-02-08 04:54:29
问题 "Every comparison-based algorithm to sort n elements must take Ω(nlogn) comparisons in the worst case. With this fact, what would be the complexity of constructing a n-node binary search tree and why?" Based on this question, I am thinking that the construction complexity must be at least O(nlogn). That said, I can't seem to figure out how to find the total complexity of construction. 回答1: The title of the question and the text you quote are asking different things. I am going to address what

Whats the best algorithm for Non Disjoint Set Union?

♀尐吖头ヾ 提交于 2021-02-08 03:32:25
问题 Lets say there are two (non disjoint) sets of points (cartesian space), what is the best case complexity algorithm to perform the union of the two sets ? 回答1: Since the point coordinates are arbitrary and there is no special relation between them, I don't see this problem as a geometric specific problem. It is the generic problem of efficiently merging S1 and S2 into a new set S. I know about two options: 1) When the sets are stored in a hash table (actually a hash set), the union takes O(|S1

Whats the best algorithm for Non Disjoint Set Union?

…衆ロ難τιáo~ 提交于 2021-02-08 03:31:28
问题 Lets say there are two (non disjoint) sets of points (cartesian space), what is the best case complexity algorithm to perform the union of the two sets ? 回答1: Since the point coordinates are arbitrary and there is no special relation between them, I don't see this problem as a geometric specific problem. It is the generic problem of efficiently merging S1 and S2 into a new set S. I know about two options: 1) When the sets are stored in a hash table (actually a hash set), the union takes O(|S1

Why is O(n) equal to O(2n)

空扰寡人 提交于 2021-02-07 14:49:58
问题 I understand that O(N) is essentially equal to O(cN) where c='some constant'. But if N = c. Doesn't that make it O(N)^2. Does this hold as c increases, or is there some formal limit. 回答1: If N = c then c is not constant. Therefore this is never the case. 回答2: O(n) means that the runtime of the algorithm increases linearly with the size of the input. If you double the size of the input, you double the runtime. If you triple the size of the input, you triple the runtime. And so on. So the graph

Specific Graph and need to more Creative solution

巧了我就是萌 提交于 2021-02-07 08:55:37
问题 Directed Graph (|V|=a, |E|=b) is given. each vertexes has specific weight. we want for each vertex (1..a) find a vertex with maximum weight that can be reachable from that vertex. Update 1: one nice answer is prepare by @Paul in O(b + a log a). but I search for O(a + b) algorithms, if any? Is there any different efficient or fastest any other ways for doing it? 回答1: Yes, it's possible to modify Tarjan's SCC algorithm to solve this problem in linear time. Tarjan's algorithm uses two node

Algorithm complexity: if/else under for loop

余生长醉 提交于 2021-02-07 08:00:34
问题 I am wondering if in a situation like the following (an if/else statement under a for loop) the complexity would be O(n) or O(n^2): for character in string: if character==something: do something else: do something else. Thank you! 回答1: It will be O(n) if 'do something' and 'do something else' are O(1) O(n^2) if 'do something' and 'do something else' are O(n) Basically the complexity of the for loop will depend on the complexity of it components and the no. of loops. 回答2: It depends what you