nth fibonacci number in sublinear time

Following from Pillsy’s reference to matrix exponentiation, such that for the matrix M = [1 1] [1 0] then fib(n) = Mn1,2 Raising matrices to powers using repeated multiplication is not very efficient. Two approaches to matrix exponentiation are divide and conquer which yields Mn in O(ln n) steps, or eigenvalue decomposition which is constant … Read more

JavaScript runtime complexity of Array functions

The ECMA specification does not specify a bounding complexity, however, you can derive one from the specification’s algorithms. push is O(1), however, in practice it will encounter an O(N) copy costs at engine defined boundaries as the slot array needs to be reallocated. These boundaries are typically logarithmic. pop is O(1) with a similar caveat … Read more

Sorting in Computer Science vs. sorting in the ‘real’ world

EDIT: I had misunderstood the mechanism of a centrifuge and it appears that it does a comparison, a massively-parallel one at that. However there are physical processes that operate on a property of the entity being sorted rather than comparing two properties. This answer covers algorithms that are of that nature. A centrifuge applies a … Read more

Breadth First Search time complexity analysis

I hope this is helpful to anybody having trouble understanding computational time complexity for Breadth First Search a.k.a BFS. Queue graphTraversal.add(firstVertex); // This while loop will run V times, where V is total number of vertices in graph. while(graphTraversal.isEmpty == false) currentVertex = graphTraversal.getVertex(); // This while loop will run Eaj times, where Eaj is … Read more

Is the time-complexity of iterative string append actually O(n^2), or O(n)?

In CPython, the standard implementation of Python, there’s an implementation detail that makes this usually O(n), implemented in the code the bytecode evaluation loop calls for + or += with two string operands. If Python detects that the left argument has no other references, it calls realloc to attempt to avoid a copy by resizing … Read more