import com.google.common.graph.ElementOrder; import com.google.common.graph.MutableValueGraph; import com.google.common.graph.ValueGraphBuilder; import java.util.HashSet; import java.util.Set; public class DijkstraSolve { private final String sourceNode; private final MutableValueGraph graph; public DijkstraSolve(String sourceNode, MutableValueGraph graph) { this.sourceNode = sourceNode; this.graph = graph; } public static void main(String[] args) { MutableValueGraph graph = buildGraph(); DijkstraSolve dijkstraSolve = new DijkstraSolve("A", graph); dijkstraSolve.dijkstra(); dijkstraSolve.printResult(); } private static MutableValueGraph buildGraph() { MutableValueGraph graph = ValueGraphBuilder.directed() .nodeOrder(ElementOrder.natural()).allowsSelfLoops(true).build(); graph.putEdgeValue("A", "B", 5); graph.putEdgeValue("A", "C", 5); graph.putEdgeValue("A", "D", 5); graph.putEdgeValue("B", "D", 5); graph.putEdgeValue("C", "B", 5); graph.putEdgeValue("C", "E", 5); graph.putEdgeValue("D", "E", 5); return graph; } private void dijkstra() { initPathFromSourceNode(sourceNode); Set nodes = graph.nodes(); if (!nodes.contains(sourceNode)) { throw new IllegalArgumentException(sourceNode + " is not in this graph!"); } Set notVisitedNodes = new HashSet<>(graph.nodes()); String currentVisitNode = sourceNode; while (!notVisitedNodes.isEmpty()) { String nextVisitNode = findNextNode(currentVisitNode, notVisitedNodes); if ("".equals(nextVisitNode)) { break; } notVisitedNodes.remove(currentVisitNode); currentVisitNode = nextVisitNode; } } private String findNextNode(String currentVisitNode, Set notVisitedNodes) { int shortestPath = Integer.MAX_VALUE; String nextVisitNode = ""; for (String node : graph.nodes()) { if (currentVisitNode.equals(node) || !notVisitedNodes.contains(node)) { continue; } if (graph.successors(currentVisitNode).contains(node)) { Integer edgeValue = graph.edgeValue(sourceNode, currentVisitNode) + graph.edgeValue(currentVisitNode, node); Integer currentPathValue = graph.edgeValue(sourceNode, node); if (edgeValue > 0) { graph.putEdgeValue(sourceNode, node, Math.min(edgeValue, currentPathValue)); } } if (graph.edgeValue(sourceNode, node) < shortestPath) { shortestPath = graph.edgeValue(sourceNode, node); nextVisitNode = node; } } return nextVisitNode; } private void initPathFromSourceNode(String sourceNode) { graph.nodes().stream().filter( node -> !graph.adjacentNodes(sourceNode).contains(node)) .forEach(node -> graph.putEdgeValue(sourceNode, node, Integer.MAX_VALUE)); graph.putEdgeValue(sourceNode, sourceNode, 0); } private void printResult() { for (String node : graph.nodes()) { System.out.println(sourceNode + "->" + node + " shortest path is:" + graph.edgeValue(sourceNode, node)); } } }