time-complexity

Order Of Growth complicated for loops

对着背影说爱祢 提交于 2019-12-19 11:34:09
问题 For the following code fragment, what is the order of growth in terms of N? int sum = 0; for (int i = 1; i <= N; i = i*2) for (int j = 1; j <= N; j = j*2) for (int k = 1; k <= i; k++) sum++; I have figured that there is lgN term, but I am stuck on evaluating this part : lgN(1 + 4 + 8 + 16 + ....). What will the last term of the sequence be? I need the last term to calculate the sum. 回答1: You have a geometric progression in your outer loops, so there is a closed form for the sum of which you

Search max value between 2 AVL nodes [duplicate]

懵懂的女人 提交于 2019-12-19 10:25:48
问题 This question already has an answer here : AVL Tree: Finding the key with the smallest data values in keys between two values in O(logn) time (1 answer) Closed 4 years ago . I have an AVL tree while each node consists of: Key Value The AVL tree is ordered by the keys . So if I got 2 keys and now I want to find the maximum value between those 2 keys. I've tried adding additional information to each node like the max value in the left subtree and same for the right subtree but I can't get the

Matrix multiplication time complexity in MATLAB

梦想的初衷 提交于 2019-12-19 06:51:34
问题 Does anyone know which algorithm MATLAB uses for matrix multiplication and what is its time complexity? 回答1: For completeness -- as mentioned in this thread, Matlab uses the DGEMM (Double GEneral Matrix Multiplication) routine from BLAS (Basic Linear Algebra Subprograms). Note that there is not one single implementation of BLAS - it is tuned for particular processor architectures. Therefore you cannot be absolutely certain which algorithm is being used on your machine without finding out

Matrix multiplication time complexity in MATLAB

ぐ巨炮叔叔 提交于 2019-12-19 06:51:10
问题 Does anyone know which algorithm MATLAB uses for matrix multiplication and what is its time complexity? 回答1: For completeness -- as mentioned in this thread, Matlab uses the DGEMM (Double GEneral Matrix Multiplication) routine from BLAS (Basic Linear Algebra Subprograms). Note that there is not one single implementation of BLAS - it is tuned for particular processor architectures. Therefore you cannot be absolutely certain which algorithm is being used on your machine without finding out

Why is the time complexity of this loop non-linear?

为君一笑 提交于 2019-12-19 05:54:14
问题 Why is the time complexity of this loop non-linear and why is it so slow? The loop takes ~38s for N=50k, and ~570s for N=200k . Is there a faster way to do this? Rprof() seems to indicate that writing to memory is very slow. df <- data.frame(replicate(5, runif(200000))) df[,1:3] <- round(df[,1:3]) Rprof(line.profiling = TRUE); timer <- proc.time() x <- df; N <- nrow(df); i <- 1 ind <- df[1:(N-1),1:3] == df[2:N,1:3]; rind <- which(apply(ind,1,all)) N <- length(rind) while(i <= N) { x$X4[rind[i

Is it possible to use a Perl hash in a manner that has `O(log(n))` lookup and insertion?

社会主义新天地 提交于 2019-12-19 04:01:57
问题 Is it possible to use a Perl hash in a manner that has O(log(n)) lookup and insertion? By default, I assume the lookup is O(n) since it's represented by an unsorted list. I know I could create a data structure to satisfy this (ie, a tree, etc) however, it would be nicer if it was built in and could be used as a normal hash (ie, with %) 回答1: Associative arrays in Perl 5 are implemented with hash tables which have amortized O(1) (i.e. constant time) insert and lookup. That is why we tend to

Matlab matrix multiplication speed

不想你离开。 提交于 2019-12-19 02:27:46
问题 I was wondering how can matlab multiply two matrices so fast. When multiplying two NxN matrices, N^3 multiplications are performed. Even with the Strassen Algorithm it takes N^2.8 multiplications, which is still a large number. I was running the following test program: a = rand(2160); b = rand(2160); tic;a*b;toc 2160 was used because 2160^3=~10^10 ( a*b should be about 10^10 multiplications) I got: Elapsed time is 1.164289 seconds. (I'm running on 2.4Ghz notebook and no threading occurs)

Matlab matrix multiplication speed

牧云@^-^@ 提交于 2019-12-19 02:27:08
问题 I was wondering how can matlab multiply two matrices so fast. When multiplying two NxN matrices, N^3 multiplications are performed. Even with the Strassen Algorithm it takes N^2.8 multiplications, which is still a large number. I was running the following test program: a = rand(2160); b = rand(2160); tic;a*b;toc 2160 was used because 2160^3=~10^10 ( a*b should be about 10^10 multiplications) I got: Elapsed time is 1.164289 seconds. (I'm running on 2.4Ghz notebook and no threading occurs)

Complexity of accessing data in an object

自古美人都是妖i 提交于 2019-12-19 00:38:08
问题 In some of the projects I'm working on as part of my day job, I need to access data in very large JS objects (on the order of thousands of key-value pairs). I'm trying to improve the efficiency of my code, so I came up with a few questions: What is the runtime complexity of JS when accessing a field in such an object? My initial hunch is that it's O(n) Is there a difference when accessing through dot notation or bracket notation? (e.g. obj.field vs obj[field] ) I'm guessing there is a

How is LinkedList's add(int, E) of O(1) complexity?

点点圈 提交于 2019-12-18 18:39:59
问题 From the linked-list tag wiki excerpt: A linked list is a data structure in which the elements contain references to the next (and optionally the previous) element. Linked lists offer O(1) insert and removal at any position , O(1) list concatenation, and O(1) access at the front (and optionally back) positions as well as O(1) next element access. Random access has O(N) complexity and is usually unimplemented. (emphasis mine) I was surprised to read this – how can the list insert at a random