complexity-theory

Is O(n) greater than O(2^log n)

左心房为你撑大大i 提交于 2019-12-13 16:26:33
问题 I read in a data structures book complexity hierarchy diagram that n is greater than 2 log n . But cannot understand how and why. On using simple examples in power of 2 as n, I get values equal to n. It is not mentioned in book , but I am assuming it to base 2 ( as context is DS complexity) a) Is O(n) > O(pow(2,logn)) ? b) Is O(pow(2,log n)) better than O(n) ? 回答1: Notice that 2 log b n = 2 log 2 n / log 2 b = n (1 / log 2 b) . If log 2 b ≥ 1 (that is, b ≥ 2), then this entire expression is

Time complexity of this for loop: for (i = 2; i < N; i = i * i)?

独自空忆成欢 提交于 2019-12-13 12:10:01
问题 We're learning about time complexity right now and I'm having a ton of trouble with this one example. for (i = 2; i < n; i = i * i) { ... do something ... } The prof said that it was O(sqrt(N)), but I'm not sure that I'm convinced. After all, if N=16, it only runs 2 times, not 4 right? My approach to solution: 2^(2k) = N, where k is the number of times the loop runs. Removing the constant factors, k runs log(N) times. Where am I going wrong here? Thanks for any advice on the matter. 回答1: You

Complexity of binary search on a string

♀尐吖头ヾ 提交于 2019-12-13 09:34:14
问题 I have an sorted array of strings: eg: ["bar", "foo", "top", "zebra"] and I want to search if an input word is present in an array or not. eg: search (String[] str, String word) { // binary search implemented + string comaparison. } Now binary search will account for complexity which is O(logn), where n is the length of an array. So for so good. But, at some point we need to do a string compare, which can be done in linear time. Now the input array can contain of words of different sizes. So

Union in context-free languages

我的未来我决定 提交于 2019-12-13 08:35:40
问题 Is the union of a collection of context-free languages always context-free ? Justify your answer ..... I know that the answer is yes, but how can I prove it ? 回答1: To show that the finite union of context-free languages is context-free you just have to build a context-free grammar for the union language, exactly as you would do to prove that the union of two context-free languages is context-free. If G1,...,GN are the context-free grammars for the N context-free languages you have, rename all

What is the time complexity of given code?

你。 提交于 2019-12-13 07:53:20
问题 While(n>1) { n=n/20; n=n/10; } I tried like this => Here, n = n/200 which means that N is getting reduced each time by a factor of 200. so, time complexity = O(log base 200 N) 回答1: Yes, n gets reduced to 1/200 each time in the loop. So, this loop would run log 200 n times. Hence, time-complexity = O(log 200 n). 来源: https://stackoverflow.com/questions/39324836/what-is-the-time-complexity-of-given-code

Data structure recommendation

你。 提交于 2019-12-13 05:20:40
问题 Developing in Java, I need a data structure to select N distinct random numbers between 0 and 999999 ? I want to be able to quickly allocate N numbers and make sure they don't repeat themselves. Main goal is not to use too much memory and still keep performance reasonable. I am considering using a BitSet But I am not sure if the memory implications. Can someone tell me if the memory requirements of this class are related to the number of bits or to the number of set bits? and what is the

How to find a upper and lower bound of code?

☆樱花仙子☆ 提交于 2019-12-13 04:34:27
问题 I have some code and the text is For following code, find a lower and upper bound if data for function f,g is given, and we know that is best and worst case is given, condition is fulfilled in most cases. f:O(log(n)), and lower bound is 1 g:O(n) and lower bound is (logn)^2 I think that first line of my code is logn, then since n>log(n) I think that second line is O(n*log(n))and the last lines is nlogn I think because if I use summarization I get logn(n+(logn)^2-1) end then the O is O(n^2(logn

How to avoid quadratic computation resulting from double 'for loop' when computing distances between vectors

落爺英雄遲暮 提交于 2019-12-13 04:33:14
问题 Given the following code that computes distances between vectors in list 'vect’: import numpy as np vect=([0.123, 0.345, 0.789], [0.234, 0.456, 0.567],[0.134, 0.246, 0.831]) def kn(): for j in vect: c=np.array(j) for z in vect: p=np.array(z) space = np.linalg.norm(c-p) print space kn() Is there a way to avoid the quadratic complexity that will result from the double ‘for loop’? Computation as a result of the double for loop (quadratic) ==3X3=9 Ideal computation (what I want) should be (3): [0

3SAT solved in polynomial time?

余生颓废 提交于 2019-12-13 04:32:45
问题 I have seen few errors in the cnf files for both satisfiable and unsatisfiable clauses files SATLIB Benchmark Problems To be more specific I have found out that the 1st file of the zip folder here: 20 variables, 91 clauses - 1000 instances, all satisfiable contains a file with the title of "uf20-01", the equation of which is unsatisfiable clearly as the 7th clause at the 15th line and the 87th clause at line number 4 are both exact inverse of each other!((5 19 17) and (-5 -19 -17)) Thus an

Modification to merge sort to implement merge sort with insertion sort Java

一笑奈何 提交于 2019-12-13 02:51:32
问题 I want to implement a modification to merge sort, where n/k sublists of length k are sorted using insertion sort and then merged using the standard merging mechanism of merg sort. I'm wondering what the value k has to equal for the modified version of merge sort to equal the original version of merge sort in terms of rum time complexity. This is a conceptual exercise by myself for myself. Code and or an explanation is appreciated. 回答1: Your n/k-way merge is O(n^2/k) (explanation here). Each