List of usage examples for org.apache.commons.collections15 ListUtils isEqualList
public static <E> boolean isEqualList(final Collection<? extends E> list1, final Collection<? extends E> list2)
From source file:mulavito.algorithms.shortestpath.ksp.Yen.java
@Override protected List<List<E>> getShortestPathsIntern(final V source, final V target, int k) { LinkedList<List<E>> found_paths = new LinkedList<List<E>>(); PriorityQueue<WeightedPath> prioQ = new PriorityQueue<WeightedPath>(); DijkstraShortestPath<V, E> blockedDijkstra; // Check if target is reachable from source. if (dijkstra.getDistance(source, target) == null) return found_paths; // Add Dijkstra solution, the first shortest path. found_paths.add(dijkstra.getPath(source, target)); while (found_paths.size() < k) { List<E> curShortestPath = found_paths.getLast(); int maxIndex = curShortestPath.size(); List<V> curShortestPathNodes = new LinkedList<V>(); curShortestPathNodes.add(source); for (E e : found_paths.getLast()) { V v = graph.getEndpoints(e).getFirst(); if (!curShortestPathNodes.contains(v)) curShortestPathNodes.add(v); v = graph.getEndpoints(e).getSecond(); if (!curShortestPathNodes.contains(v)) curShortestPathNodes.add(v); }//from w w w . j ava 2 s .c o m curShortestPathNodes.remove(target); // Split path into Head and NextEdge for (int i = 0; i < maxIndex; i++) { List<E> head = curShortestPath.subList(0, i); // V deviation = head.isEmpty() ? source : graph.getEndpoints(head.get(i - 1)).getSecond(); V deviation = curShortestPathNodes.get(i); // 1. Block edges. Graph<V, E> blocked = blockFilter(head, deviation, curShortestPathNodes, found_paths); // 2. Get shortest path in graph with blocked edges. blockedDijkstra = new DijkstraShortestPath<V, E>(blocked, nev); Number dist = blockedDijkstra.getDistance(deviation, target); if (dist == null) continue; List<E> tail = blockedDijkstra.getPath(deviation, target); // 3. Combine head and tail into new path. List<E> candidate = new ArrayList<E>(); candidate.addAll(head); candidate.addAll(tail); // Check if we already found this solution boolean duplicate = false; for (WeightedPath path : prioQ) if (ListUtils.isEqualList(path.getPath(), candidate)) { duplicate = true; break; } if (!duplicate) prioQ.add(new WeightedPath(candidate)); } if (prioQ.isEmpty()) break; // We have not found any new candidate! else found_paths.add(prioQ.poll().getPath()); } return found_paths; }
From source file:mulavito.algorithms.shortestpath.ksp.Yen.java
/** * Blocks all incident edges of the vertices in head as well as the edge * connecting head to the next node by creating a new filtered graph. * /*from w ww . j a v a2s . c o m*/ * @param head * The current head, from source to deviation node * @param deviation * The edge to the next node * @param foundPaths * The solutions already found and to check against * @return The filtered graph without the blocked edges. */ private Graph<V, E> blockFilter(List<E> head, V deviation, List<V> curShortestPathNodes, List<List<E>> foundPaths) { final Set<E> blocked = new HashSet<E>(); // Block incident edges to make all vertices in head unreachable. for (V v : curShortestPathNodes) { if (v.equals(deviation)) break; for (E e2 : graph.getIncidentEdges(v)) blocked.add(e2); } /*for (E e : head) for (E e2 : graph.getIncidentEdges(graph.getEndpoints(e).getFirst())) blocked.add(e2);*/ // Block all outgoing edges that have been used at deviation vertex for (List<E> path : foundPaths) if (path.size() > head.size() && ListUtils.isEqualList(path.subList(0, head.size()), head)) for (E e : path) if (graph.getEndpoints(e).contains(deviation)) { blocked.add(e); //break; // Continue with next path. } EdgePredicateFilter<V, E> filter = new EdgePredicateFilter<V, E>(new Predicate<E>() { @Override public boolean evaluate(E e) { return !blocked.contains(e); } }); return filter.transform(graph); }
From source file:tests.shortestpaths.KShortestPathTest.java
private boolean comparePaths(List<MyLink> expected, List<MyLink> actual) { double w1 = 0.0; for (MyLink e : expected) w1 += e.getWeight();//from w w w . j a v a 2 s.c om double w2 = 0.0; for (MyLink e : actual) w2 += e.getWeight(); if (ListUtils.isEqualList(expected, actual)) return true; else if (w1 == w2 && expected.size() == actual.size()) { System.out.println("Identical weight (" + w1 + ") and hop count (" + expected.size() + ") but different solutions: " + expected + " vs. " + actual); return true; } return false; }