Index: java/src/org/apache/fop/prototype/breaking/LegalBreakHandler.java
===================================================================
--- java/src/org/apache/fop/prototype/breaking/LegalBreakHandler.java	(revision 1)
+++ java/src/org/apache/fop/prototype/breaking/LegalBreakHandler.java	(revision 20)
@@ -22,6 +22,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.LinkedList;
 
 import org.apache.fop.prototype.breaking.FeasibleBreaks.BestBreak;
 import org.apache.fop.prototype.breaking.layout.Layout;
@@ -67,7 +68,8 @@
             BestBreak<L> best = feasibleBreaks.getBest();
             if (best != null) {
                 completedParts.add(new CompletedPart(best.layout, best.demerits, best.difference,
-                        feasibleBreaks.getAlternatives()));
+                        //feasibleBreaks.getAlternatives()));
+                		new LinkedList<Alternative>()));
             }
         }
         return completedParts;
Index: java/src/org/apache/fop/prototype/breaking/layout/LineLayout.java
===================================================================
--- java/src/org/apache/fop/prototype/breaking/layout/LineLayout.java	(revision 1)
+++ java/src/org/apache/fop/prototype/breaking/layout/LineLayout.java	(revision 20)
@@ -117,4 +117,9 @@
         throw new UnsupportedOperationException();
     }
 
+    /** {@inheritDoc} */
+    @Override
+    public Layout getAncestor(int pageNo) {
+    	return blockLayout.getAncestor(pageNo);
+    }
 }
Index: java/src/org/apache/fop/prototype/breaking/layout/Layout.java
===================================================================
--- java/src/org/apache/fop/prototype/breaking/layout/Layout.java	(revision 1)
+++ java/src/org/apache/fop/prototype/breaking/layout/Layout.java	(revision 20)
@@ -57,4 +57,11 @@
     void setPage(); // TODO page-specific
 
     boolean isPage();
+    
+    Layout getPrevPage();
+    
+    void setPrevPage(Layout prevPage);
+    
+    /** Return the ancestor layout that represents the page pageNo */
+    Layout getAncestor(int pageNo);
 }
\ No newline at end of file
Index: java/src/org/apache/fop/prototype/breaking/layout/StandardLayout.java
===================================================================
--- java/src/org/apache/fop/prototype/breaking/layout/StandardLayout.java	(revision 1)
+++ java/src/org/apache/fop/prototype/breaking/layout/StandardLayout.java	(revision 20)
@@ -82,12 +82,15 @@
     protected int dimension;
 
     private boolean isPage;
+    
+    protected Layout prevPage;
 
     /** Creates an empty layout starting on part 0. */
     StandardLayout(int dimension) {
         progress = new ProgressInfo();
         elements = new LinkedList<KnuthElement>();
         this.dimension = dimension;
+        prevPage = null;
     }
 
     StandardLayout(Layout previous, int partNumber, int dimension) {
@@ -95,6 +98,7 @@
         this.previous = previous;
         this.demerits = previous.getDemerits();
         this.progress.setPartNumber(partNumber);
+        prevPage = previous.getPrevPage();
     }
 
     /** {@inheritDoc} */
@@ -105,6 +109,7 @@
             c.progress = new ProgressInfo(progress);
             c.alternatives = alternatives;
             c.elements = new LinkedList<KnuthElement>(elements);
+            c.prevPage = prevPage;
             return c;
         } catch (CloneNotSupportedException e) {
             throw new RuntimeException(e);
@@ -195,4 +200,24 @@
     public void setPage() {
         isPage = true;
     }
+
+	public Layout getPrevPage() {
+		return prevPage;
+	}
+
+	public void setPrevPage(Layout prevPage) {
+		this.prevPage = prevPage;
+	}
+	
+    /** {@inheritDoc} */
+	public Layout getAncestor(int pageNo) {
+		Layout retval = prevPage;
+		int actualPage = getProgress().getPartNumber();
+		
+		while (actualPage-- > pageNo) {
+			retval = retval.getPrevPage();
+		}
+		
+		return retval;
+	}
 }
Index: java/src/org/apache/fop/prototype/LayoutEngine.java
===================================================================
--- java/src/org/apache/fop/prototype/LayoutEngine.java	(revision 1)
+++ java/src/org/apache/fop/prototype/LayoutEngine.java	(revision 20)
@@ -47,32 +47,117 @@
         pageDims.add(new PageDimensions(16000, 8000));
         pageDims.add(new PageDimensions(13000, 8000));
         pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
+        pageDims.add(new PageDimensions(16000, 8000));
         layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "In", "olden",
                 "times", "when", "wishing", "still", "helped", "one,", "there", "lived", "a",
-                "king", "whose", "daughters", "were", "all", "beautiful,", "soooo", "much",
+                "king", "whose", "daughters", "were", "all", "beau", "tiful,", "soooo", "much",
                 "beautiful.")));
         layoutManagers.add(new BlockLayoutManager(
                 Penalty.DEFAULT_PENALTY, new Glue(1000, 1000, 0)));
         layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "In", "olden",
                 "times", "when", "wishing", "still", "helped", "one,", "there", "lived", "a",
+                "king", "whose", "daughters", "were", "all", "beau", "tiful,", "but", "the",
+                "youngest", "was", "so", "beautiful", "that", "the", "sun", "itself,", "which",
+                "has", "seen", "so", "much,", "was", "as","tonished", "whenever", "it", "shone", "in",
+                "her", "face.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "And", "now",
+                "I", "am", "about", "to", "start", "t-", "he", "ne-", "xt", "para-", "graph", "and", "the",
+                "goal", "is", "to", "check", "that", "the", "algorithm", "is", "working",
+                "properly,", "a", "thing", "I", "am", "not", "quite", "sure", "of.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "In", "olden",
+                "times", "when", "wishing", "still", "helped", "one,", "there", "lived", "a",
                 "king", "whose", "daughters", "were", "all", "beautiful,", "but", "the",
                 "youngest", "was", "so", "beautiful", "that", "the", "sun", "itself,", "which",
-                "has", "seen", "so", "much,", "was", "astonished", "whenever", "it", "shone", "innnnnnnnnnn",
-                "herrrrrrrrrr", "face.")));
-//        layoutManagers.add(new BlockLayoutManager(
-//                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
-//        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "And", "now",
-//                "I", "am", "about", "to", "start", "the", "next", "paragraph", "and", "the",
-//                "goal", "is", "to", "check", "that", "the", "algorithm", "is", "working",
-//                "properly,", "a", "thing", "I", "am", "not", "quite", "sure", "of.")));
-//        layoutManagers.add(new BlockLayoutManager(
-//                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
-//        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "In", "olden",
-//                "times", "when", "wishing", "still", "helped", "one,", "there", "lived", "a",
-//                "king", "whose", "daughters", "were", "all", "beautiful,", "but", "the",
-//                "youngest", "was", "so", "beautiful", "that", "the", "sun", "itself,", "which",
-//                "has", "seen", "so", "much,", "was", "astonished", "whenever", "it", "shone", "in",
-//                "her", "face.")));
+                "has", "seen", "so", "much,", "was", "astonished", "whenever", "it", "shone", "in",
+                "her", "face.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "And", "now",
+                "I", "am", "about", "to", "start", "t-", "he", "ne-", "xt", "para-", "graph", "and", "the",
+                "goal", "is", "to", "check", "that", "the", "algorithm", "is", "working",
+                "properly,", "a", "thing", "I", "am", "not", "quite", "sure", "of.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "In", "olden",
+                "times", "when", "wishing", "still", "helped", "one,", "there", "lived", "a",
+                "king", "whose", "daughters", "were", "all", "beautiful,", "but", "the",
+                "youngest", "was", "so", "beautiful", "that", "the", "sun", "itself,", "which",
+                "has", "seen", "so", "much,", "was", "astonished", "whenever", "it", "shone", "in",
+                "her", "face.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "And", "now",
+                "I", "am", "about", "to", "start", "t-", "he", "ne-", "xt", "para-", "graph", "and", "the",
+                "goal", "is", "to", "check", "that", "the", "algorithm", "is", "working",
+                "properly,", "a", "thing", "I", "am", "not", "quite", "sure", "of.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "In", "olden",
+                "times", "when", "wishing", "still", "helped", "one,", "there", "lived", "a",
+                "king", "whose", "daughters", "were", "all", "beautiful,", "but", "the",
+                "youngest", "was", "so", "beautiful", "that", "the", "sun", "itself,", "which",
+                "has", "seen", "so", "much,", "was", "astonished", "whenever", "it", "shone", "in",
+                "her", "face.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "And", "now",
+                "I", "am", "about", "to", "start", "t-", "he", "ne-", "xt", "para-", "graph", "and", "the",
+                "goal", "is", "to", "check", "that", "the", "algorithm", "is", "working",
+                "properly,", "a", "thing", "I", "am", "not", "quite", "sure", "of.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "In", "olden",
+                "times", "when", "wishing", "still", "helped", "one,", "there", "lived", "a",
+                "king", "whose", "daughters", "were", "all", "beautiful,", "but", "the",
+                "youngest", "was", "so", "beautiful", "that", "the", "sun", "itself,", "which",
+                "has", "seen", "so", "much,", "was", "astonished", "whenever", "it", "shone", "in",
+                "her", "face.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "And", "now",
+                "I", "am", "about", "to", "start", "t-", "he", "ne-", "xt", "para-", "graph", "and", "the",
+                "goal", "is", "to", "check", "that", "the", "algorithm", "is", "working",
+                "properly,", "a", "thing", "I", "am", "not", "quite", "sure", "of.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "In", "olden",
+                "times", "when", "wishing", "still", "helped", "one,", "there", "lived", "a",
+                "king", "whose", "daughters", "were", "all", "beautiful,", "but", "the",
+                "youngest", "was", "so", "beautiful", "that", "the", "sun", "itself,", "which",
+                "has", "seen", "so", "much,", "was", "astonished", "whenever", "it", "shone", "in",
+                "her", "face.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "And", "now",
+                "I", "am", "about", "to", "start", "t-", "he", "ne-", "xt", "para-", "graph", "and", "the",
+                "goal", "is", "to", "check", "that", "the", "algorithm", "is", "working",
+                "properly,", "a", "thing", "I", "am", "not", "quite", "sure", "of.")));
+        layoutManagers.add(new BlockLayoutManager(
+                Penalty.DEFAULT_PENALTY, new Glue(2000, 0, 1000)));
+        layoutManagers.add(new ParagraphLayoutManager(new Paragraph(Font.TIMES_FONT, "In", "olden",
+                "times", "when", "wishing", "still", "helped", "one,", "there", "lived", "a",
+                "king", "whose", "daughters", "were", "all", "beautiful,", "but", "the",
+                "youngest", "was", "so", "beautiful", "that", "the", "sun", "itself,", "which",
+                "has", "seen", "so", "much,", "was", "astonished", "whenever", "it", "shone", "in",
+                "her", "face.")));
     }
 
 
@@ -109,6 +194,7 @@
         layoutManagers.add(new BlockLayoutManager(new Glue(0, 1000000, 0),
                 new Penalty(0, -Penalty.INFINITE)));
 
-        new PageLayoutManager(layoutManagers, pageDims).findBreaks();
+        final int pageDepth = 4; // trunk layout tree at n - pageDepth pages
+        new PageLayoutManager(layoutManagers, pageDims, pageDepth).findBreaks();
     }
 }
Index: java/src/org/apache/fop/prototype/layoutmgr/BlockLayoutManager.java
===================================================================
--- java/src/org/apache/fop/prototype/layoutmgr/BlockLayoutManager.java	(revision 1)
+++ java/src/org/apache/fop/prototype/layoutmgr/BlockLayoutManager.java	(revision 20)
@@ -22,7 +22,6 @@
 import java.util.Arrays;
 import java.util.Collection;
 
-import org.apache.fop.prototype.Dot;
 import org.apache.fop.prototype.breaking.ActiveLayouts;
 import org.apache.fop.prototype.breaking.CompletedPart;
 import org.apache.fop.prototype.breaking.KnuthIterator;
@@ -60,6 +59,12 @@
                 Layout newLayout = parent.partCompleted(part);
                 layouts.add(newLayout);
             }
+            
+            int pageNo = parent.checkPrune();
+        	
+        	if (pageNo != -1) {
+        		prune(layouts,pageNo);
+        	}
         }
     }
 
Index: java/src/org/apache/fop/prototype/layoutmgr/PageLayoutManager.java
===================================================================
--- java/src/org/apache/fop/prototype/layoutmgr/PageLayoutManager.java	(revision 1)
+++ java/src/org/apache/fop/prototype/layoutmgr/PageLayoutManager.java	(revision 20)
@@ -37,15 +37,22 @@
 public class PageLayoutManager extends DefaultLayoutManager {
 
     private List<PageDimensions> pageDims;
+    
+    private int lastFinalizedPageNo;
+    
+    private int actualPageNo;
+    
+    private final int pageTreeDepth;
 
-    /**
-     * @param childLMs
-     */
-    public PageLayoutManager(List<LayoutManager<Layout>> childLMs, final List<PageDimensions> pageDims) {
+
+    public PageLayoutManager(List<LayoutManager<Layout>> childLMs, final List<PageDimensions> pageDims, int pageTreeDepth) {
         super(childLMs);
         this.pageDims = pageDims;
         ActiveLayouts<Layout> layouts = new ActiveLayouts<Layout>();
         layouts.add(new BlockLayout(pageDims.get(0)));
+        this.pageTreeDepth = pageTreeDepth;
+        actualPageNo = 0;
+        lastFinalizedPageNo = 0;
         initialize(this, layouts);
     }
 
@@ -63,20 +70,36 @@
         Layout previous = part.getLayout().clone(alternatives);
         previous.setPage();
         previous.setDemerits(part.getDemerits());
-        int partNumber = previous.getProgress().getPartNumber() + 1;
+        actualPageNo = previous.getProgress().getPartNumber();
+        int partNumber = actualPageNo + 1;
         Layout newLayout = new BlockLayout(previous, partNumber, pageDims.get(partNumber));
+        newLayout.setPrevPage(previous);
+        
         return newLayout;
     }
+    
+    /** {@inheritDoc} */
+    @Override
+    public int checkPrune() {
+    	if ((actualPageNo - lastFinalizedPageNo) > pageTreeDepth){
+    		return ++lastFinalizedPageNo;
+    	} else {
+    		return -1;
+    	}
+    }
 
     public void findBreaks() {
         while (hasNext()) {
             next();
         }
-        try {
-            Dot.createGraph(layouts,10000);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+        System.gc();
+        String mem = new String("Mem used:\t" + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
+        System.err.println(mem);
+//        try {
+//            Dot.createGraph(layouts,10000);
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
     }
 
 }
Index: java/src/org/apache/fop/prototype/layoutmgr/NonLeafLayoutManager.java
===================================================================
--- java/src/org/apache/fop/prototype/layoutmgr/NonLeafLayoutManager.java	(revision 1)
+++ java/src/org/apache/fop/prototype/layoutmgr/NonLeafLayoutManager.java	(revision 20)
@@ -28,5 +28,8 @@
 public interface NonLeafLayoutManager<L extends Layout> extends LayoutManager<L> {
 
     Layout partCompleted(CompletedPart part);
+    
+    /** Check if it's time to prune the layouts tree */
+    int checkPrune();
 
 }
Index: java/src/org/apache/fop/prototype/layoutmgr/AbstractLayoutManager.java
===================================================================
--- java/src/org/apache/fop/prototype/layoutmgr/AbstractLayoutManager.java	(revision 1)
+++ java/src/org/apache/fop/prototype/layoutmgr/AbstractLayoutManager.java	(revision 20)
@@ -19,6 +19,8 @@
 
 package org.apache.fop.prototype.layoutmgr;
 
+import java.util.LinkedList;
+
 import org.apache.fop.prototype.breaking.ActiveLayouts;
 import org.apache.fop.prototype.breaking.layout.Layout;
 
@@ -30,5 +32,36 @@
     protected NonLeafLayoutManager<Layout> parent;
 
     protected ActiveLayouts<L> layouts;
+    
+    /**
+     * Prune the layouts tree whose leaf are those in al param by choosing as
+     * new root the best layout for the page pageNo.
+     * 
+     * @param <L2>		the type of layout object in al
+     * @param al		the layouts tree
+     * @param pageNo	the page number that will become the new root
+     */
+    public <L2 extends Layout> void prune(ActiveLayouts<L2> al, int pageNo) {
+    	LinkedList<L2> newLayouts = new LinkedList<L2>();
+		Layout bestLayout = null;
+		
+		for (L2 l: al) {
+			Layout pageLayout = l.getAncestor(pageNo); // Get the page
+			// First iteration or new best layout found
+			if ((bestLayout == null) || (pageLayout.getDemerits() < bestLayout.getDemerits())) {
+				bestLayout = pageLayout;
+				newLayouts.clear();
+				newLayouts.add(l);
+			} else if (pageLayout == bestLayout) {
+				newLayouts.add(l);
+			}
+		}
+		
+		al.clear();
+		for (L2 nl: newLayouts) {
+			al.add(nl);
+		}
+    	
+    }
 
 }
Index: java/src/org/apache/fop/prototype/layoutmgr/DefaultLayoutManager.java
===================================================================
--- java/src/org/apache/fop/prototype/layoutmgr/DefaultLayoutManager.java	(revision 1)
+++ java/src/org/apache/fop/prototype/layoutmgr/DefaultLayoutManager.java	(revision 20)
@@ -74,4 +74,7 @@
         return parent.partCompleted(part);
     }
 
+    public int checkPrune() {
+    	return parent.checkPrune();
+    }
 }
\ No newline at end of file
Index: java/src/org/apache/fop/prototype/layoutmgr/ParagraphLayoutManager.java
===================================================================
--- java/src/org/apache/fop/prototype/layoutmgr/ParagraphLayoutManager.java	(revision 1)
+++ java/src/org/apache/fop/prototype/layoutmgr/ParagraphLayoutManager.java	(revision 20)
@@ -109,11 +109,14 @@
             newBlockLayout.addElement(lineBox);
             newLayouts.add(newBlockLayout);
         }
+        
+        boolean newPage = false;
         // Don't try to break if the end of the paragraph has been reached
         if (lineIterator.hasNext()) {
             Collection<CompletedPart> blocks = blockLevelBreakHandler.considerBreak(
                     Penalty.DEFAULT_PENALTY, newLayouts);
             for (CompletedPart block: blocks) {
+            	newPage = true;
                 Layout newBlockLayout = parent.partCompleted(block);
                 LineLayout newLineLayout = new LineLayout(newBlockLayout);
                 lineLayouts.add(newLineLayout);
@@ -122,5 +125,20 @@
         for (Layout blockLayout: newLayouts) {
             lineLayouts.add(new LineLayout(blockLayout)); // TODO line number
         }
+        
+        if (newPage) {
+        	int pageNo = parent.checkPrune();
+        	
+        	if (pageNo != -1) {
+        		prune(lineLayouts, pageNo);
+        	}
+        }
+
+        
+//        if (!lineIterator.hasNext()) {
+//            System.gc();
+//            String mem = new String("Mem used:\t" + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
+//            System.err.println(mem);
+//        }
     }
 }
