Skip to content

Instantly share code, notes, and snippets.

@artlovan
Created April 14, 2017 00:13
Show Gist options
  • Save artlovan/a07f29e16ab725f8077157de7abdf125 to your computer and use it in GitHub Desktop.
Save artlovan/a07f29e16ab725f8077157de7abdf125 to your computer and use it in GitHub Desktop.
Dijkstra Algorithm in Java
public class App {
public static void main(String[] args) {
Vertex v1 = new Vertex("A");
Vertex v2 = new Vertex("B");
Vertex v3 = new Vertex("C");
v1.addNeighbour(new Edge(1, v1, v2));
v1.addNeighbour(new Edge(10, v1, v2));
v2.addNeighbour(new Edge(1, v2, v3));
Dijkstra dijkstra = new Dijkstra();
dijkstra.computePath(v1);
System.out.println(dijkstra.getShortestPathTo(v3));
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
public class Dijkstra {
public void computePath(Vertex sourceVertex) {
sourceVertex.setMinDistance(0);
PriorityQueue<Vertex> priorityQueue = new PriorityQueue<>();
priorityQueue.add(sourceVertex);
while (!priorityQueue.isEmpty()) {
Vertex vertex = priorityQueue.poll();
for (Edge edge : vertex.getEdges()) {
Vertex v = edge.getTargetVertex();
// Vertex u = edge.getStartVertex();
double weight = edge.getWeight();
double minDistance = vertex.getMinDistance() + weight;
if (minDistance < v.getMinDistance()) {
priorityQueue.remove(vertex);
v.setPreviosVertex(vertex);
v.setMinDistance(minDistance);
priorityQueue.add(v);
}
}
}
}
public List<Vertex> getShortestPathTo(Vertex targetVerte) {
List<Vertex> path = new ArrayList<>();
for (Vertex vertex = targetVerte; vertex != null; vertex = vertex.getPreviosVertex()) {
path.add(vertex);
}
Collections.reverse(path);
return path;
}
}
public class Edge {
private double weight;
private Vertex startVertex;
private Vertex targetVertex;
public Edge(double weight, Vertex startVertex, Vertex targetVertex) {
this.weight = weight;
this.startVertex = startVertex;
this.targetVertex = targetVertex;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Vertex getStartVertex() {
return startVertex;
}
public void setStartVertex(Vertex startVertex) {
this.startVertex = startVertex;
}
public Vertex getTargetVertex() {
return targetVertex;
}
public void setTargetVertex(Vertex targetVertex) {
this.targetVertex = targetVertex;
}
}
import java.util.ArrayList;
import java.util.List;
public class Vertex implements Comparable<Vertex> {
private String name;
private List<Edge> edges;
private boolean visited;
private Vertex previosVertex;
private double minDistance = Double.MAX_VALUE;
public Vertex(String name) {
this.name = name;
this.edges = new ArrayList<>();
}
public void addNeighbour(Edge edge) {
this.edges.add(edge);
}
public List<Edge> getEdges() {
return edges;
}
public void setEdges(List<Edge> edges) {
this.edges = edges;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
public Vertex getPreviosVertex() {
return previosVertex;
}
public void setPreviosVertex(Vertex previosVertex) {
this.previosVertex = previosVertex;
}
public double getMinDistance() {
return minDistance;
}
public void setMinDistance(double minDistance) {
this.minDistance = minDistance;
}
@Override
public String toString() {
return name;
}
@Override
public int compareTo(Vertex otherVertex) {
return Double.compare(this.minDistance, otherVertex.minDistance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment