# Non-recursive DFS   Posted: 2014-04-20 09:34

Depth-first search (DFS) is a powerful algorithm. The recursive DFS algorithm is probably the easiest to understand, and most of the time it is good enough.

In some problems, the recursion stack gets very large. This can easily blow the stack limit in Python. Fortunately, there is a simple way to make the algorithm iterative.

Recursive DFS implementations sometimes ignore the pre or post numbering of the vertexes, because they aren't needed – the work is done in the recursive call. Those numbers are key to the iterative algorithm.

The main idea of the recursive algorithm is to visit every vertex, and at each visit:

• Set the pre-order number.
• Visit all unvisited children.
• Set the post-order number.

In a recursive algorithm, we can also do some work before visiting the child vertexes, while visiting them, or after.

The pre-order and post-order numbers are the key to making DFS iterative. We will use an explicit stack for this algorithm.

First, we add the starting vertex to the stack. Then, while the stack is non-empty, we pop the last vertex off of the stack and do the following:

1. If the vertex has a post-order number, skip it.
2. Otherwise, if the vertex has a pre-order number, we are in the post-child step.
1. Set the post-order number and do any post processing.
1. Otherwise, we are now visiting the vertex for the first time.
1. Set the pre-order number.
2. Add the vertex back to the stack.
3. Add all of its unvisited children to the stack.

Let's informally check if this makes sense.

• A vertex is added to the stack only if it has not been visited (pre-order is not set), or while it is being visited, before its children are added to the stack. (3a and 3b)
• A vertex only hits step 3 once. After step 3, it has a pre-order number, so the next time the vertex is processed, it will go to step 2.
• The vertex only makes it to step 2 after its children have been processed.
• So, step 2 is the post-order step.

Finally, here is some code

 1 class DFS(object):
5         self.pre = [-1 for x in adj]
6         self.post = [-1 for x in adj]
7         self.pre_count = 0
8         self.post_count = 0
9
10     def dfs(self, i):
11         stack = [i]
12         while stack:
13             u = stack.pop()
14             if self.post[u] > -1:
15                 continue
16             elif self.pre[u] > - 1:
17                 self.post[u] = self.post_count
18                 self.post_count += 1
19             else:
20                 stack.append(u)
21                 self.pre[u] = self.pre_count
22                 self.pre_count += 1
24                     if self.pre[v] == -1:
25                         stack.append(v)


This code only handles starting a search tree from a specific vertex. To search the entire graph, the dfs method needs to be called for every vertex. That order is up to the caller. Pre-order or post-order steps can be added to this code, or it could be generalized to provide hooks for sub-classes to use.

# Coffee Notes   Posted: 2014-04-15 20:43

Here are some collected coffee notes that I often share with people.

I use an Aerobie Aeropress. A co-worker recommended it several years ago, and it took me too long to finally try. The hype on the web page goes too far, but I am pretty happy with it.

This video demonstrates the method I use, only I am less careful and exact with how I do it.

I use this manual burr grinder, though the price has gone way up on it since I bought one (good electric grinders cost a lot more than this...).

I buy green coffee beans from Sweet Maria's.

And roast it with a heat gun.

# JavaScript QuickSort update   Posted: 2014-04-03 22:19

The code in this post was a quicksort that used the less than operator for comparison. Here are some times of quicksort using a comparison function the same way the Array.sort works.

The times are quite a bit slower, and I need to dig into it further. These are the means of five runs each on one million values. Some of the cases (triangular, random) had a very large variation between runs.

Timing results on 1 million values (in seconds)
data type qsort2 Array.sort qsort2cmp
random double 0.14 0.95 0.38
sawtooth [1] 0.07 0.06 0.08
triangular [2] 0.14 0.71 0.42
ordered 0.03 0.46 0.13
reversed 0.03 0.59 0.10
shuffled 0.13 0.65 0.28
 [1] The values in the array are 0, 1, 2, 3, 0, 1, 2, 3, … the whole way to the end of the array.
 [2] The value in the array are 0, 1, …, N/2-1, N/2, N/2-1, …, 1,0

What does this tell us? Not much yet. I have a lot to learn about the performance of JavaScript engines.

# Raytracer   Posted: 2014-03-09 22:20

The last couple of days I have been working on a raytracer in JavaScript. Years ago I wrote one in C to learn out to do OO in C, and now I am doing it in JavaScript to see what is possible.

# Quick select   Posted: 2014-03-06 23:25

Why would a programmer want to write their own sort function?

First, and most importantly, to learn about algorithms and programming. Also, a language or library sometimes has an inadequate sort function for a specific situation.

There is also a problem in the wild that a custom sort is nice to use for.

Finding the median of an array of data (or any other percentile, for that matter.) Quickselect is closely related to Quicksort. Instead of recursively sorting both halves of the partition created in the partitioning step, we can just follow the one that contains the element we are looking for.

The C++ STL contains a partition function that can do just what we want. Sometimes a language / library doesn't include this functionality.

Quickselect is an $O(n)$ algorithm in the average case, which sounds much better than the $O(n \log n)$ case for Quicksort. But, once $n$ gets large enough, these two grow at about the same rate. The bigger win is that Quickselect has a lower constant, so even though both it and Quicksort grow at about the same rate, Quickselect is a few times faster on random data.

So, here is a quickMedian function. This function keeps calling a partitioning function on the subarray that contains the middle point. (Not really the median … but it is our example.)

The partition function returns and object containing several indexes.

• a—the start of the left partition
• b—the end of the left partition
• c—the start of the right partition
• d—the end of the right partition

We also jump out early instead of breaking the array down to a single element, and use insertion sort on the last chunk of the array.

 1     function quickMedian(xs) {
2         var N = xs.length,
3             start = 0, end = N,
4             middle = Math.floor(N/2),
5             idxs;
6
7         while(end - start > INSERT_SORT_THRESHOLD) {
8             idxs = qsort2Partition(xs, start, end);
9
10             if(idxs.b > middle) {
11                 // search left
12                 end = idxs.b;
13             } else if (idxs.c < middle) {
14                 start = idxs.c;
15             } else {
16                 // We have contained the element!
17                 break;
18             }
19         }
20
21         isort(xs, start, end);
22         if(middle < start || middle > end) {
23             throw 'bug... partitioning failed';
24         }
25         return xs[middle];
26     }


This function can be generalized to find a different value than the center value, or even to find several percentiles at once.

Here are some non-representative timing results, from my Mac, on Chrome.

Timing results on 1 million values (in seconds)
data type quick sort quick select median
random double 0.15 0.02
sawtooth [1] 0.02 0.02
ordered 0.04 0.02
reversed 0.05 0.02
shuffled 0.15 0.03
 [1] The values in the array are 0, 1, 2, 3, 0, 1, 2, 3, … the whole way to the end of the array.

Here is a link to the test driver that launches directly into the tests. It runs in Chrome, but hasn't been tested extensively.

# Alpha-Beta Pruning   Posted: 2014-03-01 14:04

Another page I made to play with is a Tic-Tac-Toe game.

Originally, I just used the MiniMax algorithm for the player. The game is a small one, and MiniMax can solve the complete game pretty quickly.

On my very old phone, though, there was a noticeable delay if the game searched the whole way to the end from the first move.

So, I converted MiniMax to Alpha-Beta. I always manage to mess that up on the first try, but eventually got it working. For a game like this, the result is still amazing. With MiniMax, the first move required about 550,000 searches to get an answer. With Alpha-Beta, that is down to less than 20,000.

And now it works fine on my phone.

# Reversi in JavaScript, again   Posted: 2014-02-28 22:42

Continuing down the path of mucking with JavaScript, I have ported Peter Norvig's Reversi code from Lisp to JavaScript.

The code is in a pretty ugly state, but it is running. Besides being ugly, the code doesn't do any feature detection or graceful degradation. Though I would like to make it pretty solid, the purpose of this was to test out JavaScript for algorithmic work, not so much for the front-end work. Check it out here.

In this version, I have used Dr. Norvig's data structures, but wrapped it so it can display on an HTML canvas, and a person can play against it on the canvas.

Some of the stuff going on here is:

• Alpha-Beta game tree searching
• Position evaluation using features from Logistello
• Learning via Samuel's strategy of using the look-ahead to train the evaluation function
• Some HTML5 stuff, like application caching, and maybe a webworker in the future.

# JavaScript Quicksort   Posted: 2014-02-21 07:05

Quicksort, yet again. This time, in JavaScript!

I have been using JavaScript more frequently, and have been pleasantly surprised at how fast it is.

So, I naturally wanted some test cases of things that I always thought JavaScript was too slow for. Sorting was one of those cases.

To check it out, I wrote some quicksort functions and compared them to Array.sort. Here is the unscientific comparison.

Using Engineering a Sort Function as a reference, along with textbooks, etc, I wrote the following:

 1     function qsort1(xs, start, end) {
2         start = start || 0;
3         if(end === undefined) { end = xs.length;}
4
5         if(end < start + INSERT_SORT_THRESHOLD) {
6             isort(xs, start, end);
7             return xs;
8         }
9
10         var i = start - 1,
11             j = end,
12             // pidx = Math.floor(Math.random() * (end - start)) + start,
13             pivot = medianOfThree(xs[start],
14                                   xs[Math.floor((start + end) / 2)],
15                                   xs[end-1]),
16             t;
17
18         while(i < j) {
19             i += 1;
20             while(i < j && xs[i] < pivot) {
21                 i += 1;
22             }
23
24             j -= 1;
25             while(i < j && pivot < xs[j]) {
26                 j -= 1;
27             }
28
29             if(i < j) {
30                 t = xs[i]; xs[i] = xs[j]; xs[j] = t;
31             }
32         }
33         if(xs[i] < pivot) { throw 'invariant'; }
34
35         qsort1(xs, start, i);
36         qsort1(xs, i, end);
37         return xs;
38     }


This method:

• Uses a median of three pivot strategy
• Leave the pivot in place while partitioning
• Is recursive down both halves of the partition.

Lines 18 – 32 partition the data.

Originally, I used a random pivot. That works well, but the median of three can help partition the data more evenly, leading to less work.

The pivot value is copied out to a temporary during the partitioning. In JavaScript, this isn't as big of a deal, because objects besides primitives are basically reference types anyways … so copying the pivot value in that case is more like a pointer copy.

A common case that I care about is when there are many equal values in the array. This version of quicksort does not handle that well.

The next version uses the fat partitioning described in the paper to group all of the values equal to the pivot.

 1     function qsort2(xs) {
2         sort(xs, 0, xs.length);
3         return xs;
4
5         // The actual sort body.
6         function sort(xs, start, end) {
7             if(end < start + INSERT_SORT_THRESHOLD ) {
8                 isort(xs, start, end);
9                 return;
10             }
11
12             var i = start - 1,
13                 j = end,
14                 u = i,
15                 v = j,
16                 pivot = medianOfThree(xs[start],
17                                       xs[Math.floor((start + end) / 2)],
18                                       xs[end-1]),
19                 t;
20             // Reorder the values, and maintain the indices so we have
21             // the following:
22             //
23             // begin (inclusive)  |  end (exclusive)  |   description
24             // start              |  u                |   == pivot
25             // u                  |  i                |    < pivot
26             // i                  |  v                |    > pivot
27             // v                  |  end              |    = pivot
28
29             while(i < j) {
30                 i += 1;
31                 while(i < j && xs[i] < pivot) {
32                     i += 1;
33                 }
34
35                 j -= 1;
36                 while(i < j && pivot < xs[j]) {
37                     j -= 1;
38                 }
39
40                 if(i < j) {
41                     t = xs[i]; xs[i] = xs[j]; xs[j] = t;
42                     /* jshint ignore:start */
43                     // We are ignoring the !.  Just trying to stick
44                     // with < for now...
45                     if(!(xs[i] < pivot)) {
46                         u += 1;
47                         t = xs[i]; xs[i] = xs[u]; xs[u] = t;
48                     }
49                     if( !(pivot < xs[j])) {
50                         v -= 1;
51                         t = xs[j]; xs[j] = xs[v]; xs[v] = t;
52                     }
53                     /* jshint ignore:end */
54                 }
55             }
56
57             // if(xs[i] < pivot) { throw 'invariant'; }
58
59             // Now, 'flip' the sides around to bring the values equal
60             // to the pivot to the middle.
61
62             j = vecswap(xs, i, v, end);
63             i = vecswap(xs, start, u + 1, i);
64
65             // xs[i] >= pivot.
66             sort(xs, start, i);
67             sort(xs, j, end);
68         }
69     }


Lines 45 – 52 swap values equal to the pivot to the outer part of the array. At the end of the partition the pivot values are brought back together in the center.

These sort routines use the < operator. The Array.sort routine requires a comparison operator that returns -1, 0, or 1 for less than, equal to, and greater than. The custom routines are less general (so far!)

Here are some timing results on Chrome comparing the two sorts and the system sort. These tests are unfair and likely misleading, but they are a start.

Timing results on 1 million values (in seconds)
data type qsort1 qsort2 Array.sort NumPy
random double 0.13 0.15 1.06 0.11
sawtooth [1] 0.09 0.02 0.04 0.02
ordered 0.04 0.04 0.59 0.01
reversed 0.02 0.03 0.59 0.02
shuffled 0.21 0.18 0.68 0.09
 [1] The values in the array are 0, 1, 2, 3, 0, 1, 2, 3, … the whole way to the end of the array.

qsort1 and qsort2 are far less generic than Array.sort, and there are likely several errors in how I am doing this timing. Sometimes, for example I seem to be getting GC pauses while doing the test, but I have not confirmed what is going on yet. Overall, I am optimistic that in these cases, JavaScript can be made to run in a reasonable amount of time. Overall, the timing is pretty variable. That will be worth investigating before too long.

The NumPy values are in there for comparison. I am assuming that the NumPy sort is in native code underneath, and it is far easier to time python code than to write some C++ wrappers just for a comparison.

Here is a link to the test driver that launches directly into the tests.

# EOPL in Javascript   Posted: 2013-12-08 00:00

I have been working through EOPL again, this time programming in JavaScript. My JS experience is limited, but I finally know enough that it isn't as painful anymore.

Some things that have helped:

• The debugging tools built into browsers
• Node.js
• Emacs -- js2-mode, flymake-jslint, and PEG.js
• And finally knowing enough that I don't have to look stuff up all of the time.

# Prime sieve in JavaScript   Posted: 2013-11-17 00:00

Update : After running the C version through Emscripten, the result is about the same speed as the C version when run through Firefox. Wow!

Update : Switching from a Javascript Array to Int8Array halved the time in Firefox, making it comparable to C and Emscripten. (Until moving C to 8 bits, then things got faster there as well …).

Ok, time for some more prime sieving. This time, we are going to try JavaScript, Python, and C.

First, the Python code

import numpy as np

def sieve1(n):
values = np.arange(n)
values[1] = 0
# values[4::2] = 0
for i in xrange(n):
i2 = i*i
if i2 >= n:
break
if values[i]:
values[i2::i] = 0
result = values[values > 0]
return result


This is the normal sieve. The inner loop is in NumPy, and running

%timeit ps = primes_np.sieve1(15485864)


Gives me a list of first million primes in about 425 ms on my laptop.

Next, C.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
const int n = 15485864;
clock_t start = clock();
int *arr = malloc(n * sizeof(int));

for(int i = 0; i < n; ++i) { arr[i] = i; }
arr[1] = 0;

for(int i = 0; i < n; ++i) {
int d = i * i;
if(d >= n) { break; }
if(arr[i]) {
for(int j = d; j < n; j += i) {
arr[j] = 0;
}
}
}
int count = 0;
for(int i = 0; i < n; ++i) {
if(arr[i]) { ++count; }
}
clock_t end = clock();
printf("found %d primes less than %d\n", count, n);
double duration = (end - start) / (double)CLOCKS_PER_SEC;
printf("took %f seconds\n", duration);
}


This code took about 280 ms. (clang -O3)

Finally, the JavaScript version.

var primes = function() {
var primes = {};

// Create an array of values as the result of calling fcn[i] for 0
// <= i < n
if(typeof Int8Array === 'function') {
primes.create_array = function(n) {
return new Int8Array(n);
};
} else {

primes.create_array = function(n) {
return new Array();
};
}

primes.array_init = function(fcn, n) {
var result = primes.create_array(n);
for(var i = 0; i < n; i++) {
result[i] = fcn(i);
}
return result;
};

// Simple sieve.
primes.sieve1 = function(n) {
var values = primes.array_init(function(i) { return 1; }, n),
i, j, result;

values[1] = 0;
values[0] = 0;

for(i = 0; i < n; i++) {
if(values[i] > 0) {
if(i*i > n) { break; }
for(j = i*i; j < n; j += i) {
values[j] = 0;
}
}
}

result = [];
for(j = 0; j < values.length; j += 1) {
if(values[j] > 0) {
result.push(j);
}
}
return result;
};

return primes;
}();


This takes around 650 ms on the same computer.

None of these examples were tweaked or tested rigorously …

Just for fun, you can run the JavaScript on this page. The millionth prime should be 15,485,863.

See the results here …