Search in sources :

Example 1 with VisibleForTesting

use of avro.shaded.com.google.common.annotations.VisibleForTesting in project incubator-gobblin by apache.

the class FindPathUtils method dijkstraBasedPathFindingHelper.

// Since Author{autumnust@gmail.com} couldn't find the proper way to conduct Library provided by JGraphT
// on the customized-edge Graph, here is the raw implementation of Dijkstra algorithm for finding shortest path.
/**
 * Given sourceNode and targetNode, find the shortest path and return shortest path.
 * @return Each edge on this shortest path, in order.
 */
@VisibleForTesting
public static List<FlowEdge> dijkstraBasedPathFindingHelper(ServiceNode sourceNode, ServiceNode targetNode, DirectedWeightedMultigraph<ServiceNode, FlowEdge> weightedGraph) {
    Map<DistancedNode, ArrayList<FlowEdge>> shortestPath = new HashMap<>();
    Map<DistancedNode, Double> shortestDist = new HashMap<>();
    PriorityQueue<DistancedNode> pq = new PriorityQueue<>(new Comparator<DistancedNode>() {

        @Override
        public int compare(DistancedNode o1, DistancedNode o2) {
            if (o1.getDistToSrc() < o2.getDistToSrc()) {
                return -1;
            } else {
                return 1;
            }
        }
    });
    pq.add(new DistancedNode(sourceNode, 0.0));
    Set<FlowEdge> visitedEdge = new HashSet<>();
    while (!pq.isEmpty()) {
        DistancedNode node = pq.poll();
        if (node.getNode().getNodeName().equals(targetNode.getNodeName())) {
            // Searching finished
            return shortestPath.get(node);
        }
        Set<FlowEdge> outgoingEdges = weightedGraph.outgoingEdgesOf(node.getNode());
        for (FlowEdge outGoingEdge : outgoingEdges) {
            // Since it is a multi-graph problem, should use edge for deduplicaiton instead of vertex.
            if (visitedEdge.contains(outGoingEdge)) {
                continue;
            }
            DistancedNode adjacentNode = new DistancedNode(weightedGraph.getEdgeTarget(outGoingEdge));
            if (shortestDist.containsKey(adjacentNode)) {
                adjacentNode.setDistToSrc(shortestDist.get(adjacentNode));
            }
            double newDist = node.getDistToSrc() + ((LoadBasedFlowEdgeImpl) outGoingEdge).getEdgeLoad();
            if (newDist < adjacentNode.getDistToSrc()) {
                if (pq.contains(adjacentNode)) {
                    pq.remove(adjacentNode);
                }
                // Update the shortest path.
                ArrayList<FlowEdge> path = shortestPath.containsKey(node) ? new ArrayList<>(shortestPath.get(node)) : new ArrayList<>();
                path.add(outGoingEdge);
                shortestPath.put(adjacentNode, path);
                shortestDist.put(adjacentNode, newDist);
                adjacentNode.setDistToSrc(newDist);
                pq.add(adjacentNode);
            }
            visitedEdge.add(outGoingEdge);
        }
    }
    log.error("No path found");
    return new ArrayList<>();
}
Also used : FlowEdge(org.apache.gobblin.runtime.api.FlowEdge) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PriorityQueue(java.util.PriorityQueue) HashSet(java.util.HashSet) VisibleForTesting(avro.shaded.com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (avro.shaded.com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 PriorityQueue (java.util.PriorityQueue)1 FlowEdge (org.apache.gobblin.runtime.api.FlowEdge)1