# binfalse

## Reassembling logical operations on boolean vectors in Gnu R

What a headline.. It’s about combining boolean vectors in R.

I just had some problems with computing a boolean vector as a result of applying `AND`

to two boolean vectors:

As you can see, it’s a nice result, but not what I want.. My hack was the following:

When Rumpel, my personal R-freak, saw that hack, he just laughed and told me the short version of this hack:

Nice, isn’t it ;)

## Export R data to tex code

We often use Gnu R to work on different things and to solve various exercises. It’s always a disgusting job to export e.g. a matrix with probabilities to a document to send it to our supervisors, but Rumpel just gave me a little hint.

The trick is called xtable and it can be found in the deb repository:

It’s an add on for R and does right that what I need:

It is not only limited to matrices and doesn’t only export to latex, but for further information take a look at `?xtable`

;)

Btw. I just noticed that the GeSHi acronym for Gnu R syntax highlighting is `rsplus`

…

## Lost my gitosis' post-update

Don’t ask me why neither how, but I’ve lost the gitosis’ `post-update`

on my server. So, among others, the `.gitosis.conf`

wasn’t updated anymore…

Ok, let’s start at the beginning. I’m hosting some git repositories on my server using gitosis. Today I tried to create a new repository by editing the `gitosis.conf`

in the `gitosis-admin`

repo, but I couldn’t push the new created repo to the server:

Damn, looking at the server all rights through the gitosis home seem to be ok. But I was wondering why the `$HOME/.gitosis.conf`

of the gitosis user wasn’t updated!? How could this happen?

After some thoughts I found out, that the link `$HOME/repositories/gitosis-admin.git/hooks/post-update`

was pointing to nirvana:

Seems that the file `/usr/share/python-support/gitosis/gitosis-0.2-py2.5.egg/gitosis/templates/admin/hooks/post-update`

was deleted through an update (one week ago I updated `gitosis 0.2+20090917-9 -> 0.2+20090917-10`

).. Didn’t find an announce of it or any workaround, but I identified a template in `/usr/share/pyshared/gitosis/templates/admin/hooks/post-update`

. So the solution (hack) is to link to that file:

Just replace `$GITOSISHOME`

with the home of your gitosis user, mine lives for example in `/home/git`

.

Now every think works fine. If anybody has a better solution please tell me!

## Suffix trees

Suffix trees are an application to particularly fast implement many important string operations like searching for a pattern or finding the longest common substring.

## Introduction and definitions

I already introduced the Z-Algorithm which optimizes the searching for a pattern by preprocessing the pattern. It is very useful if you have to search for one single pattern in a large number of words. But often you’ll try to find many patterns in a single text. So the preprocessing of each pattern is ineffective. Suffix trees come up with a preprocessing of the text, to speed up the search for any pattern.

As expected a suffix tree of the word (length ) is represented in a data structure of a rooted tree. Every path from root to a leave represents a suffix of $ with . The union . Every inner node (except root) has at least two children. Every edge is labeled with a string of , labels of leaving edges at a single node start with different symbols and each leaf is indexed with . The concatenation of all edge labels on a path from root to a leaf with index represents the suffix .

**Definition 1**:
For each node :

The path from root to is called

- The union of the labels at all edges on is
- is
*label of path*() - is
*path label to*() - instead of we can call this node

**Definition 2**:
A pattern exists in suffix tree of (further called ) if and only if there is a , so that contains a node with ().

**Definition 3**:
A substring ends in node or in an edge to with .

**Definition 4**:
A edge to a leaf is called leaf-edge.

The tree contains all suffixes of the word extended with $. This tree is visualized in figure 1.

## Building suffix trees: Write only top down

The *write-only, top-down* (WOTD) algorithm constructs the suffix tree in a top-down fashion.

### Algorithm

Let be a node in , then denotes the concatenation of all edge labels on the path to (). Each node in the suffix tree represents the set of all suffixes that have the prefix . So the set of pathlabels to leafs below can be written as (all suffixes of the set of suffixes that start with ).

This set is splitted in equivalence classes for each symbol with is the -group of .

**Case 1:**
For groups that contain only one suffix we create a leaf with the index and connect it to with an edge containing label .
**Case 2:**
In groups with a size of at least two we compute their longest common prefix that starts with and create a node . The connecting edge between and gets the label and we continue recursively with this algorithm in node with

## Applications

### Exact pattern matching

All paths from the root of the suffix tree are labeled with the prefixes of path labels. That is, they’re labeled with prefixes of suffixes of the string . Or, in other words, they’re labeled with substrings of . To search for a pattern in , just go through , following paths labeled by the characters of . At any node with is prefix of find the edge with label that starts with symbol . If such an edge doesn’t exists, isn’t a substring of . Otherwise try to match the pattern with to node . If is not a prefix of you’ll either get a mismatch denoting that isn’t a substring of , or you ran out of caracters of and found it in the tree. If is a prefix of continue searching at node . If you were able to find in , contains at any position denoted by the indexes of leafs below your point of discovery.

### Minimal unique substrings

is a *minimal unique substring* if and only if contains exactly once and any prefix of can be found at least two times in .

To find such a minimal unique substring walk through the tree to nodes with a leaf-edge . A minimal unique substring is with is the first char of , because its prefix isn’t unique ( has at least two leaving edges) and every extended version has a prefix that is also unique.

### Maximal repeats

A *maximal pair* is a tuple , so that , but and . A *maximal repeat* is the string represented by such tuple.
If is a maximal repeat there is a node in .
To find the maximal repeats do a DFS on the tree. Label each leaf with the left character of the suffix that it represents. For each internal node:

- If at least one child is labeled with c, then label it with c
- Else if its children’s labels are diverse, label with c.
- Else then all children have same label, copy it to current node.

Path labels to left-diverse nodes are maximal repeats.

### Generalized suffix trees

An extension of suffix trees are *generalized suffix trees*. With it you can represent multiple words in one single tree.
Of course you have to modify the tree, so that you know which leaf index corresponds to which word. Just a little bit more to store in the leafs ;)
A generalized suffix tree is printed in figure 2 of page one.

### Further applications

There are a lot of other applications for a suffix tree structure. For example finding palindromes, search for regular expressions, faster computing of the Levenshtein distance, data compression and so on…

## Implementation

I’ve implemented a suffix tree in Java. The tree is constructed via WOTD and finds maximal repeats and minimal unique substrings. I also wanted pictures for this post, thus, I added a functionality that prints GraphViz code that represents the tree.

**Download:**Java: Code from GitHub (Please take a look at the man-page. Browse bugs and feature requests.)

## Cracked the centenary!

Wow, the new kernel is the 100^{th}.

(That was the reason why the `dist-upgrade`

took more than an hour!^^)