complexity-theory

Java: what's the big-O time of declaring an array of size n?

假装没事ソ 提交于 2019-11-26 22:38:54
What is the running time of declaring an array of size n in Java? I suppose this would depend on whether the memory is zero'ed out on garbage collection (in which case it could be O(1) ) or on initialization (in which case it'd have to be O(n) ). It's O(n) . Consider this simple program: public class ArrayTest { public static void main(String[] args) { int[] var = new int[5]; } } The bytecode generated is: Compiled from "ArrayTest.java" public class ArrayTest extends java.lang.Object{ public ArrayTest(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return

Why SortedSet<T>.GetViewBetween isn't O(log N)?

蓝咒 提交于 2019-11-26 22:17:08
问题 In .NET 4.0+, a class SortedSet<T> has a method called GetViewBetween(l, r) , which returns an interface view on a tree part containing all the values between the two specified. Given that SortedSet<T> is implemented as a red-black tree, I naturally expect it to run in O(log N) time. The similar method in C++ is std::set::lower_bound/upper_bound , in Java it's TreeSet.headSet/tailSet , and they are logarithmic. However, that is not true. The following code runs in 32 sec, whereas the

Big O of append in Golang

天大地大妈咪最大 提交于 2019-11-26 22:04:15
问题 What is the complexity of Go's builtin append function? What about string concatenation using + ? I'd like to remove an element from a slice by appending two slices excluding that element, ex. http://play.golang.org/p/RIR5fXq-Sf nums := []int{0, 1, 2, 3, 4, 5, 6, 7} fmt.Println(append(nums[:4], nums[5:]...)) => [0 1 2 3 5 6 7] http://golang.org/pkg/builtin/#append says that if the destination has sufficient capacity, then that slice is resliced . I'm hoping that "reslicing" is a constant time

Computational complexity of the FFT in n dimensions

a 夏天 提交于 2019-11-26 21:54:04
问题 What is the computational complexity of the n-dimensional FFT with m points along each dimension? 回答1: For a 1D FFT it's O(m log m) . For a 2D FFT you have to do m x 1D FFTs in each axis so that's O(2 m^2 log m) = O(m^2 log m) . It's too early in the morning here to get my head round n >= 3 but I'm guessing it's probably: O(m^n log m) 来源: https://stackoverflow.com/questions/6514861/computational-complexity-of-the-fft-in-n-dimensions

time complexity or hidden cost of <Array Name>.length in java

北城余情 提交于 2019-11-26 21:33:11
问题 I was looking at a project in java and found a for loop which was written like below: for(int i=1; i<a.length; i++) { ........... ........... ........... } My question is: is it costly to calculate the a.length (here a is array name)? if no then how a.length is getting calculated internally (means how JVM make sure O(1) access to this)? Is is similar to: int length = a.length; for(int i=1; i<length; i++) { ........... ........... ........... } i.e. like accessing a local variable's value

Time complexity for merging two sorted arrays of size n and m

為{幸葍}努か 提交于 2019-11-26 21:24:07
问题 I was just wondering what is the time complexty of merging two sorted arrays of size n and m, given that n is always greater than m . I was thinking of using merge sort, which I assume in this case will consume O(log n+m). I am not really good with big-oh and stuff. Please suggest me the time complexity for this problem and let me know if there is an even optimized way of solving the problem. Thanks in advance. 回答1: Attention! This answer contains an error A more efficient algorithm exists

Sorting in linear time? [closed]

自古美人都是妖i 提交于 2019-11-26 20:46:49
Given an input set of n integers in the range [0..n^3-1], provide a linear time sorting algorithm. This is a review for my test on thursday, and I have no idea how to approach this problem. Also take a look at related sorts too: pigeonhole sort or counting sort , as well as radix sort as mentioned by Pukku. Have a look at radix sort . When people say "sorting algorithm" they often are referring to "comparison sorting algorithm", which is any algorithm that only depends on being able to ask "is this thing bigger or smaller than that". So if you are limited to asking this one question about the

Time complexity of accessing a Python dict

白昼怎懂夜的黑 提交于 2019-11-26 20:29:53
I am writing a simple Python program. My program seems to suffer from linear access to dictionaries, its run-time grows exponentially even though the algorithm is quadratic. I use a dictionary to memoize values. That seems to be a bottleneck. The values I'm hashing are tuples of points. Each point is: (x,y), 0 <= x,y <= 50 Each key in the dictionary is: A tuple of 2-5 points: ((x1,y1),(x2,y2),(x3,y3),(x4,y4)) The keys are read many times more often than they are written. Am I correct that python dicts suffer from linear access times with such inputs? As far as I know, sets have guaranteed

Python dictionary keys. “In” complexity

十年热恋 提交于 2019-11-26 19:52:56
Quick question to mainly satisfy my curiosity on the topic. I am writing some large python programs with an SQlite database backend and will be dealing with a large number of records in the future, so I need to optimize as much as I can. For a few functions, I am searching through keys in a dictionary. I have been using the "in" keyword for prototyping and was planning on going back and optimizing those searches later as I know the "in" keyword is generally O(n) (as this just translates to python iterating over an entire list and comparing each element). But, as a python dict is basically just

What is the complexity of regular expression?

∥☆過路亽.° 提交于 2019-11-26 19:49:33
What is the complexity with respect to the string length that takes to perform a regular expression comparison on a string? The answer depends on what exactly you mean by "regular expressions." Classic regexes can be compiled into Deterministic Finite Automata that can match a string of length N in O(N) time. Certain extensions to the regex language change that for the worse. You may find the following document of interest: Regular Expression Matching Can Be Simple And Fast . unbounded - you can create a regular expression that never terminates, on an empty input string. If you use normal (TCS