Wednesday, June 28, 2017

Arena-Based Storage Management

This is the second blog post on compiler construction. You can read the first one here.

Calling malloc incurs the obligation of a subsequent call to free which can be costly and error prone (imagine forgetting to free or freeing in-use space).

malloc allocates based on object size. I used a storage management scheme that bases allocation on object lifetime instead:

  • Memory is allocated from arenas and entire arenas are deallocated at once.
  • Objects with the same lifetime are allocated from the same arena.
  • An arena is implemented as a linked list of contiguous blocks of memory.

The allocator acts like malloc except that it uses unsigned long instead of size_t (size_t is often defined to be unsigned int or unsigned long so we use unsigned long to account for all sizes) and takes an arena identifier from which space is allocated. The allocator returns a pointer suitably aligned to hold values of any type. A basic type is properly aligned if it is stored at an address that is a multiple of its size (this is how x86 and ARM processors lay out basic data types in memory, for example). In C, a structure gets the alignment of the member that has the widest alignment. Extra space may be allocated to account for the alignment requirements. In fact, reordering the members of a struct so that they are sorted by non-increasing alignment can reduce the memory footprint of a program significantly. You can read more about this in this excellent article by Eric Raymond: The Lost Art of C Structure Packing.

The deallocator recycles space by adding it to a list of free blocks so that the next time space needs to be allocated space from the free blocks is used. This technique renders the cost of allocation and deallocation negligible.

I recently learned that arena based storage management is used in Protocol Buffers, Google’s technology for serializing structured data.

Wednesday, April 5, 2017

Optimal Coloring of Chordal Graphs

How to optimally color a graph in polynomial time ? This problem is NP-complete in general but there are classes of graphs for which there exist polynomial time algorithms: trees are a very good example. In this post, we will explore a not-so-famous class of graphs: the chordal graphs. We will see that there are fast algorithms for chordal graph coloring but let us first define what chordal graphs are.

Let us assume that we are given a finite simple undirected graph $G$. $G$ is said to be a chordal graph if every induced cycle of $G$ has length 3. In other words, if $C_k, k \ge 4$ is any cycle in $G$ then $C_k$ has a chord - an edge connecting two non-consecutive vertices of the cycle. Note that by this definition every induced subgraph of $G$ is also chordal: chordality is a hereditary property. The figure to the left shows an example of a chordal graph and the figure to the right highlights a cycle of length 4 and the only chord it has.



Let us call a vertex $v$ simplicial if its neighbors, $adj(v)$, form a clique. The vertices of a chordal graph can be ordered in such a way that the kth vertex, $v_k$, is simplicial in the subgraph induced by $\{v_1, v_2, \dots, v_k\}$. Such an ordering is called a perfect elimination ordering. In fact, the chordal graphs are precisely the ones that admit a perfect elimination ordering. Once we have a perfect elimination ordering, we will be able to optimally color the graph easily.

Let us track what happens if we scan the vertices in this order and build the graph along the way. The ordering $[A, B, D, G, E, C, F, H]$ is a perfect elimination ordering of the graph above. We will shortly give an algorithm to compute such an ordering but let us see why it is useful first. The following figure shows the partial graphs that we get by adding the vertices one at a time in the ordering given above. The algorithm picks an arbitrary vertex to start with. After that, the next vertex to add is any vertex connected to a maximum number of already added vertices.



Notice that after adding a vertex $v$, then $v$ together with the vertices that are connected to it form a clique (a complete induced subgraph). The sequence of cliques that we get for this particular ordering is highlighted in yellow in the figure.

There are a few useful facts that we can extract from this example. First, a vertex adds at most one new maximal clique and so the number of maximal cliques is linear. Second, an optimal coloring contains at least as many vertices as there are in a maximum clique. We will prove that this number is sufficient.

The following algorithm for finding a perfect elimination ordering is due to Tarjan and Yannakakis; it's called maximum cardinality search, henceforth abbreviated MCS. The algorithm maintains for each vertex $v$ an integer weight, $w(v)$, which is the number of adjacent vertices whose weight is positive. All weights are set to zero initially. An implementation of this algorithm can be found here.


There are implementations that take near linear time to execute but a $O((n+m) \lg n)$ priority queue implementation will do. Now traverse the graph in that order and color (assign a number) each vertex with the smallest number not used among its predecessors. This greedy algorithm takes linear time and together with MCS gives a $O((n+m) \lg n)$ algorithm for chordal graph coloring.

The claim is that this yields an optimal coloring. To prove this we have to show that the chromatic number $\chi(G)$ coincides with the clique number $w(G)$ (not to be confused with integer weights used in the algorithm). Recall that $w(G)$ is the size of a maximum clique of $G$.

Let $indeg(v)$ denote the number of predecessors of $v$ in the ordering, i.e., the vertices adjacent to $v$ and that come earlier in the ordering. The algorithm chooses the smallest number not used among the predecessors to color the next vertex, so the minimum number of colors $\chi(G)$ is at most $\max_i\{indeg(v_i)\}+1$. Let $v_j$ be the vertex of maximum $indeg$: $\chi(G) \le indeg(v_j)+1$. Since we have a perfect elimination ordering, $v_j$ is simplicial, and $v_j$ together with its predecessors form a clique. So $w(G) \ge indeg(v_j)+1$. But we know that $\chi(G) \ge w(G)$. This implies that $\chi(G)=w(G)=indeg(v_j)+1$. This tells us that the above graph is 3-colorable for example.

We saw how to compute a perfect elimination ordering and how to use it to optimally color a chordal graph.  In fact, lots of problems that are hard on general graphs become easy on particular classes of graphs and the chordal graphs are probably the second most important class of graphs in applications. I hope that this was an enjoyable read.

Saturday, March 25, 2017

Fenwick Trees

The Fenwick Tree is an elegant data structuring technique that helps solve the following problem. Given an array of numbers we would like to be able to:

  • Update the element at a given position.
  • Answer range sum queries.

The Fenwick tree is very easy to implement but it can be intimidating to get your head around it the first time you encounter it. This post is hopefully going to help you better understand this data structure.

Approach 1


We can use prefix sums to answer range queries in constant time but updating a position may require touching all positions in the worst case. So updates in this way are expensive.

In the usual array representation every position is responsible for itself. That is, every position remembers the value stored in it and that is it. The Fenwick Tree takes this idea of responsibility a bit further

Approach 2


We still use prefix sums but we think of each position as being responsible for a whole range ending at that position. First take a look at the diagram below. The vertical line segments are the ranges for which positions are responsible. If we take a position, write it in binary, remove all 1-bits and keep only the lowest (least significant) 1-bit then the resulting number gives the size of the range for which the position is responsible. For example, if the position is 6 (110 in binary) then the size of the range for which 6 is responsible is 2. In other words, position 6 is responsible for itself and the position before it: the range is [5..6]. Note that the size of a range is always a power of 2.

Fenwick.png

This idea of responsibility allows us to partition any prefix of the array into a union of disjoint ranges. Let's take the prefix ending at position 10 (in decimal). 10 is written as 1010 in binary. By the argument above we can say that 10 is responsible for the range [9..10]. Now we subtract the size of this range from 10 to get 8. Note that this is equivalent to removing the lowest 1-bit from 1010: 1010 becomes 1000 which is equal to 8. Now we repeat with 8 which is responsible for the range [1..8]. We remove the lowest 1-bit from 8 to get 0 and so we have partitioned the entire prefix.

The sizes of these ranges follow directly from the base-2 representation of the position. The position 10 is written as 1010 in binary, which is equal to $2^3+2^2=8+2$ and these are the ranges that we got. So nothing fancy really.

We need $\log N$ bits to represent an integer $N$ in binary, so this partitioning process takes time $O(\log N)$. This allows us to get a prefix sum in $O(\log N)$ time. Worse than the bound we got by using the first approach but this idea of responsibility is going to allow us to speed up the running time of updates.

To update a position we have to find the ranges that are responsible for it (note that a range can be represented by the position at which it ends) and update them. When a position was responsible for itself only we updated that position but now a position is responsible for itself and other positions as well.

Let us look at responsibility again. Take this bit pattern for example: 1011. If we do a query at position 1100 it gets us to position 1000 (remove lowest 1-bit) afterwards. This means that the range for which position 1100 is responsible starts at 1000 and ends at 1100. Now our bit pattern, 1011, is included in this range and so when we update position 1011 we should update 1100 as well. When we removed the lowest 1-bit from 1100 it got us to 1000 (this turned the 1 bit into a 0 and everything from 1000 to 1100 will be in the range including positions like 1011). So to get the ranges that are responsible for some position we first find the lowest 0 bit, flip it and clear all 1 bits to its right. Do the same for all 0 bits from lowest to highest. This can be simply achieved by adding the lowest 1-bit to the position each time. The ranges responsible for 1011 are: 1011, 1100, 10000, 100000, etc…

Both queries and updates require finding the lowest 1-bit. This can be done by ANDing the position with its two's complement.

  • If p = 1010, p's complement is 0101 + 1 =  0110. 1010 & 0110 = 0010.

But you can come up with other methods to isolate the lowest 1-bit.

The Fenwick tree can be used to handle other types of queries. For example, range updates and point queries. With a bit more cleverness it can also be used to perform range updates and range queries. This is all for this post. I hope that it was informative.

Wednesday, March 1, 2017

Algorithm and Data Structure Visualizations

I started a project to help visualize algorithms and data structures. The aim is to help learners grasp the problem/algorithm by visualizing the output. The code is hosted on Github and the project is a work in progress. The Github repository will grow to contain several algorithm visualizations. The demonstrations can be accessed via this link (which also contains a link to the Github repo).
Here is a preview of some of the algorithms for which visualizations have been implemented (these include Knuth's Dancing Links, Shortest Paths, Graph Traversals, etc..):

Check regularly for updates.

Monday, February 27, 2017

Splay Trees

The standard way to achieve worst case logarithmic performance per operation in a binary search tree is by imposing a balance condition on the nodes. AVL trees, for example, force logarithmic height by making each node store its height in addition to its left/right/parent pointers. The balance condition is invariant and a rebalancing operation is necessary to restore the invariant whenever it is violated.

There is another way to obtain logarithmic performance but in an amortized sense. We do not need to impose a balance condition. Rather, the tree adjusts itself after each operation by using a restructuring heuristic. Sleator and Tarjan proposed a heuristic that achieves O(lg n) time amortized per operation (over a sequence of operations). This heuristic is the splay operation. In the remainder of this post, we shall describe how the splay operation works and provide a C/C++ implementation.

We are going to use a C++ structure to represent a node. A node stores pointers to its left/right children and to its parent. Initially, all pointers point to nothing.


In a splay tree, a node x is splayed to the root of the tree whenever it is accessed. Splaying x works by climbing the x-to-root path by following parent pointers and performing rotations. A rotation is a local restructuring operation. If x is a left child, it is rotated to the right and if it is a right child, it is rotated to the left. The following is a pictorial representation of a rotation. Here x and y are nodes and A, B and C are sub-trees (possibly null).



Rotations do not change the binary search property, i.e., inorder traversals of the tree before and after the rotation give the same output. The following function performs a right rotation.


Here is code to perform a left rotation.


The splay operation performs rotations in pairs. There are two cases to consider, a zig-zig case and a zig-zag case. Let x be the node that we want to splay and let y be its parent. The zig-zig case occurs when both x and y are left children (or both right children). The zig-zag case occurs when x is a left child and y is a right child (or x is a right child and y is a left child). If y is neither a left nor a right child (it is the root), we do a single rotation. Let us elaborate.

The following figure shows the pair of rotations performed in the zig-zig step. Here x is a left child and y is a left child (the right-right case is symmetric). The order of rotations is important, we first rotate y and then we rotate x.



The following figure shows the pair of rotations performed in the zig-zag step. Here x is a right child and y is a left child (the left-right case is symmetric). We rotate x twice



To wrap, there are two cases up to symmetry and four cases in general, and we might do one last rotation at the end if x does not have a grand-parent.


By the end of the splay operation, x becomes the root of the tree. This is very useful because nodes that are accessed frequently live near the root of the tree as a result of splay.

Now you can augment nodes with keys and use the splay operation to implement the other operations. For example, to insert a node with key k, you do a regular binary search on k to find where to insert the node, and then you splay that node to the root of the tree.

That is all for this post. Thank you for reading !

Wednesday, February 15, 2017

I am Writing a Compiler for C in C

First day of the adventure: I really want to finish this work. I started reading ‘A retargetable C compiler: design and implementation’ to learn the tricks of the trade and I will commit to finishing the implementation of a working ISO C compiler. Along the way I will be taking notes and writing about the experience.

In the past, I wrote independent components of compilers but never finished a complete compiler. This was due to lack of motivation/resources and partially because I teamed up with other people without having a clear picture in mind of who was doing what and how. I decided that this project is going to be a solo adventure.

Why learn how to build a C compiler ?

  • Only few programmers know how to build compilers. They are better programmers and better C programmers.
  • The compiler writer must understand even the darkest corners of the C programming language. This reveals much about the language itself.
  • A compiler is one of the best demonstrations of the interactions between theory and practice. Writing a compiler helps understand where these interactions are smooth and elegant and where practical demands strain the theory.

A compiler translates source code to assembler or object code for a target machine. A retargetable compiler has multiple targets.

The book focuses on the implementation of a retargetable compiler for three target machines. I will lower expectations this time and shoot for one target: the x86 architecture. The book also cares less about theory and describes only the bits of theory needed for the actual implementation. I have read a lot about theory so the lack of theory coverage will go unnoticed.

Design goals:

  • Fast scanner and instruction selector.
  • Generate code of reasonable quality.
  • Machine independent optimizer (local optimization).
  • Hand-crafted scanner and parser (recursive).
  • Clean semantic checker.
This is all for this post. I will be sharing code gradually.

Sunday, January 1, 2017

Heuristic Inverse Kinematics with FABRIK

Inverse kinematics (IK for short) is the problem of computing end-positions for a chain of joints . FABRIK is a heuristic technique for solving an IK system. In the figure below, the joint $p_0$ is fixed and the joint $p_n$ is free. $p_n$ is also called an end-effector. We would like to be able to move the end-effector freely and the system should compute locations for the joints $p_1,\dots,p_{n}$ so that the end-effector reaches a target position. Think of it as a robotic arm trying to reach a target.

[figure]

Before describing the technique, I invite you to watch this demo FABRIK solver that I created.

There are several ways to solve an IK system. Some give exact solutions but require a lot of time to calculate the joint positions; these are preferred techniques when precision matters. If you are going to write a simulation or an IK solver for a video game, for example, then you might consider using FABRIK; the results are by far the best in terms of realism and the algorithm is very fast.

The overall idea is simple. We iteratively improve upon the current joint positions until the distance between the end-effector and the target position is good enough. We also set an upper-bound on the number of iterations (this is a small number in practice) to avoid looping forever.

In a single iteration we perform a forward pass in which the joints get closer to the target (this shifts the fixed joint as well) and a backwards pass in which the joints get closer to the fixed joint (this gets the fixed joint back to its position). After every iteration, the calculated new joint positions are improved. How are the new joint positions calculated ? This is where the heuristic part comes in.

Tuesday, October 11, 2016

Self-Adjusting Heaps

I am a big fan of self-adjusting data structures. The idea is that we can use a simple heuristic to adjust the data structure (perform rotations in a tree for example) and get superb bounds on the running time (amortized over a sequence of operations on the data structure). Self-adjusting can be difficult to analyze but they are very simple to implement and they perform well in practice.We are going to implement a min-heap. Here are the operations that will be used to interface with the data structure:

  • Top: Top element (or minimum).
  • Push: Insert new element.
  • Pop: Remove top element.
  • Join: Merge two heaps together.

Representation

We are going to use the left child next sibling tree representation. Each node in the tree stores a key, a pointer to its leftmost child and a pointer to its next sibling (right sibling). An empty heap is just a null pointer. With this representation, adding a new child is easy and fast.

Here is the code for what we have covered so far:


Getting the Top Element

With the above representation, getting the minimum element is easy to implement. We just return the key of the root node.


Merging Heaps Together

This operation is not so hard to implement either and we are going to use it again to implement the rest of the pairing heap interface. Let us imagine that we have two non-empty heaps $A$ and $B$. If the key of the root node of $A$ is smaller than the key of the root node of $B$ we simply add the root node of $B$ as a child of $A$ and vice versa. Note that such an operation moves the entire heap and not only the root node (we are using pointers in the representation).


Inserting a New Element

Again, this is really easy to implement. We can create a new node for the element to be inserted and merge this node with the existing heap by using the merge routine.


Removing the Top Element

So far, all of the operations were easy to implement and they are fast as well; each operations takes $\cal O(1)$ time to execute. Now removing the top element requires some re-adjusting. This is where we use a heuristic to re-adjust the tree. The heuristic we are going to use is called two-pass merge. It is actually convenient to have some figures to explain how this works. So let us imagine that our heap (or tree really) looks like the figure below.

Pairing heap before pop


The two-pass merge heuristic re-adjusts the tree by first removing the root node and then merging the children in pairs. This is a two-pass merge because we have to follow the next pointers in a first pass and then merge on the way back.

Pairing heap after pop


Recursion comes in handy here, we can implement the above idea in a single function.


This operation takes logarithmic time amortized over a sequence of operations.

Wrap Up

We have been working with nodes but it would be nice to have a type for pairing heaps themselves. This is not hard to accomplish. We add a new type for pairing heaps with its own interface. This is a clean way of doing it and in a real world implementation we can hide the node-based implementation and expose the pairing heap interface only.


This is all for this post. The entire code listing can be found here. I hope that it was a good read and as always comments and questions are welcome.

Saturday, September 17, 2016

Matrix Representation of Quaternion Rotation


Quaternions are an amazing tool for performing rotations in 3-space. Every game engine worth of the name has a module for computing with quaternions. Internally, rotations are still performed using matrices but instead of composing rotations by multiplying matrices together, quaternions are multiplied. The final resulting quaternion is then converted into a rotation matrix which is fed to the game engine for further processing. This final step is very important yet many game programmers tend to use the conversion routines as a black box, which is okay if you are not interested in the mathematics; but understanding the mathematics can make a difference.

I am going to assume that you are familiar with quaternion multiplication and that you know the quaternion rotation formula. Here is the formula for reference:

$$rot(v,n,\theta)=qvq^*.$$

Here $q=\exp(\frac{\theta}{2}n)=\cos(\frac{\theta}{2})+ \sin(\frac{\theta}{2})n_xi + \sin(\frac{\theta}{2})n_yj + \sin(\frac{\theta}{2})n_zk$, where $\theta$ is the rotation angle in radians; $n$ is the unit rotation vector, $n=n_xi + n_yj + n_zk$; and $v$ is the vector we want to rotate. $q^*$ is notation for the conjugate of $q$: $q^*=\cos(\frac{\theta}{2})- \sin(\frac{\theta}{2})n_xi - \sin(\frac{\theta}{2})n_yj - \sin(\frac{\theta}{2})n_zk$.

From Quaternion to Matrix

The formula for rotation involves quaternion multiplication and we are going to use that to represent the multiplication in the matrix world. So let us say that we are given two quaternions $p=a+bi+cj+dk$ and $q=e+fi+gj +hk$. Note that we can think of a quaternion as being a 4 dimensional column vector: $p$ can be represented as the column vector $\left( \begin{matrix} a & b & c & d \end{matrix} \right)^T$. Let us begin by multiplying the quaternions $p$ and $q$.

$$pq=(a+bi+cj+dk)(e+fi+gj +hk).$$

Recall that the rules for quaternion multiplication are: $i^2=j^2=k^2=ijk=-1$.

Now multiplying and collecting terms gives:

$$\begin{align} pq= & (ae-bf-cg-dh)+ \\ & (be+af-dg+ch)i+ \\ & (ce+df+ag-bh)j+ \\ & (de-cf+bg+ah)k  \end{align}$$

I laid out the product above in that way for a reason. If you know some linear algebra then you can tell how to represent the product above as a matrix-vector product just by looking at it. The first column has $e$'s in it, the second column has $f$'s in it, etc.. The product is crying out to be written in matrix form.

$$\left( \begin{matrix} a & -b & -c & -d \\ b & a & -d & c \\ c & d & a & -b \\ d & -c & b & a \end{matrix} \right) \left( \begin{matrix} e \\ f \\ g \\ h \end{matrix}\right)$$.

That's how you represent a product of two quaternions as a matrix-vector product. In the above matrix the quaternion $q$ is being used as a vector; can we use $p$ as a vector instead ? Yes, we just rearrange the terms.

$$\begin{align} pq= & (ae-bf-cg-dh)+ \\ & (af+be+ch-dg)i+ \\ & (ag-bh+ce+df)j+ \\ & (ah+bg-cf+de)k  \end{align}$$

This gives the following matrix-vector product.

$$\left( \begin{matrix} e & -f & -g & -h \\ f & e & h & -g \\ g & -h & e & f \\ h & g & -f & e \end{matrix} \right) \left( \begin{matrix} a \\ b \\ c \\ d \end{matrix}\right)$$.

That was not very hard. Quaternion multiplication is not commutative so the matrices above correspond to multiplication on the left and on the right. Let us now turn to the problem of expressing quaternion rotations as multiplication by matrices.

From Rotation Quaternion to Rotation Matrix

Now that we know how to represent quaternion multiplication as a matrix-vector product, we are going to use that knowledge to derive a formula to represent quaternion rotation as multiplication by a matrix. There seems to be a lot of mystery surrounding this on the internet; people who write about this give the formula for the reader to use as a black box and they don't worry about giving details about where it came from.

Look at the formula for quaternion rotation. We first multiply $v$ by the quaternion $q$ on the left and then we multiply the result by the quaternion $q^*$ on the right (or you can think of it as multiplying $v$ by $q^*$ on the right and then multiplying the result by $q$ on the left). Now call the matrix corresponding to multiplication by $q$ on the left $A$ and call the matrix corresponding to multiplication by $q^*$ on the right $B$. We first represent the product $qv$ as $Av^T$. Now we have to include $q^*$ in the product but we are multiplying on the right so the matrix product gives $BAv^T$ (here $v^T$ is the vector representation of the quaternion $v$). That's it, we have our matrix representation of quaternion rotation. In fact, $ABv^T$ gives the same effect; it depends on which multiplication you do first: $qv$ or $vq^*$. So this tells us that $A$ and $B$ commute in this case: $AB=BA$. Now let us go ahead and compute this product.

$$AB=\left( \begin{matrix} a & -b & -c & -d \\ b & a & -d & c \\ c & d & a & -b \\ d & -c & b & a \end{matrix} \right) \left( \begin{matrix} a & b & c & d \\ -b & a & -d & c \\ -c & d & a & -b \\ -d & -c & b & a \end{matrix} \right)$$

$$=\left( \begin{matrix} a^2+b^2+c^2+d^2 & 0 & 0 & 0 \\ 0 & a^2+b^2-c^2-d^2 & 2(bc-ad) & 2(ac+bd) \\ 0 & 2(ad+bc) & a^2-b^2+c^2-d^2 & 2(cd-ab) \\ 0 & 2(bd-ac) & 2(ab+cd) & a^2-b^2-c^2+d^2 \end{matrix} \right)$$

I will let you compute $BA$ to make sure that it gives the same result. One last thing to mention is that sometimes people change the order of the components in a quaternion; this will change the matrix but the terms will remain the same.

I hope that this will end up being useful to you. Feel free to drop a comment or ask a question.

Thursday, September 1, 2016

Maintaining Disjoint Sets under Union

Time for a new post. This time, we will cover an elegant data structure for maintaining a class of sets under the operation of union. Formally, we start with $n$ classes each consisting of a single element: $\{1\}, \{2\}, \dots, \{n\}$. These correspond to the equivalent classes of some equivalent relation. We would like to be able to quickly find which class an element belongs to and quickly join two classes together to form one class (destroying the old classes along the way). These two operations are known as find and union and the data structure is often times called union-find.

There are many ways to implement these operations but we will cut to the chase and implement find with path compression and union by rank.

Representation of a Class

We are going to use a tree to represent an equivalent class. Each node in the tree corresponds to an element in the class. Each node also points to its parent in the tree. The root of the tree points back to itself. The root of the tree also serves as the representative element of the class. The root node will also maintain the number of nodes in its tree (we will see why this is useful when we talk about union by rank). Initially, every element lives in a separate class, so the trees will look like this for $n=5$ (the size of the trees is dropped here).


Initial setting

Find with Path Compression

Given an element, we would like to find the class it belongs to (or the representative of that class since each class has a unique representative). Using the representation above, we just climb to the root by following parent pointers and return the root element. The path to the root could be long and so making several calls to find with the same argument could end up taking a lot of time. Path compression avoids this by making the nodes along the path (except for the root itself) point to the root. This renders subsequent calls to find very cheap in terms of running time. Here is a pictorial representation of path compression. The right tree is the new state we get after a call to $find(4)$. The path to the root is highlighted in red.

Find with path compression

Union by Rank

The union operation takes two disjoint classes as input and creates a new class that is the union of the elements in the input classes. An easy way to implement this is to take one of the root elements and make it a child of the other root element. The union by rank heuristic suggests that it is a good idea to make the root of the class with the smaller number of elements a child of the root of the class with the larger number of elements. In the following figure, the class $\{1,2\}$ is joined with the class $\{3,4,5\}$ by making $1$ a child of $3$.


Union by rank

Implementation

For each node we need to know its parent node and the size of its subtree. We will use one array, $uf$ (for union-find), of integers to account for both pieces of data. $uf[x]$ gives the parent of a node when $x$ is not a root and it gives the size of the subtree when $x$ is a root. How do we differentiate between the two ? We simply negate the size when the element is a root. So if $uf[x]$ is negative we can say that $x$ is a root element and that $-uf[x]$ is the size of the tree rooted at $x$ and when $uf[x]$ is non-negative we can say that $x$ is a non-root element and that it is a child of $uf[x]$.

Initially, every element lives in its own tree and so is the root of that tree. We can write a function to initialize the array as follows:

The union operation is very easy to implement. First we find the root elements and then make the one with the smaller size a child of the one with the larger size.

The find operation is slightly harder to implement. We find the root element in a first pass and then make the nodes along the path children of the root in a second pass.

That is all for this post. I hope that it was helpful. As always, feedback and questions are welcome.

Tuesday, June 28, 2016

The Maximum Contiguous Subarray Problem

Among all the contiguous subarrays of a given array find one in which the sum of elements is maximized. This problem is known as the maximum contiguous subarray sum problem and we will see a few ways in which it can be solved. We will call the array $A$ and assume that it has $n$ numbers.

Brute Force

The brute force solution is to loop over all of the subarrays and take any one that maximizes the sum. There are $\cal O(n^2)$ subarrays and it takes linear time to compute the sum so the brute force approach gives a $\cal{O}(n^3)$ algorithm to solve this problem. Note that we print the sum at the end but it is easy to augment the algorithm to find the ends of the subarray.

Optimized Brute Force

With a bit more cleverness we can chop a linear factor. We keep track of the current sum as we go instead of fixing the right end and then computing the sum in a nested loop.

Divide and Conquer

The divide and conquer approach to problem solving is to take the original problem, split it into smaller subproblems, solve the subproblems recursively and use the solutions to the subproblems to construct the solution to the original problem. Now imagine that we split the array into two halves; what can we say about the optimal subarray ? It can be completely contained in the left half, completely contained in the right half, or it can start somewhere in the left half and end somewhere in the right half. The first two cases are subproblems of the problem we started with so we can solve them recursively. The third case can be solved independently.

Let's call the split point $m$. The left end can be computed by starting at position $m$ and going down to position $0$ while keeping track of the maximum sum. The right end can be computed similarly: start at position $m+1$ and go up to position $n-1$ while keeping track of the maximum sum. This is illustrated in the following figure.



The following is an implementation of the above idea.

The running time of this algorithm can be calculated by using the recurrence $T(n)=2T(n/2)+\cal \Theta(n)$ which gives $\cal O(n \lg n)$ after unfolding.

Dynamic Programming

There is another way to split the problem into smaller subproblems and use the solutions to the smaller subproblems to construct the solution to the original problem. Imagine that you are given the maximum sum ending at a position $p$, i.e., the maximum coniguous subarray sum with the additional constraint that the right end of the array is at position $p$. How can this information be used to find the maximum contiguous subarray ending at position $p+1$ ? First take a look at this figure to see the whole picture.



Note that $S$ is the best sum we can get by fixing the right end at position $p$ (adding cells to the left won't improve $S$). Now to construct the maximum sum ending at position $p+1$ we have two options:

  1. Extend the previous sum by $A[p + 1]$,
  2. Take $A[p + 1]$ and ignore $S$.

These are the only options we have, trying to extend just part of $S$ is useless because we know that $S$ is the maximum sum that ends at the previous position and so adding just a part of that sum is clearly going to give us a suboptimal answer.

This way of splitting problems gives us an efficient way to find the maximum contiguous subarray sum ending at a given position. Since the optimal solution for the entire array has to end somewhere, then we just solve the problem for each of the ending points and take the one that maximizes the sum.

The difference between the divide and conquer approach and the dynamic programming approach is that the subproblems overlap in the case of dynamic programming whereas they don't in the case of divide and conquer. The running time of the dynamic programming algorithm is $\cal O(n)$ because there are $n$ subproblems and computing the answer for each of them takes a constant amount of work.


Monday, June 20, 2016

Variants of Binary Search

Introduction

Given a sorted array $A$ and a target value $k$ to search for, return a position $p$ in the array for which $A[p]=k$ or return $-1$ to signal that the element was not found. The folklore algorithm to solve this problem is binary search: you look at the middle element, compare it to $k$ and decide whether to terminate, search in the left half-array or search in the right half-array. This is pretty fast and it takes time $O(\lg N)$ where $N$ is the length of the array. In this post we will explore several ways to implement binary search.

Standard Binary Search

Let's start by giving the standard implementation. The recursive implementation is obvious so let's focus on iterative implementations instead.

Meta Binary Search

Meta binary search is a variant in which the position containing the target value is built one bit at a time, starting with the most significant bit.

To get a better idea of how this algorithm works, let's walk through an example. Imagine that the input array is $A=[1, 3, 5, 7, 11]$ and that we are looking for the value $k=7$. The last position of the array is $4$ and we need $2$ bits to store it. The first loop computes this value. Now we start at the most significant bit and we have two options: set it to $1$ or set it to $0$. We try both options and select the correct one. If setting the current bit to $1$ exceeds the length of the array then our only choice is to set it to $0$. In the other case we set the bit to $1$ and check if the value sitting in the position is strictly greater than $k$, in which case we set the bit to $0$ instead because our position never decreases in value (so by setting the current bit to $1$ we'll just be searching in parts of the array in which all values are strictly greater than the target value).

Like standard binary search, meta binary search takes $O(\lg N)$ time. The first loop computes the number of bits required to encode the largest position in the array and the second one iterates over these bits and builds up the answer.

This might be your first encounter with meta binary search but you might have used it in the past without paying attention. If you wrote code to answer lowest common ancestor (LCA) queries by using sparse tables then your LCA procedure is actually a form of meta binary search.

Binary Search over Real Numbers

How to write binary search when the search space is a monotonic real interval ? Many programmers use epsilons as a terminating condition. So you often see code that looks like:

This approach is error prone and might even time out. We'll describe a clean binary search over a real interval. Instead of storing the left and right end points of the interval as we did with the integers, we'll store the left end point and the size of the search space. We divide the size of the search space by 2 after each step and so the algorithm is guaranteed to terminate when the size reaches $0$.


Fractional Cascading

We will close this post with a useful technique that can be used to reduce the cost of searching for a target value $k$ in a list of $M \ge 1$ sorted arrays instead of just one. To simplify the discussion let's assume that all of the arrays have length $N$. An easy way to solve this problem is to binary search in each of the arrays; this takes time $O(M \lg N)$. Can we do better ? Yes !

We will preprocess the list of arrays so that search queries are fast. The idea is to cascade a fraction of the last list to the one right above it and add links from those cascaded elements to their locations in the list they came from. Now take this newly created list and cascade a fraction of its elements to the list on top of it, and so on. Now we perform one binary search in the first array and then follow the pointers to locate the values in the subsequent arrays. This technique is often used in geometric algorithms to reduce the cost of searching.

Saturday, June 11, 2016

Storing a Graph in Memory

Graphs are undoubtedly one of the most important objects in computer science. There are several algorithms that operate on graphs and these algorithms might take different amounts of execution time depending on the graph representation being used. In this post, we will explore some of the ways in which a graph can be stored in memory. Throughout the rest of this post we will assume that the underlying graph contains $n$ nodes $0,1,\dots,n-1$ and $m$ edges $0,1,\dots,m-1$.

Edge List

The simplest representation is to store the edges of the graph in a list. This representation is useful when adjacency information (which edges go out of a given node) is not important. For example, Kruskal's algorithm for minimum spanning tree does not use adjacency information but it needs the edges sorted by weight and so this representation can be used to implement Kruskal.

Adjacency Matrix

When the graph is dense: the number of edges is close to the maximal number of edges, an adjacency matrix can be used. The edges are stored as entries in a $n \times n$ matrix $A$ so that: $$A_{i,j} = \begin{cases}1 \text{, if there is an edge from i to j} \\0 \text{, elsewise}\end{cases}$$

Adjacency List

The adjacency list representation is the one that is used the most. In this representation every node stores a list of outgoing edges. Traversing the graph by using this representation takes time $\mathcal{O} (n+m)$ whereas traversing it by using an adjacency matrix takes time $\mathcal{O} (n^2)$. When the graph is dense it does not really matter because $m=\mathcal{O} (n^2)$ but when it is sparse then algorithms that use the adjacency list representation are way faster. The following is an example implementation.


A Triple-Array Representation

The adjacency list implementation uses dynamic arrays (vectors) and there is a call to push_back each time an edge is added. We can further optimize by using static arrays instead of vectors. In this representation every node remembers the last edge out of it and every edge remembers the previous edge (with respect to the node on which both edges are incident) and the node to which it is pointing. Here is an example implementation.