public void insert(Object @object, int key) { //Reset counter if number of nodes is zero, because then we start over again. if (numberOfNodes == 0) { insertNumCounter = 0; } FibHeapNode node = new FibHeapNode(@object, key, insertNumCounter); insertNumCounter++; objectToNode.Add(@object, node); if (min != null) { //insert node into the root list concatenate(node, min); //only test on key since node cannot have lower insertNum than min if (min.key > node.key) min = node; } else { min = node; } numberOfNodes++; }
public void cut(FibHeapNode child, FibHeapNode parent) { //remove child from the child list of parent removeNode(child); //set the child list to a new node or null if (parent.child == child) { if (child.rightSibling != child) { parent.child = child.rightSibling; } else { parent.child = null; } } parent.degree--; child.parent = null; child.mark = false; child.rightSibling = child.leftSibling = child; //insert the child into the root list concatenate(child, min); }
//$goals 25 //$benchmark public void removeMinTest(FibHeap fibHeap) { if (fibHeap != null && fibHeap.repOK()) { FibHeapNode ret_val = fibHeap.removeMin(); } }
public void insert(Object @object, int key) { //Reset counter if number of nodes is zero, because then we start over again. if (numberOfNodes == 0) { insertNumCounter = 0; } FibHeapNode node = new FibHeapNode(@object, key, insertNumCounter); insertNumCounter++; objectToNode.Add(@object, node); if (min != null) { //insert node into the root list concatenate(node, min); //only test on key since node cannot have lower insertNum than min if (min.key > node.key) { min = node; } } else { min = node; } numberOfNodes++; }
public FibHeap(int maxSize) { min = null; numberOfNodes = 0; insertNumCounter = 0; objectToNode = new Dictionary <Object, FibHeapNode>(maxSize); }
public FibHeapNode insertNode(FibHeapNode toInsert) { if (min != null) { { /*$goal 0 reachable*/ } toInsert.left = min; toInsert.right = min.right; min.right = toInsert; toInsert.right.left = toInsert; if (toInsert.cost < min.cost) { { /*$goal 1 reachable*/ } min = toInsert; } else { { /*$goal 2 reachable*/ } } } else { { /*$goal 3 reachable*/ } min = toInsert; } n++; { roops.util.Goals.reached(4); } return(toInsert); }
public FibHeapNode insertNode(FibHeapNode toInsert) { if (min != null) { {/*$goal 0 reachable*/} toInsert.left = min; toInsert.right = min.right; min.right = toInsert; toInsert.right.left = toInsert; if (toInsert.cost < min.cost) { {/*$goal 1 reachable*/} min = toInsert; } else { {/*$goal 2 reachable*/} } } else { {/*$goal 3 reachable*/} min = toInsert; } n++; {roops.util.Goals.reached(4);} return toInsert; }
//$goals 5 //$benchmark public void insertNodeTest(FibHeap fibHeap, FibHeapNode toInsert) { if (fibHeap != null && toInsert != null && toInsert.left == toInsert && toInsert.right == toInsert && toInsert.parent == null && toInsert.child == null && fibHeap.repOK()) { FibHeapNode ret_val = fibHeap.insertNode(toInsert); } }
public FibHeap(int maxSize) { min = null; numberOfNodes = 0; insertNumCounter = 0; objectToNode = new Dictionary<Object, FibHeapNode>(maxSize); }
//$goals 1 //$benchmark public void minimumTest(FibHeap fibHeap) { if (fibHeap != null && fibHeap.repOK()) { FibHeapNode ret_val = fibHeap.minimum(); } }
//insert a x into a list of nodes public void concatenate(FibHeapNode x, FibHeapNode y) { x.leftSibling.rightSibling = y; y.leftSibling.rightSibling = x; FibHeapNode tmp = y.leftSibling; y.leftSibling = x.leftSibling; x.leftSibling = tmp; }
public FibHeapNode(Object @object, int key, int insertNum) { this.@object = @object; this.key = key; this.insertNum = insertNum; parent = child = null; leftSibling = rightSibling = this; mark = false; degree = 0; }
//extract the min node public Object extractMin() { FibHeapNode z = min; if (z != null) { //if z has children then insert all the children into the root list if (z.child != null) { FibHeapNode tmp = z.child; while (tmp.parent != null) { tmp.parent = null; tmp = tmp.rightSibling; } concatenate(tmp, min); } //remove the min node from the root list removeNode(z); //if min is the only node in the root list if (z.rightSibling == z) { if (numberOfNodes - 1 != 0) { throw new Exception("Error"); } min = null; } //else set the rightSibling of min as the new min node and run a consolidation on the heap else { min = z.rightSibling; consolidate(); } z.rightSibling = z.leftSibling = null; numberOfNodes--; objectToNode.Remove(z.@object); //return the object of the z node (former min node) return(z.@object); } return(null); }
public FibHeapNode removeMin() { FibHeapNode z = min; if (z != null) { { /*$goal 0 reachable*/ } int i = z.degree; FibHeapNode x = z.child; while (i > 0) { { /*$goal 1 reachable*/ } FibHeapNode nextChild = x.right; x.left.right = x.right; x.right.left = x.left; x.left = min; x.right = min.right; min.right = x; x.right.left = x; x.parent = null; x = nextChild; i--; } { /*$goal 2 reachable*/ } z.left.right = z.right; z.right.left = z.left; if (z == z.right) { { /*$goal 3 reachable*/ } min = null; } else { { /*$goal 4 reachable*/ } min = z.right; consolidate(); } n--; } { /*$goal 5 reachable*/ } return(z); }
//used when we remove child from the root list and make it a child of parent public void link(FibHeapNode child, FibHeapNode parent) { //remove child from the root list removeNode(child); child.rightSibling = child.leftSibling = child; //if parent has children, then concatenate child with these if (parent.child != null) { concatenate(child, parent.child); } //child is the new child of parent parent.child = child; parent.degree++; child.parent = parent; child.mark = false; }
public void cascadingCut(FibHeapNode node) { FibHeapNode parent = node.parent; if (parent != null) { //if node's mark is false then set it to true. //The next time we remove a child from node then node will be cut from its parent. if (node.mark == false) { node.mark = true; } //cut the node and perform a cascading cut on the parent else { cut(node, parent); cascadingCut(parent); } } }
private void link(FibHeapNode node1, FibHeapNode node2) { node1.left.right = node1.right; node1.right.left = node1.left; node1.parent = node2; if (node2.child == null) { node2.child = node1; node1.right = node1; node1.left = node1; } else { node1.left = node2.child; node1.right = node2.child.right; node2.child.right = node1; node1.right.left = node1; } node2.degree++; node1.mark = false; }
public void TestFibonnaci() { FibonnaciHeap <int> fibo = new FibonnaciHeap <int>(); FibHeapNode <int> test = new FibHeapNode <int>(15, 12); FibHeapNode <int> test2 = new FibHeapNode <int>(17, 69); FibHeapNode <int> test3 = new FibHeapNode <int>(16, 70); FibHeapNode <int> test4 = new FibHeapNode <int>(19, 71); FibHeapNode <int> test5 = new FibHeapNode <int>(20, 172); fibo.Insert(test2); fibo.Insert(test3); fibo.Insert(test4); fibo.Insert(test5); fibo.Insert(test); fibo.display(); fibo.DecreaseKey(test3, 45); fibo.display(); Console.WriteLine(fibo.Min().Key); Console.WriteLine(fibo.Min().Data); FibHeapNode <int> temp = fibo.ExtractMin(); Console.WriteLine("newnodepetit" + temp.Key); Console.WriteLine("newnodepetit" + temp.Data); fibo.display(); fibo.Delete(test5); fibo.display(); fibo.Delete(test4); fibo.display(); fibo.Delete(test2); fibo.display(); //fibo.display(); }
//decrease the key of object public void decreaseKey(Object @object, int key) { if (objectToNode.ContainsKey(@object)) { //find the node FibHeapNode child = objectToNode[@object]; if (key > child.key) { return; } child.key = key; FibHeapNode parent = child.parent; //perform cut and cascading cut if parent != null and child has a lower key than parent or they have an equal key but child //has a smaller insertNum than parent. if ((parent != null) && ((child.key < parent.key) || ((child.key == parent.key) && (child.insertNum < parent.insertNum)))) { cut(child, parent); cascadingCut(parent); } //check if child is the new min node if ((child.key < min.key) || ((child.key == min.key) && (child.insertNum < min.insertNum))) { min = child; } } else { throw new Exception("Error: No such object."); } }
//remove x from a list of nodes public void removeNode(FibHeapNode x) { x.rightSibling.leftSibling = x.leftSibling; x.leftSibling.rightSibling = x.rightSibling; }
//************************************************************************* //************** From now on repOK() ************************************* //************************************************************************* public boolean repOK() { RoopsSet allNodes = new RoopsSet(); RoopsList parent_headers_to_visit = new RoopsList(); if (min != null) { // check first level { int child_cound = 0; FibHeapNode curr = min; do { if (curr.left.right != curr) { return(false); } if (curr.parent != null) { return(false); } if (curr.child != null) { parent_headers_to_visit.add(curr); } if (!allNodes.add(curr)) { return(false); // repeated node } curr = curr.left; child_cound++; } while (curr != min); } while (!parent_headers_to_visit.isEmpty()) { // check other levels FibHeapNode node = (FibHeapNode)parent_headers_to_visit.get(0); parent_headers_to_visit.remove(0); int node_count = 0; FibHeapNode curr_node = node.child; do { if (curr_node.left.right != curr_node) { return(false); } if (curr_node.parent != null) { return(false); } if (curr_node.child != null) { parent_headers_to_visit.add(curr_node); } if (curr_node.cost > node.cost) { return(false); } if (!allNodes.add(curr_node)) { return(false); // repeated node } curr_node = curr_node.left; node_count++; } while (curr_node != node.child); if (node_count != node.degree) { return(false); } } } if (allNodes.getSize() != this.n) { return(false); } return(true); }
//$goals 5 //$benchmark public void insertNodeTest(FibHeap fibHeap, FibHeapNode toInsert) { if (fibHeap!=null && toInsert!=null && toInsert.left==toInsert && toInsert.right==toInsert && toInsert.parent==null && toInsert.child==null && fibHeap.repOK()) { FibHeapNode ret_val = fibHeap.insertNode(toInsert); } }
//extract the min node public Object extractMin() { FibHeapNode z = min; if (z != null) { //if z has children then insert all the children into the root list if (z.child != null) { FibHeapNode tmp = z.child; while (tmp.parent != null) { tmp.parent = null; tmp = tmp.rightSibling; } concatenate(tmp, min); } //remove the min node from the root list removeNode(z); //if min is the only node in the root list if (z.rightSibling == z) { if (numberOfNodes - 1 != 0) { throw new Exception("Error"); } min = null; } //else set the rightSibling of min as the new min node and run a consolidation on the heap else { min = z.rightSibling; consolidate(); } z.rightSibling = z.leftSibling = null; numberOfNodes--; objectToNode.Remove(z.@object); //return the object of the z node (former min node) return z.@object; } return null; }
public void consolidate() { //the new size of the root list. see Cormen et al. for more information double phi = (1.0 + Math.Sqrt(5.0)) / 2.0; int size = (int)(Math.Floor(Math.Log((double)numberOfNodes) / Math.Log(phi))); //Array can contain trees of different sizes. We can have size trees of the //sizes 1, 2, 4, 8, 16, and so on, and only one tree of each size. FibHeapNode[] array = new FibHeapNode[size + 1]; for (int i = 0; i < array.Length; i++) { array[i] = null; } // Find the number of root nodes in the root list. int numRoots = 0; FibHeapNode x = min; if (x != null) { numRoots++; x = x.rightSibling; while (x != min) { numRoots++; x = x.rightSibling; } } // For each node in the root list while (numRoots > 0) { //The degree of x int d = x.degree; //Since we might move x we set its rightSibling as the next node that we will process FibHeapNode next = x.rightSibling; //is there a subtree with the same size in the root list, then make a bigger tree while (array[d] != null) { FibHeapNode y = array[d]; //if x has a bigger key than y, or if the keys are equal but x has a bigger insertNum, then //x becomes the child of y if ((x.key > y.key) || ((x.key == y.key) && (x.insertNum > y.insertNum))) { FibHeapNode temp = y; y = x; x = temp; } //y becomes the child of x and get removed from the root list link(y, x); //there is no node with this degree anymore. we increase the degree and see if we can build an even larger tree array[d] = null; d++; } //the tree rooted at x is inserted at d array[d] = x; //move forward through the list. x = next; numRoots--; } //set min to null since we need to find the new min node in the list min = null; for (int i = 0; i <= size; i++) { if (array[i] != null) { //array[i] is the new min node if min = null or if it has a smaller key than min or if its key equals min and insertNum of array[i] is smaller than //the insertNum of min. if ((min == null) || (array[i].key < min.key) || ((array[i].key == min.key) && (array[i].insertNum < min.insertNum))) { min = array[i]; } } } }
private void consolidate() { int D = n + 1; Object[] A = new Object[D]; for (int i = 0; i < D; i++) { {/*$goal 6 reachable*/} A[i] = null; } int k = 0; FibHeapNode x = min; if (x != null) { {/*$goal 7 reachable*/} k++; x = x.right; while (x != min) { {/*$goal 8 reachable*/} k++; x = x.right; } } else { {/*$goal 9 unreachable*/} } while (k > 0) { {/*$goal 10 reachable*/} int d = x.degree; FibHeapNode rightNode = x.right; while (A[d] != null) { {/*$goal 11 reachable*/} if (!(A[d] instanceof FibHeapNode)) { {/*$goal 12 unreachable*/} throw new RuntimeException(); } FibHeapNode y = (FibHeapNode) A[d]; if (x.cost > y.cost) { {/*$goal 13 reachable*/} FibHeapNode temp = y; y = x; x = temp; } else { {/*$goal 14 reachable*/} link(y, x); } A[d] = null; d++; } A[d] = x; x = rightNode; k--; } min = null; for (int i = 0; i < D; i++) { {/*$goal 15 reachable*/} if (A[i] != null) { {/*$goal 16 reachable*/} if (min != null) { {/*$goal 17 reachable*/} if (!(A[i] instanceof FibHeapNode)) { {/*$goal 18 unreachable*/} throw new RuntimeException(); } FibHeapNode node = (FibHeapNode) A[i]; node.left.right = node.right; node.right.left = node.left; node.left = min; node.right = min.right; min.right = node; node.right.left = node; if (node.cost < min.cost) { if (!(A[i] instanceof FibHeapNode)) { {/*$goal 19 unreachable*/} throw new RuntimeException(); } min = (FibHeapNode) A[i]; } else { {/*$goal 20 reachable*/} } } else { {/*$goal 21 reachable*/} if (!(A[i] instanceof FibHeapNode)) { {/*$goal 22 unreachable*/} throw new RuntimeException(); } min = (FibHeapNode) A[i]; } } else { {/*$goal 23 reachable*/} } } {/*$goal 24 reachable*/} }
private void consolidate() { int D = n + 1; Object[] A = new Object[D]; for (int i = 0; i < D; i++) { { /*$goal 6 reachable*/ } A[i] = null; } int k = 0; FibHeapNode x = min; if (x != null) { { /*$goal 7 reachable*/ } k++; x = x.right; while (x != min) { { /*$goal 8 reachable*/ } k++; x = x.right; } } else { { /*$goal 9 unreachable*/ } } while (k > 0) { { /*$goal 10 reachable*/ } int d = x.degree; FibHeapNode rightNode = x.right; while (A[d] != null) { { /*$goal 11 reachable*/ } if (!(A[d] instanceof FibHeapNode)) { { /*$goal 12 unreachable*/ } throw new RuntimeException(); } FibHeapNode y = (FibHeapNode)A[d]; if (x.cost > y.cost) { { /*$goal 13 reachable*/ } FibHeapNode temp = y; y = x; x = temp; } else { { /*$goal 14 reachable*/ } link(y, x); } A[d] = null; d++; } A[d] = x; x = rightNode; k--; } min = null; for (int i = 0; i < D; i++) { { /*$goal 15 reachable*/ } if (A[i] != null) { { /*$goal 16 reachable*/ } if (min != null) { { /*$goal 17 reachable*/ } if (!(A[i] instanceof FibHeapNode)) { { /*$goal 18 unreachable*/ } throw new RuntimeException(); } FibHeapNode node = (FibHeapNode)A[i]; node.left.right = node.right; node.right.left = node.left; node.left = min; node.right = min.right; min.right = node; node.right.left = node; if (node.cost < min.cost) { if (!(A[i] instanceof FibHeapNode)) { { /*$goal 19 unreachable*/ } throw new RuntimeException(); } min = (FibHeapNode)A[i]; } else { { /*$goal 20 reachable*/ } } } else { { /*$goal 21 reachable*/ } if (!(A[i] instanceof FibHeapNode)) { { /*$goal 22 unreachable*/ } throw new RuntimeException(); } min = (FibHeapNode)A[i]; } } else { { /*$goal 23 reachable*/ } } } { /*$goal 24 reachable*/ } }
public FibHeapNode removeMin() { FibHeapNode z = min; if (z != null) { {/*$goal 0 reachable*/} int i = z.degree; FibHeapNode x = z.child; while (i > 0) { {/*$goal 1 reachable*/} FibHeapNode nextChild = x.right; x.left.right = x.right; x.right.left = x.left; x.left = min; x.right = min.right; min.right = x; x.right.left = x; x.parent = null; x = nextChild; i--; } {/*$goal 2 reachable*/} z.left.right = z.right; z.right.left = z.left; if (z == z.right) { {/*$goal 3 reachable*/} min = null; } else { {/*$goal 4 reachable*/} min = z.right; consolidate(); } n--; } {/*$goal 5 reachable*/} return z; }