Advertisement
Guest User

Untitled

a guest
Mar 29th, 2018
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 57.63 KB | None | 0 0
  1. package de.unikiel.npr.thorup.ds;
  2.  
  3. import java.util.Iterator;
  4. import java.util.NoSuchElementException;
  5.  
  6. /**
  7.  * An implementation of <i>Harold N. Gabow</i>'s split-findmin structure, using
  8.  * superelements and sublists.
  9.  *
  10.  * @author
  11.  *      <a href="mailto:npr@informatik.uni-kiel.de">Nick Pr&uuml;hs</a>
  12.  * @version
  13.  *      1.0, 09/17/09
  14.  * @param <T>
  15.  *      the type of the items managed by this split-findmin structure
  16.  */
  17. public class SplitFindminStructureGabow<T> implements SplitFindminStructure<T> {
  18.     /**
  19.      * The index of this list. Every sublist has an index that equals the index
  20.      * of this list reduces by one.
  21.      */
  22.     private int i;
  23.    
  24.     /**
  25.      * The smallest cost of an element of this list.
  26.      */
  27.     private double cost;
  28.    
  29.     /**
  30.      * The elements of this list.
  31.      */
  32.     private MyList<Element<T>> elements;
  33.    
  34.     /**
  35.      * The left-overs of this list.
  36.      */
  37.     private MyList<Element<T>> singletonElements;
  38.    
  39.     /**
  40.      * The singleton superelements of this list.
  41.      */
  42.     private MyList<Superelement<T>> singletonSuperelements;
  43.    
  44.     /**
  45.      * The sublists of this list.
  46.      */
  47.     private MyList<SplitFindminStructureGabow<Superelement<T>>> sublists;
  48.    
  49.     /**
  50.      * The list containing this sublist, if this is a sublist, and
  51.      * <code>null</code> otherwise.
  52.      */
  53.     @SuppressWarnings("unchecked")
  54.     private SplitFindminStructureGabow containingList;
  55.    
  56.     /**
  57.      * The table of values of <i>Ackermann</i>'s function <i>A(i, j)</i> that
  58.      * are <i>n</i> or less, where <i>n</i> is the number of elements of the
  59.      * original split-findmin structure.
  60.      */
  61.     private AckermannTable ackermann;
  62.    
  63.     /**
  64.      * The container holding this sublist, if this is a sublist, and
  65.      * <code>null</code> otherwise.
  66.      */
  67.     private MyList.Container<SplitFindminStructureGabow<T>>
  68.         containingContainerSublists;
  69.    
  70.     /**
  71.      * Constructs a new split-findmin structure for processing a universe of
  72.      * <i>n</i> elements. If the number of <i>decreasecosts m</i> is known in
  73.      * advance, choose {@link #SplitFindminStructureGabow(int, int)} instead.
  74.      *
  75.      * @param n
  76.      *      the number of elements to be held by the new structure
  77.      */
  78.     public SplitFindminStructureGabow(int n) {
  79.         this(n, n);
  80.     }
  81.    
  82.     /**
  83.      * Constructs a new split-findmin structure for processing a universe of
  84.      * <i>n</i> elements with <i>m decreasecosts</i>.
  85.      *
  86.      * @param n
  87.      *      the number of elements to be held by the new structure
  88.      * @param m
  89.      *      the number of <i>decreasecosts</i> to be supported
  90.      */
  91.     public SplitFindminStructureGabow(int n, int m) {
  92.         this();
  93.  
  94.         ackermann = new AckermannTable(n);
  95.         i = ackermann.getInverse(m, n);
  96.     }
  97.    
  98.     /**
  99.      * Constructs a new list with the passed table of values of
  100.      * <i>Ackermann</i>'s function and the specified index.
  101.      *
  102.      * @param ackermann
  103.      *      the table of values of <i>Ackermann</i>'s function to be used by
  104.      *      the new list
  105.      * @param i
  106.      *      the index of the new list
  107.      */
  108.     private SplitFindminStructureGabow(AckermannTable ackermann, int i) {
  109.         this();
  110.        
  111.         this.ackermann = ackermann;
  112.         this.i = i;
  113.     }
  114.    
  115.     /**
  116.      * Constructs a new, empty split-findmin structure.
  117.      */
  118.     private SplitFindminStructureGabow() {
  119.         elements = new MyList<Element<T>>();
  120.         singletonElements = new MyList<Element<T>>();
  121.         singletonSuperelements = new MyList<Superelement<T>>();
  122.         sublists = new MyList<SplitFindminStructureGabow<Superelement<T>>>();
  123.     }
  124.    
  125.    
  126.     /**
  127.      * Returns <code>true</code>, if this list is a sublist, and
  128.      * <code>false</code> otherwise.
  129.      *
  130.      * @return
  131.      *      <code>true</code>, if this list is a sublist, and<br>
  132.      *      <code>false</code> otherwise
  133.      */
  134.     public boolean isSublist() {
  135.         return containingList != null;
  136.     }
  137.    
  138.     /**
  139.      * Adds the passed item with the specified cost to this split-findmin
  140.      * structure.
  141.      *
  142.      * @param item
  143.      *      the item to add
  144.      * @param cost
  145.      *      the cost of the item to add
  146.      * @return
  147.      *      the container holding the passed item
  148.      */
  149.     public Element<T> add(T item, double cost) {
  150.         Element<T> e = new Element<T>(item, cost);
  151.         MyList.Container<Element<T>> container = elements.add(e);
  152.         e.containingContainer = container;
  153.         return e;
  154.     }
  155.    
  156.     /**
  157.      * Initializes this split-findmin structure, preparing it for
  158.      * <code>decreaseCost(newCost)</code> and <code>split()</code> calls on its
  159.      * elements.
  160.      */
  161.     public void initialize() {
  162.         initializeHead();
  163.     }
  164.    
  165.     /**
  166.      * Scans the elements of this list right-to-left, partitioning them into
  167.      * superelements, sublists and singletons. Initializes all sublists the
  168.      * same way then.
  169.      */
  170.     private void initializeHead() {
  171.         // scan list right-to-left
  172.         MyList.Container<Element<T>> current = elements.lastContainer;
  173.        
  174.         // compute c(L) and the size of this list
  175.         cost = Double.POSITIVE_INFINITY;
  176.         int size = 0;
  177.        
  178.         while (current != elements.leftSentinel) {
  179.             size++;
  180.             cost = Math.min(cost, current.item.cost);
  181.             current = current.predecessor;
  182.         }
  183.        
  184.         // partition this list into superelements, sublists and singletons
  185.         current = elements.lastContainer;
  186.         int processedElements = 0;
  187.         int superelementsInCurrentSublist = 0;
  188.         Superelement<T> mostRecentSuperelement = null;
  189.         Superelement<T> currentSuperelement = null;
  190.         SplitFindminStructureGabow<Superelement<T>> currentLevelSublist =
  191.             new SplitFindminStructureGabow<Superelement<T>>(ackermann, i - 1);
  192.        
  193.         // check whether there are enough elements remaining for a superelement
  194.         while (size - processedElements > 3) {
  195.             // compute the level of the next superelement
  196.             int level = ackermann.getInverse(i, size - processedElements);
  197.            
  198.             // construct a new superelement
  199.             currentSuperelement = new Superelement<T>(level);
  200.             currentSuperelement.cost = Double.POSITIVE_INFINITY;
  201.            
  202.             // compute the number of elements of the next superelement
  203.             int numberOfElements = 2 * ackermann.getValue(i, level);
  204.  
  205.             // add the elements to the current superelement
  206.             currentSuperelement.last = current.item;
  207.             for (int k = 0; k < numberOfElements; k++) {
  208.                 // set e(x)
  209.                 current.item.superelement = currentSuperelement;
  210.                
  211.                 // update c(e(x))
  212.                 currentSuperelement.cost =
  213.                     Math.min(currentSuperelement.cost, current.item.cost);
  214.                
  215.                 current = current.predecessor;
  216.             }
  217.             currentSuperelement.first = current.successor.item;
  218.  
  219.            
  220.             if (mostRecentSuperelement != null && mostRecentSuperelement.level
  221.                     != level) {
  222.                
  223.                 // now we have to add or reject our constructed sublist
  224.                 if (superelementsInCurrentSublist > 1) {
  225.                     MyList.Container<SplitFindminStructureGabow
  226.                         <Superelement<T>>> container =
  227.                             sublists.addFirst(currentLevelSublist);
  228.                     currentLevelSublist.containingContainerSublists = container;
  229.                    
  230.                     currentLevelSublist.containingList = this;
  231.                 } else {
  232.                     // most recent superelement is a singleton
  233.                     MyList.Container<Superelement<T>> container =
  234.                         singletonSuperelements.addFirst(mostRecentSuperelement);
  235.                     mostRecentSuperelement.
  236.                         containingContainerSingletonSuperelements = container;
  237.                    
  238.                     mostRecentSuperelement.containingList = this;
  239.                     mostRecentSuperelement.elementInSublist = null;
  240.                     mostRecentSuperelement.containingSublist = null;
  241.                 }
  242.                
  243.                 // construct a new sublist - we might need it later
  244.                 currentLevelSublist =
  245.                     new SplitFindminStructureGabow<Superelement<T>>
  246.                         (ackermann, i - 1);
  247.                 superelementsInCurrentSublist = 0;
  248.             }
  249.            
  250.             // add the current superelement to the current sublist
  251.             Element<Superelement<T>> e = currentLevelSublist.addFirst
  252.                 (currentSuperelement, currentSuperelement.cost);
  253.             currentSuperelement.elementInSublist = e;
  254.             currentSuperelement.containingSublist = currentLevelSublist;
  255.             superelementsInCurrentSublist++;
  256.            
  257.             // prepare next iteration
  258.             processedElements += numberOfElements;
  259.             mostRecentSuperelement = currentSuperelement;
  260.         }
  261.        
  262.         // process the last sublist individually, if necessary
  263.         if (superelementsInCurrentSublist > 1) {
  264.             MyList.Container<SplitFindminStructureGabow<Superelement<T>>>
  265.                 container = sublists.addFirst(currentLevelSublist);
  266.             currentLevelSublist.containingContainerSublists = container;
  267.            
  268.             currentLevelSublist.containingList = this;
  269.         } else {
  270.             if (mostRecentSuperelement != null) {
  271.                 // most recent superelement is a singleton
  272.                 MyList.Container<Superelement<T>> container =
  273.                     singletonSuperelements.addFirst(mostRecentSuperelement);
  274.                 mostRecentSuperelement.
  275.                     containingContainerSingletonSuperelements = container;
  276.                
  277.                 mostRecentSuperelement.containingList = this;
  278.                 mostRecentSuperelement.elementInSublist = null;
  279.                 mostRecentSuperelement.containingSublist = null;
  280.             }
  281.         }
  282.  
  283.         // process leftovers
  284.         while (current != elements.leftSentinel) {
  285.             MyList.Container<Element<T>> container =
  286.                 singletonElements.addFirst(current.item);
  287.             current.item.containingContainerSingletonElements = container;
  288.             current.item.containingList = this;
  289.             current = current.predecessor;
  290.         }
  291.        
  292.         // call A_{i-1} to do initialize-head on each sublist
  293.         for (SplitFindminStructureGabow<Superelement<T>> sublist : sublists) {
  294.             sublist.initializeHead();
  295.         }
  296.        
  297.     }
  298.  
  299.     /**
  300.      * Scans the elements of this list left-to-right, partitioning them into
  301.      * superelements, sublists and singletons. Initializes all sublists the
  302.      * same way then.
  303.      */
  304.     private void initializeTail() {
  305.         // scan list left-to-right
  306.         MyList.Container<Element<T>> current = elements.leftSentinel.successor;
  307.        
  308.         // compute c(L) and the size of this list
  309.         cost = Double.POSITIVE_INFINITY;
  310.         int size = 0;
  311.        
  312.         while (current != null) {
  313.             size++;
  314.             cost = Math.min(cost, current.item.cost);
  315.             current = current.successor;
  316.         }
  317.        
  318.         // partition this list into superelements, sublists and singletons
  319.         current = elements.leftSentinel.successor;
  320.         int processedElements = 0;
  321.         int superelementsInCurrentSublist = 0;
  322.         Superelement<T> mostRecentSuperelement = null;
  323.         Superelement<T> currentSuperelement = null;
  324.         SplitFindminStructureGabow<Superelement<T>> currentLevelSublist =
  325.             new SplitFindminStructureGabow<Superelement<T>>(ackermann, i - 1);
  326.        
  327.         // check whether there are enough elements remaining for a superelement
  328.         while (size - processedElements > 3) {
  329.             // compute the level of the next superelement
  330.             int level = ackermann.getInverse(i, size - processedElements);
  331.            
  332.             // construct a new superelement
  333.             currentSuperelement = new Superelement<T>(level);
  334.             currentSuperelement.cost = Double.POSITIVE_INFINITY;
  335.            
  336.             // compute the number of elements of the next superelement
  337.             int numberOfElements = 2 * ackermann.getValue(i, level);
  338.  
  339.             // add the elements to the current superelement
  340.             currentSuperelement.first = current.item;
  341.             for (int k = 0; k < numberOfElements; k++) {
  342.                 // set e(x)
  343.                 current.item.superelement = currentSuperelement;
  344.                
  345.                 // update c(e(x))
  346.                 currentSuperelement.cost =
  347.                     Math.min(currentSuperelement.cost, current.item.cost);
  348.                
  349.                 current = current.successor;
  350.             }
  351.             currentSuperelement.last = current.predecessor.item;
  352.  
  353.            
  354.             if (mostRecentSuperelement != null && mostRecentSuperelement.level
  355.                     != level) {
  356.                
  357.                 // now we have to add or reject our constructed sublist
  358.                 if (superelementsInCurrentSublist > 1) {
  359.                     MyList.Container<SplitFindminStructureGabow
  360.                         <Superelement<T>>> container =
  361.                             sublists.add(currentLevelSublist);
  362.                     currentLevelSublist.containingContainerSublists = container;
  363.                    
  364.                     currentLevelSublist.containingList = this;
  365.                 } else {
  366.                     // most recent superelement is a singleton
  367.                     MyList.Container<Superelement<T>> container =
  368.                         singletonSuperelements.add(mostRecentSuperelement);
  369.                     mostRecentSuperelement.
  370.                         containingContainerSingletonSuperelements = container;
  371.                    
  372.                     mostRecentSuperelement.containingList = this;
  373.                     mostRecentSuperelement.elementInSublist = null;
  374.                     mostRecentSuperelement.containingSublist = null;
  375.                 }
  376.                
  377.                 // construct a new sublist - we might need it later
  378.                 currentLevelSublist = new SplitFindminStructureGabow
  379.                     <Superelement<T>>(ackermann, i - 1);
  380.                 superelementsInCurrentSublist = 0;
  381.             }
  382.            
  383.             // add the current superelement to the current sublist
  384.             Element<Superelement<T>> e = currentLevelSublist.add
  385.                 (currentSuperelement, currentSuperelement.cost);
  386.             currentSuperelement.elementInSublist = e;
  387.             currentSuperelement.containingSublist = currentLevelSublist;
  388.             superelementsInCurrentSublist++;
  389.            
  390.             // prepare next iteration
  391.             processedElements += numberOfElements;
  392.             mostRecentSuperelement = currentSuperelement;
  393.         }
  394.        
  395.         // process the last sublist individually, if necessary
  396.         if (superelementsInCurrentSublist > 1) {
  397.             MyList.Container<SplitFindminStructureGabow<Superelement<T>>>
  398.                 container = sublists.add(currentLevelSublist);
  399.             currentLevelSublist.containingContainerSublists = container;
  400.            
  401.             currentLevelSublist.containingList = this;
  402.         } else {
  403.             if (mostRecentSuperelement != null) {
  404.                 // most recent superelement is a singleton
  405.                 MyList.Container<Superelement<T>> container =
  406.                     singletonSuperelements.add(mostRecentSuperelement);
  407.                 mostRecentSuperelement.
  408.                     containingContainerSingletonSuperelements = container;
  409.                
  410.                 mostRecentSuperelement.containingList = this;
  411.                 mostRecentSuperelement.elementInSublist = null;
  412.                 mostRecentSuperelement.containingSublist = null;
  413.             }
  414.         }
  415.  
  416.         // process leftovers
  417.         while (current != null) {
  418.             MyList.Container<Element<T>> container =
  419.                 singletonElements.add(current.item);
  420.             current.item.containingContainerSingletonElements = container;
  421.             current.item.containingList = this;
  422.             current = current.successor;
  423.         }
  424.        
  425.         // call A_{i-1} to do initialize-tail on each sublist
  426.         for (SplitFindminStructureGabow<Superelement<T>> sublist : sublists) {
  427.             sublist.initializeTail();
  428.         }
  429.     }
  430.  
  431.     /**
  432.      * Scans the elements of this list right-to-left from <code>first</code> to
  433.      * <code>last</code>, partitioning them into superelements, sublists and
  434.      * singletons, which are inserted into the three passed lists. Initializes
  435.      * all sublists the same way then.
  436.      *
  437.      * @param first
  438.      *      the first element to scan
  439.      * @param last
  440.      *      the last element to scan
  441.      * @param newSingletonElements
  442.      *      the list to insert the new singleton elements into
  443.      * @param newSingletonSuperelements
  444.      *      the list to insert the new singleton superelements into
  445.      * @param newSublists
  446.      *      the list to insert the new sublists into
  447.      */
  448.     private void initializeHead
  449.             (MyList.Container<Element<T>> first,
  450.             MyList.Container<Element<T>> last,
  451.             MyList<Element<T>> newSingletonElements,
  452.             MyList<Superelement<T>> newSingletonSuperelements,
  453.             MyList<SplitFindminStructureGabow<Superelement<T>>> newSublists) {
  454.        
  455.         // scan list right-to-left
  456.         MyList.Container<Element<T>> current = last;
  457.        
  458.         // compute the size of this list
  459.         int size = 0;
  460.        
  461.         while (current != first.predecessor) {
  462.             size++;
  463.             current = current.predecessor;
  464.         }
  465.        
  466.         // partition this list into superelements, sublists and singletons
  467.         current = last;
  468.         int processedElements = 0;
  469.         int superelementsInCurrentSublist = 0;
  470.         Superelement<T> mostRecentSuperelement = null;
  471.         Superelement<T> currentSuperelement = null;
  472.         SplitFindminStructureGabow<Superelement<T>> currentLevelSublist =
  473.             new SplitFindminStructureGabow<Superelement<T>>(ackermann, i - 1);
  474.        
  475.         // check whether there are enough elements remaining for a superelement
  476.         while (size - processedElements > 3) {
  477.             // compute the level of the next superelement
  478.             int level = ackermann.getInverse(i, size - processedElements);
  479.            
  480.             // construct a new superelement
  481.             currentSuperelement = new Superelement<T>(level);
  482.             currentSuperelement.cost = Double.POSITIVE_INFINITY;
  483.            
  484.             // compute the number of elements of the next superelement
  485.             int numberOfElements = 2 * ackermann.getValue(i, level);
  486.  
  487.             // add the elements to the current superelement
  488.             currentSuperelement.last = current.item;
  489.             for (int k = 0; k < numberOfElements; k++) {
  490.                 // set e(x)
  491.                 current.item.superelement = currentSuperelement;
  492.                
  493.                 // update c(e(x))
  494.                 currentSuperelement.cost =
  495.                     Math.min(currentSuperelement.cost, current.item.cost);
  496.                
  497.                 current = current.predecessor;
  498.             }
  499.             currentSuperelement.first = current.successor.item;
  500.  
  501.            
  502.             if (mostRecentSuperelement != null && mostRecentSuperelement.level
  503.                     != level) {
  504.                
  505.                 // now we have to add or reject our constructed sublist
  506.                 if (superelementsInCurrentSublist > 1) {
  507.                     MyList.Container<SplitFindminStructureGabow
  508.                         <Superelement<T>>> container =
  509.                             newSublists.addFirst(currentLevelSublist);
  510.                     currentLevelSublist.containingContainerSublists = container;
  511.                    
  512.                     currentLevelSublist.containingList = this;
  513.                 } else {
  514.                     // most recent superelement is a singleton
  515.                     MyList.Container<Superelement<T>> container =
  516.                         newSingletonSuperelements.addFirst
  517.                             (mostRecentSuperelement);
  518.                     mostRecentSuperelement.
  519.                         containingContainerSingletonSuperelements = container;
  520.                    
  521.                     mostRecentSuperelement.containingList = this;
  522.                     mostRecentSuperelement.elementInSublist = null;
  523.                     mostRecentSuperelement.containingSublist = null;
  524.                 }
  525.                
  526.                 // construct a new sublist - we might need it later
  527.                 currentLevelSublist =
  528.                     new SplitFindminStructureGabow<Superelement<T>>
  529.                         (ackermann, i - 1);
  530.                 superelementsInCurrentSublist = 0;
  531.             }
  532.            
  533.             // add the current superelement to the current sublist
  534.             Element<Superelement<T>> e = currentLevelSublist.addFirst
  535.                 (currentSuperelement, currentSuperelement.cost);
  536.             currentSuperelement.elementInSublist = e;
  537.             currentSuperelement.containingSublist = currentLevelSublist;
  538.             superelementsInCurrentSublist++;
  539.            
  540.             // prepare next iteration
  541.             processedElements += numberOfElements;
  542.             mostRecentSuperelement = currentSuperelement;
  543.         }
  544.        
  545.         // process the last sublist individually, if necessary
  546.         if (superelementsInCurrentSublist > 1) {
  547.             MyList.Container<SplitFindminStructureGabow<Superelement<T>>>
  548.                 container = newSublists.addFirst(currentLevelSublist);
  549.             currentLevelSublist.containingContainerSublists = container;
  550.            
  551.             currentLevelSublist.containingList = this;
  552.         } else {
  553.             // most recent superelement is a singleton
  554.             if (mostRecentSuperelement != null) {
  555.                 MyList.Container<Superelement<T>> container =
  556.                     newSingletonSuperelements.addFirst(mostRecentSuperelement);
  557.                 mostRecentSuperelement.
  558.                     containingContainerSingletonSuperelements = container;
  559.                
  560.                 mostRecentSuperelement.containingList = this;
  561.                 mostRecentSuperelement.elementInSublist = null;
  562.                 mostRecentSuperelement.containingSublist = null;
  563.             }
  564.         }
  565.  
  566.         // process leftovers
  567.         while (current != first.predecessor) {
  568.             MyList.Container<Element<T>> container =
  569.                 newSingletonElements.addFirst(current.item);
  570.             current.item.containingContainerSingletonElements = container;
  571.             current.item.containingList = this;
  572.             current.item.superelement = null;
  573.             current = current.predecessor;
  574.         }
  575.        
  576.         // call A_{i-1} to do initialize-head on each sublist
  577.         for (SplitFindminStructureGabow<Superelement<T>> sublist : newSublists)
  578.         {
  579.             sublist.initializeHead();
  580.         }
  581.     }
  582.    
  583.     /**
  584.      * Scans the elements of this list left-to-right from <code>first</code> to
  585.      * <code>last</code>, partitioning them into superelements, sublists and
  586.      * singletons, which are inserted into the three passed lists. Initializes
  587.      * all sublists the same way then.
  588.      *
  589.      * @param first
  590.      *      the first element to scan
  591.      * @param last
  592.      *      the last element to scan
  593.      * @param newSingletonElements
  594.      *      the list to insert the new singleton elements into
  595.      * @param newSingletonSuperelements
  596.      *      the list to insert the new singleton superelements into
  597.      * @param newSublists
  598.      *      the list to insert the new sublists into
  599.      */
  600.     private void initializeTail
  601.             (MyList.Container<Element<T>> first,
  602.             MyList.Container<Element<T>> last,
  603.             MyList<Element<T>> newSingletonElements,
  604.             MyList<Superelement<T>> newSingletonSuperelements,
  605.             MyList<SplitFindminStructureGabow<Superelement<T>>> newSublists) {
  606.            
  607.             // scan list left-to-right
  608.             MyList.Container<Element<T>> current = first;
  609.            
  610.             // compute the size of this list
  611.             int size = 0;
  612.            
  613.             while (current != last.successor) {
  614.                 size++;
  615.                 current = current.successor;
  616.             }
  617.            
  618.             // partition this list into superelements, sublists and singletons
  619.             current = first;
  620.             int processedElements = 0;
  621.             int superelementsInCurrentSublist = 0;
  622.             Superelement<T> mostRecentSuperelement = null;
  623.             Superelement<T> currentSuperelement = null;
  624.             SplitFindminStructureGabow<Superelement<T>> currentLevelSublist =
  625.                 new SplitFindminStructureGabow<Superelement<T>>
  626.                     (ackermann, i - 1);
  627.            
  628.             /*
  629.              * check whether there are enough elements remaining for a
  630.              * superelement
  631.              */
  632.             while (size - processedElements > 3) {
  633.                 // compute the level of the next superelement
  634.                 int level = ackermann.getInverse(i, size - processedElements);
  635.                
  636.                 // construct a new superelement
  637.                 currentSuperelement = new Superelement<T>(level);
  638.                 currentSuperelement.cost = Double.POSITIVE_INFINITY;
  639.                
  640.                 // compute the number of elements of the next superelement
  641.                 int numberOfElements = 2 * ackermann.getValue(i, level);
  642.  
  643.                 // add the elements to the current superelement
  644.                 currentSuperelement.first = current.item;
  645.                 for (int k = 0; k < numberOfElements; k++) {
  646.                     // set e(x)
  647.                     current.item.superelement = currentSuperelement;
  648.                    
  649.                     // update c(e(x))
  650.                     currentSuperelement.cost =
  651.                         Math.min(currentSuperelement.cost, current.item.cost);
  652.                    
  653.                     current = current.successor;
  654.                 }
  655.                 currentSuperelement.last = current.predecessor.item;
  656.  
  657.                
  658.                 if (mostRecentSuperelement != null &&
  659.                     mostRecentSuperelement.level != level) {
  660.                     // now we have to add or reject our constructed sublist
  661.                     if (superelementsInCurrentSublist > 1) {
  662.                         MyList.Container<SplitFindminStructureGabow
  663.                             <Superelement<T>>> container =
  664.                                 newSublists.add(currentLevelSublist);
  665.                         currentLevelSublist.containingContainerSublists =
  666.                             container;
  667.                        
  668.                         currentLevelSublist.containingList = this;
  669.                     } else {
  670.                         // most recent superelement is a singleton
  671.                         MyList.Container<Superelement<T>> container =
  672.                             newSingletonSuperelements.add
  673.                                 (mostRecentSuperelement);
  674.                         mostRecentSuperelement.
  675.                             containingContainerSingletonSuperelements =
  676.                                 container;
  677.                        
  678.                         mostRecentSuperelement.containingList = this;
  679.                         mostRecentSuperelement.elementInSublist = null;
  680.                         mostRecentSuperelement.containingSublist = null;
  681.                     }
  682.                    
  683.                     // construct a new sublist - we might need it later
  684.                     currentLevelSublist = new SplitFindminStructureGabow
  685.                         <Superelement<T>>(ackermann, i - 1);
  686.                     superelementsInCurrentSublist = 0;
  687.                 }
  688.                
  689.                 // add the current superelement to the current sublist
  690.                 Element<Superelement<T>> e = currentLevelSublist.add
  691.                     (currentSuperelement, currentSuperelement.cost);
  692.                 currentSuperelement.elementInSublist = e;
  693.                 currentSuperelement.containingSublist = currentLevelSublist;
  694.                 superelementsInCurrentSublist++;
  695.                
  696.                 // prepare next iteration
  697.                 processedElements += numberOfElements;
  698.                 mostRecentSuperelement = currentSuperelement;
  699.             }
  700.            
  701.             // process the last sublist individually, if necessary
  702.             if (superelementsInCurrentSublist > 1) {
  703.                 MyList.Container<SplitFindminStructureGabow<Superelement<T>>>
  704.                     container = newSublists.add(currentLevelSublist);
  705.                 currentLevelSublist.containingContainerSublists = container;
  706.                
  707.                 currentLevelSublist.containingList = this;
  708.             } else {
  709.                 if (mostRecentSuperelement != null) {
  710.                     // most recent superelement is a singleton
  711.                     MyList.Container<Superelement<T>> container =
  712.                         newSingletonSuperelements.add(mostRecentSuperelement);
  713.                     mostRecentSuperelement.
  714.                         containingContainerSingletonSuperelements = container;
  715.                    
  716.                     mostRecentSuperelement.containingList = this;
  717.                     mostRecentSuperelement.elementInSublist = null;
  718.                     mostRecentSuperelement.containingSublist = null;
  719.                 }
  720.             }
  721.  
  722.             // process leftovers
  723.             while (current != last.successor) {
  724.                 MyList.Container<Element<T>> container =
  725.                     newSingletonElements.add(current.item);
  726.                 current.item.containingContainerSingletonElements = container;
  727.                 current.item.containingList = this;
  728.                 current.item.superelement = null;
  729.                 current = current.successor;
  730.             }
  731.            
  732.             // call A_{i-1} to do initialize-tail on each sublist
  733.             for (SplitFindminStructureGabow<Superelement<T>> sublist :
  734.                     newSublists) {
  735.                
  736.                 sublist.initializeTail();
  737.             }
  738.     }
  739.    
  740.     /**
  741.      * Adds the passed item with the specified cost to the front of this
  742.      * split-findmin structure.
  743.      *
  744.      * @param item
  745.      *      the item to add
  746.      * @param cost
  747.      *      the cost of the item to add
  748.      * @return
  749.      *      the container holding the passed item
  750.      */
  751.     private Element<T> addFirst(T item, double cost) {
  752.         Element<T> e = new Element<T>(item, cost);
  753.         MyList.Container<Element<T>> container = elements.addFirst(e);
  754.         e.containingContainer = container;
  755.         return e;
  756.     }
  757.  
  758.     /**
  759.      * Returns the smallest cost of an element of this list, if this is no
  760.      * sublist, and the smallest cost of an element of the list containing this
  761.      * sublist, otherwise.
  762.      *
  763.      * @return
  764.      *      the cost of this list
  765.      */
  766.     private double getCost() {
  767.         if (containingList == null) {
  768.             return cost;
  769.         } else {
  770.             return containingList.getCost();
  771.         }
  772.     }
  773.  
  774.     /**
  775.      * An element of <i>Harold N. Gabow</i>'s split-findmin structure.
  776.      *
  777.      * @author
  778.      *      <a href="mailto:npr@informatik.uni-kiel.de">Nick Pr&uuml;hs</a>
  779.      * @version
  780.      *      1.0, 09/17/09
  781.      * @param <T>
  782.      *      the type of the item held by this element
  783.      */
  784.     public static class Element<T> implements SplitFindminStructureElement<T> {
  785.         /**
  786.          * The cost of this element.
  787.          */
  788.         private double cost;
  789.        
  790.         /**
  791.          * The superelement that contains this element, if any, and
  792.          * <code>null</code> otherwise.
  793.          */
  794.         private Superelement<T> superelement;
  795.        
  796.         /**
  797.          * The list containing this element, if this element is a left-over,
  798.          * and <code>null</code> otherwise.
  799.          */
  800.         private SplitFindminStructureGabow<T> containingList;
  801.        
  802.         /**
  803.          * The container holding this element in the list of elements of the
  804.          * list containing this element.
  805.          */
  806.         private MyList.Container<Element<T>> containingContainer;
  807.        
  808.         /**
  809.          * The container holding this element in the list of left-overs of the
  810.          * list containing this element.
  811.          */
  812.         private MyList.Container<Element<T>>
  813.             containingContainerSingletonElements;
  814.        
  815.         /**
  816.          * The item held by this element.
  817.          */
  818.         private T item;
  819.        
  820.        
  821.         /**
  822.          * Constructs a new element holding the passed item with the specified
  823.          * cost.
  824.          *  
  825.          * @param item
  826.          *      the item held by the new element
  827.          * @param cost
  828.          *      the cost of the new element
  829.          */
  830.         public Element(T item, double cost) {
  831.             this.item = item;
  832.             this.cost = cost;
  833.         }
  834.        
  835.        
  836.         /**
  837.          * Returns <code>true</code>, if this element is a singleton, and
  838.          * <code>false</code> otherwise.
  839.          *
  840.          * @return
  841.          *      <code>true</code>, if this element is a singleton, and<br>
  842.          *      <code>false</code> otherwise
  843.          */
  844.         public boolean isSingleton() {
  845.             return containingList != null ||
  846.                     (superelement != null && superelement.isSingleton());
  847.         }
  848.        
  849.         /**
  850.          * Decreases the cost of this element and updates the minimum of the
  851.          * list containing it, if necessary.
  852.          *
  853.          * @param newCost
  854.          *      the new cost of this element
  855.          * @return
  856.          *      the list containing this element
  857.          */
  858.         @SuppressWarnings("unchecked")
  859.         public SplitFindminStructureGabow<T> decreaseCost(double newCost) {
  860.             if (isSingleton()) {
  861.                 // update c(x)
  862.                 cost = Math.min(cost, newCost);
  863.                
  864.                 if (superelement != null) {
  865.                     /* x is contained by a singleton superelement */
  866.                    
  867.                     // update c(e(x))
  868.                     superelement.cost = Math.min(superelement.cost, newCost);
  869.                    
  870.                     // update c(L(x))
  871.                     superelement.containingList.cost =
  872.                         Math.min(superelement.containingList.cost, newCost);
  873.                    
  874.                     // return L(x)
  875.                     return superelement.containingList;
  876.                 } else {
  877.                     /* x is a left-over */
  878.                    
  879.                     // update c(L(x))
  880.                     containingList.cost =
  881.                         Math.min(containingList.cost, newCost);
  882.                    
  883.                     // return L(x)
  884.                     return containingList;
  885.                 }
  886.             } else {
  887.                 /* x is contained by a superelement in a sublist */
  888.                
  889.                 // call A_{i-1} to do decreasecost(e(x), d)
  890.                 SplitFindminStructureGabow<Superelement<T>> sublist =
  891.                     superelement.elementInSublist.decreaseCost(newCost);
  892.                
  893.                 // update c(e(x))
  894.                 superelement.cost = Math.min(superelement.cost, newCost);
  895.                
  896.                 // update c(x)
  897.                 cost = Math.min(cost, newCost);
  898.                
  899.                 // find L(x)
  900.                 SplitFindminStructureGabow list = sublist.containingList;
  901.                
  902.                 // update c(L(x))
  903.                 list.cost = Math.min(list.cost, newCost);
  904.                
  905.                 // return L(x)
  906.                 return list;
  907.             }
  908.         }
  909.        
  910.         /**
  911.          * Replaces the list containing this element by the list of all elements
  912.          * up to and including it, and the list of all remaining elements. The
  913.          * costs of* the two new lists are set to their proper values.
  914.          *
  915.          * @return
  916.          *      the (second) list of all remaining elements
  917.          */
  918.         @SuppressWarnings("unchecked")
  919.         public SplitFindminStructureGabow<T> split() {
  920.             //System.err.println(item);
  921.             SplitFindminStructureGabow<T> l1 = null;
  922.             SplitFindminStructureGabow<T> l2 = null;
  923.            
  924.             if (isSingleton()) {
  925.                 if (superelement == null) {
  926.                     /* this element is a left-over */
  927.                     l1 = containingList;
  928.                    
  929.                     // initialize the new list L2
  930.                     l2 = new SplitFindminStructureGabow<T>(l1.ackermann, l1.i);
  931.                     l2.singletonElements = l1.singletonElements.cutAfter
  932.                         (containingContainerSingletonElements);
  933.                    
  934.                     /*
  935.                      * look for the last singleton superelement in front of this
  936.                      * singleton
  937.                      */
  938.                     MyList.Container<Element<T>> current =
  939.                         containingContainer.predecessor;
  940.                    
  941.                     while (current != l1.elements.leftSentinel) {
  942.                         Superelement<T> se = current.item.superelement;
  943.                        
  944.                         if (se != null && se.isSingleton()) {
  945.                             l2.singletonSuperelements =
  946.                                 l1.singletonSuperelements.cutAfter
  947.                                 (se.containingContainerSingletonSuperelements);
  948.                             break;
  949.                         }
  950.                        
  951.                         current = current.predecessor;
  952.                     }
  953.                    
  954.                     if (current == l1.elements.leftSentinel) {
  955.                         l2.singletonSuperelements = l1.singletonSuperelements;
  956.                         l1.singletonSuperelements =
  957.                             new MyList<Superelement<T>>();
  958.                     }
  959.                    
  960.                     // look for the last sublist in front of this singleton
  961.                     current = containingContainer.predecessor;
  962.                    
  963.                     while (current != l1.elements.leftSentinel) {
  964.                         Superelement<T> se = current.item.superelement;
  965.                        
  966.                         if (se != null && !se.isSingleton()) {
  967.                             l2.sublists = l1.sublists.cutAfter
  968.                                 (se.containingSublist.
  969.                                         containingContainerSublists);
  970.                             break;
  971.                         }
  972.                        
  973.                         current = current.predecessor;
  974.                     }
  975.                    
  976.                     if (current == l1.elements.leftSentinel) {
  977.                         l2.sublists = l1.sublists;
  978.                         l1.sublists = new MyList<SplitFindminStructureGabow
  979.                             <Superelement<T>>>();
  980.                     }
  981.                 } else {
  982.                     /* this element is contained by a singleton superelement */
  983.                     l1 = superelement.containingList;
  984.                    
  985.                     // initialize the new list L2
  986.                     l2 = new SplitFindminStructureGabow<T>(l1.ackermann, l1.i);
  987.                    
  988.                     if (this == superelement.last) {
  989.                         // look for the last singleton in front of this one
  990.                         MyList.Container<Element<T>> current =
  991.                             containingContainer.predecessor;
  992.                        
  993.                         while (current != l1.elements.leftSentinel) {
  994.                             Element<T> e = current.item;
  995.                            
  996.                             if (e.isSingleton() && e.superelement == null) {
  997.                                 l2.singletonElements = l1.singletonElements.
  998.                                     cutAfter(e.
  999.                                         containingContainerSingletonElements);
  1000.                                 break;
  1001.                             }
  1002.                            
  1003.                             current = current.predecessor;
  1004.                         }
  1005.                        
  1006.                         if (current == l1.elements.leftSentinel) {
  1007.                             l2.singletonElements = l1.singletonElements;
  1008.                             l1.singletonElements = new MyList<Element<T>>();
  1009.                         }
  1010.                        
  1011.                         l2.singletonSuperelements =
  1012.                             l1.singletonSuperelements.cutAfter
  1013.                                 (superelement.
  1014.                                     containingContainerSingletonSuperelements);
  1015.                        
  1016.                         // look for the last sublist in front of this singleton
  1017.                         current = containingContainer.predecessor;
  1018.                        
  1019.                         while (current != l1.elements.leftSentinel) {
  1020.                             Superelement<T> se = current.item.superelement;
  1021.                            
  1022.                             if (se != null && !se.isSingleton()) {
  1023.                                 l2.sublists = l1.sublists.cutAfter
  1024.                                     (se.containingSublist.
  1025.                                         containingContainerSublists);
  1026.                                 break;
  1027.                             }
  1028.                            
  1029.                             current = current.predecessor;
  1030.                         }
  1031.                        
  1032.                         if (current == l1.elements.leftSentinel) {
  1033.                             l2.sublists = l1.sublists;
  1034.                             l1.sublists = new MyList<SplitFindminStructureGabow
  1035.                                 <Superelement<T>>>();
  1036.                         }
  1037.                     } else {
  1038.                         // look for the position to insert the new left-overs at
  1039.                         MyList.Container<Element<T>> lastSingletonElement =
  1040.                             null;
  1041.                         MyList.Container<Element<T>> current =
  1042.                             containingContainer.predecessor;
  1043.                        
  1044.                         while (current != l1.elements.leftSentinel) {
  1045.                             Element<T> e = current.item;
  1046.                            
  1047.                             if (e.isSingleton() && e.superelement == null) {
  1048.                                 lastSingletonElement =
  1049.                                     e.containingContainerSingletonElements;
  1050.                                 break;
  1051.                             }
  1052.                            
  1053.                             current = current.predecessor;
  1054.                         }
  1055.  
  1056.                         if (current == l1.elements.leftSentinel) {
  1057.                             lastSingletonElement =
  1058.                                 l1.singletonElements.leftSentinel;
  1059.                         }
  1060.                        
  1061.                         /*
  1062.                          * look for the position to insert the new singleton
  1063.                          * superelements at
  1064.                          */
  1065.                         MyList.Container<Superelement<T>>
  1066.                             lastSingletonSuperelement =
  1067.                                 superelement.
  1068.                                 containingContainerSingletonSuperelements;
  1069.                        
  1070.                         // look for the position to insert the new sublists at
  1071.                         MyList.Container<SplitFindminStructureGabow
  1072.                             <Superelement<T>>> lastSublist = null;
  1073.                         current = containingContainer.predecessor;
  1074.                        
  1075.                         while (current != l1.elements.leftSentinel) {
  1076.                             Superelement<T> se = current.item.superelement;
  1077.                            
  1078.                             if (se != null && !se.isSingleton()) {
  1079.                                 lastSublist = se.containingSublist.
  1080.                                     containingContainerSublists;
  1081.                                 break;
  1082.                             }
  1083.                            
  1084.                             current = current.predecessor;
  1085.                         }
  1086.                        
  1087.                         if (current == l1.elements.leftSentinel) {
  1088.                             lastSublist = l1.sublists.leftSentinel;
  1089.                         }
  1090.                        
  1091.                         /*
  1092.                          * remove e(x) from the list of singleton superelements
  1093.                          * of this list
  1094.                          */
  1095.                         lastSingletonSuperelement =
  1096.                             l1.singletonSuperelements.remove
  1097.                             (lastSingletonSuperelement);
  1098.  
  1099.                         /*
  1100.                          *  prepare three new lists for doing initialize-head on
  1101.                          *  the elements of e(x) up to and including x
  1102.                          */
  1103.                         MyList<Element<T>> newSingletonElements =
  1104.                             new MyList<Element<T>>();
  1105.                         MyList<Superelement<T>> newSingletonSuperelements =
  1106.                             new MyList<Superelement<T>>();
  1107.                         MyList<SplitFindminStructureGabow<Superelement<T>>>
  1108.                             newSublists =
  1109.                                 new MyList<SplitFindminStructureGabow
  1110.                                     <Superelement<T>>>();
  1111.                        
  1112.                         // remember the old superelement of this element
  1113.                         Superelement<T> oldSuperelement = superelement;
  1114.                        
  1115.                         /*
  1116.                          * do initialize-head on the elements of e(x) up to and
  1117.                          * including x
  1118.                          */
  1119.                         l1.initializeHead
  1120.                             (superelement.first.containingContainer,
  1121.                              containingContainer,
  1122.                              newSingletonElements,
  1123.                              newSingletonSuperelements,
  1124.                              newSublists);
  1125.                        
  1126.                         // concat the three lists
  1127.                         lastSingletonElement =
  1128.                             l1.singletonElements.insertListAfter
  1129.                                 (lastSingletonElement, newSingletonElements);
  1130.                         lastSingletonSuperelement =
  1131.                             l1.singletonSuperelements.insertListAfter
  1132.                                 (lastSingletonSuperelement,
  1133.                                  newSingletonSuperelements);
  1134.                         lastSublist =
  1135.                             l1.sublists.insertListAfter
  1136.                                 (lastSublist, newSublists);
  1137.                        
  1138.                         // cut the three lists of L1
  1139.                         l2.singletonElements =
  1140.                             l1.singletonElements.cutAfter(lastSingletonElement);
  1141.                         l2.singletonSuperelements =
  1142.                             l1.singletonSuperelements.cutAfter
  1143.                                 (lastSingletonSuperelement);
  1144.                         l2.sublists = l1.sublists.cutAfter(lastSublist);
  1145.                        
  1146.                         /*
  1147.                          * prepare three new lists for doing initialize-tail on
  1148.                          * the remaining elements
  1149.                          */
  1150.                         newSingletonElements = new MyList<Element<T>>();
  1151.                         newSingletonSuperelements =
  1152.                             new MyList<Superelement<T>>();
  1153.                         newSublists = new MyList<SplitFindminStructureGabow
  1154.                             <Superelement<T>>>();
  1155.  
  1156.                         // do initialize-tail on the remaining elements
  1157.                         l1.initializeTail
  1158.                             (containingContainer.successor,
  1159.                              oldSuperelement.last.containingContainer,
  1160.                              newSingletonElements,
  1161.                              newSingletonSuperelements,
  1162.                              newSublists);
  1163.                        
  1164.                         // concat the three lists
  1165.                         newSingletonElements.concat(l2.singletonElements);
  1166.                         newSingletonSuperelements.concat
  1167.                             (l2.singletonSuperelements);
  1168.                         newSublists.concat(l2.sublists);
  1169.                        
  1170.                         l2.singletonElements = newSingletonElements;
  1171.                         l2.singletonSuperelements = newSingletonSuperelements;
  1172.                         l2.sublists = newSublists;
  1173.                     }
  1174.                 }
  1175.             } else {
  1176.                 /* this element is contained by superelement in a sublist */
  1177.                 l1 = superelement.containingSublist.containingList;
  1178.                
  1179.                 // initialize the new list L2
  1180.                 l2 = new SplitFindminStructureGabow<T>(l1.ackermann, l1.i);
  1181.                
  1182.                 /*
  1183.                  * get the sublist-container of the sublist containing this
  1184.                  * element
  1185.                  */
  1186.                 MyList.Container<SplitFindminStructureGabow<Superelement<T>>>
  1187.                     containerToInsertAfter =
  1188.                         superelement.containingSublist.
  1189.                         containingContainerSublists;
  1190.                
  1191.                 // call A_{i-1} to do split(e(x))
  1192.                 SplitFindminStructureGabow<Superelement<T>> subl2 = null;
  1193.                 SplitFindminStructureGabow<Superelement<T>> subl3 =
  1194.                     superelement.elementInSublist.split();
  1195.                
  1196.                 // update the pointers of the superelements of subl3 to subl3
  1197.                 for (Element<Superelement<T>> e : subl3.elements) {
  1198.                     Superelement<T> se = e.item;
  1199.                     se.containingSublist = subl3;
  1200.                 }
  1201.                
  1202.                 // and then another split to make {e(x)} into a list
  1203.                 if (superelement.elementInSublist.containingContainer.
  1204.                         predecessor.item != null) {
  1205.                    
  1206.                     subl2 = superelement.elementInSublist.containingContainer.
  1207.                         predecessor.item.split();
  1208.                    
  1209.                     /*
  1210.                      * update the pointers of the superelements of subl2 to
  1211.                      * subl2
  1212.                      */
  1213.                     for (Element<Superelement<T>> e : subl2.elements) {
  1214.                         Superelement<T> se = e.item;
  1215.                         se.containingSublist = subl2;
  1216.                     }
  1217.                 }
  1218.                
  1219.                 /*
  1220.                  * inserted the (probably two) new sublist(s) into the list of
  1221.                  * sublists
  1222.                  */
  1223.                 if (subl2 != null) {
  1224.                     containerToInsertAfter = l1.sublists.insertAfter
  1225.                         (containerToInsertAfter, subl2);
  1226.                     subl2.containingContainerSublists = containerToInsertAfter;
  1227.                     subl2.containingList = l1;
  1228.                 }
  1229.                
  1230.                 containerToInsertAfter = l1.sublists.insertAfter
  1231.                     (containerToInsertAfter, subl3);
  1232.                 subl3.containingContainerSublists = containerToInsertAfter;
  1233.                 subl3.containingList = l1;
  1234.                
  1235.                 if (this == superelement.last) {
  1236.                     // look for the last singleton in front of this one
  1237.                     MyList.Container<Element<T>> current =
  1238.                         containingContainer.predecessor;
  1239.                    
  1240.                     while (current != l1.elements.leftSentinel) {
  1241.                         Element<T> e = current.item;
  1242.                        
  1243.                         if (e.isSingleton() && e.superelement == null) {
  1244.                             l2.singletonElements =
  1245.                                 l1.singletonElements.cutAfter
  1246.                                     (e.containingContainerSingletonElements);
  1247.                             break;
  1248.                         }
  1249.                        
  1250.                         current = current.predecessor;
  1251.                     }
  1252.                    
  1253.                     if (current == l1.elements.leftSentinel) {
  1254.                         l2.singletonElements = l1.singletonElements;
  1255.                         l1.singletonElements = new MyList<Element<T>>();
  1256.                     }
  1257.                    
  1258.                     /*
  1259.                      * look for the last singleton superelement in front of this
  1260.                      * singleton
  1261.                      */
  1262.                     current = containingContainer.predecessor;
  1263.                    
  1264.                     while (current != l1.elements.leftSentinel) {
  1265.                         Superelement<T> se = current.item.superelement;
  1266.                        
  1267.                         if (se != null && se.isSingleton()) {
  1268.                             l2.singletonSuperelements =
  1269.                                 l1.singletonSuperelements.cutAfter
  1270.                                 (se.containingContainerSingletonSuperelements);
  1271.                             break;
  1272.                         }
  1273.                        
  1274.                         current = current.predecessor;
  1275.                     }
  1276.                    
  1277.                     if (current == l1.elements.leftSentinel) {
  1278.                         l2.singletonSuperelements = l1.singletonSuperelements;
  1279.                         l1.singletonSuperelements =
  1280.                             new MyList<Superelement<T>>();
  1281.                     }
  1282.                    
  1283.                     // cut the list of sublists of this list in front of {e(x)}
  1284.                     if (subl2 != null) {
  1285.                         l2.sublists = l1.sublists.cutAfter
  1286.                             (subl2.containingContainerSublists);
  1287.                     } else {
  1288.                         l2.sublists = l1.sublists.cutAfter
  1289.                             (superelement.containingSublist.
  1290.                                 containingContainerSublists);
  1291.                     }
  1292.                 } else {
  1293.                     // look for the position to insert the new left-overs at
  1294.                     MyList.Container<Element<T>> lastSingletonElement = null;
  1295.                     MyList.Container<Element<T>> current =
  1296.                         containingContainer.predecessor;
  1297.                    
  1298.                     while (current != l1.elements.leftSentinel) {
  1299.                         Element<T> e = current.item;
  1300.                        
  1301.                         if (e.isSingleton() && e.superelement == null) {
  1302.                             lastSingletonElement =
  1303.                                 e.containingContainerSingletonElements;
  1304.                             break;
  1305.                         }
  1306.                        
  1307.                         current = current.predecessor;
  1308.                     }
  1309.                    
  1310.                     if (current == l1.elements.leftSentinel) {
  1311.                         lastSingletonElement = l1.singletonElements.
  1312.                             leftSentinel;
  1313.                     }
  1314.                    
  1315.                     /*
  1316.                      * look for the position to insert the new singleton
  1317.                      * superelements at
  1318.                      */
  1319.                     MyList.Container<Superelement<T>>
  1320.                         lastSingletonSuperelement = null;
  1321.                     current = containingContainer.predecessor;
  1322.                    
  1323.                     while (current != l1.elements.leftSentinel) {
  1324.                         Superelement<T> se = current.item.superelement;
  1325.                        
  1326.                         if (se != null && se.isSingleton()) {
  1327.                             lastSingletonSuperelement =
  1328.                                 se.containingContainerSingletonSuperelements;
  1329.                             break;
  1330.                         }
  1331.                        
  1332.                         current = current.predecessor;
  1333.                     }
  1334.                    
  1335.                     if (current == l1.elements.leftSentinel) {
  1336.                         lastSingletonSuperelement =
  1337.                             l1.singletonSuperelements.leftSentinel;
  1338.                     }
  1339.                    
  1340.                     // look for the position to insert the new sublists at
  1341.                     MyList.Container<SplitFindminStructureGabow
  1342.                         <Superelement<T>>> lastSublist =
  1343.                             superelement.containingSublist.
  1344.                                 containingContainerSublists.predecessor;
  1345.                    
  1346.                     /*
  1347.                      * prepare three new lists for doing initialize-head on the
  1348.                      * elements of e(x) up to and including x
  1349.                      */
  1350.                     MyList<Element<T>> newSingletonElements =
  1351.                         new MyList<Element<T>>();
  1352.                     MyList<Superelement<T>> newSingletonSuperelements =
  1353.                         new MyList<Superelement<T>>();
  1354.                     MyList<SplitFindminStructureGabow<Superelement<T>>>
  1355.                         newSublists =
  1356.                             new MyList<SplitFindminStructureGabow
  1357.                                 <Superelement<T>>>();
  1358.                    
  1359.                     // remember the old superelement of this element
  1360.                     Superelement<T> oldSuperelement = superelement;
  1361.                    
  1362.                     /*
  1363.                      * do initialize-head on the elements of e(x) up to and
  1364.                      * including x
  1365.                      */
  1366.                     l1.initializeHead
  1367.                         (superelement.first.containingContainer,
  1368.                          containingContainer,
  1369.                          newSingletonElements,
  1370.                          newSingletonSuperelements,
  1371.                          newSublists);
  1372.                    
  1373.                     // concat the three lists
  1374.                     lastSingletonElement = l1.singletonElements.insertListAfter
  1375.                         (lastSingletonElement, newSingletonElements);
  1376.                     lastSingletonSuperelement =
  1377.                         l1.singletonSuperelements.insertListAfter
  1378.                             (lastSingletonSuperelement,
  1379.                              newSingletonSuperelements);
  1380.                     lastSublist = l1.sublists.insertListAfter
  1381.                         (lastSublist, newSublists);
  1382.                    
  1383.                     // cut the three lists of L1
  1384.                     l2.singletonElements = l1.singletonElements.cutAfter
  1385.                         (lastSingletonElement);
  1386.                     l2.singletonSuperelements =
  1387.                         l1.singletonSuperelements.cutAfter
  1388.                             (lastSingletonSuperelement);
  1389.                     l2.sublists = l1.sublists.cutAfter(lastSublist);
  1390.                    
  1391.                     // remove the list {e(x)} from the list of sublists of l2
  1392.                     l2.sublists = l2.sublists.cutAfter
  1393.                         (l2.sublists.leftSentinel.successor);
  1394.                    
  1395.                     /*
  1396.                      * prepare three new lists for doing initialize-tail on the
  1397.                      * remaining elements
  1398.                      */
  1399.                     newSingletonElements = new MyList<Element<T>>();
  1400.                     newSingletonSuperelements = new MyList<Superelement<T>>();
  1401.                     newSublists = new MyList<SplitFindminStructureGabow
  1402.                         <Superelement<T>>>();
  1403.  
  1404.                     // do initialize-tail on the remaining elements
  1405.                     l1.initializeTail
  1406.                         (containingContainer.successor,
  1407.                          oldSuperelement.last.containingContainer,
  1408.                          newSingletonElements,
  1409.                          newSingletonSuperelements,
  1410.                          newSublists);
  1411.                    
  1412.                     // concat the three lists
  1413.                     newSingletonElements.concat(l2.singletonElements);
  1414.                     newSingletonSuperelements.concat(l2.singletonSuperelements);
  1415.                     newSublists.concat(l2.sublists);
  1416.                    
  1417.                     l2.singletonElements = newSingletonElements;
  1418.                     l2.singletonSuperelements = newSingletonSuperelements;
  1419.                     l2.sublists = newSublists;
  1420.                 }
  1421.             }
  1422.            
  1423.             // cut the list of elements of this list after this element
  1424.             l2.elements = l1.elements.cutAfter(containingContainer);
  1425.            
  1426.             // set the list containing L2, if it is a sublist
  1427.             l2.containingList = l1.containingList;
  1428.            
  1429.             // prepare computing the new costs of L1 and L2
  1430.             l1.cost = Double.POSITIVE_INFINITY;
  1431.             l2.cost = Double.POSITIVE_INFINITY;
  1432.            
  1433.             // compute the new cost of L1
  1434.             for (Element<T> e : l1.singletonElements) {
  1435.                 l1.cost = Math.min(l1.cost, e.cost);
  1436.             }
  1437.            
  1438.             for (Superelement<T> se : l1.singletonSuperelements) {
  1439.                 l1.cost = Math.min(l1.cost, se.cost);
  1440.             }
  1441.            
  1442.             for (SplitFindminStructureGabow<Superelement<T>> sublist :
  1443.                 l1.sublists) {
  1444.                
  1445.                 l1.cost = Math.min(l1.cost, sublist.cost);
  1446.             }
  1447.            
  1448.             // iterate all of the three lists of L2 and set all pointers to L2
  1449.             for (Element<T> e : l2.singletonElements) {
  1450.                 e.containingList = l2;
  1451.                 l2.cost = Math.min(l2.cost, e.cost);
  1452.             }
  1453.            
  1454.             for (Superelement<T> se : l2.singletonSuperelements) {
  1455.                 se.containingList = l2;
  1456.                 l2.cost = Math.min(l2.cost, se.cost);
  1457.             }
  1458.            
  1459.             for (SplitFindminStructureGabow<Superelement<T>> sublist :
  1460.                 l2.sublists) {
  1461.                
  1462.                 deepSetPointers(sublist, l2);
  1463.                 l2.cost = Math.min(l2.cost, sublist.cost);
  1464.             }
  1465.            
  1466.             return l2;
  1467.         }
  1468.        
  1469.         /**
  1470.          * Returns the cost of this element.
  1471.          *
  1472.          * @return
  1473.          *      the cost of this element
  1474.          */
  1475.         public double getCost() {
  1476.             return cost;
  1477.         }
  1478.        
  1479.         /**
  1480.          * Returns the cost of the list containing this element.
  1481.          *
  1482.          * @return
  1483.          *      the cost of the list containing this element
  1484.          */
  1485.         public double getListCost() {
  1486.             if (isSingleton()) {
  1487.                 if (containingList != null) {
  1488.                     return containingList.cost;
  1489.                 } else {
  1490.                     return superelement.cost;
  1491.                 }
  1492.             } else {
  1493.                 return superelement.containingSublist.getCost();
  1494.             }
  1495.         }
  1496.  
  1497.         /**
  1498.          * Sets the containing list of the passed <code>sublist</code> to
  1499.          * <code>containingList</code>. After that, properly updates the
  1500.          * pointers of all sublists contained by <code>sublist</code> the same
  1501.          * way.
  1502.          *
  1503.          * @param sublist
  1504.          *      the sublist contained by <code>containingList</code>
  1505.          * @param containingList
  1506.          *      the list containing <code>sublist</code>
  1507.          */
  1508.         @SuppressWarnings("unchecked")
  1509.         private void deepSetPointers
  1510.             (SplitFindminStructureGabow<Superelement<T>> sublist,
  1511.              SplitFindminStructureGabow containingList) {
  1512.            
  1513.             sublist.containingList = containingList;
  1514.            
  1515.             for (SplitFindminStructureGabow subsublist : sublist.sublists) {
  1516.                 deepSetPointers(subsublist, sublist);
  1517.             }
  1518.         }
  1519.     }
  1520.    
  1521.     /**
  1522.      * A superelemtn of <i>Harold N. Gabow</i>'s split-findmin structure.
  1523.      *
  1524.      * @author
  1525.      *      <a href="mailto:npr@informatik.uni-kiel.de">Nick Pr&uuml;hs</a>
  1526.      * @version
  1527.      *      1.0, 09/17/09
  1528.      * @param <T>
  1529.      *      the type of the item held by this superelement
  1530.      */
  1531.     public static class Superelement<T> {
  1532.         /**
  1533.          * The level of this superelement.
  1534.          */
  1535.         private int level;
  1536.        
  1537.         /**
  1538.          * The first element contained by this superelement.
  1539.          */
  1540.         private Element<T> first;
  1541.        
  1542.         /**
  1543.          * The last element contained by this superelement.
  1544.          */
  1545.         private Element<T> last;
  1546.        
  1547.         /**
  1548.          * The smallest cost of an element of this superelement.
  1549.          */
  1550.         private double cost;
  1551.        
  1552.         /**
  1553.          * The list containing this superelement, if this superelement is a
  1554.          * singleton, and <code>null</code> otherwise.
  1555.          */
  1556.         private SplitFindminStructureGabow<T> containingList;
  1557.  
  1558.         /**
  1559.          * The container holding this superelement in the list of singleton
  1560.          * superelements of the list containing this superelement.
  1561.          */
  1562.         private MyList.Container<Superelement<T>>
  1563.             containingContainerSingletonSuperelements;
  1564.        
  1565.         /**
  1566.          * The sublist element holding this superelement, if this superelement
  1567.          * is no singleton.
  1568.          */
  1569.         private Element<Superelement<T>> elementInSublist;
  1570.        
  1571.         /**
  1572.          * The sublist holding this superelement, if this superelement is no
  1573.          * singleton.
  1574.          */
  1575.         private SplitFindminStructureGabow<Superelement<T>> containingSublist;
  1576.        
  1577.        
  1578.         /**
  1579.          * Constructs a new superelement of the specified level.
  1580.          *
  1581.          * @param level
  1582.          *      the level of the new superelement
  1583.          */
  1584.         public Superelement(int level) {
  1585.             this.level = level;
  1586.         }
  1587.        
  1588.        
  1589.         /**
  1590.          * Returns <code>true</code>, if this superelement is a singleton, and
  1591.          * <code>false</code> otherwise.
  1592.          *
  1593.          * @return
  1594.          *      <code>true</code>, if this superelement is a singleton, and<br>
  1595.          *      <code>false</code> otherwise
  1596.          */
  1597.         public boolean isSingleton() {
  1598.             return containingList != null;
  1599.         }
  1600.     }
  1601.    
  1602.    
  1603.     /**
  1604.      * An implementation of a doubly-linked list which allows cutting this list
  1605.      * after a specified element, or concatenating two lists, in constant time.
  1606.      *  
  1607.      * @author
  1608.      *      <a href="mailto:npr@informatik.uni-kiel.de">Nick Pr&uuml;hs</a>
  1609.      * @version
  1610.      *      1.0, 09/17/09
  1611.      * @param <E>
  1612.      *      the type of the elements held by this list
  1613.      */
  1614.     private static class MyList<E> implements Iterable<E> {
  1615.         /**
  1616.          * The predecessor of the container holding the first element of this
  1617.          * list.
  1618.          */
  1619.         private Container<E> leftSentinel;
  1620.        
  1621.         /**
  1622.          * The container holding the last element of this list.
  1623.          */
  1624.         private Container<E> lastContainer;
  1625.        
  1626.        
  1627.         /**
  1628.          * Constructs a new, empty list.
  1629.          */
  1630.         public MyList() {
  1631.             leftSentinel = new Container<E>(null, null, null);
  1632.             lastContainer = leftSentinel;
  1633.         }
  1634.        
  1635.         /**
  1636.          * Constructs a new list the passed first and last elements.
  1637.          *  
  1638.          * @param first
  1639.          *      the first element of the new list
  1640.          * @param last
  1641.          *      the last element of the new list
  1642.          */
  1643.         public MyList(Container<E> first, Container<E> last) {
  1644.             leftSentinel = new Container<E>(null, null, first);
  1645.             first.predecessor = leftSentinel;
  1646.            
  1647.             lastContainer = last;
  1648.         }
  1649.        
  1650.        
  1651.         /**
  1652.          * Returns <code>true</code>, if this list is empty, and
  1653.          * <code>false</code> otherwise.
  1654.          *
  1655.          * @return
  1656.          *      <code>true</code>, if this list is empty, and<br>
  1657.          *      <code>false</code>, otherwise
  1658.          */
  1659.         public boolean isEmpty() {
  1660.             return leftSentinel == lastContainer;
  1661.         }
  1662.        
  1663.         /**
  1664.          * Adds the passed item to this list.
  1665.          *
  1666.          * @param item
  1667.          *      the item to add
  1668.          * @return
  1669.          *      the container holding the passed item
  1670.          */
  1671.         public Container<E> add(E item) {
  1672.             lastContainer.insertAfter(item);
  1673.             lastContainer = lastContainer.successor;
  1674.             return lastContainer;
  1675.         }
  1676.  
  1677.         /**
  1678.          * Adds the passed item to the front of list.
  1679.          *
  1680.          * @param item
  1681.          *      the item to add
  1682.          * @return
  1683.          *      the container holding the passed item
  1684.          */
  1685.         public Container<E> addFirst(E item) {
  1686.             leftSentinel.insertAfter(item);
  1687.            
  1688.            
  1689.             if (leftSentinel == lastContainer) {
  1690.                 lastContainer = leftSentinel.successor;
  1691.             }
  1692.            
  1693.             return leftSentinel.successor;
  1694.         }
  1695.        
  1696.         /**
  1697.          * Removes the specified container from this list in constant time.
  1698.          *  
  1699.          * @param container
  1700.          *      the container to remove
  1701.          * @return
  1702.          *      the predecessor of the removed container
  1703.          */
  1704.         public Container<E> remove(Container<E> container) {
  1705.             if (container == lastContainer) {
  1706.                 lastContainer = lastContainer.predecessor;
  1707.             }
  1708.  
  1709.             return container.remove();
  1710.         }
  1711.  
  1712.         /**
  1713.          * Inserts the passed item after the specified container in constant
  1714.          * time.
  1715.          *
  1716.          * @param containerToInsertAfter
  1717.          *      the container the new item is to be inserted after
  1718.          * @param item
  1719.          *      the item to insert
  1720.          * @return
  1721.          *      the container holding the inserted item
  1722.          */
  1723.         public Container<E> insertAfter(Container<E> containerToInsertAfter,
  1724.                 E item) {
  1725.            
  1726.             containerToInsertAfter.insertAfter(item);
  1727.            
  1728.             if (containerToInsertAfter == lastContainer) {
  1729.                 lastContainer = containerToInsertAfter.successor;
  1730.             }
  1731.            
  1732.             return containerToInsertAfter.successor;
  1733.         }
  1734.  
  1735.         /**
  1736.          * Cuts this list after the specified container in constant time.
  1737.          *
  1738.          * @param container
  1739.          *      the container to cut this list after
  1740.          * @return
  1741.          *      the (second) list containing all elements after the specified
  1742.          *      container
  1743.          */
  1744.         public MyList<E> cutAfter(Container<E> container) {
  1745.             if (container == lastContainer) {
  1746.                 return new MyList<E>();
  1747.             } else {
  1748.                 MyList<E> newList = new MyList<E>
  1749.                     (container.successor, lastContainer);
  1750.                 container.successor = null;
  1751.                 lastContainer = container;
  1752.                 return newList;
  1753.             }
  1754.         }
  1755.        
  1756.         /**
  1757.          * Inserts the passed list into this one after the specified position.
  1758.          *
  1759.          * @param position
  1760.          *      the position to insert the passed list after
  1761.          * @param newElements
  1762.          *      the list to insert
  1763.          * @return
  1764.          *      the last container of the inserted list
  1765.          */
  1766.         public Container<E> insertListAfter(Container<E> position,
  1767.                 MyList<E> newElements) {
  1768.            
  1769.             if (newElements.isEmpty()) {
  1770.                 return position;
  1771.             } else {
  1772.                 if (position.successor != null) {
  1773.                     position.successor.predecessor = newElements.lastContainer;
  1774.                     newElements.lastContainer.successor = position.successor;
  1775.                 }
  1776.                
  1777.                 position.successor = newElements.leftSentinel.successor;
  1778.                 newElements.leftSentinel.successor.predecessor = position;
  1779.                
  1780.                 if (position == lastContainer) {
  1781.                     lastContainer = newElements.lastContainer;
  1782.                 }
  1783.                
  1784.                 return newElements.lastContainer;
  1785.             }
  1786.         }
  1787.        
  1788.         /**
  1789.          * Concatenates this list and the passed one in constant time.
  1790.          *
  1791.          * @param newElements
  1792.          *      the list to concatenate with this one
  1793.          */
  1794.         public void concat(MyList<E> newElements) {
  1795.             if (!newElements.isEmpty()) {
  1796.                 lastContainer.successor = newElements.leftSentinel.successor;
  1797.                 newElements.leftSentinel.successor.predecessor = lastContainer;
  1798.                
  1799.                 lastContainer = newElements.lastContainer;
  1800.             }
  1801.         }
  1802.        
  1803.        
  1804.         /**
  1805.          * Returns an iterator over this list.
  1806.          */
  1807.         @Override
  1808.         public Iterator<E> iterator() {
  1809.             return new Iterator<E>() {
  1810.                 private Container<E> current = leftSentinel;
  1811.                
  1812.                 @Override
  1813.                 public boolean hasNext() {
  1814.                     return current.successor != null;
  1815.                 }
  1816.  
  1817.                 @Override
  1818.                 public E next() {
  1819.                     if (current.successor == null) {
  1820.                         throw new NoSuchElementException();
  1821.                     }
  1822.                    
  1823.                     E item = current.successor.item;
  1824.                     current = current.successor;
  1825.                     return item;
  1826.                 }
  1827.  
  1828.                 @Override
  1829.                 public void remove() {
  1830.                     throw new UnsupportedOperationException();
  1831.                 }              
  1832.             };
  1833.         }
  1834.        
  1835.         /**
  1836.          * A container holding an item in a doubly-linked list.
  1837.          *
  1838.          * @author
  1839.          *      <a href="mailto:npr@informatik.uni-kiel.de">Nick Pr&uuml;hs</a>
  1840.          * @version
  1841.          *      1.0, 09/17/09
  1842.          * @param <E>
  1843.          *      the type of the elements held by this list
  1844.          */
  1845.         private static class Container<E> {
  1846.             /**
  1847.              * The item held by this container.
  1848.              */
  1849.             private E item;
  1850.            
  1851.             /**
  1852.              * The successor of this container.
  1853.              */
  1854.             private Container<E> successor;
  1855.            
  1856.             /**
  1857.              * The predecessor of this container.
  1858.              */
  1859.             private Container<E> predecessor;
  1860.            
  1861.            
  1862.             /**
  1863.              * Constructs a new container for the passed item with the specified
  1864.              * predecessor and successor.
  1865.              *
  1866.              * @param item
  1867.              *      the item to construct the new container for
  1868.              * @param predecessor
  1869.              *      the predecessor of the new container
  1870.              * @param successor
  1871.              *      the successor of the new container
  1872.              */
  1873.             public Container(E item, Container<E> predecessor,
  1874.                     Container<E> successor) {
  1875.                
  1876.                 this.item = item;
  1877.                 this.predecessor = predecessor;
  1878.                 this.successor = successor;
  1879.             }
  1880.            
  1881.            
  1882.             /**
  1883.              * Inserts a new container with the passed item after this one.
  1884.              *
  1885.              * @param item
  1886.              *      the item to insert
  1887.              * @return
  1888.              *      the new container
  1889.              */
  1890.             public Container<E> insertAfter(E item) {
  1891.                 Container<E> newContainer =
  1892.                     new Container<E>(item, this, successor);
  1893.                
  1894.                 if (successor != null) {
  1895.                     successor.predecessor = newContainer;
  1896.                 }
  1897.                
  1898.                 successor = newContainer;
  1899.                 return newContainer;
  1900.             }
  1901.            
  1902.             /**
  1903.              * Removes this container from its containing list, updating the
  1904.              * pointers of its predecessor and successor.
  1905.              *
  1906.              * @return
  1907.              *      the predecessor of this container
  1908.              */
  1909.             public Container<E> remove() {
  1910.                 predecessor.successor = successor;
  1911.                
  1912.                 if (successor != null) {
  1913.                     successor.predecessor = predecessor;
  1914.                 }
  1915.                
  1916.                 return predecessor;
  1917.             }
  1918.         }
  1919.     }
  1920. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement