Skip to content

Instantly share code, notes, and snippets.

@b00blik
Last active February 11, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b00blik/9d69f283602b74d7e8197bb82ae9a552 to your computer and use it in GitHub Desktop.
Save b00blik/9d69f283602b74d7e8197bb82ae9a552 to your computer and use it in GitHub Desktop.
Dijkstra algo impl Java draft
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class GraphDijkstra {
final String START_NAME = "start";
final String FINAL_NAME = "final";
HashMap<String, HashMap<String, Integer>> graph;
HashMap<String, Integer> costs;
HashMap<String, String> parents;
HashSet<String> processed;
GraphDijkstra() {
graph = new HashMap<>();
costs = new HashMap<>();
parents = new HashMap<>();
processed = new HashSet<>();
initGraph();
initCosts();
initParents();
}
void initGraph() {
HashMap<String, Integer> startAdj = new HashMap<>();
startAdj.put("A", 6);
startAdj.put("B", 2);
HashMap<String, Integer> aAdj = new HashMap<>();
aAdj.put(FINAL_NAME, 1);
HashMap<String, Integer> bAdj = new HashMap<>();
bAdj.put("A", 3);
bAdj.put(FINAL_NAME, 5);
HashMap<String, Integer> finalAdj = new HashMap<>();
finalAdj.put(FINAL_NAME, null);
graph.put(START_NAME, startAdj);
graph.put("A", aAdj);
graph.put("B", bAdj);
graph.put(FINAL_NAME, null);
}
void initCosts() {
costs.put("A", 6);
costs.put("B", 2);
costs.put(FINAL_NAME, Integer.MAX_VALUE);
}
void initParents() {
parents.put("A", START_NAME);
parents.put("B", START_NAME);
parents.put(FINAL_NAME, null);
}
String findLowestCostNode() {
int lowestCost = Integer.MAX_VALUE;
String lowestCostNode = null;
for (HashMap.Entry<String, Integer> entry : costs.entrySet()) {
int tmpCost = entry.getValue();
String tmpNodeName = entry.getKey();
if (!processed.contains(tmpNodeName))
if (!START_NAME.equals(tmpNodeName) && !FINAL_NAME.equals(tmpNodeName)) {
if (tmpCost < lowestCost) {
lowestCost = tmpCost;
lowestCostNode = tmpNodeName;
}
}
}
return lowestCostNode;
}
void performDijkstra() {
String nodeName = findLowestCostNode();
while (nodeName != null) {
//get cost of node
int cost = costs.get(nodeName);
//let's get all neighbors of this node
HashMap<String, Integer> neighbors = graph.get(nodeName);
for (String n : neighbors.keySet()) {
//compare cost of this neighbor (old parent vs. another parent)
int newCost = cost + neighbors.get(n);
if (costs.get(n) > newCost) {
costs.put(n, newCost);
parents.put(n, nodeName);
}
}
processed.add(nodeName);
nodeName = findLowestCostNode();
}
}
void printCosts(){
System.out.println("[Costs]");
for (Map.Entry<String, Integer> entry : costs.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
void printParentsPath() {
System.out.println("[Parents path]");
String parent = parents.get(FINAL_NAME);
System.out.println(FINAL_NAME + " <- " + parent);
while (parents.get(parent) != null) {
System.out.println(parent + " <- " + parents.get(parent));
parent = parents.get(parent);
}
}
public static void main(String[] args) {
GraphDijkstra graphDijkstra = new GraphDijkstra();
System.out.println("Starting Dijkstra algorithm, costs before: ");
graphDijkstra.printCosts();
graphDijkstra.performDijkstra();
System.out.println("\nUpdated costs:");
graphDijkstra.printCosts();
graphDijkstra.printParentsPath();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment