C++ set: counting elements less than a value

What you need is an ‘order-statistics tree’. It is essentially an augmented (binary search) tree that supports the additional operation rank(x) which gives you the number of elements with less or equal key as element x. Chapter 14 in Cormen, Leiserson, Rivest, Stein; “Introduction to Algorithms” should give you the algorithmic background. There is also … Read more

Constructing efficient monad instances on `Set` (and other containers with constraints) using the continuation monad

Monads are one particular way of structuring and sequencing computations. The bind of a monad cannot magically restructure your computation so as to happen in a more efficient way. There are two problems with the way you structure your computation. When evaluating stepN 20 0, the result of step 0 will be computed 20 times. … Read more

String analysis

This whole problem is known as “Common Subexpression Elimination” or CSE. It is a slightly smaller version of the problem called “Graph Reduction” faced by the implementer of compilers for functional programming languages. Googling “Common Subexpression elimination algorithm” gives lots of solutions, though none that I can see especially for the constraints given by matrix … Read more

Explanation of Algorithm for finding articulation points or cut vertices of a graph

Finding articulation vertices is an application of DFS. In a nutshell, Apply DFS on a graph. Get the DFS tree. A node which is visited earlier is a “parent” of those nodes which are reached by it and visited later. If any child of a node does not have a path to any of the … Read more

Java CharAt() and deleteCharAt() performance

For String, StringBuffer, and StringBuilder, charAt() is a constant-time operation. For StringBuffer and StringBuilder, deleteCharAt() is a linear-time operation. StringBuffer and StringBuilder have very similar performance characteristics. The primary difference is that the former is synchronized (so is thread-safe) while the latter is not.