Algorithms for Compressing Space and Time by Tomas G. Rokicki Example 1: (a) int fib(int n) { if (n < 3) return 1 ; return fib(n-1) + fib(n-2) ; } (b) int cache[] ; int fib(int n) { if (n < 3) return 1 ; if (cache[n]) return cache[n] ; return cache[n] = fib(n-1) + fib(n-2) ; } Listing One class Node { Node nextGeneration() { if (level == 2) { ... do base case through normal simulation ... } else { return new Node(nw.nextGeneration(), ne.nextGeneration(), sw.nextGeneration(), se.nextGeneration()) ; } } } Listing Two Node centeredSubnode() { return new Node(nw.se, ne.sw, sw.ne, se.nw) ; } Node centeredHorizontal(Node w, Node e) { return new Node(w.ne.se, e.nw.sw, w.se.ne, e.sw.nw) ; } Node centeredVertical(Node n, Node s) { return new Node(n.sw.se, n.se.sw, s.nw.ne, s.ne.nw); } Node centeredSubSubnode() { return new Node(nw.se.se, ne.sw.sw, sw.ne.ne, se.nw.nw) ; } Node nextGeneration() { if (level == 2) { ... do base case through normal simulation ... } else { Node n00 = nw.centeredSubnode(), n01 = centeredHorizontal(nw, ne), n02 = ne.centeredSubnode(), n10 = centeredVertical(nw, sw), n11 = centeredSubSubnode(), n12 = centeredVertical(ne, se), n20 = sw.centeredSubnode(), n21 = centeredHorizontal(sw, se), n22 = se.centeredSubnode() ; return new Node( new Node(n00, n01, n10, n11).nextGeneration(), new Node(n01, n02, n11, n12).nextGeneration(), new Node(n10, n11, n20, n21).nextGeneration(), new Node(n11, n12, n21, n22).nextGeneration()); } } Listing Three Node horizontalForward(Node w, Node e) { return node(w.ne, e.nw, w.se, e.sw).nextGeneration(); } Node verticalForward(Node n, Node s) { return node(n.sw, n.se, s.nw, s.ne).nextGeneration(); } Node centeredForward() { return node(nw.se, ne.sw, sw.ne, se.nw).nextGeneration() ; } Node nextGeneration() { if (level == 2) { ... do base case through normal simulation ... } else { Node n00 = nw.nextGeneration(), n01 = horizontalForward(nw, ne), n02 = ne.nextGeneration(), n10 = verticalForward(nw, sw), n11 = centeredForward(), n12 = verticalForward(ne, se), n20 = sw.nextGeneration(), n21 = horizontalForward(sw, se), n22 = se.nextGeneration() ; return new Node( new Node(n00, n01, n10, n11).nextGeneration(), new Node(n01, n02, n11, n12).nextGeneration(), new Node(n10, n11, n20, n21).nextGeneration(), new Node(n11, n12, n21, n22).nextGeneration()); } } 2