@Override public boolean offer(T o) { return add(o); }
/** * Creates a <code>MapBinaryHeap</code> based on the specified * collection whose heap ordering * will be based on the <i>natural ordering</i> of the elements, * which must be <code>Comparable</code>. * @param c the collection of {@code Comparable} elements to add to the heap */ public MapBinaryHeap(Collection<T> c) { this(); addAll(c); }
private void initialize(Comparator<T> comp) { this.comp = comp; clear(); }
/** * Moves the element at position <code>cur</code> closer to the bottom of the heap, or returns if * no further motion is necessary. Calls itself recursively if further motion is possible. */ private void percolateDown(int cur) { int left = lChild(cur); int right = rChild(cur); int smallest; if ((left < heap.size()) && (comp.compare(heap.get(left), heap.get(cur)) < 0)) { smallest = left; } else { smallest = cur; } if ((right < heap.size()) && (comp.compare(heap.get(right), heap.get(smallest)) < 0)) { smallest = right; } if (cur != smallest) { swap(cur, smallest); percolateDown(smallest); } }
new MapBinaryHeap<>((a, b) -> Double.compare(nodeData.get(a).cost, nodeData.get(b).cost)); heap.addAll(remainingNodes); N node = heap.poll(); // remove the node with the minimum incident edge cost remainingNodes.remove(node); E edge = nodeData.get(node).connection; double connectingEdgeWeight = edgeWeights.apply(connectingEdge); if (connectingEdgeWeight < adjacentNodeData.cost) { adjacentNodeData.update(connectingEdgeWeight, connectingEdge); heap.update(adjacentNode);
new MapBinaryHeap<>((a, b) -> Double.compare(nodeData.get(a).cost, nodeData.get(b).cost)); heap.addAll(remainingNodes); N node = heap.peek(); // get the node with the minimum incident edge cost remainingNodes.remove(node); N connectedNode = nodeData.get(node).connection; .doubleValue(); if (connectingEdgeWeight < adjacentNodeData.cost) { adjacentNodeData.update(connectingEdgeWeight, node); heap.update(adjacentNode);
public T poll() { T top = this.peek(); if (top != null) { T bottom_elt = heap.lastElement(); heap.setElementAt(bottom_elt, TOP); object_indices.put(bottom_elt, new Integer(TOP)); heap.setSize(heap.size() - 1); // remove the last element if (heap.size() > 1) percolateDown(TOP); object_indices.remove(top); } return top; }
heap = new MapBinaryHeap<PNode>(nodeComparator); while (! heap.isEmpty()) { PNode n = heap.remove(); double newD = nextDistFrom(n);
/** * Creates a <code>MapBinaryHeap</code> whose heap ordering * is based on the ordering of the elements specified by <code>comp</code>. * @param comp the comparator to use to order elements in the heap */ public MapBinaryHeap(Comparator<T> comp) { initialize(comp); }
/** * Calculates betweenness scores based on the all-pairs weighted shortest paths in the graph. * * <p>NOTE: This version of the algorithm may not work correctly on all graphs; we're still * working out the bugs. Use at your own risk. * * @param graph the graph for which the scores are to be calculated * @param edge_weights the edge weights to be used in the path length calculations */ public BetweennessCentrality( Network<N, E> graph, Function<? super E, ? extends Number> edge_weights) { // reject negative-weight edges up front for (E e : graph.edges()) { double e_weight = edge_weights.apply(e).doubleValue(); Preconditions.checkArgument(e_weight >= 0, "Weight for edge '%s' is < 0: %d", e, e_weight); } initialize(graph); computeBetweenness( new MapBinaryHeap<N>( (v1, v2) -> Double.compare(node_data.get(v1).distance, node_data.get(v2).distance)), edge_weights); }
while (!sd.unknownNodes.isEmpty() && (sd.distances.size() < numDistances || !toGet.isEmpty())) { Map.Entry<N, Number> p = sd.getNextNode(); N v = p.getKey();
((MapBinaryHeap<V>)queue).update(x);
@Override public T element() throws NoSuchElementException { T top = this.peek(); if (top == null) { throw new NoSuchElementException(); } return top; }
/** * Moves the element <code>o</code> at position <code>cur</code> as high as it can go in the heap. * Returns the new position of the element in the heap. */ private int percolateUp(int cur, T o) { int i = cur; while ((i > TOP) && (comp.compare(heap.get(parent(i)), o) > 0)) { T parentElt = heap.get(parent(i)); heap.set(i, parentElt); objectIndices.put(parentElt, i); // reset index to i (new location) i = parent(i); } // place object in heap at appropriate place objectIndices.put(o, i); heap.set(i, o); return i; }
/** * Moves the element at position <code>cur</code> closer to the bottom of * the heap, or returns if no further motion is necessary. Calls itself * recursively if further motion is possible. */ private void percolateDown(int cur) { int left = lChild(cur); int right = rChild(cur); int smallest; if ((left < heap.size()) && (comp.compare(heap.elementAt(left), heap.elementAt(cur)) < 0)) { smallest = left; } else { smallest = cur; } if ((right < heap.size()) && (comp.compare(heap.elementAt(right), heap.elementAt(smallest)) < 0)) { smallest = right; } if (cur != smallest) { swap(cur, smallest); percolateDown(smallest); } }
@Override public T poll() { T top = this.peek(); if (top != null) { T bottom_elt = heap.lastElement(); heap.setElementAt(bottom_elt, TOP); object_indices.put(bottom_elt, Integer.valueOf(TOP)); heap.setSize(heap.size() - 1); // remove the last element if (heap.size() > 1) { percolateDown(TOP); } object_indices.remove(top); } return top; }
/** * Creates a <code>MapBinaryHeap</code> whose heap ordering is based on the * ordering of the elements specified by <code>c</code>. */ public MapBinaryHeap(Comparator<T> comp) { initialize(comp); }
/** * Calculates betweenness scores based on the all-pairs weighted shortest paths in the * graph. * * <p>NOTE: This version of the algorithm may not work correctly on all graphs; we're still * working out the bugs. Use at your own risk. * @param graph the graph for which the scores are to be calculated * @param edge_weights the edge weights to be used in the path length calculations */ public BetweennessCentrality(Graph<V, E> graph, Function<? super E, ? extends Number> edge_weights) { // reject negative-weight edges up front for (E e : graph.getEdges()) { double e_weight = edge_weights.apply(e).doubleValue(); if (e_weight < 0) throw new IllegalArgumentException(String.format( "Weight for edge '%s' is < 0: %d", e, e_weight)); } initialize(graph); computeBetweenness(new MapBinaryHeap<V>(new BetweennessComparator()), edge_weights); }
while (!sd.unknownVertices.isEmpty() && (sd.distances.size() < numDests || !to_get.isEmpty()))