complexity-theory

Asymptotic analysis of three nested for loops

泄露秘密 提交于 2019-12-07 03:06:04
问题 I want to calculate the theta complexity of this nested for loop: for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { for (int k = 0; k < j; k++) { // statement I'd say it's n^3, but I don't think this is correct, because each for loop does not go from 1 to n. I did some tests: n = 5 -> 10 10 -> 120 30 -> 4060 50 -> 19600 So it must be between n^2 and n^3. I tried the summation formula and such, but my results are way too high. Though of n^2 log(n), but that's also wrong... 回答1: It is

How is schoolbook long division an O(n^2) algorithm?

旧街凉风 提交于 2019-12-07 02:15:51
问题 Premise: This Wikipedia page suggests that the computational complexity of "Schoolbook" long division is O(n^2) . Deduction: Instead of taking two n-digit numbers, if I take one n-digit number and one m-digit number, then the complexity would be O(n*m) . Contradiction: Suppose you divide 100000000 (n digits) by 1000 (m digits), you get 100000, which takes six steps to arrive at. Now, if you divide 100000000 (n digits) by 10000 (m digits), you get 10000. Now this takes only five steps .

What is the space complexity of this string manipulation code?

回眸只為那壹抹淺笑 提交于 2019-12-07 02:09:07
问题 This piece of code is from Cracking the Coding interview book. public static boolean isUniqueChars2(String str) { boolean[] char_set = new boolean[256]; for (int i = 0; i < str.length(); i++) { int val = str.charAt(i); if (char_set[val]) return false; char_set[val] = true; } return true; } And the author mentions that, Time complexity is O(n), where n is the length of the string, and space complexity is O(n). I understand time complexity being O(n) but I don't understand how could space

Is it possible to remove duplicates from a sorted list in less than O(n) time?

落爺英雄遲暮 提交于 2019-12-07 00:25:01
问题 I suspect there is a way if you can save by locating the other end of a range of repeated values faster than by iterating through that sublist 回答1: In general, no. Imagine a list of N duplicates. You would have to make N-1 removals, hence O(N). If you specify a particular data structure with better than O(1) removal of elements, then there might better way for certain sorts of inputs. Even if you can efficiently remove a range of elements in O(1), and it takes O(1) time to find a duplicate -

Is there any practical application of Tango Trees?

岁酱吖の 提交于 2019-12-07 00:24:54
问题 Balanced binary search tree gives an O(log(n)) guaranteed search time. Tango trees achieves a search of O(log(log(n)) while compromising small amount of memory per node. While I understand that from theoretical point of view log(n) and log(log(n)) makes a huge difference, for majority of practical applications it provides almost no advantage. For example even for a huge number like n = 10^20 (which is like few thousand petabytes) the difference between log(n) = 64 and log(log(n)) = 6 is

Computing circle intersections in O( (n+s) log n)

风格不统一 提交于 2019-12-07 00:00:53
问题 I'm trying to figure out how to design an algorithm that can complete this task with a O((n+s) log n) complexity. s being the amount of intersections. I've tried searching on the internet, yet couldn't really find something. Anyway, I realise having a good data structure is key here. I am using a Red Black Tree implementation in java: TreeMap. I also use the famous(?) sweep-line algorithm to help me deal with my problem. Let me explain my setup first. I have a Scheduler. This is a

I have a new algorithm to find factors or primes in linear time - need verification for this

不问归期 提交于 2019-12-06 22:38:23
问题 I have developed an algorithm to find factors of a given number. Thus it also helps in finding if the given number is a prime number. I feel this is the fastest algorithm for finding factors or prime numbers. This algorithm finds if a give number is prime in time frame of 5*N (where N is the input number). So I hope I can call this a linear time algorithm. How can I verify if this is the fastest algorithm available? Can anybody help in this matter? (faster than GNFS and others known)

McCabe Cyclomatic Complexity for switch in Java

帅比萌擦擦* 提交于 2019-12-06 21:00:13
问题 I am using a switch statement with 13 cases, each case only has an one line return value. McCabe paints this in red. Is there an easier way to write a big switch statement? It doesn't seem complex to read, but I don't like the default setting turning red. If other people use the same tool on my code and see red stuff they might think I'm stupid :-) Edit: I'm mapping different SQL-Types to my own more abstract types, therefore reducing the total amount of types. case Types.TIME: return

complexity of two dependent for loops with outer loop of log n complexity

送分小仙女□ 提交于 2019-12-06 20:59:36
问题 The problem Compute the complexity of this algorithm: for(i=n; i>1;i=i/2) for(j=i;j<n;j++){ statement; } What have I done on this topic before: The first loop runs logn times. The second loop runs n-i times with i starting from n and changing to i/2 in each outer loop iteration. So the inner loop runs like this: n-n 0 times n - n/2 n/2 times n - n/4 3n/4 times n - n/8 7n/8 times n - n/16 15n/16 times and so on till n - 1 times so the general term is n * ((2^n)-1)/(2^n) Now this sequence is

Random sequence iteration in O(1) memory?

那年仲夏 提交于 2019-12-06 18:40:33
问题 Say you want to iterate over a sequence [0 to n] in a random order, visiting every element exactly once. Is there any way to do this in O (1) memory, i.e. without creating an [1..n] sequence with std::iota and running it through std::random_shuffle ? Some kind of iterator spitting out the sequence in a random order would be optimal. A requirement is that it should be possible to get another random order by picking another seed. 回答1: In theory, if you built a random number generator whose