List of usage examples for java.util PriorityQueue contains
public boolean contains(Object o)
From source file:Main.java
public static void main(String args[]) { PriorityQueue<Integer> prq = new PriorityQueue<Integer>(); for (int i = 0; i < 10; i++) { prq.add(i);//from w w w . j a v a2 s. com } System.out.println(prq); // check if queue contains 5 boolean b = prq.contains(5); System.out.println("Priority queue contains 5: " + b); }
From source file:de.tudarmstadt.lt.n2n.annotators.RelationAnnotator.java
protected List<Dependency> find_path_dijkstra(Token start, Token dest, Collection<Token> nodes, Map<Token, List<Dependency>> edges) throws IllegalStateException { List<Dependency> shortest_path = new ArrayList<Dependency>(); final Map<Token, Integer> dist = new HashMap<Token, Integer>(); final Map<Token, Dependency> prev = new HashMap<Token, Dependency>(); for (Token t : nodes) dist.put(t, Integer.MAX_VALUE); dist.put(start, 0);//from ww w.j av a 2s .co m PriorityQueue<Token> Q = new PriorityQueue<Token>(edges.size(), new Comparator<Token>() { @Override public int compare(Token o1, Token o2) { return dist.get(o1).compareTo(dist.get(o2)); } }); Q.addAll(nodes); while (!Q.isEmpty()) { Token u = Q.poll(); // initially source node if (u.equals(dest)) // stop if dest break; if (dist.get(u) == Integer.MAX_VALUE) throw new IllegalStateException(String.format( "Could not find path from token '%s' to token '%s'. Perhaps start or dest is part of a preposition? (%s)", start.getCoveredText(), dest.getCoveredText(), DocumentMetaData.get(u.getCAS()).getDocumentId())); List<Dependency> connected_edges = edges.get(u); if (connected_edges == null) continue; for (Dependency d : connected_edges) { Token v = null; if (u.equals(d.getGovernor())) v = d.getDependent(); else v = d.getGovernor(); if (!Q.contains(v)) continue; int alt = dist.get(u) + 1; // dist(u,v) = 1 if (alt < dist.get(v)) { dist.put(v, alt); prev.put(v, d); Q.remove(v); // reinsert v so that Q is recomputed Q.offer(v); } } } Token u = dest; Dependency e = prev.get(u); while (e != null) { shortest_path.add(0, e); if (u == e.getGovernor()) u = e.getDependent(); else u = e.getGovernor(); e = prev.get(u); } return shortest_path; }