Skip to content

Instantly share code, notes, and snippets.

@unnisworld
Created December 20, 2018 11:54
Show Gist options
  • Save unnisworld/1ab27a921a9852b30567adf4d21a9751 to your computer and use it in GitHub Desktop.
Save unnisworld/1ab27a921a9852b30567adf4d21a9751 to your computer and use it in GitHub Desktop.
// Based on - https://www.baeldung.com/java-dijkstra
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class DijkstrasAlgorithm {
public static void main(String[] args) {
Node nodeA = new Node("A");
Node nodeB = new Node("B");
Node nodeC = new Node("C");
Node nodeD = new Node("D");
Node nodeE = new Node("E");
Node nodeF = new Node("F");
nodeA.addAdjacentNode(nodeB, 10);
nodeA.addAdjacentNode(nodeC, 15);
nodeB.addAdjacentNode(nodeD, 12);
nodeB.addAdjacentNode(nodeF, 15);
nodeC.addAdjacentNode(nodeE, 10);
nodeD.addAdjacentNode(nodeE, 2);
nodeD.addAdjacentNode(nodeF, 1);
nodeF.addAdjacentNode(nodeE, 5);
Graph graph = new Graph();
graph.addNode(nodeA);
graph.addNode(nodeB);
graph.addNode(nodeC);
graph.addNode(nodeD);
graph.addNode(nodeE);
graph.addNode(nodeF);
calculateShortestPath(graph, nodeA);
for(Node node : nodeE.shortestPath) {
System.out.print(node + "->");
}
}
private static void calculateShortestPath(Graph g, Node source) {
Set<Node> unsettledNodes = new HashSet<>();
Set<Node> settledNodes = new HashSet<>();
source.distance = 0;
unsettledNodes.add(source);
while( !(unsettledNodes.isEmpty()) ) {
Node currentNode = getLowestDistanceNode(unsettledNodes);
// process adjacent nodes of this node.
for(Entry<Node, Integer> entry : currentNode.adjList.entrySet()) {
Node adjacentNode = entry.getKey();
int edgeWeight = entry.getValue();
updateShortestPathAndDistance(adjacentNode, edgeWeight, currentNode);
unsettledNodes.add(adjacentNode);
}
unsettledNodes.remove(currentNode);
settledNodes.add(currentNode);
}
}
private static Node getLowestDistanceNode(Set<Node> unsettledNodes) {
Node lowestDistancenNode = null;
int lowestDistance = Integer.MAX_VALUE;
for(Node node : unsettledNodes) {
if (node.distance < lowestDistance) {
lowestDistance = node.distance;
lowestDistancenNode = node;
}
}
return lowestDistancenNode;
}
private static void updateShortestPathAndDistance(Node adjacentNode, int edgeWeight, Node currentNode) {
if (currentNode.distance + edgeWeight < adjacentNode.distance) {
adjacentNode.distance = currentNode.distance + edgeWeight;
adjacentNode.shortestPath = new LinkedList<>(currentNode.shortestPath);
adjacentNode.shortestPath.add(currentNode);
}
}
static class Graph {
Set<Node> nodes = new HashSet<>();
void addNode(Node node) {
nodes.add(node);
}
}
static class Node {
String name;
Map<Node, Integer> adjList = new HashMap<>();
List<Node> shortestPath = new LinkedList<Node>();
Integer distance = Integer.MAX_VALUE;
void addAdjacentNode(Node node, int distance) {
adjList.put(node, distance);
}
Node(String name) {
this.name = name;
}
public String toString() {
return "(" + name + ")";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment