Shortest path

xiaoxiao2021-03-06  41

Import java.util. *;

Public class bbshortest {static class Heapnode Implements Comparable // Minimum stack indicates {INT i; // vertex number float length; // current length HeapNode (int II, float ll) {i = II; length = ll;} public INT CompareTo (Object X) {Float XL = ((Heapnode) x) .length; if (Length

}} Class UnderflowException extends RuntimeException {/ ** * Construct this exception object * @param message the error message * / public UnderflowException (String message) {super (message);..}} Interface PriorityQueue // setup priority queue interface { / ** * The position interface represents a type that can * be used for the decreaseKey operation. * / public interface position {/ ** * Returns the value stored at this position. * @return the value stored at this position. * / Comparable getValue ();} / ** * Insert into the priority queue, maintaining heap order * Duplicates are allowed * @param x the item to insert * @return may return a Position useful for decreaseKey * / Position insert (.... Comparable x); / ** * Find The smallest item in the priority queue. * @Return the smallest item. * @Throws underflowexception if Empty. * / Comparable findmin ();

/ ** * Remove The smallest item from the priority queue. * @Return the smallest item. * @Throws underflowexception if Empty. * / Comparable deletemin ();

/ ** * Test if the priority queue is logically empty. * @Return true if Empty, false ketherwise. * / Boolean iesmpty ();

/ ** * makeempty. * / Void makeempty (); / ** * returns the size. * @Return current size. * / Int size (); void DecreaseKey;}

class BinaryHeap implements PriorityQueue // minimal heap priority queue interface implemented {/ ** * Construct the binary heap * / public BinaryHeap () {currentSize = 0;. array = new Comparable [DEFAULT_CAPACITY 1];} / ** * .. Construct the binary heap from an array * @param items the inital items in the binary heap * / public BinaryHeap (Comparable [] items) {currentSize = items.length; array = new Comparable [items.length 1]; for (INT i = 0; I

/ ** * @throws UnsupportedOperationException because no Positions are returned * by the insert method for BinaryHeap * / public void decreaseKey (PriorityQueue.Position p, Comparable newVal). {Throw new UnsupportedOperationException ( "Can not use decreaseKey for binary heap");} / ** * Find the smallest item in the priority queue. * @return the smallest item. * @throws UnderflowException if empty. * / public Comparable findMin () {if (isEmpty ()) throw new UnderflowException ( "empty binary heap" ); return array [1];...} / ** * Remove the smallest item from the priority queue * @return the smallest item * @throws UnderflowException if empty * / public Comparable deleteMin () {Comparable minItem = findMin () Array [1] = array [currentsize--]; PERCOLATEDOWN (1);

Return minItem;

/ ** * Establish Heap Order Property from an Arbitrary * Arrangement of items. Runs in linear time. * / Private void buildheap () {for (int i = currentsize / 2; i> 0; I -) Percolatedown (i) }

/ ** * Test if the priority queue is logically empty. * @Return true if Empty, false ketherwise. * / Public boolean ity == 0;

.. / ** * Returns size * @return current size * / public int size () {return currentSize;}. / ** * Make the priority queue logically empty * / public void makeEmpty () {currentSize = 0;}

PRIVATE Static Final Int default_capacity = 100;

private int currentSize; // Number of elements in heap private Comparable [] array; // The heap array / ** * Internal method to percolate down in the heap * @param hole the index at which the percolate begins * / private.. Void Percolatedown (int hole) {Int Child; Comparable Tmp = Array [Hole];

For (; hole * 2 <= currentsize; hole = child) {child = Hole * 2; if (child! = currentsize && array [child 1] .compareto (array [child]) <0) Child ; if (Array [child] .Compareto (TMP) <0) Array [Hole] = array [child]; else break;} array [Hole] = TMP;}}

转载请注明原文地址:https://www.9cbs.com/read-86252.html

New Post(0)