List of usage examples for org.apache.commons.collections15 Buffer add
boolean add(E e);
From source file:edu.uci.ics.jung.algorithms.cluster.WeakComponentClusterer.java
/** * Extracts the weak components from a graph. * @param graph the graph whose weak components are to be extracted * @return the list of weak components/* w w w .j a va 2 s. c o m*/ */ public Set<Set<V>> transform(Graph<V, E> graph) { Set<Set<V>> clusterSet = new HashSet<Set<V>>(); HashSet<V> unvisitedVertices = new HashSet<V>(graph.getVertices()); while (!unvisitedVertices.isEmpty()) { Set<V> cluster = new HashSet<V>(); V root = unvisitedVertices.iterator().next(); unvisitedVertices.remove(root); cluster.add(root); Buffer<V> queue = new UnboundedFifoBuffer<V>(); queue.add(root); while (!queue.isEmpty()) { V currentVertex = queue.remove(); Collection<V> neighbors = graph.getNeighbors(currentVertex); for (V neighbor : neighbors) { if (unvisitedVertices.contains(neighbor)) { queue.add(neighbor); unvisitedVertices.remove(neighbor); cluster.add(neighbor); } } } clusterSet.add(cluster); } return clusterSet; }
From source file:edu.uci.ics.jung.algorithms.importance.BetweennessCentrality.java
protected void computeBetweenness(Graph<V, E> graph) { Map<V, BetweennessData> decorator = new HashMap<V, BetweennessData>(); Map<V, Number> bcVertexDecorator = vertexRankScores.get(getRankScoreKey()); bcVertexDecorator.clear();//from w w w . j a v a2 s . co m Map<E, Number> bcEdgeDecorator = edgeRankScores.get(getRankScoreKey()); bcEdgeDecorator.clear(); Collection<V> vertices = graph.getVertices(); for (V s : vertices) { initializeData(graph, decorator); decorator.get(s).numSPs = 1; decorator.get(s).distance = 0; Stack<V> stack = new Stack<V>(); Buffer<V> queue = new UnboundedFifoBuffer<V>(); queue.add(s); while (!queue.isEmpty()) { V v = queue.remove(); stack.push(v); for (V w : getGraph().getSuccessors(v)) { if (decorator.get(w).distance < 0) { queue.add(w); decorator.get(w).distance = decorator.get(v).distance + 1; } if (decorator.get(w).distance == decorator.get(v).distance + 1) { decorator.get(w).numSPs += decorator.get(v).numSPs; decorator.get(w).predecessors.add(v); } } } while (!stack.isEmpty()) { V w = stack.pop(); for (V v : decorator.get(w).predecessors) { double partialDependency = (decorator.get(v).numSPs / decorator.get(w).numSPs); partialDependency *= (1.0 + decorator.get(w).dependency); decorator.get(v).dependency += partialDependency; E currentEdge = getGraph().findEdge(v, w); double edgeValue = bcEdgeDecorator.get(currentEdge).doubleValue(); edgeValue += partialDependency; bcEdgeDecorator.put(currentEdge, edgeValue); } if (w != s) { double bcValue = bcVertexDecorator.get(w).doubleValue(); bcValue += decorator.get(w).dependency; bcVertexDecorator.put(w, bcValue); } } } if (graph instanceof UndirectedGraph) { for (V v : vertices) { double bcValue = bcVertexDecorator.get(v).doubleValue(); bcValue /= 2.0; bcVertexDecorator.put(v, bcValue); } for (E e : graph.getEdges()) { double bcValue = bcEdgeDecorator.get(e).doubleValue(); bcValue /= 2.0; bcEdgeDecorator.put(e, bcValue); } } for (V vertex : vertices) { decorator.remove(vertex); } }
From source file:edu.uci.ics.jung.algorithms.flows.EdmondsKarpMaxFlow.java
protected boolean hasAugmentingPath() { mSinkPartitionNodes.clear();//from w ww . ja v a 2 s . co m mSourcePartitionNodes.clear(); mSinkPartitionNodes.addAll(mFlowGraph.getVertices()); Set<E> visitedEdgesMap = new HashSet<E>(); Buffer<V> queue = new UnboundedFifoBuffer<V>(); queue.add(source); while (!queue.isEmpty()) { V currentVertex = queue.remove(); mSinkPartitionNodes.remove(currentVertex); mSourcePartitionNodes.add(currentVertex); Number currentCapacity = parentCapacityMap.get(currentVertex); Collection<E> neighboringEdges = mFlowGraph.getOutEdges(currentVertex); for (E neighboringEdge : neighboringEdges) { V neighboringVertex = mFlowGraph.getDest(neighboringEdge); Number residualCapacity = residualCapacityMap.get(neighboringEdge); if (residualCapacity.intValue() <= 0 || visitedEdgesMap.contains(neighboringEdge)) continue; V neighborsParent = parentMap.get(neighboringVertex); Number neighborCapacity = parentCapacityMap.get(neighboringVertex); int newCapacity = Math.min(residualCapacity.intValue(), currentCapacity.intValue()); if ((neighborsParent == null) || newCapacity > neighborCapacity.intValue()) { parentMap.put(neighboringVertex, currentVertex); parentCapacityMap.put(neighboringVertex, new Integer(newCapacity)); visitedEdgesMap.add(neighboringEdge); if (neighboringVertex != target) { queue.add(neighboringVertex); } } } } boolean hasAugmentingPath = false; Number targetsParentCapacity = parentCapacityMap.get(target); if (targetsParentCapacity != null && targetsParentCapacity.intValue() > 0) { updateResidualCapacities(); hasAugmentingPath = true; } clearParentValues(); return hasAugmentingPath; }
From source file:edu.brown.benchmark.seats.SEATSClient.java
/** * Take an existing Reservation that we know is legit and randomly decide to * either queue it for a later update or delete transaction * @param r//from w ww .j a v a2s . c o m */ protected void requeueReservation(Reservation r) { int idx = rng.nextInt(100); if (idx > 20) return; // Queue this motha trucka up for a deletin' or an updatin' CacheType ctype = null; if (rng.nextBoolean()) { ctype = CacheType.PENDING_DELETES; } else { ctype = CacheType.PENDING_UPDATES; } assert (ctype != null); Buffer<Reservation> cache = CACHE_RESERVATIONS.get(ctype); assert (cache != null); cache.add(r); if (debug.val) LOG.debug(String.format("Queued %s for %s [cacheSize=%d]\nFlightId: %d\nCustomerId: %d", r, ctype, cache.size(), r.flight_id, r.customer_id)); }