- 21:55 Watched 2007's Die Falscher: at last, a good movie. #
Yet another rarely read blog.
Posted by zmeeagain from twitter at 5:55 am View Comments
Posted by zmeeagain from twitter at 1:33 pm View Comments
Posted by zmeeagain from twitter at 12:59 pm View Comments
Posted by zmeeagain from twitter at 1:05 pm View Comments
I've been using ISO paper sizes (A4, A3, etc) since the dawn of (my) time. I have even worked for a company that makes optimisation software for paper industries around the world. But I have never ever questioned the rationale behind their sizes. Well it turns out that the sizes are such that if you take any paper sheet of size An and fold it across the long side you'll get two An+1 sheets. So, if An has (x, y) dimensions, then An+1 will have (y, x/2) dimensions. Keeping the ratio constant gives us x/y = y/(x/2) or x/y = 2y/x hence x/y=sqrt(2)! So the aspect ratio of the sides for every paper size is 1:sqrt(2). In other words the ratio of any square's side to its diagonal. Given that A0 has this ratio and an area of 1sqm, we get the (0.841, 1.189) dimensions, and applying the halving process we reach the all too familiar A4 paper size of (0.210, 0.297). Since the sqrt(2) is an irrational number (1,4142135623730950488016887242097...), actual dimensions are always rounded to the nearest millimetre; there's even a type for calculating that but I'll spare you the details. ISO 216 has all that and more. For a free summary of the standard you may also have a look at Wikipedia.
Posted by zmeeagain at 11:40 am 0 comments
A friend of mine wondered the other day if one-pass tree pruning was possible. Common sense dictates that a two-pass algorithm is required to first mark the subtrees due for deletion and then a second walk to do the actual pruning. I have since revisited this thought and managed to get a one-pass proof of concept:
boolean pruneTree(Iterator parentIterator, TreeNode root, Set allowedNodeValues) {
boolean pruneNode = true;
Iterator nodeIterator = root.getChildren().iterator();
while (nodeIterator.hasNext()) {
TreeNode child = nodeIterator.next();
pruneNode = pruneTree(nodeIterator, child, allowedNodeValues) && pruneNode;
}
pruneNode = pruneNode && !allowedNodeValues.contains(root.getValue());
if (pruneNode && parentIterator != null) {
parentIterator.remove();
}
return pruneNode;
}
This will remove parts of the tree whose node values are of no interest to us, otherwise retaining the same structural properties as those of the original tree:
a a
/ \ / \
b c b c
/ \ | \ pruneTree(it, a, {a,b,c,d}) --> / |
d e b e d b
| / \
c d f
To make sure this was doing what was meant to do, a pretty print method was necessary. I used indentation and nested parentheses (with slight adjustments from Knuth's original):
void printTreeIndented(TreeNode root, int currentDepth) {
System.out.println();
for (int i = 0; i < currentDepth - 1; i++) {
System.out.print("\t");
}
System.out.print(root.getValue());
currentDepth++;
for (TreeNode node : root.getChildren()) {
printTreeIndented(node, currentDepth);
}
}
void printTreeParenthesized(TreeNode root) {
System.out.print(root);
List children = root.getChildren();
if (children.size() == 0) {
return;
}
System.out.print('(');
printTreeParenthesized(children.get(0));
for (int i = 1; i < children.size(); i++) {
System.out.print(", ");
printTreeParenthesized(children.get(i));
}
System.out.print(')');
}
The first one will print:
a
b
d
e
c
c
b
e
d
f
while the second will print a(b(d, e(c)), c(b, e(d, f)))
.
Unfortunately after some runs I realised that the tree pruning algorithm's performance is dismal; it prunes by removing nodes bottom up, one by one. After all, it has no other way of knowing what to prune in one pass. Compare this with a fast mark pass and a second prune pass whereby whole sub-trees can be pruned. What's worse, as it's written, it runs out of heap space for large trees with more than a handful of levels. I guess the question is, does anybody know of any fast one-pass tree pruning algorithm?
Posted by zmeeagain at 5:58 pm View Comments
Posted by zmeeagain from twitter at 1:12 pm View Comments
Posted by zmeeagain from twitter at 1:07 pm View Comments
Posted by zmeeagain from twitter at 1:23 pm View Comments
Posted by zmeeagain from twitter at 1:22 pm View Comments